forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhook_spec.js
151 lines (117 loc) · 3.33 KB
/
hook_spec.js
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import { expect } from 'chai';
import { createHook, hooks } from 'src/hook';
describe('the hook module', () => {
let sandbox;
beforeEach(() => {
sandbox = sinon.sandbox.create();
});
afterEach(() => {
sandbox.restore();
});
it('should call all sync hooks attached to a function', () => {
let called = [];
let calledWith;
let testFn = () => {
called.push(testFn);
};
let testHook = (...args) => {
called.push(testHook);
calledWith = args;
};
let testHook2 = () => {
called.push(testHook2);
};
let testHook3 = () => {
called.push(testHook3);
};
let hookedTestFn = createHook('sync', testFn, 'testHook');
hookedTestFn.addHook(testHook, 50);
hookedTestFn.addHook(testHook2, 100);
// make sure global test hooks work as well (with default priority)
hooks['testHook'].addHook(testHook3);
hookedTestFn(1, 2, 3);
expect(called).to.deep.equal([
testHook2,
testHook,
testHook3,
testFn
]);
expect(calledWith).to.deep.equal([1, 2, 3]);
called = [];
hookedTestFn.removeHook(testHook);
hooks['testHook'].removeHook(testHook3);
hookedTestFn(1, 2, 3);
expect(called).to.deep.equal([
testHook2,
testFn
]);
});
it('should allow context to be passed to hooks, but keep bound contexts', () => {
let context;
let fn = function() {
context = this;
};
let boundContext = {};
let calledBoundContext;
let hook = function() {
calledBoundContext = this;
}.bind(boundContext);
let hookFn = createHook('sync', fn);
hookFn.addHook(hook);
let newContext = {};
hookFn.bind(newContext)();
expect(context).to.equal(newContext);
expect(calledBoundContext).to.equal(boundContext);
});
describe('asyncSeries', () => {
it('should call function as normal if no hooks attached', () => {
let fn = sandbox.spy();
let hookFn = createHook('asyncSeries', fn);
hookFn(1);
expect(fn.calledOnce).to.equal(true);
expect(fn.firstCall.args[0]).to.equal(1);
});
it('should call hooks correctly applied in asyncSeries', () => {
let called = [];
let testFn = (called) => {
called.push(testFn);
};
let testHook = (called, next) => {
called.push(testHook);
next(called);
};
let testHook2 = (called, next) => {
called.push(testHook2);
next(called);
};
let hookedTestFn = createHook('asyncSeries', testFn);
hookedTestFn.addHook(testHook);
hookedTestFn.addHook(testHook2);
hookedTestFn(called);
expect(called).to.deep.equal([
testHook,
testHook2,
testFn
]);
});
it('should allow context to be passed to hooks, but keep bound contexts', () => {
let context;
let fn = function() {
context = this;
};
let boundContext1 = {};
let calledBoundContext1;
let hook1 = function(next) {
calledBoundContext1 = this;
next()
}.bind(boundContext1);
let hookFn = createHook('asyncSeries', fn);
hookFn.addHook(hook1);
let newContext = {};
hookFn = hookFn.bind(newContext);
hookFn();
expect(context).to.equal(newContext);
expect(calledBoundContext1).to.equal(boundContext1);
});
});
});