Skip to content

Commit

Permalink
Fix: don't clear RecordArray if remaining record does not match the r…
Browse files Browse the repository at this point in the history
…emoved record (emberjs#8570)

* Update record-array-test.js

* add fix

---------

Co-authored-by: Chris Thoburn <runspired@gmail.com>
  • Loading branch information
esbanarango and runspired committed Apr 16, 2023
1 parent 867f5ba commit 5e649a0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
4 changes: 3 additions & 1 deletion packages/store/src/-private/managers/record-array-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,9 @@ function sync(array: IdentifierArray, changes: Map<StableRecordIdentifier, 'add'
}
adds.push(key);
} else {
removes.push(key);
if (state.includes(key)) {
removes.push(key);
}
}
});
if (removes.length) {
Expand Down
17 changes: 16 additions & 1 deletion tests/main/tests/integration/record-array-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ module('integration/record-array - RecordArray', function (hooks) {
assert.strictEqual(recordArray.length, 0, 'initial length 0');

// eslint-disable-next-line no-unused-vars
const [_one, _two, three] = store.push({
const [one, two, three] = store.push({
data: [
{ type: 'person', id: '1', attributes: { name: 'Chris' } },
{ type: 'person', id: '2', attributes: { name: 'Ross' } },
Expand All @@ -214,10 +214,25 @@ module('integration/record-array - RecordArray', function (hooks) {
assert.strictEqual(recordArray.length, 3, 'populated length 3');
await three.save();
assert.strictEqual(recordArray.length, 2, 'after save persisted length 2');
assert.strictEqual(recordArray.at(0).name, 'Chris', 'after save persisted first record is Chris');
assert.strictEqual(recordArray.at(1).name, 'Ross', 'after save persisted second record is Ross');
three.unloadRecord();

await settled();

assert.strictEqual(recordArray.length, 2, 'updated length 2');

// Leaving a single record
two.deleteRecord();
assert.strictEqual(recordArray.length, 2, 'populated length 2');
await two.save();
assert.strictEqual(recordArray.length, 1, 'after save persisted length 1');
assert.strictEqual(recordArray.at(0).name, 'Chris', 'after save persisted first record is Chris');

two.unloadRecord();
await settled();

assert.strictEqual(recordArray.length, 1, 'updated length 1');
});

test('destroyRecord on a newly create record that is staged for a live record array only removes itself', async function (assert) {
Expand Down

0 comments on commit 5e649a0

Please sign in to comment.