-
Notifications
You must be signed in to change notification settings - Fork 1
/
chai-signals.js
269 lines (230 loc) · 9.53 KB
/
chai-signals.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
(function () {
"use strict";
// Module compatibility.
/* istanbul ignore else */
if (typeof require === "function" && typeof exports === "object" && typeof module === "object") {
// NodeJS
module.exports = chaiSignals;
} else if (typeof define === "function" && define.amd) {
// AMD
define(function () {
return chaiSignals;
});
} else {
/*global self: false */
// Other environment (usually <script> tag): plug in to global chai instance directly.
chai.use(chaiSignals);
// Expose as a property of the global object so that consumers can configure the `transferPromiseness` property.
self.chaiSignals = chaiSignals;
}
function chaiSignals(chai, utils) {
var spies = [];
chai.signals = {};
var Assertion = chai.Assertion;
var assert = chai.assert;
function assertIsAboutSignal(assertion) {
if (typeof assertion._obj.dispatch !== "function") {
throw new TypeError(utils.inspect(assertion._obj) + " is not a signal spy.");
}
}
// add method wrapper
function method(name, asserter) {
utils.addMethod(Assertion.prototype, name, function () {
//assertIsAboutSignal(this);
return asserter.apply(this, arguments);
});
}
// add property wrapper
function property(name, asserter) {
utils.addProperty(Assertion.prototype, name, function () {
assertIsAboutSignal(this);
return asserter.apply(this, arguments);
});
}
// These are for clarity and to bypass Chai refusing to allow `undefined` as actual when used with `assert`.
function assertIfNegated(assertion, message, extra) {
assertion.assert(true, null, message, extra.expected, extra.actual);
}
function assertIfNotNegated(assertion, message, extra) {
assertion.assert(false, message, null, extra.expected, extra.actual);
}
/**
* Returns true if the signal has been dispatched
*
* Where n is the number of times the signal has been dispatched:
*
* expect(signalSpy).to.have.been.dispatched();
* expect(signalSpy).to.have.been.dispatched(n);
* expect(signal).to.have.been.dispatched();
* expect(signal).to.have.been.dispatched(n);
*
* expect(signalSpy).to.not.have.been.dispatched();
* expect(signalSpy).to.not.have.been.dispatched(n);
* expect(signal).to.not.have.been.dispatched();
* expect(signal).to.not.have.been.dispatched(n);
*
*/
method('dispatched', function(expectedCount) {
var result, spy = getSpy(this._obj);
if (!(spy instanceof chai.signals.SignalSpy)) {
throw new Error('Expected a SignalSpy');
}
result = {
pass: (expectedCount === undefined) ? !!(spy.count) : spy.count === expectedCount
};
result.message = result.pass ?
'Expected ' + spy.signal.toString() + ' not to have been dispatched' :
'Expected ' + spy.signal.toString() + ' to have been dispatched';
if (expectedCount > 0) {
result.message += ' ' + expectedCount + ' times but was ' + spy.count;
}
if (spy.expectedArgs !== undefined) {
result.message += ' with (' + spy.expectedArgs.join(',') + ')';
result.message += ' but was with ' + actualToString(spy);
}
this.assert(
result.pass,
result.message,
result.message
);
});
/**
* Returns true if the signal has been dispatched with argument
*
* Where x and y are objects or primitives:
*
* expect(signalSpy).to.have.been.dispatched(x);
* expect(signalSpy).to.have.been.dispatchedWith(x, y);
* expect(signal).to.have.been.dispatched(x);
* expect(signal).to.have.been.dispatchedWith(x, y);
*
* expect(signalSpy).to.not.have.been.dispatchedWith(x);
* expect(signalSpy).to.not.have.been.dispatchedWith(x, y);
* expect(signal).to.not.have.been.dispatchedWith(x);
* expect(signal).to.not.have.been.dispatchedWith(x, y);
*
*/
method('dispatchedWith', function(expectedParam){
var result, spy = getSpy(this._obj), args = [].slice.call(arguments);
if (!(spy instanceof chai.signals.SignalSpy)) {
throw new Error('Expected a SignalSpy');
}
result = {
pass: spy.dispatches.filter(spy.signalMatcher).map(function (d) {
return utils.eql(d, args);
}).reduce(function (a, b) {
return a || b;
}, false)
};
result.message = result.pass ?
'Expected ' + spy.signal.toString() + ' not to have been dispatched' :
'Expected ' + spy.signal.toString() + ' to have been dispatched';
if (expectedParam !== undefined) {
result.message += ' with (' + args.join(', ') + ')';
result.message += ' but was ' + (spy.dispatches.length ? 'with ' + actualToString(spy) : 'not dispatched');
}
this.assert(
result.pass,
result.message,
result.message
);
});
/*
* Spies definitions
*/
chai.signals.spyOnSignal = function (signal, matcher) {
var spy = new chai.signals.SignalSpy(signal, matcher);
spies.push(spy);
return spy;
};
chai.signals.spyOnSignal.spyOnSignal = chai.signals.spyOnSignal;
/*
* Matchers
*/
function actualToString(spy) {
return spy.dispatches.map(function (d) {
return '(' + d + ')';
}).join('');
}
function getSpy(actual) {
if (!(actual === undefined) && !(actual === null) && (typeof actual.dispatch == 'function')) {
return spies.filter(function spiesForSignal(d) {
return d.signal === actual;
})[0];
}
return actual;
}
/*
* Spy implementation
*/
(function (namespace) {
namespace.SignalSpy = function (signal, matcher) {
if ((signal === undefined) || (signal === null) || !(typeof signal.dispatch == 'function')) {
console.info("hit");
console.log(signal);
throw 'spyOnSignal requires a signal as a parameter';
}
this.signal = signal;
this.signalMatcher = matcher || allSignalsMatcher;
this.count = 0;
this.dispatches = [];
this.plan = function() { };
this.initialize();
};
function allSignalsMatcher() {
return true;
}
function onSignal() {
var paramArray = (arguments.length) ? Array.prototype.slice.call(arguments) : [];
this.dispatches.push(paramArray);
if (this.signalMatcher.apply(this, Array.prototype.slice.call(arguments))) {
this.count++;
}
this.signal.halt();
return this.plan.apply(this, arguments);
}
namespace.SignalSpy.prototype.initialize = function () {
this.signal.add(onSignal, this, 999);
};
namespace.SignalSpy.prototype.stop = function () {
this.signal.remove(onSignal, this);
};
namespace.SignalSpy.prototype.matching = function (predicate) {
this.signalMatcher = predicate;
return this;
};
namespace.SignalSpy.prototype.andCallThrough = function() {
this.plan = function() {
var planArgs = arguments;
this.stop(); //stop spying - remove the spy binding
this.signal._bindings && this.signal._bindings.forEach(function(binding) { //apply args to original listeners
var listener = binding.getListener();
listener.apply(this, planArgs);
}.bind(this));
this.initialize(); //start again - add our spy back
}.bind(this);
return this;
};
namespace.SignalSpy.prototype.andThrow = function(exceptionMsg) {
this.plan = function() {
throw exceptionMsg;
};
return this;
};
namespace.SignalSpy.prototype.andCallFake = function(fakeFunc) {
this.plan = fakeFunc;
return this;
};
})(chai.signals);
chai.signals.createSignalSpyObj = function(methodNames) {
var obj = {};
if (!Array.isArray(methodNames) || methodNames.length === 0) {
throw new Error('createSignalSpyObj requires a non-empty array of method names to create spies for');
}
methodNames.forEach(function(name) {
obj[name] = chai.signals.spyOnSignal(name);
});
return obj;
};
}
}());