Skip to content

Commit 0c4352e

Browse files
authored
refactor: improve funbox-validation, add tests (@fehmer) (monkeytypegame#6478)
1 parent d651f28 commit 0c4352e

File tree

2 files changed

+93
-15
lines changed

2 files changed

+93
-15
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import { canSetConfigWithCurrentFunboxes } from "../../../src/ts/test/funbox/funbox-validation";
2+
3+
import * as Notifications from "../../../src/ts/elements/notifications";
4+
describe("funbox-validation", () => {
5+
describe("canSetConfigWithCurrentFunboxes", () => {
6+
const addNotificationMock = vi.spyOn(Notifications, "add");
7+
afterEach(() => {
8+
addNotificationMock.mockReset();
9+
});
10+
11+
const testCases = [
12+
//checks for frontendForcedConfig
13+
{
14+
key: "mode",
15+
value: "zen",
16+
funbox: ["memory"],
17+
error: "You can't set mode to zen with currently active funboxes.",
18+
},
19+
{ key: "mode", value: "words", funbox: ["memory"] }, //ok
20+
21+
//checks for zen mode
22+
...[
23+
"58008", //getWord
24+
"wikipedia", //pullSection
25+
"morse", //alterText
26+
"polyglot", //withWords
27+
"rAnDoMcAsE", //changesCapitalisation
28+
"nospace", //nospace
29+
"plus_one", //toPush:
30+
"read_ahead_easy", //changesWordVisibility
31+
"tts", //speaks
32+
"layout_mirror", //changesLayout
33+
"zipf", //changesWordsFrequency
34+
].map((funbox) => ({
35+
key: "mode",
36+
value: "zen",
37+
funbox: [funbox],
38+
error: "You can't set mode to zen with currently active funboxes.",
39+
})),
40+
{ key: "mode", value: "zen", funbox: ["mirror"] }, //ok
41+
{ key: "mode", value: "zen", funbox: ["space_balls"] }, //no frontendFunctions
42+
43+
//checks for words and custom
44+
...["quote", "custom"].flatMap((value) =>
45+
[
46+
"58008", //getWord
47+
"wikipedia", //pullSection
48+
"polyglot", //withWords
49+
"zipf", //changesWordsFrequency
50+
].map((funbox) => ({
51+
key: "mode",
52+
value,
53+
funbox: [funbox],
54+
error: `You can't set mode to ${value} with currently active funboxes.`,
55+
}))
56+
),
57+
{ key: "mode", value: "quote", funbox: ["space_balls"] }, //no frontendFunctions
58+
];
59+
it.for(testCases)(
60+
`check $funbox with $key=$value`,
61+
({ key, value, funbox, error }) => {
62+
expect(
63+
canSetConfigWithCurrentFunboxes(key, value, funbox.join("#"))
64+
).toBe(error === undefined);
65+
66+
if (error !== undefined) {
67+
expect(addNotificationMock).toHaveBeenCalledWith(error, 0, {
68+
duration: 5,
69+
});
70+
}
71+
}
72+
);
73+
});
74+
});

frontend/src/ts/test/funbox/funbox-validation.ts

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -86,30 +86,34 @@ export function canSetConfigWithCurrentFunboxes(
8686
if (value === "zen") {
8787
fb = fb.concat(
8888
getFunboxesFromString(funbox).filter((f) => {
89+
const funcs = f.frontendFunctions ?? [];
90+
const props = f.properties ?? [];
8991
return (
90-
f.frontendFunctions?.includes("getWord") ||
91-
f.frontendFunctions?.includes("pullSection") ||
92-
f.frontendFunctions?.includes("alterText") ||
93-
f.frontendFunctions?.includes("withWords") ||
94-
f.properties?.includes("changesCapitalisation") ||
95-
f.properties?.includes("nospace") ||
96-
f.properties?.some((fp) => fp.startsWith("toPush:")) ||
97-
f.properties?.includes("changesWordsVisibility") ||
98-
f.properties?.includes("speaks") ||
99-
f.properties?.includes("changesLayout") ||
100-
f.properties?.includes("changesWordsFrequency")
92+
funcs.includes("getWord") ||
93+
funcs.includes("pullSection") ||
94+
funcs.includes("alterText") ||
95+
funcs.includes("withWords") ||
96+
props.includes("changesCapitalisation") ||
97+
props.includes("nospace") ||
98+
props.some((fp) => fp.startsWith("toPush:")) ||
99+
props.includes("changesWordsVisibility") ||
100+
props.includes("speaks") ||
101+
props.includes("changesLayout") ||
102+
props.includes("changesWordsFrequency")
101103
);
102104
})
103105
);
104106
}
105107
if (value === "quote" || value === "custom") {
106108
fb = fb.concat(
107109
getFunboxesFromString(funbox).filter((f) => {
110+
const funcs = f.frontendFunctions ?? [];
111+
const props = f.properties ?? [];
108112
return (
109-
f.frontendFunctions?.includes("getWord") ||
110-
f.frontendFunctions?.includes("pullSection") ||
111-
f.frontendFunctions?.includes("withWords") ||
112-
f.properties?.includes("changesWordsFrequency")
113+
funcs.includes("getWord") ||
114+
funcs.includes("pullSection") ||
115+
funcs.includes("withWords") ||
116+
props.includes("changesWordsFrequency")
113117
);
114118
})
115119
);

0 commit comments

Comments
 (0)