2019-07-23 19:40:32 +00:00
|
|
|
import { findAll, find, render } from '@ember/test-helpers';
|
2019-03-13 00:04:16 +00:00
|
|
|
import { module, skip, test } from 'qunit';
|
|
|
|
import { setupRenderingTest } from 'ember-qunit';
|
2019-10-03 14:13:08 +00:00
|
|
|
import faker from 'nomad-ui/mirage/faker';
|
2017-09-19 14:47:10 +00:00
|
|
|
import hbs from 'htmlbars-inline-precompile';
|
2020-08-25 15:56:02 +00:00
|
|
|
import { componentA11yAudit } from 'nomad-ui/tests/helpers/a11y-audit';
|
2017-09-19 14:47:10 +00:00
|
|
|
|
2019-03-13 00:04:16 +00:00
|
|
|
module('Integration | Component | list table', function(hooks) {
|
|
|
|
setupRenderingTest(hooks);
|
2017-09-19 14:47:10 +00:00
|
|
|
|
2019-03-13 00:04:16 +00:00
|
|
|
const commonTable = Array(10)
|
|
|
|
.fill(null)
|
|
|
|
.map(() => ({
|
|
|
|
firstName: faker.name.firstName(),
|
|
|
|
lastName: faker.name.lastName(),
|
|
|
|
age: faker.random.number({ min: 18, max: 60 }),
|
|
|
|
}));
|
2017-09-19 14:47:10 +00:00
|
|
|
|
2019-03-13 00:04:16 +00:00
|
|
|
// thead
|
|
|
|
test('component exposes a thead contextual component', async function(assert) {
|
|
|
|
this.set('source', commonTable);
|
|
|
|
await render(hbs`
|
2020-06-01 19:03:56 +00:00
|
|
|
<ListTable @source={{source}} @sortProperty={{sortProperty}} @sortDescending={{sortDescending}} as |t|>
|
|
|
|
<t.head @class="head">
|
2019-03-13 00:04:16 +00:00
|
|
|
<th>First Name</th>
|
|
|
|
<th>Last Name</th>
|
|
|
|
<th>Age</th>
|
2020-06-01 19:03:56 +00:00
|
|
|
</t.head>
|
|
|
|
</ListTable>
|
2019-03-13 00:04:16 +00:00
|
|
|
`);
|
2017-09-19 14:47:10 +00:00
|
|
|
|
2019-03-13 00:04:16 +00:00
|
|
|
assert.ok(findAll('.head').length, 'Table head is rendered');
|
|
|
|
assert.equal(find('.head').tagName.toLowerCase(), 'thead', 'Table head is a thead element');
|
2017-09-19 14:47:10 +00:00
|
|
|
});
|
|
|
|
|
2019-03-13 00:04:16 +00:00
|
|
|
// tbody
|
|
|
|
test('component exposes a tbody contextual component', async function(assert) {
|
|
|
|
this.setProperties({
|
|
|
|
source: commonTable,
|
|
|
|
sortProperty: 'firstName',
|
|
|
|
sortDescending: false,
|
|
|
|
});
|
|
|
|
await render(hbs`
|
2020-06-01 19:03:56 +00:00
|
|
|
<ListTable @source={{source}} @sortProperty={{sortProperty}} @sortDescending={{sortDescending}} as |t|>
|
2020-10-29 12:46:42 +00:00
|
|
|
<t.body @class="body" as |row index|>
|
2019-03-13 00:04:16 +00:00
|
|
|
<tr class="item">
|
|
|
|
<td>{{row.model.firstName}}</td>
|
|
|
|
<td>{{row.model.lastName}}</td>
|
|
|
|
<td>{{row.model.age}}</td>
|
2020-10-29 12:46:42 +00:00
|
|
|
<td>{{index}}</td>
|
2019-03-13 00:04:16 +00:00
|
|
|
</tr>
|
2020-06-01 19:03:56 +00:00
|
|
|
</t.body>
|
|
|
|
</ListTable>
|
2019-03-13 00:04:16 +00:00
|
|
|
`);
|
2017-09-19 14:47:10 +00:00
|
|
|
|
2019-03-13 00:04:16 +00:00
|
|
|
assert.ok(findAll('.body').length, 'Table body is rendered');
|
|
|
|
assert.equal(find('.body').tagName.toLowerCase(), 'tbody', 'Table body is a tbody element');
|
2017-09-19 14:47:10 +00:00
|
|
|
|
2019-03-13 00:04:16 +00:00
|
|
|
assert.equal(findAll('.item').length, this.get('source.length'), 'Each item gets its own row');
|
|
|
|
|
|
|
|
// list-table is not responsible for sorting, only dispatching sort events. The table is still
|
|
|
|
// rendered in index-order.
|
|
|
|
this.get('source').forEach((item, index) => {
|
2019-10-15 18:32:58 +00:00
|
|
|
const $item = this.element.querySelectorAll('.item')[index];
|
|
|
|
assert.equal($item.querySelectorAll('td')[0].innerHTML.trim(), item.firstName, 'First name');
|
|
|
|
assert.equal($item.querySelectorAll('td')[1].innerHTML.trim(), item.lastName, 'Last name');
|
|
|
|
assert.equal($item.querySelectorAll('td')[2].innerHTML.trim(), item.age, 'Age');
|
2020-10-29 12:46:42 +00:00
|
|
|
assert.equal($item.querySelectorAll('td')[3].innerHTML.trim(), index, 'Index');
|
2019-03-13 00:04:16 +00:00
|
|
|
});
|
2020-08-25 15:56:02 +00:00
|
|
|
|
|
|
|
await componentA11yAudit(this.element, assert);
|
2017-09-19 14:47:10 +00:00
|
|
|
});
|
|
|
|
|
2019-03-13 00:04:16 +00:00
|
|
|
// Ember doesn't support query params (or controllers or routes) in integration tests,
|
|
|
|
// so sorting links can only be tested in acceptance tests.
|
|
|
|
// Leaving this test here for posterity.
|
|
|
|
skip('sort-by creates links using the appropriate links given sort property and sort descending', function() {});
|
|
|
|
});
|