Skip to content

Commit

Permalink
Merge pull request #3083 from pangratz/invalid_new_record_can_be_roll…
Browse files Browse the repository at this point in the history
…backed

A new record which is marked as invalid can be rollbacked
  • Loading branch information
igorT committed Jun 2, 2015
2 parents 984d8e0 + 242ad45 commit 34efef0
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/ember-data/lib/system/model/states.js
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,9 @@ var createdState = dirtyState({
isNew: true
});

createdState.invalid.rolledBack = function(record) {
record.transitionTo('deleted.saved');
};
createdState.uncommitted.rolledBack = function(record) {
record.transitionTo('deleted.saved');
};
Expand Down
45 changes: 45 additions & 0 deletions packages/ember-data/tests/unit/model/rollback-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,51 @@ test("new record can be rollbacked", function() {
equal(person.get('isDeleted'), true, "must be deleted");
});

test("invalid new record can be rollbacked", function() {
var person;
var adapter = DS.RESTAdapter.extend({
ajax: function(url, type, hash) {
var adapter = this;

return new Ember.RSVP.Promise(function(resolve, reject) {
/* If InvalidError is passed back in the reject it will throw the
exception which will bubble up the call stack (crashing the test)
instead of hitting the failure route of the promise.
So wrapping the reject in an Ember.run.next makes it so save
completes without failure and the failure hits the failure route
of the promise instead of crashing the save. */
Ember.run.next(function() {
reject(adapter.ajaxError({ name: 'is invalid' }));
});
});
},

ajaxError: function(jqXHR) {
return new DS.InvalidError(jqXHR);
}
});

env = setupStore({ person: Person, adapter: adapter });

run(function() {
person = env.store.createRecord('person', { id: 1 });
});

equal(person.get('isNew'), true, "must be new");
equal(person.get('isDirty'), true, "must be dirty");

run(function() {
person.save().then(null, async(function() {
equal(person.get('isValid'), false);
person.rollback();

equal(person.get('isNew'), false, "must not be new");
equal(person.get('isDirty'), false, "must not be dirty");
equal(person.get('isDeleted'), true, "must be deleted");
}));
});
});

test("deleted record can be rollbacked", function() {
var person, people;

Expand Down

0 comments on commit 34efef0

Please sign in to comment.