-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathcallback-replayer-test.ts
53 lines (50 loc) · 1.38 KB
/
callback-replayer-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
/* eslint-env jest */
import {
CallbackReplayer,
CancellableCallbackReplayer,
} from "./callback-replayer";
describe("CallbackReplayer", () => {
test("should call callback with all events", () => {
const unsub = jest.fn();
const replayer = new CallbackReplayer(unsub);
const unsub1 = replayer.sub((a, b) => {
expect(a).toBe(1);
expect(b).toBe(2);
});
const unsub2 = replayer.sub((a, b) => {
expect(a).toBe(1);
expect(b).toBe(2);
});
replayer.event(1, 2);
unsub1();
expect(unsub).not.toBeCalled();
unsub2();
expect(unsub).toBeCalled();
});
});
describe("CancellableCallbackReplayer", () => {
test("should call callback with all events", () => {
let unsub = jest.fn();
let onevent: ((a: number, b: number) => void) | undefined;
const cc = (onevent_: (a: number, b: number) => void) => {
onevent = onevent_;
return unsub;
};
const replayer: CancellableCallbackReplayer<[number, number]> =
new CancellableCallbackReplayer(cc);
expect(onevent).toBeDefined();
const unsub1 = replayer.sub()((a, b) => {
expect(a).toBe(1);
expect(b).toBe(2);
});
const unsub2 = replayer.sub()((a, b) => {
expect(a).toBe(1);
expect(b).toBe(2);
});
onevent?.(1, 2);
unsub1();
expect(unsub).not.toBeCalled();
unsub2();
expect(unsub).toBeCalled();
});
});