Skip to content

Commit

Permalink
fix: deprecated record-array reject use callback (#8400)
Browse files Browse the repository at this point in the history
`reject` expects a callback function
  • Loading branch information
mrloop authored Feb 25, 2023
1 parent 1adf81c commit 7872184
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 9 deletions.
13 changes: 4 additions & 9 deletions packages/store/src/-private/record-arrays/identifier-array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -865,16 +865,11 @@ if (DEPRECATE_ARRAY_LIKE) {
);
};

// @ts-expect-error
IdentifierArray.prototype.reject = function (key: string, value?: unknown) {
IdentifierArray.prototype.reject = function (callback, target?: unknown) {
deprecateArrayLike(this.DEPRECATED_CLASS_NAME, 'reject', 'filter');
if (arguments.length === 2) {
return this.filter((value) => {
return !get(value, key);
});
}
return this.filter((value) => {
return !get(value, key);
assert('`reject` expects a function as first argument.', typeof callback === 'function');
return this.filter((...args) => {
return !callback.apply(target, args);
});
};

Expand Down
39 changes: 39 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 @@ -141,6 +141,45 @@ module('unit/record-arrays/record-array - DS.RecordArray', function (hooks) {
}
);

deprecatedTest('#reject', { id: 'ember-data:deprecate-array-like', until: '5.0', count: 3 }, async function (assert) {
this.owner.register('model:tag', Tag);
let store = this.owner.lookup('service:store');

let records = store.push({
data: [
{
type: 'tag',
id: '1',
attributes: {
name: 'first',
},
},
{
type: 'tag',
id: '3',
},
{
type: 'tag',
id: '5',
attributes: {
name: 'fifth',
},
},
],
});

let recordArray = new RecordArray({
type: 'recordType',
identifiers: records.map(recordIdentifierFor),
store,
});

assert.strictEqual(recordArray.length, 3);
assert.strictEqual(recordArray.reject(({ id }) => id === '3').length, 2);
assert.strictEqual(recordArray.reject(({ id }) => id).length, 0);
assert.strictEqual(recordArray.reject(({ name }) => name).length, 1);
});

deprecatedTest(
'#rejectBy',
{ id: 'ember-data:deprecate-array-like', until: '5.0', count: 3 },
Expand Down

0 comments on commit 7872184

Please sign in to comment.