diff --git a/helpers/legacyStore.js b/helpers/legacyStore.js index a7f3575..142f60e 100644 --- a/helpers/legacyStore.js +++ b/helpers/legacyStore.js @@ -6,13 +6,31 @@ var assign = require("can-util/js/assign/assign"); module.exports = function (count, make, filter) { /*jshint eqeqeq:false */ - var getUniqueId = (function () { + var nextUniqueId = (function () { var i = 0; return function () { return i++; } })(); + // Check to see if an existing item has the ID we want to assign + var isIdUnique = function (items, id) { + for (var key in items) { + if (items[key].id === id) { + return false; + } + } + return true; + } + + var getUniqueId = function (items) { + var id = nextUniqueId(); + while (!isIdUnique(items, id)) { + id = nextUniqueId(); + } + return id; + } + var items, findOne = function (id) { for (var i = 0; i < items.length; i++) { @@ -47,7 +65,7 @@ module.exports = function (count, make, filter) { var item = make(i, items); if (!item.id) { - item.id = getUniqueId(); + item.id = getUniqueId(items); } items.push(item); } @@ -256,7 +274,7 @@ module.exports = function (count, make, filter) { // If an ID wasn't passed into the request, we give the item // a unique ID. if (!item.id) { - item.id = getUniqueId(); + item.id = getUniqueId(items); } // Push the new item into the store. diff --git a/test/fixture_test.js b/test/fixture_test.js index 3eaa62e..4dc23f4 100644 --- a/test/fixture_test.js +++ b/test/fixture_test.js @@ -225,6 +225,57 @@ test('fixture.store fixtures should have unique IDs', function () { }); }); +test('fixture.store should assign unique IDs when fixtures provide IDs', function () { + /* NOTE: We are testing whether the unique ID we are assigning to a new + item will account for IDs which the user has provided. + */ + + /* NOTE: These integers are used because IDs are created sequentially from 0. + Here, 0 1 and 2 must be skipped because they exist already. + If the implementation is changed this test will need updated. + */ + var store = fixture.store([ + {id: 0, name: 'Object 0'}, + {id: 1, name: 'Object 1'}, + {id: 2, name: 'Object 2'} + ]); + + fixture('POST /models', store.createData); + + function then (ajax, callback) { + ajax.then(callback, function (error) { + ok(false, 'ajax failure: ' + error); + start(); + }); + } + + var request = $.ajax({ + url: '/models', + dataType: 'json', + type: 'post', + data: { + name: 'My test object' + } + }); + + stop(); + then(request, function (response) { + notEqual(response.id, 0); + notEqual(response.id, 1); + notEqual(response.id, 2); + + /* NOTE: This check will fail if the underlying implementation changes. + This 3 is tightly coupled to the implementation. + If this is the only breaking assertion, update the provided IDs to + properly test the edge-case and update these assertions. + This check only serves to notify you to update the checks. + */ + equal(response.id, 3); + + start(); + }); +}); + test('simulating an error', function () { fixture('/foo', function (request, response) {