ce4c83ff
wxy
初始提交
|
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
|
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
require("babel-polyfill");
const SyncBailHook = require("../SyncBailHook");
describe("SyncBailHook", () => {
it("should allow to create sync bail hooks", async () => {
const h1 = new SyncBailHook(["a"]);
const h2 = new SyncBailHook(["a", "b"]);
const h3 = new SyncBailHook(["a"]);
let r = h1.call(1);
expect(r).toEqual(undefined);
h1.tap("A", a => undefined);
h2.tap("A", (a, b) => [a, b]);
expect(h1.call(1)).toEqual(undefined);
expect(await h1.promise(1)).toEqual(undefined);
expect(await pify(cb => h1.callAsync(1, cb))).toEqual(undefined);
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]);
h1.tap("B", a => "ok" + a);
h2.tap("B", (a, b) => "wrong");
expect(h1.call(10)).toEqual("ok10");
expect(await h1.promise(10)).toEqual("ok10");
expect(await pify(cb => h1.callAsync(10, cb))).toEqual("ok10");
expect(h2.call(10, 20)).toEqual([10, 20]);
expect(await h2.promise(10, 20)).toEqual([10, 20]);
expect(await pify(cb => h2.callAsync(10, 20, cb))).toEqual([10, 20]);
});
it("should allow to intercept calls", () => {
const hook = new SyncBailHook(["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();
});
});
function pify(fn) {
return new Promise((resolve, reject) => {
fn((err, result) => {
if (err) reject(err);
else resolve(result);
});
});
}
|