-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathevent_test.ts
More file actions
102 lines (83 loc) · 2.9 KB
/
event_test.ts
File metadata and controls
102 lines (83 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import { assertEquals } from "@std/assert";
import { consume, dispatch, type Event } from "./event.ts";
Deno.test("Event", async (t) => {
await t.step("consume consumes dispatched events", () => {
dispatch({ type: "vim-cmdline-changed", cmdline: "echo 'hello'" });
dispatch({ type: "vim-cmdpos-changed", cmdpos: 10 });
let dispatchedEvents: Event[] = [];
consume((event) => {
dispatchedEvents.push(event);
});
assertEquals(dispatchedEvents, [
{ type: "vim-cmdline-changed", cmdline: "echo 'hello'" },
{ type: "vim-cmdpos-changed", cmdpos: 10 },
]);
dispatchedEvents = [];
consume((event) => {
dispatchedEvents.push(event);
});
assertEquals(dispatchedEvents, []);
});
await t.step("multiple consumers receive all events in order", () => {
dispatch({ type: "vim-cmdline-changed", cmdline: "test1" });
dispatch({ type: "vim-cmdpos-changed", cmdpos: 5 });
dispatch({ type: "vim-cmdline-changed", cmdline: "test2" });
const results: Event[][] = [];
consume((event) => {
if (!results[0]) results[0] = [];
results[0].push(event);
});
assertEquals(results[0], [
{ type: "vim-cmdline-changed", cmdline: "test1" },
{ type: "vim-cmdpos-changed", cmdpos: 5 },
{ type: "vim-cmdline-changed", cmdline: "test2" },
]);
});
await t.step("handles large number of events", () => {
const eventCount = 10000;
for (let i = 0; i < eventCount; i++) {
dispatch({ type: "vim-cmdpos-changed", cmdpos: i });
}
let receivedCount = 0;
consume((event) => {
assertEquals(event.type, "vim-cmdpos-changed");
receivedCount++;
});
assertEquals(receivedCount, eventCount);
});
await t.step("events are cleared after consume", () => {
dispatch({ type: "vim-cmdline-changed", cmdline: "test" });
let firstConsumeCount = 0;
consume(() => {
firstConsumeCount++;
});
assertEquals(firstConsumeCount, 1);
let secondConsumeCount = 0;
consume(() => {
secondConsumeCount++;
});
assertEquals(secondConsumeCount, 0);
});
await t.step("handles events dispatched during consume", () => {
dispatch({ type: "vim-cmdline-changed", cmdline: "initial" });
const events: Event[] = [];
consume((event) => {
events.push(event);
if (event.type === "vim-cmdline-changed" && event.cmdline === "initial") {
// This dispatch happens during consume - should not be consumed in this cycle
dispatch({ type: "vim-cmdpos-changed", cmdpos: 42 });
}
});
assertEquals(events, [
{ type: "vim-cmdline-changed", cmdline: "initial" },
]);
// The event dispatched during consume should be available in next consume
const nextEvents: Event[] = [];
consume((event) => {
nextEvents.push(event);
});
assertEquals(nextEvents, [
{ type: "vim-cmdpos-changed", cmdpos: 42 },
]);
});
});