Permalink
Browse files

In tests, replace async methods with sync versions

This approach is an experimental attempt to allow
tests to make assertions about the side-effects of
asynchronous methods without having to explicitly
declare a run loop using Ember.run.

Now, any method that would trigger an autorun
will instead wrap a run loop around its
invocation (assuming that you have not already
created a run loop).

This means that if your async method makes other
async calls downstack, those calls will still
retain async behavior, but the top-level entry
point will become synchronous.
  • Loading branch information...
1 parent 666157d commit 2fd02eadc73251e38a3fe01d6a41b51b48fcf2ab Tomhuda Katzdale committed Jun 22, 2012
@@ -6,19 +6,15 @@ module("Application boot", {
root: Ember.Route.extend()
});
- Ember.run(function() {
- app = Ember.Application.create({
- Router: Router,
- Store: DS.Store,
- FooController: Ember.Controller.extend()
- });
+ app = Ember.Application.create({
+ Router: Router,
+ Store: DS.Store,
+ FooController: Ember.Controller.extend()
});
},
teardown: function() {
- Ember.run(function() {
- app.destroy();
- });
+ app.destroy();
}
});
@@ -45,9 +45,7 @@ test("when adding a record to an association that belongs to another record that
}
};
- Ember.run(function() {
- transaction.commit();
- });
+ transaction.commit();
});
test("if a record is added to the store while a child is pending, auto-committing the child record should not commit the new record", function() {
@@ -74,9 +72,7 @@ test("if a record is added to the store while a child is pending, auto-committin
}
};
- Ember.run(function() {
- store.commit();
- });
+ store.commit();
});
test("if a parent record and an uncommitted pending child belong to different transactions, committing the parent's transaction does not cause the child's transaction to commit", function() {
@@ -102,9 +98,7 @@ test("if a parent record and an uncommitted pending child belong to different tr
}
};
- Ember.run(function() {
- parentTransaction.commit();
- });
+ parentTransaction.commit();
});
var async = function(callback, timeout) {
@@ -178,9 +178,7 @@ test("if an adapter implements the generateIdForRecord method, it gets invoked w
equal(comment.toJSON().post_id, "id-2", "assigned id is immediately available in JSON form of record");
- Ember.run(function() {
- store.commit();
- });
+ store.commit();
});
test("when a store is committed, the adapter's commit method is called with updates", function() {
@@ -117,9 +117,7 @@ test("if an association's added and removed records are persisted, its isDirty i
equal(get(person3, 'isDirty'), true, "precond - person 3 is dirty");
- Ember.run(function() {
- store.commit();
- });
+ store.commit();
equal(get(person3, 'isDirty'), false, "precond - person 3 is clean");
equal(get(people, 'isDirty'), false, "the association becomes clean after records are committed");
@@ -265,16 +265,12 @@ test("when a DS.Model updates its attributes, its changes affect its filtered Ar
equal(get(person, 'name'), "Scumbag Katz", "precond - the item is correct");
- Ember.run(function() {
- set(person, 'name', "Yehuda Katz");
- });
+ set(person, 'name', "Yehuda Katz");
equal(get(people, 'length'), 1, "there is still one item");
equal(get(person, 'name'), "Yehuda Katz", "it has the updated item");
- Ember.run(function() {
- set(person, 'name', "Yehuda Katz-Foo");
- });
+ set(person, 'name', "Yehuda Katz-Foo");
equal(get(people, 'length'), 0, "there are now no items");
});
@@ -307,16 +303,12 @@ test("when a DS.Model updates its attributes, its changes affect its filtered Ar
equal(get(person, 'name'), "Scumbag Katz", "precond - the item is correct");
- Ember.run(function() {
- set(person, 'name', "Yehuda Katz");
- });
+ set(person, 'name', "Yehuda Katz");
equal(get(people, 'length'), 1, "there is still one item");
equal(get(person, 'name'), "Yehuda Katz", "it has the updated item");
- Ember.run(function() {
- set(person, 'name', "Yehuda Katz-Foo");
- });
+ set(person, 'name', "Yehuda Katz-Foo");
equal(get(people, 'length'), 0, "there are now no items");
});
@@ -342,9 +334,7 @@ test("when a new record depends on the state of another record, it enters the pe
equal(get(childComment, 'isPending'), true, "Child comment is pending on the parent comment");
- Ember.run(function() {
- store.commit();
- });
+ store.commit();
equal(get(parentComment, 'isLoaded'), true, "precond - Parent comment is loaded");
equal(get(parentComment, 'isDirty'), false, "precond - Parent comment is not dirty");
@@ -375,9 +365,7 @@ test("when a new record depends on the state of another record which depends on
equal(get(childComment, 'isPending'), true, "Child comment is pending on the parent comment");
equal(get(grandchildComment, 'isPending'), true, "Grandchild comment is pending on the child comment");
- Ember.run(function() {
- store.commit();
- });
+ store.commit();
equal(get(parentComment, 'isLoaded'), true, "precond - Parent comment is loaded");
equal(get(parentComment, 'isDirty'), false, "precond - Parent comment is not dirty");
@@ -411,9 +399,7 @@ test("when an updated record depends on the state of another record, it enters t
var childComment = store.createRecord(Comment);
- Ember.run(function() {
- store.commit();
- });
+ store.commit();
parentComment = store.createRecord(Comment);
@@ -427,9 +413,7 @@ test("when an updated record depends on the state of another record, it enters t
equal(get(childComment, 'isPending'), true, "Child comment is pending on the parent comment");
- Ember.run(function() {
- store.commit();
- });
+ store.commit();
equal(get(parentComment, 'isLoaded'), true, "precond - Parent comment is loaded");
equal(get(parentComment, 'isDirty'), false, "precond - Parent comment is not dirty");
@@ -460,9 +444,7 @@ test("when a loaded record depends on the state of another record, it enters the
childComment = store.createRecord(Comment);
- Ember.run(function() {
- store.commit();
- });
+ store.commit();
equal(childComment.get('isDirty'), false, "precond - record is not marked as dirty");
equal(childComment.get('isNew'), false, "precond - record is not new");
@@ -473,9 +455,7 @@ test("when a loaded record depends on the state of another record, it enters the
equal(get(childComment, 'isDirty'), true, "child comment is marked as dirty once a dependency has been created");
equal(get(childComment, 'isPending'), true, "Child comment is pending on the parent comment");
- Ember.run(function() {
- store.commit();
- });
+ store.commit();
equal(get(parentComment, 'isLoaded'), true, "precond - Parent comment is loaded");
equal(get(parentComment, 'isDirty'), false, "precond - Parent comment is not dirty");
@@ -514,9 +494,7 @@ test("when a record depends on another record, we can delete the first record an
equal(get(childComment, 'isDirty'), false, "child record should not be dirty since it was deleted and never saved");
equal(get(parentComment, 'isDirty'), true, "parent comment has not yet been saved");
- Ember.run(function() {
- store.commit();
- });
+ store.commit();
equal(get(parentComment, 'isDirty'), false, "parent comment has been saved");
ok(true, "no exception was thrown");
@@ -50,26 +50,25 @@ test("a newly created record is removed from a record array when it is deleted",
var store = DS.Store.create({ adapter: null }),
recordArray;
- Ember.run(function() {
- recordArray = store.findAll(Person);
+ recordArray = store.findAll(Person);
- var scumbag = store.createRecord(Person, {
- name: "Scumbag Dale"
- });
+ var scumbag = store.createRecord(Person, {
+ name: "Scumbag Dale"
+ });
+ // guarantee coalescence
+ Ember.run(function() {
store.createRecord(Person, { name: 'p1'});
store.createRecord(Person, { name: 'p2'});
store.createRecord(Person, { name: 'p3'});
+ });
- equal(get(recordArray, 'length'), 4, "precond - record array has the created item");
- equal(get(recordArray.objectAt(0), 'name'), "Scumbag Dale", "item at index 0 is record with id 1");
+ equal(get(recordArray, 'length'), 4, "precond - record array has the created item");
+ equal(get(recordArray.objectAt(0), 'name'), "Scumbag Dale", "item at index 0 is record with id 1");
- scumbag.deleteRecord();
- });
+ scumbag.deleteRecord();
- Ember.run(function() {
- equal(get(recordArray, 'length'), 3, "record is removed from the record array");
- });
+ equal(get(recordArray, 'length'), 3, "record is removed from the record array");
});
test("a record array can have a filter on it", function() {
@@ -250,6 +249,8 @@ test("a Record Array can update its filter and notify array observers", function
var store, recordArray;
var clientEdits = function(ids) {
+ // wrap in an Ember.run to guarantee coalescence of the
+ // iterated `set` calls.
Ember.run( function() {
ids.forEach( function(id) {
var person = store.find(Person, id);
@@ -259,6 +260,8 @@ test("a Record Array can update its filter and notify array observers", function
};
var clientCreates = function(names) {
+ // wrap in an Ember.run to guarantee coalescence of the
+ // iterated `set` calls.
Ember.run( function() {
names.forEach( function( name ) {
Person.createRecord({ name: 'Client-side ' + name });
@@ -267,7 +270,7 @@ test("a Record Array can update its filter and notify array observers", function
};
var serverResponds = function(){
- Ember.run(function() { store.commit(); });
+ store.commit();
};
var setup = function(serverCallbacks) {
@@ -379,9 +382,7 @@ test("a record array that backs a collection view functions properly", function(
content: store.findAll(Person)
});
- Ember.run(function() {
- container.appendTo('#qunit-fixture');
- });
+ container.appendTo('#qunit-fixture');
function compareArrays() {
var recordArray = container.content;
@@ -66,7 +66,10 @@ module("the REST adapter", {
adapter.destroy();
store.destroy();
- if (person) { person.destroy(); }
+ if (person) {
+ person.destroy();
+ person = null;
+ }
}
});
@@ -599,7 +602,7 @@ test("creating several people (with bulkCommit) makes a POST to /people, with a
test("bulk commits can sideload data", function() {
set(adapter, 'bulkCommit', true);
-
+
var tom = store.createRecord(Person, { name: "Tom Dale" });
var yehuda = store.createRecord(Person, { name: "Yehuda Katz" });
@@ -631,7 +634,7 @@ test("bulk commits can sideload data", function() {
test("updating several people (with bulkCommit) makes a PUT to /people/bulk with the data hash Array", function() {
set(adapter, 'bulkCommit', true);
-
+
store.loadMany(Person, [
{ id: 1, name: "Yehuda Katz" },
{ id: 2, name: "Carl Lerche" }
@@ -669,7 +672,7 @@ test("updating several people (with bulkCommit) makes a PUT to /people/bulk with
test("bulk updates can sideload data", function() {
set(adapter, 'bulkCommit', true);
-
+
adapter.mappings = {
groups: Group
};
@@ -717,7 +720,7 @@ test("bulk updates can sideload data", function() {
test("deleting several people (with bulkCommit) makes a PUT to /people/bulk", function() {
set(adapter, 'bulkCommit', true);
-
+
store.loadMany(Person, [
{ id: 1, name: "Yehuda Katz" },
{ id: 2, name: "Carl Lerche" }
@@ -752,7 +755,7 @@ test("deleting several people (with bulkCommit) makes a PUT to /people/bulk", fu
test("bulk deletes can sideload data", function() {
set(adapter, 'bulkCommit', true);
-
+
adapter.mappings = {
groups: Group
};
@@ -393,9 +393,7 @@ test("records inside a collection view should have their ids updated", function(
content: store.findAll(Person)
});
- Ember.run(function() {
- container.appendTo('#qunit-fixture');
- });
+ container.appendTo('#qunit-fixture');
store.createRecord(Person, {name: 'Tom Dale'});
store.createRecord(Person, {name: 'Yehuda Katz'});
@@ -335,10 +335,8 @@ module("DS.Transaction - relationships", {
},
teardown: function() {
- Ember.run(function() {
- adapter.destroy();
- store.destroy();
- });
+ adapter.destroy();
+ store.destroy();
}
});
@@ -394,13 +392,8 @@ test("If a child is removed from a parent it was recently added to, the dirty re
transaction.add(post);
transaction.add(comment);
- Ember.run(function() {
- post.get('comments').removeObject(comment);
- });
-
- Ember.run(function() {
- post.get('comments').pushObject(comment);
- });
+ post.get('comments').removeObject(comment);
+ post.get('comments').pushObject(comment);
var relationships = transaction.dirtyRelationships;
@@ -424,21 +417,10 @@ test("If a child was added to one parent, and then another, the changes coalesce
transaction.add(post);
transaction.add(comment);
- Ember.run(function() {
- post.get('comments').removeObject(comment);
- });
-
- Ember.run(function() {
- post2.get('comments').pushObject(comment);
- });
-
- Ember.run(function() {
- post2.get('comments').removeObject(comment);
- });
-
- Ember.run(function() {
- post3.get('comments').pushObject(comment);
- });
+ post.get('comments').removeObject(comment);
+ post2.get('comments').pushObject(comment);
+ post2.get('comments').removeObject(comment);
+ post3.get('comments').pushObject(comment);
var relationships = transaction.dirtyRelationships;
Oops, something went wrong.

0 comments on commit 2fd02ea

Please sign in to comment.