This repository has been archived by the owner on Jun 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
jasmine-signals.js
211 lines (176 loc) · 6.91 KB
/
jasmine-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
(function (global) {
var spies = [];
jasmine.signals = {};
/*
* Spies definitions
*/
jasmine.signals.spyOnSignal = function (signal, matcher) {
var spy = new jasmine.signals.SignalSpy(signal, matcher);
spies.push(spy);
return spy;
};
jasmine.signals.spyOnSignal.spyOnSignal = jasmine.signals.spyOnSignal;
/*
* Matchers
*/
function actualToString(spy) {
return spy.dispatches.map(function (d) {
return '(' + d + ')';
}).join('');
}
function getSpy(actual) {
if (actual instanceof signals.Signal) {
return spies.filter(function spiesForSignal(d) {
return d.signal === actual;
})[0];
}
return actual;
}
jasmine.signals.matchers = {
toHaveBeenDispatched: function (util, customEqualityTesters) {
return {
compare: function (actual, expectedCount) {
var result, spy = getSpy(actual);
if (!(spy instanceof jasmine.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 (actual.expectedArgs !== undefined) {
result.message += ' with (' + spy.expectedArgs.join(',') + ')';
result.message += ' but was with ' + actualToString(spy);
}
return result;
}
};
},
toHaveBeenDispatchedWith: function (util, customEqualityTesters) {
return {
compare: function (actual, expectedParam) {
var result, spy = getSpy(actual), args = [].slice.call(arguments);
args.shift();
if (!(spy instanceof jasmine.signals.SignalSpy)) {
throw new Error('Expected a SignalSpy');
}
result = {
pass: spy.dispatches.filter(spy.signalMatcher).map(function (d) {
return util.equals(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');
}
return result;
}
};
}
};
/*
* Spy implementation
*/
(function (namespace) {
namespace.SignalSpy = function (signal, matcher) {
if (!(signal instanceof signals.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;
};
})(jasmine.signals);
jasmine.createSignalSpyObj = function(methodNames) {
var obj = {};
if (!jasmine.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] = jasmine.signals.spyOnSignal(new signals.Signal());
});
return obj;
};
beforeEach(function () {
jasmine.addMatchers(jasmine.signals.matchers);
});
afterEach(function () {
spies.forEach(function (d) {
d.stop();
});
spies = [];
});
function setGlobals(signals, spyOnSignal) {
global['signals'] = signals;
global['spyOnSignal'] = spyOnSignal;
}
// exports to multiple environments
if (typeof define === 'function' && define.amd) { // AMD
define(['signals'], function (amdSignals) {
setGlobals(amdSignals, jasmine.signals.spyOnSignal);
return jasmine.signals.spyOnSignal;
});
} else {
setGlobals(global['signals'], jasmine.signals.spyOnSignal);
if (typeof module !== 'undefined' && module.exports) { // node
module.exports = jasmine.signals.spyOnSignal;
} else { } // browser
}
} (this));