Skip to content

Commit

Permalink
Split find and findAll tests
Browse files Browse the repository at this point in the history
This commit also adds an invokeAsync helper
for invoking a callback after a specified
interval, while not screwing up QUnit tests.
  • Loading branch information
tomhuda committed Jul 25, 2012
1 parent 839e413 commit 5354b46
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 28 deletions.
3 changes: 2 additions & 1 deletion .jshintrc
Expand Up @@ -23,7 +23,8 @@
"module", "module",
"expect", "expect",
"minispade", "minispade",
"async" "async",
"invokeAsync"
], ],


"node" : false, "node" : false,
Expand Down
75 changes: 75 additions & 0 deletions packages/ember-data/tests/integration/find_all_test.js
@@ -0,0 +1,75 @@
var get = Ember.get, set = Ember.set;

var Person, adapter, store, allRecords;

module("Finding All Records of a Type", {
setup: function() {
Person = DS.Model.extend({
updatedAt: DS.attr('string'),
name: DS.attr('string'),
firstName: DS.attr('string'),
lastName: DS.attr('string')
});

adapter = DS.Adapter.create();
store = DS.Store.create({ adapter: adapter });
allRecords = null;
},

teardown: function() {
if (allRecords) { allRecords.destroy(); }
adapter.destroy();
store.destroy();
}
});

test("When all records for a type are requested, the store should call the adapter's `findAll` method.", function() {
expect(5);

adapter.findAll = function(store, type) {
ok(true, "the adapter's findAll method should be invoked");

// Simulate latency to ensure correct behavior in asynchronous conditions.
invokeAsync(function() {
store.load(type, { id: 1, name: "Braaaahm Dale" });

equal(get(allRecords, 'length'), 1, "the record array's length is 1 after a record is loaded into it");
equal(allRecords.objectAt(0).get('name'), "Braaaahm Dale", "the first item in the record array is Braaaahm Dale");

// Only one record array per type should ever be created (identity map)
strictEqual(allRecords, store.find(Person), "the same record array is returned every time all records of a type are requested");
});
};

allRecords = store.find(Person);
equal(get(allRecords, 'length'), 0, "the record array's length is zero before any records are loaded");
});

test("When all records for a type are requested, records that are already loaded should be returned immediately.", function() {
expect(3);

// Load a record from the server
store.load(Person, { id: 1, name: "Jeremy Ashkenas" });

// Create a new, unsaved record in the store
store.createRecord(Person, { name: "Alex MacCaw" });

allRecords = store.find(Person);

equal(get(allRecords, 'length'), 2, "the record array's length is 2");
equal(allRecords.objectAt(0).get('name'), "Jeremy Ashkenas", "the first item in the record array is Jeremy Ashkenas");
equal(allRecords.objectAt(1).get('name'), "Alex MacCaw", "the first item in the record array is Jeremy Ashkenas");
});

test("When all records for a type are requested, records that are created on the client should be added to the record array.", function() {
expect(3);

allRecords = store.find(Person);

equal(get(allRecords, 'length'), 0, "precond - the record array's length is zero before any records are loaded");

store.createRecord(Person, { name: "Carsten Nielsen" });

equal(get(allRecords, 'length'), 1, "the record array's length is 1");
equal(allRecords.objectAt(0).get('name'), "Carsten Nielsen", "the first item in the record array is Carsten Nielsen");
});
27 changes: 0 additions & 27 deletions packages/ember-data/tests/integration/store_adapter_test.js
Expand Up @@ -63,33 +63,6 @@ test("when an association is loaded, the adapter's find method should not be cal
store.load(Person, { id: 1, comments: [ 1 ] }); store.load(Person, { id: 1, comments: [ 1 ] });
}); });


test("when all records for a type are requested, the adapter's findAll method is called", function() {
expect(2);

var count = 0;

adapter.findAll = function(store, type) {
count++;

if (count === 1) {
stop();

setTimeout(function() {
start();

store.load(type, { id: 1, name: "Braaaahm Dale" });
equal(get(array, 'length'), 1, "The array is now 1 length");

store.findAll(Person);
}, 100);
} else {
ok(false, "Should not get here");
}
};

var array = store.findAll(Person);
equal(get(array, 'length'), 0, "The array is 0 length do far");
});


test("if an adapter implements the generateIdForRecord method, it gets invoked when new records are created", function() { test("if an adapter implements the generateIdForRecord method, it gets invoked when new records are created", function() {
expect(7); expect(7);
Expand Down
6 changes: 6 additions & 0 deletions tests/index.html
Expand Up @@ -101,6 +101,12 @@ <h2 id="qunit-userAgent"></h2>
}; };
}; };


window.invokeAsync = function(callback, timeout) {
timeout = timeout || 1;

setTimeout(async(callback, timeout+100), timeout);
};

var syncForTest = function(fn) { var syncForTest = function(fn) {
var callSuper; var callSuper;


Expand Down

0 comments on commit 5354b46

Please sign in to comment.