From 58819ccad9fb737bebae19ac0734db35ab0d1efe Mon Sep 17 00:00:00 2001 From: Stefan Penner Date: Thu, 3 Sep 2015 10:21:52 -0700 Subject: [PATCH] tests for #3707 --- .../tests/integration/records/error-test.js | 157 ++++++++++++++++++ 1 file changed, 157 insertions(+) create mode 100644 packages/ember-data/tests/integration/records/error-test.js diff --git a/packages/ember-data/tests/integration/records/error-test.js b/packages/ember-data/tests/integration/records/error-test.js new file mode 100644 index 00000000000..e5e56eb4454 --- /dev/null +++ b/packages/ember-data/tests/integration/records/error-test.js @@ -0,0 +1,157 @@ +var env, store, Person; +var attr = DS.attr; +var run = Ember.run; + +module('integration/records/error', { + setup: function() { + Person = DS.Model.extend({ + firstName: attr('string'), + lastName: attr('string') + }); + Person.toString = function() { return 'Person'; }; + + env = setupStore({ + person: Person + }); + + store = env.store; + }, + + teardown: function() { + Ember.run(function() { + env.container.destroy(); + }); + } +}); + +test('adding errors during root.loaded.created.invalid works', function() { + expect(3); + + var person = run(() => { + store.push({ + data: { + type: 'person', + id: 'wat', + attributes: { + firstName: 'Yehuda', + lastName: 'Katz' + } + } + }); + return store.peekRecord('person', 'wat'); + }); + + Ember.run(() => { + person.set('firstName', null); + person.set('lastName', null); + }); + + equal(person._internalModel.currentState.stateName, 'root.loaded.updated.uncommitted'); + Ember.run(() => person.get('errors').add('firstName', 'is invalid') ); + + equal(person._internalModel.currentState.stateName, 'root.loaded.updated.invalid'); + + Ember.run(() => person.get('errors').add('lastName', 'is invalid') ); + + deepEqual(person.get('errors').toArray(), [ + { attribute: 'firstName', message: 'is invalid' }, + { attribute: 'lastName', message: 'is invalid' } + ]); +}); + + +test('adding errors root.loaded.created.invalid works', function() { + expect(3); + + var person = run(() => { + return store.createRecord('person', { + id: 'wat', + firstName: 'Yehuda', + lastName: 'Katz' + }); + }); + + Ember.run(() => { + person.set('firstName', null); + person.set('lastName', null); + }); + + equal(person._internalModel.currentState.stateName, 'root.loaded.created.uncommitted'); + + Ember.run(() => person.get('errors').add('firstName', 'is invalid') ); + + equal(person._internalModel.currentState.stateName, 'root.loaded.created.invalid'); + + Ember.run(() => person.get('errors').add('lastName', 'is invalid') ); + + deepEqual(person.get('errors').toArray(), [ + { attribute: 'firstName', message: 'is invalid' }, + { attribute: 'lastName', message: 'is invalid' } + ]); +}); + +test('adding errors root.loaded.created.invalid works add + remove + add', function() { + expect(4); + + var person = run(() => { + return store.createRecord('person', { + id: 'wat', + firstName: 'Yehuda' + }); + }); + + Ember.run(() => { + person.set('firstName', null); + }); + + equal(person._internalModel.currentState.stateName, 'root.loaded.created.uncommitted'); + + Ember.run(() => person.get('errors').add('firstName', 'is invalid') ); + + equal(person._internalModel.currentState.stateName, 'root.loaded.created.invalid'); + + Ember.run(() => person.get('errors').remove('firstName')); + + deepEqual(person.get('errors').toArray(), []); + + Ember.run(() => person.get('errors').add('firstName', 'is invalid') ); + + deepEqual(person.get('errors').toArray(), [ + { attribute: 'firstName', message: 'is invalid' } + ]); +}); + +test('adding errors root.loaded.created.invalid works add + (remove, add)', function() { + expect(4); + + var person = run(() => { + return store.createRecord('person', { + id: 'wat', + firstName: 'Yehuda' + }); + }); + + Ember.run(() => { + person.set('firstName', null); + }); + + equal(person._internalModel.currentState.stateName, 'root.loaded.created.uncommitted'); + + Ember.run(() => { + person.get('errors').add('firstName', 'is invalid'); + }); + + equal(person._internalModel.currentState.stateName, 'root.loaded.created.invalid'); + + Ember.run(() => { + person.get('errors').remove('firstName'); + person.get('errors').add('firstName', 'is invalid'); + }); + + + equal(person._internalModel.currentState.stateName, 'root.loaded.created.invalid'); + + deepEqual(person.get('errors').toArray(), [ + { attribute: 'firstName', message: 'is invalid' } + ]); +});