diff --git a/frontend/__tests__/test/funbox/funbox-validation.spec.ts b/frontend/__tests__/test/funbox/funbox-validation.spec.ts new file mode 100644 index 000000000..7a570c20a --- /dev/null +++ b/frontend/__tests__/test/funbox/funbox-validation.spec.ts @@ -0,0 +1,74 @@ +import { canSetConfigWithCurrentFunboxes } from "../../../src/ts/test/funbox/funbox-validation"; + +import * as Notifications from "../../../src/ts/elements/notifications"; +describe("funbox-validation", () => { + describe("canSetConfigWithCurrentFunboxes", () => { + const addNotificationMock = vi.spyOn(Notifications, "add"); + afterEach(() => { + addNotificationMock.mockReset(); + }); + + const testCases = [ + //checks for frontendForcedConfig + { + key: "mode", + value: "zen", + funbox: ["memory"], + error: "You can't set mode to zen with currently active funboxes.", + }, + { key: "mode", value: "words", funbox: ["memory"] }, //ok + + //checks for zen mode + ...[ + "58008", //getWord + "wikipedia", //pullSection + "morse", //alterText + "polyglot", //withWords + "rAnDoMcAsE", //changesCapitalisation + "nospace", //nospace + "plus_one", //toPush: + "read_ahead_easy", //changesWordVisibility + "tts", //speaks + "layout_mirror", //changesLayout + "zipf", //changesWordsFrequency + ].map((funbox) => ({ + key: "mode", + value: "zen", + funbox: [funbox], + error: "You can't set mode to zen with currently active funboxes.", + })), + { key: "mode", value: "zen", funbox: ["mirror"] }, //ok + { key: "mode", value: "zen", funbox: ["space_balls"] }, //no frontendFunctions + + //checks for words and custom + ...["quote", "custom"].flatMap((value) => + [ + "58008", //getWord + "wikipedia", //pullSection + "polyglot", //withWords + "zipf", //changesWordsFrequency + ].map((funbox) => ({ + key: "mode", + value, + funbox: [funbox], + error: `You can't set mode to ${value} with currently active funboxes.`, + })) + ), + { key: "mode", value: "quote", funbox: ["space_balls"] }, //no frontendFunctions + ]; + it.for(testCases)( + `check $funbox with $key=$value`, + ({ key, value, funbox, error }) => { + expect( + canSetConfigWithCurrentFunboxes(key, value, funbox.join("#")) + ).toBe(error === undefined); + + if (error !== undefined) { + expect(addNotificationMock).toHaveBeenCalledWith(error, 0, { + duration: 5, + }); + } + } + ); + }); +}); diff --git a/frontend/src/ts/test/funbox/funbox-validation.ts b/frontend/src/ts/test/funbox/funbox-validation.ts index bd0cb8ba7..6004526ff 100644 --- a/frontend/src/ts/test/funbox/funbox-validation.ts +++ b/frontend/src/ts/test/funbox/funbox-validation.ts @@ -86,18 +86,20 @@ export function canSetConfigWithCurrentFunboxes( if (value === "zen") { fb = fb.concat( getFunboxesFromString(funbox).filter((f) => { + const funcs = f.frontendFunctions ?? []; + const props = f.properties ?? []; return ( - f.frontendFunctions?.includes("getWord") || - f.frontendFunctions?.includes("pullSection") || - f.frontendFunctions?.includes("alterText") || - f.frontendFunctions?.includes("withWords") || - f.properties?.includes("changesCapitalisation") || - f.properties?.includes("nospace") || - f.properties?.some((fp) => fp.startsWith("toPush:")) || - f.properties?.includes("changesWordsVisibility") || - f.properties?.includes("speaks") || - f.properties?.includes("changesLayout") || - f.properties?.includes("changesWordsFrequency") + funcs.includes("getWord") || + funcs.includes("pullSection") || + funcs.includes("alterText") || + funcs.includes("withWords") || + props.includes("changesCapitalisation") || + props.includes("nospace") || + props.some((fp) => fp.startsWith("toPush:")) || + props.includes("changesWordsVisibility") || + props.includes("speaks") || + props.includes("changesLayout") || + props.includes("changesWordsFrequency") ); }) ); @@ -105,11 +107,13 @@ export function canSetConfigWithCurrentFunboxes( if (value === "quote" || value === "custom") { fb = fb.concat( getFunboxesFromString(funbox).filter((f) => { + const funcs = f.frontendFunctions ?? []; + const props = f.properties ?? []; return ( - f.frontendFunctions?.includes("getWord") || - f.frontendFunctions?.includes("pullSection") || - f.frontendFunctions?.includes("withWords") || - f.properties?.includes("changesWordsFrequency") + funcs.includes("getWord") || + funcs.includes("pullSection") || + funcs.includes("withWords") || + props.includes("changesWordsFrequency") ); }) );