Skip to content

Commit

Permalink
add test with sort computed macro
Browse files Browse the repository at this point in the history
  • Loading branch information
snewcomer committed Sep 24, 2020
1 parent 06c81c7 commit be2af80
Showing 1 changed file with 53 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { action } from '@ember/object';
import { sort } from '@ember/object/computed';
import { inject as service } from '@ember/service';
import { click, render } from '@ember/test-helpers';
import Component from '@glimmer/component';
Expand Down Expand Up @@ -674,4 +675,56 @@ module('autotracking has-many', function(hooks) {
names = domListToArray(items).map(e => e.textContent);
assert.deepEqual(names, ['RGB', 'RGB'], 'rendered 2 children');
});

test('We can re-render hasMany with sort computed macro', async function(assert) {
class ChildrenList extends Component {
@service store;

sortProperties = ['name'];
@sort('args.person.children', 'sortProperties') sortedChildren;

@action
createChild() {
const parent = this.args.person;
const name = 'RGB';
this.store.createRecord('person', { name, parent });
}
}

let layout = hbs`
<button id="createChild" {{on "click" this.createChild}}>Add child</button>
<h2>{{this.sortedChildren.length}}</h2>
<ul>
{{#each this.sortedChildren as |child|}}
<li>{{child.name}}</li>
{{/each}}
</ul>
`;
this.owner.register('component:children-list', ChildrenList);
this.owner.register('template:components/children-list', layout);

store.createRecord('person', { id: '1', name: 'Doodad' });
let person = store.peekRecord('person', '1');
this.person = person;

await render(hbs`<ChildrenList @person={{this.person}} />`);

let items = this.element.querySelectorAll('li');
let names = domListToArray(items).map(e => e.textContent);

assert.deepEqual(names, [], 'rendered no children');

await click('#createChild');

items = this.element.querySelectorAll('li');
names = domListToArray(items).map(e => e.textContent);
assert.deepEqual(names, ['RGB'], 'rendered 1 child');

await click('#createChild');

items = this.element.querySelectorAll('li');
names = domListToArray(items).map(e => e.textContent);
assert.deepEqual(names, ['RGB', 'RGB'], 'rendered 2 children');
});
});

0 comments on commit be2af80

Please sign in to comment.