-
Notifications
You must be signed in to change notification settings - Fork 3
/
main_test.ts
98 lines (94 loc) · 2.82 KB
/
main_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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import {
assertEquals,
assertThrows,
} from "https://deno.land/[email protected]/testing/asserts.ts";
import {
assertSpyCall,
assertSpyCalls,
spy,
} from "https://deno.land/[email protected]/testing/mock.ts";
import { radEventListener, rad, on } from "./mod.ts";
Deno.test(function onTest() {
const addEventListener = spy((type: "load", cb: () => void) => {});
const removeEventListener = spy((type: "load", cb: () => void) => {});
const onEvent = spy();
// setup
const cleanup = on(
{ addEventListener, removeEventListener, onload: () => {} },
"load",
onEvent
);
assertSpyCalls(addEventListener, 1);
assertSpyCalls(removeEventListener, 0);
assertSpyCall(addEventListener, 0, {
args: ["load", onEvent],
returned: undefined,
});
// cleanup
assertEquals(cleanup(), undefined);
assertSpyCalls(addEventListener, 1);
assertSpyCalls(removeEventListener, 1);
assertSpyCall(removeEventListener, 0, {
args: ["load", onEvent],
returned: undefined,
});
});
Deno.test(function radEventListenerTest() {
const addEventListener = spy((type: "load", cb: () => void) => {});
const removeEventListener = spy((type: "load", cb: () => void) => {});
const onEvent = spy();
// setup
const cleanup = radEventListener(
{ addEventListener, removeEventListener, onload: () => {} },
"load",
onEvent
);
assertSpyCalls(addEventListener, 1);
assertSpyCalls(removeEventListener, 0);
assertSpyCall(addEventListener, 0, {
args: ["load", onEvent],
returned: undefined,
});
// cleanup
assertEquals(cleanup(), undefined);
assertSpyCalls(addEventListener, 1);
assertSpyCalls(removeEventListener, 1);
assertSpyCall(removeEventListener, 0, {
args: ["load", onEvent],
returned: undefined,
});
});
Deno.test(function radTest() {
const addEventListener = spy((type: "load", cb: () => void) => {});
const removeEventListener = spy((type: "load", cb: () => void) => {});
const onEvent = spy();
// setup
const cleanup = rad({ addEventListener, removeEventListener }, (add) =>
add("load", onEvent)
);
assertSpyCalls(addEventListener, 1);
assertSpyCalls(removeEventListener, 0);
assertSpyCall(addEventListener, 0, {
args: ["load", onEvent],
returned: undefined,
});
// cleanup
assertEquals(cleanup(), undefined);
assertSpyCalls(addEventListener, 1);
assertSpyCalls(removeEventListener, 1);
assertSpyCall(removeEventListener, 0, {
args: ["load", onEvent],
returned: undefined,
});
});
Deno.test(function radShouldFailTest() {
const addEventListener = spy((type: "load", cb: () => void) => {});
const removeEventListener = spy((type: "load", cb: () => void) => {});
// setup
assertThrows(
() => rad({ addEventListener, removeEventListener }, () => {}),
Error,
"you forgot to add event listener"
);
assertSpyCalls(addEventListener, 0);
});