Skip to content

Commit

Permalink
#9242: add migration test case
Browse files Browse the repository at this point in the history
  • Loading branch information
twschiller committed Oct 8, 2024
1 parent 699d59c commit 495c35e
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 24 deletions.
16 changes: 9 additions & 7 deletions src/pageEditor/store/editor/baseFormStateTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,13 +244,15 @@ export type BaseFormStateV5<
export type BaseFormStateV6<
TModComponent extends BaseModComponentState = BaseModComponentState,
TStarterBrick extends BaseStarterBrickState = BaseStarterBrickState,
> = Except<BaseFormStateV5<TModComponent, TStarterBrick>, "modMetadata"> & {
/**
* The mod metadata for the mod component
* @see createNewUnsavedModMetadata
*/
modMetadata: ModMetadata;
};
> =
// Can't use SetRequired because the property is required (it does not use ?), but it can be set to undefined
Except<BaseFormStateV5<TModComponent, TStarterBrick>, "modMetadata"> & {
/**
* The mod metadata for the mod component
* @see createNewUnsavedModMetadata
*/
modMetadata: ModMetadata;
};

export type BaseFormState<
TModComponent extends BaseModComponentState = BaseModComponentState,
Expand Down
4 changes: 2 additions & 2 deletions src/pageEditor/tabs/modMetadata/ModMetadataEditor.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ describe("ModMetadataEditor", () => {
modComponentSlice.actions.UNSAFE_setModComponents([modComponent]),
);
dispatch(editorActions.addModComponentFormState(formState));
dispatch(editorActions.setActiveModId(formState.modMetadata!.id));
dispatch(editorActions.setActiveModId(formState.modMetadata.id));
},
});

Expand Down Expand Up @@ -98,7 +98,7 @@ describe("ModMetadataEditor", () => {
modComponentSlice.actions.UNSAFE_setModComponents([modComponent]),
);
dispatch(editorActions.addModComponentFormState(formState));
dispatch(editorActions.setActiveModId(formState.modMetadata!.id));
dispatch(editorActions.setActiveModId(formState.modMetadata.id));
},
});

Expand Down
68 changes: 66 additions & 2 deletions src/store/editorMigrations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
type EditorStateV6,
type EditorStateV7,
type EditorStateV8,
type EditorStateV9,
} from "@/pageEditor/store/editor/pageEditorTypes";
import { cloneDeep, mapValues, omit } from "lodash";
import {
Expand All @@ -40,13 +41,17 @@ import {
uuidSequence,
} from "@/testUtils/factories/stringFactories";
import { modMetadataFactory } from "@/testUtils/factories/modComponentFactories";
import { validateRegistryId } from "@/types/helpers";
import {
isInnerDefinitionRegistryId,
validateRegistryId,
} from "@/types/helpers";
import {
type BaseFormStateV1,
type BaseFormStateV2,
type BaseFormStateV3,
type BaseFormStateV4,
type BaseFormStateV5,
type BaseFormStateV6,
type BaseModComponentStateV1,
type BaseModComponentStateV2,
} from "@/pageEditor/store/editor/baseFormStateTypes";
Expand All @@ -59,6 +64,7 @@ import {
migrateEditorStateV5,
migrateEditorStateV6,
migrateEditorStateV7,
migrateEditorStateV8,
} from "@/store/editorMigrations";
import { type FactoryConfig } from "cooky-cutter/dist/define";
import { StarterBrickTypes } from "@/types/starterBrickTypes";
Expand Down Expand Up @@ -286,6 +292,12 @@ const initialStateV8: EditorStateV8 & PersistedState = {
},
};

const initialStateV9: EditorStateV9 & PersistedState = {
...cloneDeep(initialStateV8),
modComponentFormStates: [],
deletedModComponentFormStatesByModId: {},
};

