Skip to content

Commit 4a6ef90

Browse files
committed
fix
1 parent f7865f9 commit 4a6ef90

6 files changed

Lines changed: 72 additions & 37 deletions

File tree

Lines changed: 65 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,81 @@
11
import { createRoot } from "solid-js";
2-
import { describe, expect, it } from "vitest";
2+
import { describe, expect, it, vi } from "vitest";
33
import { createEvent } from "../../src/ts/hooks/createEvent";
44

55
describe("createEvent", () => {
6-
it("initial value is 0", () => {
7-
createRoot((dispose) => {
8-
const [event] = createEvent();
9-
expect(event()).toBe(0);
10-
dispose();
11-
});
6+
it("dispatch notifies subscribers", () => {
7+
const event = createEvent<string>();
8+
const fn = vi.fn();
9+
event.subscribe(fn);
10+
event.dispatch("hello");
11+
expect(fn).toHaveBeenCalledWith("hello");
1212
});
1313

14-
it("dispatch increments the value by 1", () => {
15-
createRoot((dispose) => {
16-
const [event, dispatch] = createEvent();
17-
dispatch();
18-
expect(event()).toBe(1);
19-
dispose();
20-
});
14+
it("dispatch notifies multiple subscribers", () => {
15+
const event = createEvent<number>();
16+
const fn1 = vi.fn();
17+
const fn2 = vi.fn();
18+
event.subscribe(fn1);
19+
event.subscribe(fn2);
20+
event.dispatch(42);
21+
expect(fn1).toHaveBeenCalledWith(42);
22+
expect(fn2).toHaveBeenCalledWith(42);
2123
});
2224

23-
it("each dispatch increments independently", () => {
24-
createRoot((dispose) => {
25-
const [event, dispatch] = createEvent();
26-
dispatch();
27-
dispatch();
28-
dispatch();
29-
expect(event()).toBe(3);
30-
dispose();
31-
});
25+
it("dispatch with no type arg requires no arguments", () => {
26+
const event = createEvent();
27+
const fn = vi.fn();
28+
event.subscribe(fn);
29+
event.dispatch();
30+
expect(fn).toHaveBeenCalledTimes(1);
31+
});
32+
33+
it("subscribe returns an unsubscribe function", () => {
34+
const event = createEvent<string>();
35+
const fn = vi.fn();
36+
const unsub = event.subscribe(fn);
37+
event.dispatch("a");
38+
unsub();
39+
event.dispatch("b");
40+
expect(fn).toHaveBeenCalledTimes(1);
41+
expect(fn).toHaveBeenCalledWith("a");
3242
});
3343

3444
it("two independent events do not share state", () => {
45+
const eventA = createEvent<string>();
46+
const eventB = createEvent<string>();
47+
const fnA = vi.fn();
48+
const fnB = vi.fn();
49+
eventA.subscribe(fnA);
50+
eventB.subscribe(fnB);
51+
eventA.dispatch("a");
52+
expect(fnA).toHaveBeenCalledWith("a");
53+
expect(fnB).not.toHaveBeenCalled();
54+
});
55+
56+
it("useListener auto-unsubscribes on dispose", () => {
57+
const event = createEvent<string>();
58+
const fn = vi.fn();
3559
createRoot((dispose) => {
36-
const [eventA, dispatchA] = createEvent();
37-
const [eventB, dispatchB] = createEvent();
38-
dispatchA();
39-
dispatchA();
40-
dispatchB();
41-
expect(eventA()).toBe(2);
42-
expect(eventB()).toBe(1);
60+
event.useListener(fn);
61+
event.dispatch("inside");
4362
dispose();
4463
});
64+
event.dispatch("outside");
65+
expect(fn).toHaveBeenCalledTimes(1);
66+
expect(fn).toHaveBeenCalledWith("inside");
67+
});
68+
69+
it("subscriber errors do not prevent other subscribers from running", () => {
70+
const event = createEvent<string>();
71+
const fn1 = vi.fn(() => {
72+
throw new Error("oops");
73+
});
74+
const fn2 = vi.fn();
75+
event.subscribe(fn1);
76+
event.subscribe(fn2);
77+
event.dispatch("test");
78+
expect(fn1).toHaveBeenCalled();
79+
expect(fn2).toHaveBeenCalled();
4580
});
4681
});

frontend/src/ts/input/handlers/insert-text.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ export async function onInsertText(options: OnInsertTextParams): Promise<void> {
172172
TestInput.pushMissedWord(TestWords.words.getCurrent());
173173
}
174174
if (Config.keymapMode === "react") {
175-
void flash(data, correct);
175+
flash(data, correct);
176176
}
177177
if (testInput.length === 0) {
178178
TestInput.setBurstStart(now);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ const list: Partial<Record<FunboxName, FunboxFunctions>> = {
234234
showErrorNotification("Failed to load text-to-speech script");
235235
return;
236236
}
237-
if (params[0] !== undefined) void ttsEvent.dispatch(params[0]);
237+
if (params[0] !== undefined) ttsEvent.dispatch(params[0]);
238238
},
239239
},
240240
arrows: {
@@ -451,7 +451,7 @@ const list: Partial<Record<FunboxName, FunboxFunctions>> = {
451451
LayoutfluidFunboxTimer.hide();
452452
}
453453
setTimeout(() => {
454-
void highlight(
454+
highlight(
455455
TestWords.words.getCurrent().charAt(TestInput.input.current.length),
456456
);
457457
}, 1);

frontend/src/ts/test/test-logic.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ async function init(): Promise<boolean> {
571571
}
572572

573573
if (Config.keymapMode === "next" && Config.mode !== "zen") {
574-
void highlight(
574+
highlight(
575575
Arrays.nthElementFromArray(
576576
// ignoring for now but this might need a different approach
577577
// oxlint-disable-next-line no-misused-spread
@@ -1537,7 +1537,7 @@ configEvent.subscribe(({ key, newValue, nosave }) => {
15371537

15381538
if (key === "keymapMode" && newValue === "next" && Config.mode !== "zen") {
15391539
setTimeout(() => {
1540-
void highlight(
1540+
highlight(
15411541
Arrays.nthElementFromArray(
15421542
// ignoring for now but this might need a different approach
15431543
// oxlint-disable-next-line no-misused-spread

frontend/src/ts/test/test-timer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ function layoutfluid(): void {
141141

142142
if (Config.keymapMode === "next") {
143143
setTimeout(() => {
144-
void highlight(
144+
highlight(
145145
TestWords.words.getCurrent().charAt(TestInput.input.current.length),
146146
);
147147
}, 1);

frontend/src/ts/test/test-ui.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1750,7 +1750,7 @@ function afterAnyTestInput(
17501750
}
17511751

17521752
if (Config.keymapMode === "next") {
1753-
void highlight(
1753+
highlight(
17541754
TestWords.words.getCurrent().charAt(TestInput.input.current.length),
17551755
);
17561756
}

0 commit comments

Comments
 (0)