-
-
Notifications
You must be signed in to change notification settings - Fork 396
Expand file tree
/
Copy pathHookMap.test.js
More file actions
93 lines (76 loc) · 2.57 KB
/
Copy pathHookMap.test.js
File metadata and controls
93 lines (76 loc) · 2.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
/*
MIT License http://www.opensource.org/licenses/mit-license.php
*/
"use strict";
const HookMapTest = require("../lib/HookMap");
const SyncHook = require("../lib/SyncHook");
describe("HookMap", () => {
it("should return undefined from get when the key is unknown", () => {
const map = new HookMapTest(() => new SyncHook());
expect(map.get("missing")).toBeUndefined();
});
it("should lazily create hooks through for(...) and cache them", () => {
const factory = jest.fn(() => new SyncHook(["a"]));
const map = new HookMapTest(factory, "myMap");
expect(map.name).toBe("myMap");
const hook1 = map.for("key1");
const hook2 = map.for("key1");
const hook3 = map.for("key2");
expect(hook1).toBe(hook2);
expect(hook1).not.toBe(hook3);
expect(factory).toHaveBeenCalledTimes(2);
expect(factory).toHaveBeenNthCalledWith(1, "key1");
expect(factory).toHaveBeenNthCalledWith(2, "key2");
expect(map.get("key1")).toBe(hook1);
});
it("should apply interceptor factories when creating hooks", () => {
const map = new HookMapTest(() => new SyncHook());
const wrapped = new SyncHook();
map.intercept({
factory: (key, hook) => {
expect(key).toBe("foo");
expect(hook).toBeDefined();
return wrapped;
}
});
expect(map.for("foo")).toBe(wrapped);
});
it("should default the interceptor factory to pass-through", () => {
const map = new HookMapTest(() => new SyncHook());
map.intercept({});
const hook = map.for("bar");
expect(hook).toBeDefined();
expect(map.get("bar")).toBe(hook);
});
it("should forward deprecated tap helpers to the underlying hook", () => {
const warn = jest.spyOn(console, "warn").mockImplementation(() => {});
const map = new HookMapTest(() => new SyncHook(["a"]));
const syncMock = jest.fn();
map.tap("k", "plugin-sync", syncMock);
map.for("k").call(1);
expect(syncMock).toHaveBeenCalledWith(1);
const asyncMap = new HookMapTest(
() => new (require("../lib/AsyncSeriesHook"))(["a"])
);
const asyncMock = jest.fn((_a, cb) => cb());
asyncMap.tapAsync("k", "plugin-async", asyncMock);
return new Promise((resolve) => {
asyncMap.for("k").callAsync(2, () => {
expect(asyncMock).toHaveBeenCalled();
const promiseMap = new HookMapTest(
() => new (require("../lib/AsyncSeriesHook"))(["a"])
);
const promiseMock = jest.fn(() => Promise.resolve());
promiseMap.tapPromise("k", "plugin-promise", promiseMock);
promiseMap
.for("k")
.promise(3)
.then(() => {
expect(promiseMock).toHaveBeenCalledWith(3);
warn.mockRestore();
resolve();
});
});
});
});
});