From ce6f0a3c0f4a5b5e3eb194f806ef250534790c87 Mon Sep 17 00:00:00 2001 From: Garrett Murphey Date: Wed, 20 Nov 2019 12:58:49 -0500 Subject: [PATCH] Updating intersect helper to work with ManyArrays. --- addon/helpers/intersect.js | 6 ++--- tests/integration/helpers/intersect-test.js | 25 +++++++++++++++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/addon/helpers/intersect.js b/addon/helpers/intersect.js index 02ca7a11..f70e4b71 100644 --- a/addon/helpers/intersect.js +++ b/addon/helpers/intersect.js @@ -1,5 +1,5 @@ import { helper } from '@ember/component/helper'; -import { isArray as isEmberArray } from '@ember/array'; +import { isArray as isEmberArray, A as emberArray } from '@ember/array'; export function intersect([...arrays]) { let confirmedArrays = arrays.map(array => { @@ -9,9 +9,9 @@ export function intersect([...arrays]) { let results = confirmedArrays.pop().filter(candidate => { for (let i = 0; i < arrays.length; i++) { let found = false; - let array = arrays[i]; + let array = emberArray(arrays[i]); for (let j = 0; j < array.length; j++) { - if (array[j] === candidate) { + if (array.objectAt(j) === candidate) { found = true; break; } diff --git a/tests/integration/helpers/intersect-test.js b/tests/integration/helpers/intersect-test.js index 93cbb5a4..fe9b9ff1 100644 --- a/tests/integration/helpers/intersect-test.js +++ b/tests/integration/helpers/intersect-test.js @@ -51,4 +51,29 @@ module('Integration | Helper | {{intersect}}', function(hooks) { assert.equal(find('*').textContent.trim(), 'this is all that will render', 'no error is thrown'); }); + + test('it accepts an ember data array', async function(assert) { + let store = this.owner.lookup('service:store'); + let person = store.createRecord('person', { + name: 'Adam' + }); + + person.get('pets').pushObjects([ + store.createRecord('pet', { name: 'Kirby' }), + store.createRecord('pet', { name: 'Jake' }) + ]); + + store.createRecord('pet', { name: 'Eva' }); + + this.set('person', person); + this.set('allPets', store.peekAll('pet')); + + await this.render(hbs` + {{~#each (intersect person.pets allPets) as |pet|~}} + {{~pet.name~}} + {{~/each~}} + `); + + assert.equal(this.element.textContent.trim(), 'KirbyJake', 'the pets belonging to the person are shown'); + }); });