Skip to content

Commit

Permalink
Refactor sandbox to assimilate and remove collection
Browse files Browse the repository at this point in the history
  • Loading branch information
mroderick committed Apr 30, 2018
1 parent fe1e9b4 commit ce374aa
Show file tree
Hide file tree
Showing 9 changed files with 763 additions and 854 deletions.
8 changes: 4 additions & 4 deletions lib/sinon.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
"use strict";

exports.assert = require("./sinon/assert");
exports.collection = require("./sinon/collection");
exports.match = require("./sinon/match");
exports.spy = require("./sinon/spy");
exports.spyCall = require("./sinon/call");
exports.stub = require("./sinon/stub");
exports.mock = require("./sinon/mock");

var sandbox = require("./sinon/sandbox");
exports.sandbox = sandbox;
var Sandbox = require("./sinon/sandbox");
exports.sandbox = new Sandbox();
exports.createSandbox = require("./sinon/create-sandbox");

exports.expectation = require("./sinon/mock-expectation");
exports.createStubInstance = require("./sinon/stub").createStubInstance;

Expand All @@ -28,7 +29,6 @@ exports.useFakeXMLHttpRequest = nise.fakeXhr.useFakeXMLHttpRequest;
exports.fakeServer = nise.fakeServer;
exports.fakeServerWithClock = nise.fakeServerWithClock;

exports.createSandbox = sandbox.create;
exports.createFakeServer = nise.fakeServer.create.bind(nise.fakeServer);
exports.createFakeServerWithClock = nise.fakeServerWithClock.create.bind(nise.fakeServerWithClock);

Expand Down
161 changes: 0 additions & 161 deletions lib/sinon/collection.js

This file was deleted.

64 changes: 64 additions & 0 deletions lib/sinon/create-sandbox.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
"use strict";

var Sandbox = require("./sandbox");
var push = [].push;

function prepareSandboxFromConfig(config) {
var sandbox = new Sandbox();

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;
}

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 createSandbox(config) {
if (!config) {
return new Sandbox();
}

var configuredSandbox = prepareSandboxFromConfig(config);
configuredSandbox.args = configuredSandbox.args || [];
configuredSandbox.injectedKeys = [];
configuredSandbox.injectInto = config.injectInto;
var exposed = configuredSandbox.inject({});

if (config.properties) {
config.properties.forEach(function (prop) {
var value = exposed[prop] || prop === "sandbox" && configuredSandbox;
exposeValue(configuredSandbox, config, prop, value);
});
} else {
exposeValue(configuredSandbox, config, "sandbox");
}

return configuredSandbox;
}

module.exports = createSandbox;
Loading

0 comments on commit ce374aa

Please sign in to comment.