-
-
Notifications
You must be signed in to change notification settings - Fork 396
Expand file tree
/
Copy pathSyncWaterfallHook.test.js
More file actions
213 lines (171 loc) · 5.81 KB
/
Copy pathSyncWaterfallHook.test.js
File metadata and controls
213 lines (171 loc) · 5.81 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
const SyncWaterfallHook = require("../lib/SyncWaterfallHook");
const HookTester = require("./HookTester.test");
function pify(fn) {
return new Promise((resolve, reject) => {
fn((err, result) => {
if (err) reject(err);
else resolve(result);
});
});
}
describe("SyncWaterfallHook", () => {
it("should throw an error when hook has no argument", () => {
expect(() => new SyncWaterfallHook()).toThrow(
"Waterfall hooks must have at least one argument"
);
});
it("should have to correct behavior", async () => {
const tester = new HookTester((args) => new SyncWaterfallHook(args));
const result = await tester.run(true);
expect(result).toMatchSnapshot();
}, 15000);
it("should work", () => {
const hook = new SyncWaterfallHook(["x"]);
hook.tap("number", () => 42);
hook.tap("string", () => "str");
hook.tap("false", () => false);
return expect(hook.call()).toBe(false);
});
it("should work with undefined", async () => {
const hook = new SyncWaterfallHook(["x"]);
hook.tap("number", () => 42);
hook.tap("undefined", () => undefined);
return expect(hook.call()).toBe(42);
});
it("should work with void", async () => {
const hook = new SyncWaterfallHook(["x"]);
hook.tap("number", () => 42);
hook.tap("undefined", () => {});
return expect(hook.call()).toBe(42);
});
it("should work with undefined and number again", async () => {
const hook = new SyncWaterfallHook(["x"]);
hook.tap("number", () => 42);
hook.tap("undefined", () => {});
hook.tap("number-again", () => 43);
return expect(hook.call()).toBe(43);
});
it("should work with null", async () => {
const hook = new SyncWaterfallHook(["x"]);
hook.tap("number", () => 42);
hook.tap("undefined", () => null);
return expect(hook.call()).toBeNull();
});
it("should work with different types", async () => {
const hook = new SyncWaterfallHook(["x"]);
hook.tap("number", () => 42);
hook.tap("string", () => "string");
return expect(hook.call()).toBe("string");
});
it("should allow to create sync hooks", async () => {
const hook = new SyncWaterfallHook(["arg1", "arg2"]);
const mock0 = jest.fn((arg) => `${arg},0`);
const mock1 = jest.fn((arg) => `${arg},1`);
const mock2 = jest.fn((arg) => `${arg},2`);
hook.tap("A", mock0);
hook.tap("B", mock1);
hook.tap("C", mock2);
const returnValue0 = hook.call("sync", "a2");
expect(returnValue0).toBe("sync,0,1,2");
expect(mock0).toHaveBeenLastCalledWith("sync", "a2");
expect(mock1).toHaveBeenLastCalledWith("sync,0", "a2");
expect(mock2).toHaveBeenLastCalledWith("sync,0,1", "a2");
const returnValue1 = await new Promise((resolve) => {
hook.callAsync("async", "a2", (...args) => resolve(args));
});
expect(returnValue1).toEqual([null, "async,0,1,2"]);
expect(mock0).toHaveBeenLastCalledWith("async", "a2");
expect(mock1).toHaveBeenLastCalledWith("async,0", "a2");
expect(mock2).toHaveBeenLastCalledWith("async,0,1", "a2");
const returnValue2 = await hook.promise("promise", "a2");
expect(returnValue2).toBe("promise,0,1,2");
expect(mock0).toHaveBeenLastCalledWith("promise", "a2");
expect(mock1).toHaveBeenLastCalledWith("promise,0", "a2");
expect(mock2).toHaveBeenLastCalledWith("promise,0,1", "a2");
});
it("should allow to intercept calls", () => {
const hook = new SyncWaterfallHook(["arg1", "arg2"]);
const mockCall = jest.fn();
const mock0 = jest.fn(() => "mock0");
const mockRegister = jest.fn((_x) => ({
name: "huh",
type: "sync",
fn: mock0
}));
const mock1 = jest.fn(() => "mock1");
hook.tap("Test1", mock1);
hook.intercept({
call: mockCall,
register: mockRegister
});
const mock2 = jest.fn(() => "mock2");
hook.tap("Test2", mock2);
const returnValue = hook.call(1, 2);
expect(returnValue).toBe("mock0");
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.mock.calls).toEqual([
[1, 2],
["mock0", 2]
]);
});
it("should allow to create waterfall hooks", async () => {
const h1 = new SyncWaterfallHook(["a"]);
const h2 = new SyncWaterfallHook(["a", "b"]);
expect(h1.call(1)).toBe(1);
h1.tap("A", (_a) => undefined);
h2.tap("A", (a, b) => [a, b]);
expect(h1.call(1)).toBe(1);
expect(await h1.promise(1)).toBe(1);
expect(await pify((cb) => h1.callAsync(1, cb))).toBe(1);
expect(h2.call(1, 2)).toEqual([1, 2]);
expect(await h2.promise(1, 2)).toEqual([1, 2]);
expect(await pify((cb) => h2.callAsync(1, 2, cb))).toEqual([1, 2]);
let count = 1;
count = h1.call(count + ++count); // 1 + 2 => 3
count = h1.call(count + ++count); // 3 + 4 => 7
count = h1.call(count + ++count); // 7 + 8 => 15
expect(count).toBe(15);
});
it("should throw when args have length less than 1", () => {
expect(() => {
// eslint-disable-next-line no-new
new SyncWaterfallHook([]);
}).toThrow(/Waterfall/);
});
it("should allow to intercept calls #2", () => {
const hook = new SyncWaterfallHook(["x"]);
const mockCall = jest.fn();
const mockTap = jest.fn((x) => x);
hook.intercept({
call: mockCall,
tap: mockTap
});
hook.call(5);
expect(mockCall).toHaveBeenLastCalledWith(5);
expect(mockTap).not.toHaveBeenCalled();
hook.tap("test", () => 10);
hook.call(7);
expect(mockCall).toHaveBeenLastCalledWith(7);
expect(mockTap).toHaveBeenCalled();
});
it("should throw on tapAsync", () => {
const hook = new SyncWaterfallHook(["x"]);
expect(() => hook.tapAsync()).toThrow(/tapAsync/);
});
it("should throw on tapPromise", () => {
const hook = new SyncWaterfallHook(["x"]);
expect(() => hook.tapPromise()).toThrow(/tapPromise/);
});
});