Skip to content

Commit

Permalink
Make IDs always unique; fixes #102
Browse files Browse the repository at this point in the history
  • Loading branch information
andrejewski committed Apr 24, 2017
1 parent e5873a9 commit 87711c3
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 10 deletions.
17 changes: 10 additions & 7 deletions helpers/legacyStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ var assign = require("can-util/js/assign/assign");

module.exports = function (count, make, filter) {
/*jshint eqeqeq:false */
// check
// the currentId to use when a new instance is created.
var currentId = 0,
items,
var getUniqueId = (function () {
var i = 0;
return function () {
return i++;
}
})();

var items,
findOne = function (id) {
for (var i = 0; i < items.length; i++) {
if (id == items[i].id) {
Expand Down Expand Up @@ -43,9 +47,8 @@ module.exports = function (count, make, filter) {
var item = make(i, items);

if (!item.id) {
item.id = i;
item.id = getUniqueId();
}
currentId = Math.max(item.id + 1, currentId + 1) || items.length;
items.push(item);
}
};
Expand Down Expand Up @@ -253,7 +256,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 = currentId++;
item.id = getUniqueId();
}

// Push the new item into the store.
Expand Down
36 changes: 33 additions & 3 deletions test/fixture_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,36 @@ test('fixture.store fixtures', function () {
});
});

test('fixture.store fixtures should have unique IDs', function () {
stop();
var store = fixture.store('thing', 100, function (i) {
return {name: 'Test ' + i};
});
fixture('things', store.findAll);

$.ajax({
url: 'things',
dataType: 'json',
data: {
offset: 0,
limit: 200,
order: ['name ASC'],
searchText: 'thing 2'
},
success: function (result) {
debugger;
var seenIds = [];
var things = result.data;
for (var thingKey in things) {
var thing = things[thingKey];
ok(seenIds.indexOf(thing.id) === -1);
seenIds.push(thing.id);
}
start();
}
});
});

test('simulating an error', function () {

fixture('/foo', function (request, response) {
Expand Down Expand Up @@ -1541,7 +1571,7 @@ asyncTest("response headers are set", function(){

xhr.addEventListener('load', function(){
var headers = parseHeaders(xhr.getAllResponseHeaders());

ok(headers.foo === "bar", "header was set");
start();
});
Expand Down Expand Up @@ -1633,7 +1663,7 @@ if ("onabort" in XMLHttpRequest._XHR.prototype) {
setTimeout(function() {
xhr.abort();
}, 50);


xhr.addEventListener('abort', function() {
fixture('/onload', null);
Expand Down Expand Up @@ -1721,7 +1751,7 @@ if ("onabort" in XMLHttpRequest._XHR.prototype) {
start();
});

xhr.send();
xhr.send();
});

asyncTest('should be able to call getResponseHeader onload', function() {
Expand Down

0 comments on commit 87711c3

Please sign in to comment.