-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathHookMap.test.js
More file actions
135 lines (95 loc) · 3.45 KB
/
HookMap.test.js
File metadata and controls
135 lines (95 loc) · 3.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
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
/**
* Tests for HookMap tap convenience methods
*/
"use strict";
const { HookMap, SyncHook, AsyncSeriesHook } = require("../");
describe("HookMap", () => {
describe("tap convenience methods", () => {
it("should redirect tap to for(key).tap", () => {
const hookMap = new HookMap(() => new SyncHook(["arg"]));
const calls = [];
hookMap.tap("key1", "Plugin1", (arg) => {
calls.push({ key: "key1", arg });
});
hookMap.tap("key2", "Plugin2", (arg) => {
calls.push({ key: "key2", arg });
});
hookMap.for("key1").call("value1");
hookMap.for("key2").call("value2");
expect(calls).toEqual([
{ key: "key1", arg: "value1" },
{ key: "key2", arg: "value2" }
]);
});
it("should redirect tapAsync to for(key).tapAsync", async () => {
const hookMap = new HookMap(() => new AsyncSeriesHook(["arg"]));
const calls = [];
hookMap.tapAsync("key1", "Plugin1", (arg, callback) => {
calls.push({ key: "key1", arg });
callback();
});
await hookMap.for("key1").promise("value1");
expect(calls).toEqual([{ key: "key1", arg: "value1" }]);
});
it("should redirect tapPromise to for(key).tapPromise", async () => {
const hookMap = new HookMap(() => new AsyncSeriesHook(["arg"]));
const calls = [];
hookMap.tapPromise("key1", "Plugin1", async (arg) => {
calls.push({ key: "key1", arg });
});
await hookMap.for("key1").promise("value1");
expect(calls).toEqual([{ key: "key1", arg: "value1" }]);
});
it("should create hook on first tap if not exists", () => {
const hookMap = new HookMap(() => new SyncHook(["arg"]));
expect(hookMap.get("key1")).toBeUndefined();
hookMap.tap("key1", "Plugin1", () => {});
expect(hookMap.get("key1")).toBeDefined();
});
it("should reuse existing hook when tapping multiple times", () => {
const hookMap = new HookMap(() => new SyncHook(["arg"]));
const calls = [];
hookMap.tap("key1", "Plugin1", () => calls.push("first"));
hookMap.tap("key1", "Plugin2", () => calls.push("second"));
hookMap.for("key1").call();
expect(calls).toEqual(["first", "second"]);
});
});
describe("for method", () => {
it("should create hook on first access", () => {
const hookMap = new HookMap(() => new SyncHook(["arg"]));
expect(hookMap.get("key1")).toBeUndefined();
const hook = hookMap.for("key1");
expect(hook).toBeDefined();
expect(hookMap.get("key1")).toBe(hook);
});
it("should return same hook on subsequent access", () => {
const hookMap = new HookMap(() => new SyncHook(["arg"]));
const hook1 = hookMap.for("key1");
const hook2 = hookMap.for("key1");
expect(hook1).toBe(hook2);
});
});
describe("get method", () => {
it("should return undefined for non-existent key", () => {
const hookMap = new HookMap(() => new SyncHook(["arg"]));
expect(hookMap.get("nonexistent")).toBeUndefined();
});
it("should return hook after it has been created", () => {
const hookMap = new HookMap(() => new SyncHook(["arg"]));
hookMap.for("key1");
expect(hookMap.get("key1")).toBeDefined();
});
});
describe("isUsed", () => {
it("should return false when no hooks are used", () => {
const hookMap = new HookMap(() => new SyncHook(["arg"]));
expect(hookMap.isUsed()).toBe(false);
});
it("should return true when a hook has taps", () => {
const hookMap = new HookMap(() => new SyncHook(["arg"]));
hookMap.tap("key1", "Plugin1", () => {});
expect(hookMap.isUsed()).toBe(true);
});
});
});