-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmediator.test.ts
81 lines (69 loc) · 2.3 KB
/
mediator.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
import { Mediator } from "./mediator.ts";
import { Notification } from "./notification.ts";
import { PublishStrategy } from "./publish-strategy.ts";
import { Request } from "./request.ts";
import { assertEquals, assertThrows } from "@std/assert";
import { describe, test } from "https://deno.land/[email protected]/testing/bdd.ts";
describe("Mediator", () => {
// Setup
class TestClass1 extends Request<Promise<number>> {}
class TestClass3 extends Request {}
class UnregisteredRequest extends Request<number> {}
class UnregisteredPromiseRequest extends Request<Promise<number>> {}
class TestNotification1 extends Notification {}
const mediator = new Mediator({
publishStratey: PublishStrategy.SyncContinueOnException,
});
const expected = 42;
describe("handle()", () => {
test("when request type, it succeeds", () => {
mediator.handle(TestClass1, () => Promise.resolve(expected));
});
test("when notification type, it succeeds", () => {
mediator.handle(TestNotification1, () => Promise.resolve());
});
test(
"when multiple handlers for same notification type, it succeeds",
() => {
mediator.handle(TestNotification1, () => Promise.resolve());
},
);
test(
"when handler for request type previously registered, it fails",
() => {
assertThrows(() => {
mediator.handle(TestClass1, () => Promise.resolve(expected));
});
},
);
});
describe("send()", () => {
mediator.handle(TestClass3, () => {});
test("when handler with void response, it succeeds", () => {
mediator.send(new TestClass3());
});
test("when handler with value response, it succeeds", async () => {
assertEquals(
await mediator.send(new TestClass1()),
expected,
);
});
test(
"when no registered handler, it throws exception",
() => {
assertThrows(() => mediator.send(new UnregisteredRequest()));
},
);
test(
"when no registered async handler, it throws exception",
() => {
assertThrows(() => mediator.send(new UnregisteredPromiseRequest()));
},
);
});
describe("publish()", () => {
test("when notification, it calls correct handlers", async () => {
await mediator.publish(new TestNotification1());
});
});
});