Skip to content

Commit

Permalink
[BUGFIX] Fix #7015 - calling changedAttributes for non instantiated r…
Browse files Browse the repository at this point in the history
…elationships
  • Loading branch information
igorT committed Mar 6, 2020
1 parent 118d729 commit 78e0b41
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
36 changes: 36 additions & 0 deletions packages/-ember-data/tests/integration/snapshot-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,42 @@ module('integration/snapshot - Snapshot', function(hooks) {
assert.equal(relationship.attr('title'), 'Hello World', 'post title is correct');
});

test('snapshot.belongsTo().changedAttributes() returns an empty object if belongsTo record in not instantiated #7015', function(assert) {
assert.expect(2);

store.push({
data: [
{
type: 'comment',
id: '2',
attributes: {
body: 'This is comment',
},
relationships: {
post: {
data: { type: 'post', id: '1' },
},
},
},
],
included: [
{
type: 'post',
id: '1',
attributes: {
title: 'Hello World',
},
},
],
});
let comment = store.peekRecord('comment', 2);
let snapshot = comment._createSnapshot();
let relationship = snapshot.belongsTo('post');

assert.ok(relationship instanceof Snapshot, 'snapshot is an instance of Snapshot');
assert.deepEqual(relationship.changedAttributes(), {}, 'changedAttributes are correct');
});

test('snapshot.belongsTo() returns null if relationship is deleted', function(assert) {
assert.expect(1);

Expand Down
5 changes: 4 additions & 1 deletion packages/store/addon/-private/system/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ export default class Snapshot implements Snapshot {
@type {String}
*/
this.modelName = internalModel.modelName;

if (internalModel.hasRecord) {
this._changedAttributes = recordDataFor(internalModel).changedAttributes();
}
Expand Down Expand Up @@ -246,6 +245,10 @@ export default class Snapshot implements Snapshot {
*/
changedAttributes(): ChangedAttributesHash {
let changedAttributes = Object.create(null);
if (!this._changedAttributes) {
return changedAttributes;
}

let changedAttributeKeys = Object.keys(this._changedAttributes);

for (let i = 0, length = changedAttributeKeys.length; i < length; i++) {
Expand Down

0 comments on commit 78e0b41

Please sign in to comment.