Skip to content

Commit

Permalink
clear relationships during delete (#7109)
Browse files Browse the repository at this point in the history
  • Loading branch information
pieter-v authored and runspired committed Apr 25, 2020
1 parent b88e820 commit 0a9641d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { settled } from '@ember/test-helpers';

import { module, test } from 'qunit';

import { setupTest } from 'ember-qunit';
Expand Down Expand Up @@ -650,4 +652,43 @@ module('integration/relationships/inverse_relationships - Inverse Relationships'

assert.equal(comment.inverseFor('user'), null, 'Defaults to a null inverse');
});

test('Unload a destroyed record should clean the relations', async function(assert) {
assert.expect(3);

class Post extends Model {
@hasMany('comment', { async: true })
comments;
}

class Comment extends Model {
@belongsTo('post', { async: true })
post;
}

register('model:Post', Post);
register('model:Comment', Comment);

const comment = store.createRecord('comment');
const post = store.createRecord('post');

post.get('comments').pushObject(comment);

await comment.destroyRecord();
comment.unloadRecord();

await settled();

assert.deepEqual(
comment._internalModel.__recordData.__relationships.initializedRelationships,
{},
'relationships are cleared'
);
assert.equal(
comment._internalModel.__recordData.__implicitRelationships,
null,
'implicitRelationships are cleared'
);
assert.ok(comment._internalModel.__recordData.isDestroyed, 'recordData is destroyed');
});
});
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 @@ -693,6 +693,7 @@ export default class RecordDataDefault implements RelationshipRecordData {
rel.clear();
}
});
this.__relationships = null;

let implicitRelationships = this._implicitRelationships;
this.__implicitRelationships = null;
Expand Down

0 comments on commit 0a9641d

Please sign in to comment.