Permalink
Browse files

Split find and findAll tests

This commit also adds an invokeAsync helper
for invoking a callback after a specified
interval, while not screwing up QUnit tests.
  • Loading branch information...
1 parent 839e413 commit 5354b4666502ef7aaa1281c5fbe13006430dc17e tomhuda committed Jul 25, 2012
View
3 .jshintrc
@@ -23,7 +23,8 @@
"module",
"expect",
"minispade",
- "async"
+ "async",
+ "invokeAsync"
],
"node" : false,
View
75 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");
+});
View
0 ...tests/integration/finding_records_test.js → ...ember-data/tests/integration/find_test.js
File renamed without changes.
View
27 packages/ember-data/tests/integration/store_adapter_test.js
@@ -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 ] });
});
-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() {
expect(7);
View
6 tests/index.html
@@ -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 callSuper;

0 comments on commit 5354b46

Please sign in to comment.