Skip to content

Commit

Permalink
feat: Improve typing of migrate config
Browse files Browse the repository at this point in the history
  • Loading branch information
ptbrowne committed Jan 31, 2023
1 parent 42c1166 commit 2c08601
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 23 deletions.
4 changes: 3 additions & 1 deletion app/configurator/configurator-state.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ afterEach(() => {

describe("initChartStateFromChart", () => {
const setup = ({ chartConfig }: { chartConfig: object }) => {
mockedApi.fetchChartConfig.mockResolvedValue(chartConfig);
mockedApi.fetchChartConfig.mockResolvedValue(
chartConfig as ReturnType<typeof api.fetchChartConfig>
);
};
it("should fetch work if existing chart is valid", async () => {
setup({
Expand Down
14 changes: 8 additions & 6 deletions app/utils/chart-config/versioning.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { decodeChartConfig } from "@/configurator/config-types";
import { decodeChartConfig, MapConfig } from "@/configurator/config-types";

import { migrateChartConfig } from "./versioning";

Expand Down Expand Up @@ -56,17 +56,19 @@ describe("config migrations", () => {

const migratedOldConfig = migrateChartConfig(decodedConfig, {
toVersion: "1.0.0",
});
}) as MapConfig;
expect(migratedOldConfig.version).toEqual("1.0.0");
expect(migratedOldConfig.fields.symbolLayer.show).toEqual(false);
const symbolLayer = migratedOldConfig.fields.symbolLayer!;
// @ts-ignore - show does not existing in the newer version of the types
expect(symbolLayer.show).toEqual(false);
// Should migrate "GeoCoordinatesDimensionIri" to iri defined in Area Layer.
expect(migratedOldConfig.fields.symbolLayer.componentIri).toEqual(
expect(symbolLayer.componentIri).toEqual(
oldConfig.fields.areaLayer.componentIri
);
expect(migratedOldConfig.fields.symbolLayer.measureIri).toEqual(
expect(symbolLayer.measureIri).toEqual(
oldConfig.fields.areaLayer.measureIri
);
expect(migratedOldConfig.fields.symbolLayer.color).toEqual("#1f77b4");
expect(symbolLayer.color).toEqual("#1f77b4");
});

it("should migrate to initial config from migrated config for minor version changes", () => {
Expand Down
42 changes: 26 additions & 16 deletions app/utils/chart-config/versioning.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import produce from "immer";

import { ChartConfig } from "@/configurator";

export const CHART_CONFIG_VERSION = "1.3.0";

type Migration = {
Expand Down Expand Up @@ -484,25 +486,33 @@ export const migrateChartConfig = (
fromVersion,
toVersion = CHART_CONFIG_VERSION,
}: { fromVersion?: string; toVersion?: string } = {}
): any => {
const fromVersionFinal = fromVersion || config.version || "1.0.0";
const direction = upOrDown(fromVersionFinal, toVersion);

if (direction === "same") {
return config;
}
): ChartConfig => {
const _migrateChartConfig = (
config: any,
{
fromVersion,
toVersion = CHART_CONFIG_VERSION,
}: { fromVersion?: string; toVersion?: string } = {}
): any => {
const fromVersionFinal = fromVersion || config.version || "1.0.0";
const direction = upOrDown(fromVersionFinal, toVersion);

if (direction === "same") {
return config;
}

const currentMigration = migrations.find(
(migration) =>
migration[direction === "up" ? "from" : "to"] === fromVersionFinal
);
const currentMigration = migrations.find(
(migration) =>
migration[direction === "up" ? "from" : "to"] === fromVersionFinal
);

if (currentMigration) {
const newConfig = currentMigration[direction](config);
return migrateChartConfig(newConfig, { fromVersion, toVersion });
}
if (currentMigration) {
const newConfig = currentMigration[direction](config);
return _migrateChartConfig(newConfig, { fromVersion, toVersion });
}
};

return config;
return _migrateChartConfig(config, { fromVersion, toVersion });
};

const upOrDown = (
Expand Down

0 comments on commit 2c08601

Please sign in to comment.