diff --git a/packages/store/addon/-private/record-arrays/identifier-array.ts b/packages/store/addon/-private/record-arrays/identifier-array.ts index a60ef059735..b895aafbe8f 100644 --- a/packages/store/addon/-private/record-arrays/identifier-array.ts +++ b/packages/store/addon/-private/record-arrays/identifier-array.ts @@ -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; } } @@ -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 }; diff --git a/tests/main/tests/unit/record-arrays/record-array-test.js b/tests/main/tests/unit/record-arrays/record-array-test.js index 1b496a9c32c..5dec54574b8 100644 --- a/tests/main/tests/unit/record-arrays/record-array-test.js +++ b/tests/main/tests/unit/record-arrays/record-array-test.js @@ -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) {