Skip to content

Commit

Permalink
#34 return removed fixtures when adding new fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
therealraw authored and cherifGsoul committed Dec 19, 2018
1 parent 36c079f commit 575da22
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
16 changes: 13 additions & 3 deletions core.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,12 @@ function getSettingsFromString (route) {
// from our array of fixture overwrites.
function upsertFixture (fixtureList, settings, fixture) {
var index = exports.index(settings, true);
var oldFixture;
if (index > -1) {
fixtures.splice(index, 1);
oldFixture = fixtures.splice(index, 1);
}
if (fixture == null) {
return;
return oldFixture;
}
if(typeof fixture === "object") {
var data = fixture;
Expand All @@ -139,10 +140,19 @@ function upsertFixture (fixtureList, settings, fixture) {
}
settings.fixture = fixture;
fixtures.unshift(settings);
return oldFixture;
}

// Adds a fixture to the list of fixtures.
exports.add = function (settings, fixture) {

if (Array.isArray(settings)) {
var allFixtures = settings.reduce(function(oldFixtures, currentValue){
return oldFixtures.concat(upsertFixture(fixtures, currentValue, currentValue.fixture));
},[]);
return allFixtures;
}

// If a fixture isn't provided, we assume that settings is
// an array of fixtures, and we should iterate over it, and set up
// the new fixtures.
Expand All @@ -164,7 +174,7 @@ exports.add = function (settings, fixture) {
if (typeof settings === 'string') {
settings = getSettingsFromString(settings);
}
upsertFixture(fixtures, settings, fixture);
return upsertFixture(fixtures, settings, fixture);
};

var $fixture = exports.add;
Expand Down
31 changes: 31 additions & 0 deletions test/fixture_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1827,6 +1827,37 @@ asyncTest('should allow FormData as data type (#133)', function() {
xhr.send(data);
});

asyncTest('fixture returns the old fixture callback when fixtures are removed (#34)', 2, function() {

function funcA (){
QUnit.ok(true);
return {};
}
fixture("services/thing", funcA);

// in a test, remove default fixture and provide your own
var oldFixtures = fixture("services/thing", null);

QUnit.deepEqual(oldFixtures, [{fixture: funcA, url: 'services/thing'}]);
fixture(oldFixtures);

var xhr = new XMLHttpRequest();
xhr.addEventListener('load', function() {
start();
});
xhr.open('GET', 'services/thing');
xhr.send({});

// fixture("services/thing", function(){ ... CODE B ... });
// run test
//TEST

// restore old fixture
// can.fixture("services/thing", oldFixture)

// var data = [];
});

if ("onabort" in XMLHttpRequest._XHR.prototype) {
asyncTest('fixture with timeout aborts if xhr timeout less than delay', function() {
fixture('/onload', 1000);
Expand Down

0 comments on commit 575da22

Please sign in to comment.