-
Notifications
You must be signed in to change notification settings - Fork 0
/
notification-handler-store.test.ts
128 lines (105 loc) · 3.46 KB
/
notification-handler-store.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import { Notification } from "./notification.ts";
import { assertEquals, assertThrows } from "@std/assert";
import { beforeEach, describe, test } from "@std/testing/bdd";
import { NotificationHandlerStore } from "./notification-handler-store.ts";
import { Handler } from "./types.ts";
// Setup
class TestNotification1 extends Notification {
public test1 = "test1";
}
class TestNotification2 extends Notification {
public test2 = "test2";
}
class TestNotification3 extends Notification {
public test2 = "test3";
}
describe("NotificationHandlerStore", () => {
describe("constructor()", () => {
const store = new NotificationHandlerStore();
test("initializes", () => {
assertEquals(
store instanceof NotificationHandlerStore,
true,
);
});
});
describe("add()", () => {
let store: NotificationHandlerStore;
beforeEach(() => {
store = new NotificationHandlerStore();
});
test("can add NotificationHandlers", () => {
store.add(TestNotification1, (notification) => {
notification.test1;
});
store.add(TestNotification2, (notification) => {
notification.test2;
});
});
test("can add multiple NotificationHandlers for same type", () => {
store.add(TestNotification1, (notification) => {
notification.test1;
});
store.add(TestNotification1, (notification) => {
notification.test1;
});
});
});
describe("get()", () => {
let store: NotificationHandlerStore;
const notificationHandler1 = (notification: TestNotification1) => {
notification.test1;
};
const notificationHandler2 = (notification: TestNotification1) => {
notification.test1;
};
const notificationHandler3 = (notification: TestNotification2) => {
notification.test2;
};
beforeEach(() => {
store = new NotificationHandlerStore();
store.add(TestNotification1, notificationHandler1);
store.add(TestNotification1, notificationHandler2);
store.add(TestNotification2, notificationHandler3);
});
test("returns correct NotificationHandlers", () => {
const handlers = store.get(new TestNotification1());
assertEquals(handlers.length, 2);
assertEquals(handlers, [
notificationHandler1,
notificationHandler2,
]);
const handlers2 = store.get(new TestNotification2());
assertEquals(handlers2.length, 1);
assertEquals(handlers2, [
notificationHandler3,
]);
});
test("when no register handlers, it returns empty array", () => {
const handlers = store.get(new TestNotification3());
assertEquals(handlers.length, 0);
assertEquals(handlers, []);
});
});
describe("remove()", () => {
let store: NotificationHandlerStore;
beforeEach(() => {
store = new NotificationHandlerStore();
});
test("can remove a NotificationHandler", () => {
const handler: Handler<TestNotification1> = () => {};
const notification = new TestNotification1();
store.add(TestNotification1, handler);
assertEquals(store.get(notification), [handler]);
store.remove(TestNotification1, handler);
assertEquals(store.get(notification), []);
});
test(
"removing a RequestHandler that is not in store, throws exception",
() => {
const handler: Handler<TestNotification1> = () => {};
assertThrows(() => store.remove(TestNotification1, handler));
},
);
});
});