deno.land / x / lume@v2.1.4 / tests / core / events.test.ts

events.test.ts
نووسراو ببینە
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
import { assertStrictEquals as equals } from "../../deps/assert.ts";import Events from "../../core/events.ts";
Deno.test("Events", async (t) => { const events = new Events(); const called = { beforeBuild: 0, afterBuild: 0, afterRender: 0, beforeSave: 0, beforeUpdate: 0, };
equals(events.listeners.size, 0);
await t.step("Add events", () => { events.addEventListener("beforeBuild", () => called.beforeBuild++); equals(events.listeners.size, 1);
events.addEventListener("afterBuild", () => called.afterBuild++); equals(events.listeners.size, 2); });
await t.step("Dispatch events", async () => { await events.dispatchEvent({ type: "afterBuild" }); equals(called.beforeBuild, 0); equals(called.afterBuild, 1);
await events.dispatchEvent({ type: "beforeBuild" }); equals(called.beforeBuild, 1); equals(called.afterBuild, 1); });
await t.step("Once events", async () => { events.addEventListener("afterRender", () => called.afterRender++, { once: true, });
await events.dispatchEvent({ type: "afterRender" }); equals(called.afterRender, 1);
await events.dispatchEvent({ type: "afterRender" }); equals(called.afterRender, 1); });
await t.step("Signal events", async () => { const controller = new AbortController();
events.addEventListener("beforeSave", () => called.beforeSave++, { signal: controller.signal, });
await events.dispatchEvent({ type: "beforeSave" }); equals(called.beforeSave, 1);
await events.dispatchEvent({ type: "beforeSave" }); equals(called.beforeSave, 2);
controller.abort();
await events.dispatchEvent({ type: "beforeSave" }); equals(called.beforeSave, 2); });
await t.step("Return false", async () => { events.addEventListener("beforeUpdate", () => called.beforeUpdate++); events.addEventListener("beforeUpdate", () => true); events.addEventListener("beforeUpdate", () => called.beforeUpdate++);
equals(events.listeners.get("beforeUpdate")?.size, 3); await events.dispatchEvent({ type: "beforeUpdate" }); equals(called.beforeUpdate, 2);
events.addEventListener("beforeUpdate", () => called.beforeUpdate++); events.addEventListener("beforeUpdate", () => false); events.addEventListener("beforeUpdate", () => called.beforeUpdate++);
await events.dispatchEvent({ type: "beforeUpdate" }); equals(called.beforeUpdate, 5); });});
lume

Version Info

Tagged at
7 months ago