SyncHook.js
2.45 KB
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
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
require("babel-polyfill");
const SyncHook = require("../SyncHook");
describe("SyncHook", () => {
it("should allow to create sync hooks", async () => {
const h0 = new SyncHook();
const h1 = new SyncHook(["test"]);
const h2 = new SyncHook(["test", "arg2"]);
const h3 = new SyncHook(["test", "arg2", "arg3"]);
h0.call();
await h0.promise();
await new Promise(resolve => h0.callAsync(resolve));
const mock0 = jest.fn();
h0.tap("A", mock0);
h0.call();
expect(mock0).toHaveBeenLastCalledWith();
const mock1 = jest.fn();
h0.tap("B", mock1);
h0.call();
expect(mock1).toHaveBeenLastCalledWith();
const mock2 = jest.fn();
const mock3 = jest.fn();
const mock4 = jest.fn();
const mock5 = jest.fn();
h1.tap("C", mock2);
h2.tap("D", mock3);
h3.tap("E", mock4);
h3.tap("F", mock5);
h1.call("1");
h2.call("1", 2);
h3.call("1", 2, 3);
expect(mock2).toHaveBeenLastCalledWith("1");
expect(mock3).toHaveBeenLastCalledWith("1", 2);
expect(mock4).toHaveBeenLastCalledWith("1", 2, 3);
expect(mock5).toHaveBeenLastCalledWith("1", 2, 3);
await new Promise(resolve => h1.callAsync("a", resolve));
await h2.promise("a", "b");
await new Promise(resolve => h3.callAsync("a", "b", "c", resolve));
expect(mock2).toHaveBeenLastCalledWith("a");
expect(mock3).toHaveBeenLastCalledWith("a", "b");
expect(mock4).toHaveBeenLastCalledWith("a", "b", "c");
expect(mock5).toHaveBeenLastCalledWith("a", "b", "c");
await h3.promise("x", "y");
expect(mock4).toHaveBeenLastCalledWith("x", "y", undefined);
expect(mock5).toHaveBeenLastCalledWith("x", "y", undefined);
});
it("should allow to intercept calls", () => {
const hook = new SyncHook(["arg1", "arg2"]);
const mockCall = jest.fn();
const mock0 = jest.fn();
const mockRegister = jest.fn(x => ({
name: "huh",
type: "sync",
fn: mock0
}));
const mock1 = jest.fn();
hook.tap("Test1", mock1);
hook.intercept({
call: mockCall,
register: mockRegister
});
const mock2 = jest.fn();
hook.tap("Test2", mock2);
hook.call(1, 2);
expect(mockCall).toHaveBeenLastCalledWith(1, 2);
expect(mockRegister).toHaveBeenLastCalledWith({
type: "sync",
name: "Test2",
fn: mock2
});
expect(mock1).not.toHaveBeenLastCalledWith(1, 2);
expect(mock2).not.toHaveBeenLastCalledWith(1, 2);
expect(mock0).toHaveBeenLastCalledWith(1, 2);
});
});