Skip to content

Commit

Permalink
[BUG emberjs#4970] Fix IdentifierArray Extended Prototype Sync
Browse files Browse the repository at this point in the history
  • Loading branch information
jghansell committed Aug 29, 2023
1 parent 6fbda02 commit 433fe07
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
14 changes: 14 additions & 0 deletions packages/store/addon/-private/record-arrays/identifier-array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,12 @@ class IdentifierArray {
} else if (prop === 'lastObject' || prop === 'last') {
deprecateArrayLike(self.DEPRECATED_CLASS_NAME, prop, 'at(-1)');
return receiver[receiver.length - 1];
} else if (prop === 'empty') {
return receiver.length === 0;
} else if (prop === 'exist' || prop === 'exists') {
return receiver.length > 0;
} else if (prop === 'lastIndex') {
return receiver.length - 1;
}
}

Expand Down Expand Up @@ -955,6 +961,14 @@ if (DEPRECATE_ARRAY_LIKE) {
IdentifierArray.prototype.initial = null;
// @ts-expect-error
IdentifierArray.prototype.tail = null;
// @ts-expect-error
IdentifierArray.prototype.lastIndex = null;
// @ts-expect-error
IdentifierArray.prototype.empty = null;
// @ts-expect-error
IdentifierArray.prototype.exist = null;
// @ts-expect-error
IdentifierArray.prototype.exists = null;
}

type PromiseProxyRecord = { then(): void; content: RecordInstance | null | undefined };
Expand Down
71 changes: 71 additions & 0 deletions tests/main/tests/unit/record-arrays/record-array-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,77 @@ module('unit/record-arrays/record-array - DS.RecordArray', function (hooks) {
assert.deepEqual(recordArray.map(r => r.id), ['1', '3', '4'], 'All but first element set to provided value.');
assert.strictEqual(mutationsByRecordArray.get(recordArray[SOURCE]), 1, 'Modifying tail caused 1 mutation');
});

test('lastIndex', async function (assert) {
this.owner.register('model:tag', Tag);
let store = this.owner.lookup('service:store');

let model1 = {
id: '1',
type: 'tag',
};

let model2 = {
id: '2',
type: 'tag',
};

const recordArray = store.peekAll('tag');

assert.strictEqual(recordArray.lastIndex, -1, 'lastIndex should return -1 if there are no records');

store.push({
data: [model1],
});

assert.strictEqual(recordArray.lastIndex, 0, 'lastIndex should return the index of the last records');

store.push({
data: [model2],
});

assert.strictEqual(recordArray.lastIndex, 1, 'lastIndex should update after a record has been pushed');
});

test('empty', async function (assert) {
this.owner.register('model:tag', Tag);
let store = this.owner.lookup('service:store');

let model1 = {
id: '1',
type: 'tag',
};

const recordArray = store.peekAll('tag');

assert.true(recordArray.empty, 'record array should initially be empty');

store.push({
data: [model1],
});
assert.false(recordArray.empty, 'record array should not be empty after a record was pushed');
});

test('exists', async function (assert) {
this.owner.register('model:tag', Tag);
let store = this.owner.lookup('service:store');

let model1 = {
id: '1',
type: 'tag',
};

const recordArray = store.peekAll('tag');

assert.false(recordArray.exist, 'record array should initially be empty');
assert.false(recordArray.exists, 'record array should initially be empty');

store.push({
data: [model1],
});
assert.true(recordArray.exist, 'record array should not be empty after a record was pushed');
assert.true(recordArray.exists, 'record array should not be empty after a record was pushed');
});
}

if (DEPRECATE_PROMISE_MANY_ARRAY_BEHAVIORS) {
Expand Down

0 comments on commit 433fe07

Please sign in to comment.