Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions lib/MultiHook.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,38 +12,40 @@ class MultiHook {

tap(options, fn) {
for (const hook of this.hooks) {
hook.tap(options, fn);
if (hook.tap) hook.tap(options, fn);
}
}

tapAsync(options, fn) {
for (const hook of this.hooks) {
hook.tapAsync(options, fn);
if (hook.tapAsync) hook.tapAsync(options, fn);
}
}

tapPromise(options, fn) {
for (const hook of this.hooks) {
hook.tapPromise(options, fn);
if (hook.tapPromise) hook.tapPromise(options, fn);
}
}

isUsed() {
for (const hook of this.hooks) {
if (hook.isUsed()) return true;
if (hook.isUsed && hook.isUsed()) return true;
}
return false;
}

intercept(interceptor) {
for (const hook of this.hooks) {
hook.intercept(interceptor);
if (hook.intercept) hook.intercept(interceptor);
}
}

withOptions(options) {
return new MultiHook(
this.hooks.map((hook) => hook.withOptions(options)),
this.hooks.map((hook) =>
hook.withOptions ? hook.withOptions(options) : hook
),
this.name
);
}
Expand Down
23 changes: 23 additions & 0 deletions lib/__tests__/MultiHook.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,27 @@ describe("MultiHook", () => {
expect(new MultiHook([fakeHook2, fakeHook1]).isUsed()).toBe(true);
expect(new MultiHook([fakeHook2, fakeHook2]).isUsed()).toBe(false);
});

it("should support different types of hooks", () => {
const calls = [];
const fakeHook1 = {
tap: (options, fn) => {
calls.push({ options, fn });
}
};
const fakeHook2 = {
tapPromise: (options, fn) => {
calls.push({ options, fn });
}
};
const hook = new MultiHook([fakeHook1, fakeHook1, fakeHook2, fakeHook2]);
hook.tap("options", "fn1");
hook.tapPromise("options", "fn2");
expect(calls).toEqual([
{ options: "options", fn: "fn1" },
{ options: "options", fn: "fn1" },
{ options: "options", fn: "fn2" },
{ options: "options", fn: "fn2" }
]);
});
});