Skip to content

Commit

Permalink
Make IDs unique based on existing items; fixes #102
Browse files Browse the repository at this point in the history
  • Loading branch information
andrejewski committed Apr 25, 2017
1 parent 685ea92 commit 0c5d0d4
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 3 deletions.
24 changes: 21 additions & 3 deletions helpers/legacyStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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.
Expand Down
51 changes: 51 additions & 0 deletions test/fixture_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 0c5d0d4

Please sign in to comment.