Skip to content

Commit

Permalink
fix: <Cache>.changedAttrs should be empty if no local mutations exist (
Browse files Browse the repository at this point in the history
…#8335)

* Add failing test for #8329

* fix lint

* fix: clear changes if no local mutations exist

Co-authored-by: Chris Thoburn <runspired@users.noreply.github.com>
  • Loading branch information
timmorey and runspired authored Dec 7, 2022
1 parent 95971ec commit 55e137e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/record-data/addon/-private/record-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,7 @@ function setupRelationships(
function patchLocalAttributes(cached: CachedResource): boolean {
const { localAttrs, remoteAttrs, inflightAttrs, changes } = cached;
if (!localAttrs) {
cached.changes = null;
return false;
}
let hasAppliedPatch = false;
Expand Down
40 changes: 40 additions & 0 deletions tests/main/tests/unit/model-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1217,6 +1217,46 @@ module('unit/model - Model', function (hooks) {
await cat.save();
});

test('changedAttributes() reset after save', async function (assert) {
adapter.updateRecord = function (store, type, snapshot) {
return resolve({
data: {
id: '1',
type: 'person',
attributes: {
name: snapshot.attr('name'),
},
},
});
};

const originalName = 'the original name';
const newName = 'a new name';

const person = store.push({
data: {
type: 'person',
id: '1',
attributes: {
name: originalName,
},
},
});

person.name = newName;

assert.deepEqual(
person.changedAttributes().name,
[originalName, newName],
'changedAttributes() reports old/new values before save'
);

await person.save();

const changes = person.changedAttributes();
assert.deepEqual(changes, {}, 'changedAttributes() reset after save');
});

if (gte('3.10.0')) {
test('@attr decorator works without parens', async function (assert) {
assert.expect(1);
Expand Down

0 comments on commit 55e137e

Please sign in to comment.