Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUGFIX beta] invalid record becomes loaded when property is reset #5216

Merged
merged 1 commit into from
Oct 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions addon/-private/system/model/states.js
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,13 @@ function assertAgainstUnloadRecord(internalModel) {
assert("You can only unload a record which is not inFlight. `" + internalModel + "`", false);
}

updatedState.invalid.becameValid = function(internalModel) {
// we're eagerly transition into the loaded.saved state, even though we could
// be still dirty; but the setup hook of the loaded.saved state checks for
// dirty attributes and transitions into the corresponding dirty state
internalModel.transitionTo('loaded.saved');
};

updatedState.inFlight.unloadRecord = assertAgainstUnloadRecord;

updatedState.uncommitted.deleteRecord = function(internalModel) {
Expand Down
77 changes: 77 additions & 0 deletions tests/unit/model-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,83 @@ test('a record becomes clean again only if all changed properties are reset', fu
});
});

test('an invalid record becomes clean again if changed property is reset', function(assert) {
env.adapter.shouldBackgroundReloadRecord = () => false;
env.adapter.updateRecord = () => {
var error = new DS.InvalidError([{ name: 'not valid' }]);

return EmberPromise.reject(error);
};

return run(() => {
store.push({
data: {
type: 'person',
id: '1',
attributes: {
name: 'Peter',
isDrugAddict: true
}
}
});

let person = store.peekRecord('person', 1);

assert.equal(person.get('hasDirtyAttributes'), false, 'precond - person record should not be dirty');
person.set('name', 'Wolf');
assert.equal(person.get('hasDirtyAttributes'), true, 'record becomes dirty after setting one property to a new value');

return person.save().catch(() => {
assert.equal(person.get('isValid'), false, 'record is not valid');
assert.equal(person.get('hasDirtyAttributes'), true, 'record still has dirty attributes');

person.set('name', 'Peter');

assert.equal(person.get('isValid'), true, 'record is valid after resetting attribute to old value');
assert.equal(person.get('hasDirtyAttributes'), false, "record becomes clean after resetting property to the old value");
});
});
});

test('an invalid record stays dirty if only invalid property is reset', function(assert) {
env.adapter.shouldBackgroundReloadRecord = () => false;
env.adapter.updateRecord = () => {
var error = new DS.InvalidError([{ name: 'not valid' }]);

return EmberPromise.reject(error);
};

return run(() => {
store.push({
data: {
type: 'person',
id: '1',
attributes: {
name: 'Peter',
isDrugAddict: true
}
}
});

let person = store.peekRecord('person', 1);

assert.equal(person.get('hasDirtyAttributes'), false, 'precond - person record should not be dirty');
person.set('name', 'Wolf');
person.set('isDrugAddict', false);
assert.equal(person.get('hasDirtyAttributes'), true, 'record becomes dirty after setting one property to a new value');

return person.save().catch(() => {
assert.equal(person.get('isValid'), false, 'record is not valid');
assert.equal(person.get('hasDirtyAttributes'), true, 'record still has dirty attributes');

person.set('name', 'Peter');

assert.equal(person.get('isValid'), true, 'record is valid after resetting invalid attribute to old value');
assert.equal(person.get('hasDirtyAttributes'), true, "record still has dirty attributes");
});
});
});

test('a record reports its unique id via the `id` property', function(assert) {
assert.expect(1);
env.adapter.shouldBackgroundReloadRecord = () => false;
Expand Down