function unmigrateServices(
integrationDependencies: IntegrationDependencyV2[] = [],
): IntegrationDependencyV1[] {
Expand Down Expand Up @@ -420,6 +432,15 @@ function unmigrateFormStateV5toV4(formState: BaseFormStateV5): BaseFormStateV4 {
return omit(formState, "variablesDefinition");
}

function unmigrateFormStateV6toV5(formState: BaseFormStateV6): BaseFormStateV5 {
return {
...formState,
modMetadata: isInnerDefinitionRegistryId(formState.modMetadata.id)
? undefined
: formState.modMetadata,
};
}

function unmigrateEditorStateV5toV4(
state: EditorStateV5 & PersistedState,
): EditorStateV4 & PersistedState {
Expand Down Expand Up @@ -471,13 +492,27 @@ function unmigrateEditorStateV8toV7(
};
}

function unmigrateEditorStateV9toV8(
state: EditorStateV9 & PersistedState,
): EditorStateV8 & PersistedState {
return {
...state,
modComponentFormStates: state.modComponentFormStates.map((formState) =>
unmigrateFormStateV6toV5(formState),
),
};
}

type SimpleFactory<T> = (override?: FactoryConfig<T>) => T;

const formStateFactoryV5: SimpleFactory<BaseFormStateV5> = (override) =>
const formStateFactoryV6: SimpleFactory<BaseFormStateV6> = (override) =>
formStateFactory({
formStateConfig: override as FactoryConfig<InternalFormStateOverride>,
});

const formStateFactoryV5: SimpleFactory<BaseFormStateV5> = (override) =>
unmigrateFormStateV6toV5(formStateFactoryV6());

const formStateFactoryV4: SimpleFactory<BaseFormStateV4> = (override) =>
unmigrateFormStateV5toV4(formStateFactoryV5());

Expand Down Expand Up @@ -700,4 +735,33 @@ describe("editor state migrations", () => {
);
});
});

describe("migrateEditorState V8 to V9", () => {
it("migrates empty state", () => {
expect(migrateEditorStateV8(initialStateV8)).toStrictEqual(
initialStateV9,
);
});

it("adds missing modMetadata", () => {
const expectedEditorStateV9: EditorStateV9 & PersistedState = {
...initialStateV9,
modComponentFormStates: [formStateFactoryV6()],
};
const unmigrated = unmigrateEditorStateV9toV8(expectedEditorStateV9);
expect(migrateEditorStateV8(unmigrated)).toStrictEqual({
...expectedEditorStateV9,
modComponentFormStates: [
{
...expectedEditorStateV9.modComponentFormStates[0],
modMetadata: expect.objectContaining({
// Won't match exactly because the naming scheme is different between the factory and generation
id: expect.stringMatching(/@internal\/mod\/\S+/),
name: expect.toBeString(),
}),
},
],
});
});
});
});
2 changes: 1 addition & 1 deletion src/store/editorMigrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ export function migrateEditorStateV8(
): EditorStateV9 & PersistedState {
return produce(state, (draft) => {
// Don't need to also loop over deletedModComponentFormStatesByModId, because there can't be any standalone
// mod components in there
// mod components in there. (Standalone mods when deleted are removed from the editor state entirely.)
for (const formState of draft.modComponentFormStates) {
(formState as BaseFormStateV6).modMetadata ??=
createNewUnsavedModMetadata({
Expand Down
12 changes: 0 additions & 12 deletions src/store/modComponents/modComponentSelectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import { createSelector } from "@reduxjs/toolkit";
import { type ActivatedModComponent } from "@/types/modComponentTypes";
import { type RegistryId } from "@/types/registryTypes";
import { isEmpty, memoize } from "lodash";
import { type UUID } from "@/types/stringTypes";

export function selectActivatedModComponents({
options,
Expand All @@ -35,17 +34,6 @@ export function selectActivatedModComponents({
return options.activatedModComponents;
}

const isModComponentSavedOnCloudSelector = createSelector(
selectActivatedModComponents,
(_state: ModComponentsRootState, modComponentId: UUID) => modComponentId,
(modComponents, modComponentId) =>
modComponents.some((modComponent) => modComponent.id === modComponentId),
);

export const selectIsModComponentSavedOnCloud =
(modComponentId: UUID) => (state: ModComponentsRootState) =>
isModComponentSavedOnCloudSelector(state, modComponentId);

export const selectGetModComponentsForMod = createSelector(
selectActivatedModComponents,
(activatedModComponents) =>
Expand Down

0 comments on commit 495c35e

Please sign in to comment.