-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* write the standalone component migration using a placeholder V4 type, write tests for the conversion to mods * fix deployment updater test * Migrate form states also * add to strict null checks * fix strict null checks * fix null checks again * fix another test * remove debug log * move migrate up in the tree --------- Co-authored-by: Ben Loe <[email protected]>
- Loading branch information
Showing
13 changed files
with
372 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
src/pageEditor/hooks/useMigrateStandaloneComponentsToMods.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright (C) 2024 PixieBrix, Inc. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import { useDispatch, useSelector } from "react-redux"; | ||
import { selectModComponentFormStates } from "@/pageEditor/store/editor/editorSelectors"; | ||
import { selectActivatedModComponents } from "@/store/extensionsSelectors"; | ||
import { useEffect } from "react"; | ||
import { actions } from "@/pageEditor/store/editor/editorSlice"; | ||
|
||
/** | ||
* Note: This must be run underneath the PersistGate component in the React component tree | ||
*/ | ||
export default function useMigrateStandaloneComponentsToMods() { | ||
const dispatch = useDispatch(); | ||
const formStates = useSelector(selectModComponentFormStates); | ||
const activatedModComponents = useSelector(selectActivatedModComponents); | ||
|
||
useEffect(() => { | ||
const standaloneComponentFormStates = formStates.filter( | ||
(formState) => formState.modMetadata == null, | ||
); | ||
|
||
for (const formState of standaloneComponentFormStates) { | ||
const modMetadata = activatedModComponents.find( | ||
({ id }) => id === formState.uuid, | ||
)?._recipe; | ||
|
||
if (modMetadata == null) { | ||
dispatch(actions.removeModComponentFormState(formState.uuid)); | ||
} else { | ||
formState.modMetadata = modMetadata; | ||
dispatch(actions.syncModComponentFormState(formState)); | ||
} | ||
} | ||
// eslint-disable-next-line -- Only need to run this migration once | ||
}, []); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
/* | ||
* Copyright (C) 2024 PixieBrix, Inc. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import { | ||
createModMetadataForStandaloneComponent, | ||
migrateStandaloneComponentsToMods, | ||
} from "@/store/extensionsMigrations"; | ||
import { | ||
activatedModComponentFactory, | ||
modMetadataFactory, | ||
} from "@/testUtils/factories/modComponentFactories"; | ||
import { | ||
autoUUIDSequence, | ||
timestampFactory, | ||
} from "@/testUtils/factories/stringFactories"; | ||
import { toLower } from "lodash"; | ||
|
||
const testUserScope = "@test-user"; | ||
|
||
describe("createModMetadataForStandaloneComponent", () => { | ||
it("creates mod metadata for standalone component", () => { | ||
const componentId = autoUUIDSequence(); | ||
const componentLabel = "My Test Mod Component"; | ||
const componentUpdateTimestamp = timestampFactory(); | ||
const component = activatedModComponentFactory({ | ||
id: componentId, | ||
label: componentLabel, | ||
updateTimestamp: componentUpdateTimestamp, | ||
}); | ||
expect( | ||
createModMetadataForStandaloneComponent(component, testUserScope), | ||
).toEqual({ | ||
...component, | ||
_recipe: { | ||
id: `${testUserScope}/converted/${toLower(componentId)}`, | ||
name: componentLabel, | ||
version: "1.0.0", | ||
description: "Page Editor mod automatically converted to a package", | ||
sharing: { | ||
public: false, | ||
organizations: [], | ||
}, | ||
updated_at: componentUpdateTimestamp, | ||
}, | ||
}); | ||
}); | ||
}); | ||
|
||
describe("migrateStandaloneComponentsToMods", () => { | ||
it("does not throw error when extensions is empty", () => { | ||
expect(migrateStandaloneComponentsToMods([], testUserScope)).toEqual([]); | ||
}); | ||
|
||
it("returns mod components when there are no standalone components", () => { | ||
const modMetadata = modMetadataFactory(); | ||
const modComponents = [ | ||
activatedModComponentFactory({ | ||
_recipe: modMetadata, | ||
}), | ||
activatedModComponentFactory({ | ||
_recipe: modMetadata, | ||
}), | ||
activatedModComponentFactory({ | ||
_recipe: modMetadata, | ||
}), | ||
]; | ||
|
||
expect( | ||
migrateStandaloneComponentsToMods(modComponents, testUserScope), | ||
).toEqual(modComponents); | ||
}); | ||
|
||
it("returns only mod components when userScope is null", () => { | ||
const modMetadata = modMetadataFactory(); | ||
const modComponents = [ | ||
activatedModComponentFactory({ | ||
_recipe: modMetadata, | ||
}), | ||
activatedModComponentFactory({ | ||
_recipe: modMetadata, | ||
}), | ||
activatedModComponentFactory({ | ||
_recipe: modMetadata, | ||
}), | ||
]; | ||
const standaloneComponents = [ | ||
activatedModComponentFactory(), | ||
activatedModComponentFactory(), | ||
]; | ||
|
||
expect( | ||
migrateStandaloneComponentsToMods( | ||
[...modComponents, ...standaloneComponents], | ||
null, | ||
), | ||
).toEqual(modComponents); | ||
}); | ||
|
||
it("converts standalone components correctly", () => { | ||
const modMetadata = modMetadataFactory(); | ||
const modComponents = [ | ||
activatedModComponentFactory({ | ||
_recipe: modMetadata, | ||
}), | ||
activatedModComponentFactory({ | ||
_recipe: modMetadata, | ||
}), | ||
activatedModComponentFactory({ | ||
_recipe: modMetadata, | ||
}), | ||
]; | ||
const standaloneComponents = [ | ||
activatedModComponentFactory(), | ||
activatedModComponentFactory(), | ||
]; | ||
const migratedStandaloneComponents = standaloneComponents.map((component) => | ||
createModMetadataForStandaloneComponent(component, testUserScope), | ||
); | ||
|
||
expect( | ||
migrateStandaloneComponentsToMods( | ||
[...modComponents, ...standaloneComponents], | ||
testUserScope, | ||
), | ||
).toEqual([...modComponents, ...migratedStandaloneComponents]); | ||
}); | ||
}); |
Oops, something went wrong.