Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: filterBy should compare values #8171

Merged
merged 1 commit into from
Sep 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions packages/-ember-data/tests/unit/record-arrays/record-array-test.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤦 you may want to check the other implementations to make sure I didn't make a similar mistake with them

Copy link
Contributor Author

@jrjohnson jrjohnson Sep 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😆 copy paste so useful, so easy to mess up. I eyeballed the rest and they looked good, decided not to write a bunch of soon to be deprecated tests though seemed like overkill.

});
};

Expand Down