Skip to content

Commit c36d42a

Browse files
Igor TerzicigorT
authored andcommitted
[BUGFIX] Fix #7015 - calling changedAttributes for non instantiated relationships
1 parent aacde0a commit c36d42a

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

packages/-ember-data/tests/integration/snapshot-test.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,42 @@ module('integration/snapshot - Snapshot', function(hooks) {
373373
assert.equal(relationship.attr('title'), 'Hello World', 'post title is correct');
374374
});
375375

376+
test('snapshot.belongsTo().changedAttributes() returns an empty object if belongsTo record in not instantiated #7015', function(assert) {
377+
assert.expect(2);
378+
379+
store.push({
380+
data: [
381+
{
382+
type: 'comment',
383+
id: '2',
384+
attributes: {
385+
body: 'This is comment',
386+
},
387+
relationships: {
388+
post: {
389+
data: { type: 'post', id: '1' },
390+
},
391+
},
392+
},
393+
],
394+
included: [
395+
{
396+
type: 'post',
397+
id: '1',
398+
attributes: {
399+
title: 'Hello World',
400+
},
401+
},
402+
],
403+
});
404+
let comment = store.peekRecord('comment', 2);
405+
let snapshot = comment._createSnapshot();
406+
let relationship = snapshot.belongsTo('post');
407+
408+
assert.ok(relationship instanceof Snapshot, 'snapshot is an instance of Snapshot');
409+
assert.deepEqual(relationship.changedAttributes(), {}, 'changedAttributes are correct');
410+
});
411+
376412
test('snapshot.belongsTo() returns null if relationship is deleted', function(assert) {
377413
assert.expect(1);
378414

packages/store/addon/-private/system/snapshot.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ export default class Snapshot implements Snapshot {
119119
@type {String}
120120
*/
121121
this.modelName = internalModel.modelName;
122-
123122
if (internalModel.hasRecord) {
124123
this._changedAttributes = recordDataFor(internalModel).changedAttributes();
125124
}
@@ -246,6 +245,10 @@ export default class Snapshot implements Snapshot {
246245
*/
247246
changedAttributes(): ChangedAttributesHash {
248247
let changedAttributes = Object.create(null);
248+
if (!this._changedAttributes) {
249+
return changedAttributes;
250+
}
251+
249252
let changedAttributeKeys = Object.keys(this._changedAttributes);
250253

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

0 commit comments

Comments
 (0)