Skip to content

Commit 4f57418

Browse files
authored
refactor(config): split config and config-metadata tests (@fehmer) (monkeytypegame#6770)
1 parent e357efc commit 4f57418

File tree

4 files changed

+485
-428
lines changed

4 files changed

+485
-428
lines changed
Lines changed: 278 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,278 @@
1+
import { configMetadata } from "../../src/ts/config-metadata";
2+
import * as Config from "../../src/ts/config";
3+
import { ConfigKey, Config as ConfigType } from "@monkeytype/schemas/configs";
4+
5+
const { replaceConfig, getConfig } = Config.__testing;
6+
7+
type TestsByConfig<T> = Partial<{
8+
[K in keyof ConfigType]: (T & { value: ConfigType[K] })[];
9+
}>;
10+
11+
describe("ConfigMeta", () => {
12+
afterAll(() => {
13+
replaceConfig({});
14+
vi.resetModules();
15+
});
16+
it("should have changeRequiresRestart defined", () => {
17+
const configsRequiringRestarts = Object.entries(configMetadata)
18+
.filter(([_key, value]) => value.changeRequiresRestart === true)
19+
.map(([key]) => key)
20+
.sort();
21+
22+
expect(configsRequiringRestarts).toEqual(
23+
[
24+
"punctuation",
25+
"numbers",
26+
"words",
27+
"time",
28+
"mode",
29+
"quoteLength",
30+
"language",
31+
"difficulty",
32+
"minWpmCustomSpeed",
33+
"minWpm",
34+
"minAcc",
35+
"minAccCustom",
36+
"minBurst",
37+
"minBurstCustomSpeed",
38+
"britishEnglish",
39+
"funbox",
40+
"customLayoutfluid",
41+
"strictSpace",
42+
"stopOnError",
43+
"lazyMode",
44+
"layout",
45+
"codeUnindentOnBackspace",
46+
].sort()
47+
);
48+
});
49+
50+
it("should have triggerResize defined", () => {
51+
const configsWithTriggeResize = Object.entries(configMetadata)
52+
.filter(([_key, value]) => value.triggerResize === true)
53+
.map(([key]) => key)
54+
.sort();
55+
56+
expect(configsWithTriggeResize).toEqual(
57+
["fontSize", "keymapSize", "maxLineWidth", "tapeMode"].sort()
58+
);
59+
});
60+
describe("overrideValue", () => {
61+
const testCases: TestsByConfig<{
62+
given?: Partial<ConfigType>;
63+
expected: Partial<ConfigType>;
64+
}> = {
65+
punctuation: [
66+
{ value: true, expected: { punctuation: true } },
67+
{
68+
value: true,
69+
given: { mode: "quote" },
70+
expected: { punctuation: false },
71+
},
72+
],
73+
numbers: [
74+
{ value: true, expected: { numbers: true } },
75+
{
76+
value: true,
77+
given: { mode: "quote" },
78+
expected: { numbers: false },
79+
},
80+
],
81+
customLayoutfluid: [
82+
{
83+
value: ["qwerty", "qwerty", "qwertz"],
84+
expected: { customLayoutfluid: ["qwerty", "qwertz"] },
85+
},
86+
],
87+
customPolyglot: [
88+
{
89+
value: ["english", "polish", "english"],
90+
expected: { customPolyglot: ["english", "polish"] },
91+
},
92+
],
93+
keymapSize: [
94+
{ value: 1, expected: { keymapSize: 1 } },
95+
{ value: 1.234, expected: { keymapSize: 1.2 } },
96+
{ value: 0.4, expected: { keymapSize: 0.5 } },
97+
{ value: 3.6, expected: { keymapSize: 3.5 } },
98+
],
99+
customBackground: [
100+
{
101+
value: " https://example.com/test.jpg ",
102+
expected: { customBackground: "https://example.com/test.jpg" },
103+
},
104+
],
105+
accountChart: [
106+
{
107+
value: ["on", "off", "off", "off"],
108+
expected: { accountChart: ["on", "off", "off", "off"] },
109+
},
110+
{
111+
value: ["off", "off", "off", "off"],
112+
given: { accountChart: ["on", "off", "off", "off"] },
113+
expected: { accountChart: ["off", "on", "off", "off"] },
114+
},
115+
{
116+
value: ["off", "off", "on", "on"],
117+
given: { accountChart: ["off", "on", "off", "off"] },
118+
expected: { accountChart: ["on", "off", "on", "on"] },
119+
},
120+
],
121+
};
122+
123+
it.for(
124+
Object.entries(testCases).flatMap(([key, value]) =>
125+
value.flatMap((it) => ({ key: key as ConfigKey, ...it }))
126+
)
127+
)(
128+
`$key value=$value given=$given expect=$expected`,
129+
({ key, value, given, expected }) => {
130+
//GIVEN
131+
replaceConfig(given ?? {});
132+
133+
//WHEN
134+
Config.genericSet(key, value as any);
135+
136+
//THEN
137+
expect(getConfig()).toMatchObject(expected);
138+
}
139+
);
140+
});
141+
describe("isBlocked", () => {
142+
const testCases: TestsByConfig<{
143+
given?: Partial<ConfigType>;
144+
fail?: true;
145+
}> = {
146+
funbox: [
147+
{
148+
value: "gibberish" as any,
149+
given: { mode: "quote" },
150+
fail: true,
151+
},
152+
],
153+
showAllLines: [
154+
{ value: true, given: { tapeMode: "off" } },
155+
{ value: false, given: { tapeMode: "word" } },
156+
{ value: true, given: { tapeMode: "word" }, fail: true },
157+
],
158+
};
159+
160+
it.for(
161+
Object.entries(testCases).flatMap(([key, value]) =>
162+
value.flatMap((it) => ({ key: key as ConfigKey, ...it }))
163+
)
164+
)(
165+
`$key value=$value given=$given fail=$fail`,
166+
({ key, value, given, fail }) => {
167+
//GIVEN
168+
replaceConfig(given ?? {});
169+
170+
//WHEN
171+
const applied = Config.genericSet(key, value as any);
172+
173+
//THEN
174+
expect(applied).toEqual(!fail);
175+
}
176+
);
177+
});
178+
179+
describe("overrideConfig", () => {
180+
const testCases: TestsByConfig<{
181+
given: Partial<ConfigType>;
182+
expected?: Partial<ConfigType>;
183+
}> = {
184+
mode: [
185+
{ value: "time", given: { numbers: true, punctuation: true } },
186+
{
187+
value: "custom",
188+
given: { numbers: true, punctuation: true },
189+
expected: { numbers: false, punctuation: false },
190+
},
191+
{
192+
value: "quote",
193+
given: { numbers: true, punctuation: true },
194+
expected: { numbers: false, punctuation: false },
195+
},
196+
{
197+
value: "zen",
198+
given: { numbers: true, punctuation: true },
199+
expected: { numbers: false, punctuation: false },
200+
},
201+
],
202+
numbers: [{ value: false, given: { mode: "quote" } }],
203+
freedomMode: [
204+
{
205+
value: false,
206+
given: { confidenceMode: "on" },
207+
expected: { confidenceMode: "on" },
208+
},
209+
{
210+
value: true,
211+
given: { confidenceMode: "on" },
212+
expected: { confidenceMode: "off" },
213+
},
214+
],
215+
stopOnError: [
216+
{
217+
value: "off",
218+
given: { confidenceMode: "on" },
219+
expected: { confidenceMode: "on" },
220+
},
221+
{
222+
value: "word",
223+
given: { confidenceMode: "on" },
224+
expected: { confidenceMode: "off" },
225+
},
226+
],
227+
confidenceMode: [
228+
{
229+
value: "off",
230+
given: { freedomMode: true, stopOnError: "word" },
231+
expected: { freedomMode: true, stopOnError: "word" },
232+
},
233+
{
234+
value: "on",
235+
given: { freedomMode: true, stopOnError: "word" },
236+
expected: { freedomMode: false, stopOnError: "off" },
237+
},
238+
],
239+
tapeMode: [
240+
{
241+
value: "off",
242+
given: { showAllLines: true },
243+
expected: { showAllLines: true },
244+
},
245+
{
246+
value: "letter",
247+
given: { showAllLines: true },
248+
expected: { showAllLines: false },
249+
},
250+
],
251+
theme: [
252+
{
253+
value: "8008",
254+
given: { customTheme: true },
255+
expected: { customTheme: false },
256+
},
257+
],
258+
};
259+
260+
it.for(
261+
Object.entries(testCases).flatMap(([key, value]) =>
262+
value.flatMap((it) => ({ key: key as ConfigKey, ...it }))
263+
)
264+
)(
265+
`$key value=$value given=$given expected=$expected`,
266+
({ key, value, given, expected }) => {
267+
//GIVEN
268+
replaceConfig(given);
269+
270+
//WHEN
271+
Config.genericSet(key, value as any);
272+
273+
//THEN
274+
expect(getConfig()).toMatchObject(expected ?? {});
275+
}
276+
);
277+
});
278+
});

0 commit comments

Comments
 (0)