-
-
Notifications
You must be signed in to change notification settings - Fork 787
Expand file tree
/
Copy pathsandbox.js
More file actions
152 lines (114 loc) · 3.67 KB
/
sandbox.js
File metadata and controls
152 lines (114 loc) · 3.67 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
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
"use strict";
var extend = require("./util/core/extend");
var sinonCollection = require("./collection");
var sinonMatch = require("./match");
var sinonAssert = require("./assert");
var sinonClock = require("./util/fake_timers");
var fakeServer = require("nise").fakeServer;
var fakeXhr = require("nise").fakeXhr;
var fakeServerWithClock = require("nise").fakeServerWithClock;
var push = [].push;
var sinonSandbox = Object.create(sinonCollection);
function exposeValue(sandbox, config, key, value) {
if (!value) {
return;
}
if (config.injectInto && !(key in config.injectInto)) {
config.injectInto[key] = value;
sandbox.injectedKeys.push(key);
} else {
push.call(sandbox.args, value);
}
}
function prepareSandboxFromConfig(config) {
var sandbox = Object.create(sinonSandbox);
if (config.useFakeServer) {
if (typeof config.useFakeServer === "object") {
sandbox.serverPrototype = config.useFakeServer;
}
sandbox.useFakeServer();
}
if (config.useFakeTimers) {
if (typeof config.useFakeTimers === "object") {
sandbox.useFakeTimers.call(sandbox, config.useFakeTimers);
} else {
sandbox.useFakeTimers();
}
}
return sandbox;
}
extend(sinonSandbox, {
useFakeTimers: function (args) {
this.clock = sinonClock.useFakeTimers.call(null, args);
return this.add(this.clock);
},
serverPrototype: fakeServerWithClock,
useFakeServer: function useFakeServer() {
var proto = this.serverPrototype || fakeServer;
if (!proto || !proto.create) {
return null;
}
this.server = proto.create();
return this.add(this.server);
},
useFakeXMLHttpRequest: function useFakeXMLHttpRequest() {
var xhr = fakeXhr.useFakeXMLHttpRequest();
return this.add(xhr);
},
inject: function (obj) {
sinonCollection.inject.call(this, obj);
if (this.clock) {
obj.clock = this.clock;
}
if (this.server) {
obj.server = this.server;
obj.requests = this.server.requests;
}
obj.match = sinonMatch;
return obj;
},
usingPromise: function (promiseLibrary) {
this.promiseLibrary = promiseLibrary;
return this;
},
restore: function () {
if (arguments.length) {
throw new Error("sandbox.restore() does not take any parameters. Perhaps you meant stub.restore()");
}
sinonCollection.restore.apply(this, arguments);
this.restoreContext();
},
restoreContext: function () {
var injectedKeys = this.injectedKeys;
var injectInto = this.injectInto;
if (!injectedKeys) {
return;
}
injectedKeys.forEach(function (injectedKey) {
delete injectInto[injectedKey];
});
injectedKeys = [];
},
create: function (config) {
if (!config) {
return Object.create(sinonSandbox);
}
var sandbox = prepareSandboxFromConfig(config);
sandbox.args = sandbox.args || [];
sandbox.injectedKeys = [];
sandbox.injectInto = config.injectInto;
var exposed = sandbox.inject({});
if (config.properties) {
config.properties.forEach(function (prop) {
var value = exposed[prop] || prop === "sandbox" && sandbox;
exposeValue(sandbox, config, prop, value);
});
} else {
exposeValue(sandbox, config, "sandbox");
}
return sandbox;
},
match: sinonMatch,
assert: sinonAssert
});
module.exports = sinonSandbox;