-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshould-spies.js
193 lines (144 loc) · 4.13 KB
/
should-spies.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
(function(root, factory) {
if (typeof define === 'function' && define.amd) {
define(['should'], factory);
} else if (typeof module === 'object' && module.exports) {
factory(require('should'));
} else {
factory(root.should);
}
}(this, function(should) {
var t = should.modules.type;
var format = should.modules.format;
var ARRAY_SLICE = Array.prototype.slice;
var callId = 0;
function createProxyFunction(func) {
var proxyFunc = function() {
var args = ARRAY_SLICE.call(arguments);
var call = { args: args, context: this, id: callId++ };
proxyFunc.calls.push(call);
return func.apply(this, args);
};
Object.defineProperties(proxyFunc, {
called: {
get: function() {
return this.calls.length > 0;
}
},
isSpy: {
value: true,
configurable: false
},
lastCall: {
get: function() {
if (this.called) {
return this.calls[this.calls.length - 1];
}
}
}
});
proxyFunc.reset = function() {
this.calls = [];
};
proxyFunc.reset();
return proxyFunc;
}
function spy(func) {
var proxy = createProxyFunction(func || function() {});
return proxy;
}
var SPY = new t.Type(t.OBJECT, 'should-spy');
t.checker.addBeforeFirstMatch(function() {}, function(obj) {
if (obj.isSpy) {
return SPY;
}
});
format.Formatter.addType(SPY, function() {
return '{ Spy }';
});
/**
* should spy
* @param {function} [func] Optional function to be called
* @returns {Spy}
* @memberOf should
* @static
*/
should.spy = spy;
function on(obj, methodName) {
var originalFunc = obj[methodName];
var proxy = spy(obj[methodName]);
obj[methodName] = proxy;
proxy.restore = function() {
obj[methodName] = originalFunc;
};
return proxy;
}
spy.on = on;
function returns(something) {
return spy(function() {
return something;
});
}
spy.returns = returns;
function throws(err, msg) {
switch (typeof err) {
case 'string':
err = new Error(msg);
break;
case 'function':
msg = (typeof msg !== 'undefined') ? msg : 'Error thrown';
err = new err(msg);
break;
}
return spy(function() {
throw err;
});
}
spy.throws = throws;
var Assertion = should.Assertion;
Assertion.add('Spy', function() {
this.params = { operator: 'is spy' };
this.have.property('isSpy', true);
});
Assertion.add('called', function() {
this.params = { operator: 'to be called' };
this.obj.should.be.Spy();
this.assert(this.obj.called);
});
Assertion.add('callCount', function(number) {
this.params = { operator: 'to be called ' + should.format(number) + ' times' };
this.obj.should.be.Spy();
this.have.property('calls').which.has.length(number);
});
Assertion.add('calledOn', function(obj) {
this.params = { operator: 'to be called on ' + should.format(obj) };
this.obj.should.be.called();
var lastCall = this.obj.lastCall;
should(lastCall.context).be.exactly(obj);
});
Assertion.add('calledWith', function() {
this.params = { operator: 'to be called with ' + should.format(arguments) };
this.obj.should.be.called();
var lastCall = this.obj.lastCall;
should(lastCall.args).be.eql(ARRAY_SLICE.call(arguments));
});
Assertion.add('calledBefore', function(otherSpy) {
this.params = { operator: 'to be called before ' + should.format(otherSpy) };
var thisSpy = this.obj;
thisSpy.should.be.called();
if (!otherSpy.called) {
return;
}
var thisFirstCall = thisSpy.calls[0];
var otherLastCall = otherSpy.lastCall;
this.assert(thisFirstCall.id < otherLastCall.id);
});
Assertion.add('calledAfter', function(otherSpy) {
this.params = { operator: 'to be called after ' + should.format(otherSpy) };
var thisSpy = this.obj;
thisSpy.should.be.called();
otherSpy.should.be.called();
var thisLastCall = thisSpy.lastCall;
var otherLastCall = otherSpy.lastCall;
this.assert(thisLastCall.id > otherLastCall.id);
});
}));