-
-
Notifications
You must be signed in to change notification settings - Fork 396
Expand file tree
/
Copy pathAsyncSeriesHooks.js
More file actions
120 lines (98 loc) · 3.57 KB
/
AsyncSeriesHooks.js
File metadata and controls
120 lines (98 loc) · 3.57 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
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
const AsyncSeriesBailHook = require("../AsyncSeriesBailHook");
const AsyncSeriesHook = require("../AsyncSeriesHook");
const AsyncSeriesLoopHook = require("../AsyncSeriesLoopHook");
const AsyncSeriesWaterfallHook = require("../AsyncSeriesWaterfallHook");
const HookTester = require("./HookTester");
describe("AsyncSeriesHook", () => {
it("should not have call method", () => {
const hook = new AsyncSeriesHook([]);
expect(hook.call).toBeUndefined();
expect(typeof hook.callAsync).toBe("function");
expect(typeof hook.promise).toBe("function");
});
it("should have tap method", (done) => {
const hook = new AsyncSeriesHook([]);
const mockTap = jest.fn();
hook.tap("somePlugin", mockTap);
hook.callAsync(() => done());
expect(mockTap).toHaveBeenCalledTimes(1);
});
it("should have promise method", (done) => {
const hook = new AsyncSeriesHook([]);
const mockTap = jest.fn();
hook.tap("somePlugin", mockTap);
hook.promise().then(() => done());
expect(mockTap).toHaveBeenCalledTimes(1);
});
it("should have to correct behavior", async () => {
const tester = new HookTester((args) => new AsyncSeriesHook(args));
const result = await tester.run();
expect(result).toMatchSnapshot();
});
});
describe("AsyncSeriesBailHook", () => {
it("should have to correct behavior", async () => {
const tester = new HookTester((args) => new AsyncSeriesBailHook(args));
const result = await tester.run();
expect(result).toMatchSnapshot();
});
it("should not crash with many plugins", () => {
const hook = new AsyncSeriesBailHook(["x"]);
for (let i = 0; i < 1000; i++) {
hook.tap("Test", () => 42);
}
hook.tapAsync("Test", (x, callback) => callback(null, 42));
hook.tapPromise("Test", (_x) => Promise.resolve(42));
return expect(hook.promise()).resolves.toBe(42);
});
});
describe("AsyncSeriesWaterfallHook", () => {
it("should have to correct behavior", async () => {
const tester = new HookTester((args) => new AsyncSeriesWaterfallHook(args));
const result = await tester.run();
expect(result).toMatchSnapshot();
});
it("should work with undefined", async () => {
const hook = new AsyncSeriesWaterfallHook(["x"]);
hook.tap("number", () => 42);
hook.tap("undefined", () => undefined);
return expect(hook.promise()).resolves.toBe(42);
});
it("should work with void", async () => {
const hook = new AsyncSeriesWaterfallHook(["x"]);
hook.tap("number", () => 42);
hook.tap("undefined", () => {});
return expect(hook.promise()).resolves.toBe(42);
});
it("should work with undefined and number again", async () => {
const hook = new AsyncSeriesWaterfallHook(["x"]);
hook.tap("number", () => 42);
hook.tap("undefined", () => {});
hook.tap("number-again", () => 43);
return expect(hook.promise()).resolves.toBe(43);
});
it("should work with null", async () => {
const hook = new AsyncSeriesWaterfallHook(["x"]);
hook.tap("number", () => 42);
hook.tap("undefined", () => null);
return expect(hook.promise()).resolves.toBeNull();
});
it("should work with different types", async () => {
const hook = new AsyncSeriesWaterfallHook(["x"]);
hook.tap("number", () => 42);
hook.tap("string", () => "string");
return expect(hook.promise()).resolves.toBe("string");
});
});
describe("AsyncSeriesLoopHook", () => {
it("should have to correct behavior", async () => {
const tester = new HookTester((args) => new AsyncSeriesLoopHook(args));
const result = await tester.runForLoop();
expect(result).toMatchSnapshot();
});
});