-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmock.js
More file actions
100 lines (84 loc) · 2.86 KB
/
mock.js
File metadata and controls
100 lines (84 loc) · 2.86 KB
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
var mock;
(function () {
"use strict";
mock = function () {
var returnValues = [], calls = [], returnValue;
function f() {
var now = new Date(), then = new Date();
calls.push({
callDate : now,
args : arguments,
that : this,
before : function (call) {
return this.callDate < call.callDate;
},
after : function (call) {
return this.callDate > call.callDate;
},
calledWith : function () {
var i;
for (i = 0; i < arguments.length; i += 1) {
if (arguments[i] !== this.args[i]) {
return false;
}
}
return true;
}
});
// wait 1 millisecond so that callDates are different
while (then - now < 1) {
then = new Date();
}
returnValue = returnValues[calls.length - 1] || returnValues[returnValues.length - 1];
return returnValue && returnValue.apply(this, arguments);
}
function addReturnValue(times, f) {
var t = 1, i;
if (times !== undefined) {
t = times;
}
for (i = 0; i < t; i += 1) {
returnValues.push(f);
}
}
f.returnValue = function (value, times) {
addReturnValue(times, function () {
return value;
});
return f;
};
f.throwError = function (value, times) {
addReturnValue(times, function () {
throw value;
});
return f;
};
f.invoke = function (value, times) {
addReturnValue(times, function () {
return value.apply(this, arguments);
});
return f;
};
f.calls = calls;
f.called = function () {
return calls.length;
};
f.before = function (mock) {
return this.calls[0].before(mock.calls[0]);
};
f.after = function (mock) {
return this.calls[0].after(mock.calls[0]);
};
f.calledWith = function () {
var result;
f.calledWith.index = f.calledWith.index || 0;
if (f.calledWith.index >= this.calls.length) {
return false;
}
result = this.calls[f.calledWith.index].calledWith.apply(this.calls[f.calledWith.index], arguments);
f.calledWith.index = f.calledWith.index + 1;
return result;
};
return f;
};
}());