-
Notifications
You must be signed in to change notification settings - Fork 0
/
request-handler-store.test.ts
127 lines (104 loc) · 3.01 KB
/
request-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
import { Request } from "./request.ts";
import { RequestHandlerStore } from "./request-handler-store.ts";
import { assertEquals, assertExists, assertThrows } from "@std/assert";
import { beforeEach, describe, test } from "@std/testing/bdd";
import { Handler } from "./types.ts";
// Setup
class TestRequest1 extends Request {
public test1 = "test1";
}
class TestRequest2 extends Request<string> {
public test2 = "test2";
}
class TestRequest3 extends Request {
public test2 = "test3";
}
describe("RequestHandlerStore", () => {
describe("constructor()", () => {
const store = new RequestHandlerStore();
test("initializes", () => {
assertEquals(
store instanceof RequestHandlerStore,
true,
);
});
});
describe("add()", () => {
let store: RequestHandlerStore;
beforeEach(() => {
store = new RequestHandlerStore();
});
test("can add RequestHandlers", () => {
store.add(TestRequest1, (request) => {
request.test1;
});
store.add(TestRequest2, (request) => {
return request.test2;
});
});
test(
"multiple RequestHandlers for same type, throws exception",
() => {
store.add(TestRequest1, (request) => {
request.test1;
});
assertThrows(() => {
store.add(TestRequest1, (request) => {
request.test1;
});
});
},
);
});
describe("get()", () => {
let store: RequestHandlerStore;
const requestHandler1 = (request: TestRequest1) => {
request.test1;
};
const requestHandler2 = (request: TestRequest2) => {
return request.test2;
};
beforeEach(() => {
store = new RequestHandlerStore();
store.add(TestRequest1, requestHandler1);
store.add(TestRequest2, requestHandler2);
});
test("returns correct RequestHandler", () => {
const handler = store.get(new TestRequest1());
assertExists(handler);
const handler2 = store.get(new TestRequest2());
assertExists(handler2);
});
test("when no registered handlers, it returns empty array", () => {
assertThrows(() => {
store.get(new TestRequest3());
});
});
});
describe("remove()", () => {
let store: RequestHandlerStore;
beforeEach(() => {
store = new RequestHandlerStore();
});
test("can remove a RequestHandler", () => {
const handler: Handler<TestRequest1> = (request) => {
request.test1;
};
const request = new TestRequest1();
store.add(TestRequest1, handler);
const foundHandler = store.get(request);
assertEquals(foundHandler, handler);
store.remove(TestRequest1, handler);
assertThrows(() => store.get(request));
});
test(
"removing a RequestHandler that is not in store, throws exception",
() => {
const handler: Handler<TestRequest1> = (request) => {
request.test1;
};
assertThrows(() => store.remove(TestRequest1, handler));
},
);
});
});