Skip to content

Commit

Permalink
Merge pull request #8171 from jrjohnson/filter-by-test
Browse files Browse the repository at this point in the history
fix: filterBy should compare values
  • Loading branch information
jrjohnson committed Sep 8, 2022
2 parents b7e0da7 + 8107b2f commit 94cf39f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
44 changes: 44 additions & 0 deletions packages/-ember-data/tests/unit/record-arrays/record-array-test.js
Expand Up @@ -6,6 +6,7 @@ import { setupTest } from 'ember-qunit';
import Model, { attr } from '@ember-data/model';
import { recordIdentifierFor } from '@ember-data/store';
import { RecordArray, SnapshotRecordArray, SOURCE } from '@ember-data/store/-private';
import { deprecatedTest } from '@ember-data/unpublished-test-infra/test-support/deprecated-test';
import testInDebug from '@ember-data/unpublished-test-infra/test-support/test-in-debug';

class Tag extends Model {
Expand Down Expand Up @@ -97,6 +98,49 @@ module('unit/record-arrays/record-array - DS.RecordArray', function (hooks) {
assert.strictEqual(recordArray[3], undefined);
});

deprecatedTest(
'#filterBy',
{ 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.filterBy('id', '3').length, 1);
assert.strictEqual(recordArray.filterBy('id').length, 3);
assert.strictEqual(recordArray.filterBy('name').length, 2);
}
);

test('#update', async function (assert) {
let findAllCalled = 0;
let deferred = RSVP.defer();
Expand Down
Expand Up @@ -746,12 +746,12 @@ if (DEPRECATE_ARRAY_LIKE) {
IdentifierArray.prototype.filterBy = function (key: string, value?: unknown) {
deprecateArrayLike(this.DEPRECATED_CLASS_NAME, 'filterBy', 'filter');
if (arguments.length === 2) {
return this.filter((value) => {
return Boolean(get(value, key));
return this.filter((record) => {
return get(record, key) === value;
});
}
return this.filter((value) => {
return Boolean(get(value, key));
return this.filter((record) => {
return Boolean(get(record, key));
});
};

Expand Down

0 comments on commit 94cf39f

Please sign in to comment.