-
Notifications
You must be signed in to change notification settings - Fork 0
/
stub.js
55 lines (44 loc) · 1.44 KB
/
stub.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
espionage.extend("stub", function(e) {
var stubbedFunctions = [],
resolveNamespace = espionage._util.resolveNamespace;
e.extendGlobals("stub", function(namespace, property, value) {
if (typeof namespace === "string") {
value = property;
property = namespace;
namespace = window;
}
var resolved = resolveNamespace(namespace, property);
namespace = resolved.namespace;
property = resolved.property;
stubbedFunctions.push({
original: namespace[property],
stubbed: value,
namespace: namespace,
property: property
});
namespace[property] = value;
});
e.extendGlobals("unstub", function(namespace, property) {
if (typeof namespace === "function" && typeof property == "undefined") {
return findStubbed(namespace).original;
}
var resolved = resolveNamespace(namespace, property);
namespace = resolved.namespace;
property = resolved.property;
namespace[property] = findStubbed(namespace[property]).original;
});
e.extendTeardown(function() {
for (var i = stubbedFunctions.length - 1; i >= 0; i--) {
var stubbed = stubbedFunctions[i];
stubbed.namespace[stubbed.property] = stubbed.original;
}
stubbedFunctions = [];
});
function findStubbed(stubbed) {
for (var i = 0; i < stubbedFunctions.length; i++) {
if (stubbedFunctions[i].stubbed === stubbed) {
return stubbedFunctions[i];
}
}
}
});