From 55fd92467809f00fb25371fa89b64502745e6eaa Mon Sep 17 00:00:00 2001 From: Chris Thoburn Date: Sat, 17 Mar 2018 01:35:41 -0700 Subject: [PATCH] add test and fix for #5350 --- addon/-private/system/model/internal-model.js | 4 +- .../record-arrays/peeked-records-test.js | 116 +++++++++++++++++- 2 files changed, 118 insertions(+), 2 deletions(-) diff --git a/addon/-private/system/model/internal-model.js b/addon/-private/system/model/internal-model.js index e1043f11d8c..07a6a336de2 100644 --- a/addon/-private/system/model/internal-model.js +++ b/addon/-private/system/model/internal-model.js @@ -293,6 +293,7 @@ export default class InternalModel { // models to rematerialize their records. return this._isDematerializing || + this.hasScheduledDestroy() || this.isDestroyed || this.currentState.stateName === 'root.deleted.saved' || this.isEmpty(); @@ -381,9 +382,10 @@ export default class InternalModel { this._isDematerializing = true; this._record.destroy(); this.destroyRelationships(); - this.updateRecordArrays(); this.resetRecord(); } + + this.updateRecordArrays(); } deleteRecord() { diff --git a/tests/integration/record-arrays/peeked-records-test.js b/tests/integration/record-arrays/peeked-records-test.js index 297dc93d156..c20c8d7fb69 100644 --- a/tests/integration/record-arrays/peeked-records-test.js +++ b/tests/integration/record-arrays/peeked-records-test.js @@ -14,7 +14,7 @@ const Person = DS.Model.extend({ } }); -module('integration/unload-peeked-records', { +module('integration/peeked-records', { beforeEach() { store = createStore({ person: Person @@ -262,3 +262,117 @@ test('unloadAll followed by peekAll in the same run-loop works as expected', fun 'RecordArray state has not changed any further' ); }); + +test('push+materialize => unloadAll => push+materialize works as expected', function(assert) { + function push() { + run(() => { + store.push({ + data: [ + { + type: 'person', + id: '1', + attributes: { + name: 'John' + } + }, + { + type: 'person', + id: '2', + attributes: { + name: 'Joe' + } + } + ] + }); + }); + } + function unload() { + run(() => store.unloadAll('person')); + } + function peek() { + return run(() => store.peekAll('person')); + } + + let peekedRecordArray = peek(); + let watcher = watchProperties(peekedRecordArray, ['length', '[]']); + + push(); + assert.watchedPropertyCounts( + watcher, + { length: 1, '[]': 1 }, + 'RecordArray state after a single push with multiple records to add' + ); + + unload(); + assert.equal(get(peekedRecordArray, 'length'), 0, 'We no longer have any array content'); + assert.watchedPropertyCounts( + watcher, + { length: 2, '[]': 2 }, + 'RecordArray state has signaled the unload' + ); + + push(); + assert.equal(get(peekedRecordArray, 'length'), 2, 'We have array content'); + assert.watchedPropertyCounts( + watcher, + { length: 3, '[]': 3 }, + 'RecordArray state now has records again' + ); +}); + +test('push-without-materialize => unloadAll => push-without-materialize works as expected', function(assert) { + function _push() { + run(() => { + store._push({ + data: [ + { + type: 'person', + id: '1', + attributes: { + name: 'John' + } + }, + { + type: 'person', + id: '2', + attributes: { + name: 'Joe' + } + } + ] + }); + }); + } + function unload() { + run(() => store.unloadAll('person')); + } + function peek() { + return run(() => store.peekAll('person')); + } + + let peekedRecordArray = peek(); + let watcher = watchProperties(peekedRecordArray, ['length', '[]']); + + _push(); + assert.watchedPropertyCounts( + watcher, + { length: 1, '[]': 1 }, + 'RecordArray state after a single push with multiple records to add' + ); + + unload(); + assert.equal(get(peekedRecordArray, 'length'), 0, 'We no longer have any array content'); + assert.watchedPropertyCounts( + watcher, + { length: 2, '[]': 2 }, + 'RecordArray state has signaled the unload' + ); + + _push(); + assert.equal(get(peekedRecordArray, 'length'), 2, 'We have array content'); + assert.watchedPropertyCounts( + watcher, + { length: 3, '[]': 3 }, + 'RecordArray state now has records again' + ); +});