open-nomad/ui/tests/integration/components/app-breadcrumbs-test.js

92 lines
2.6 KiB
JavaScript
Raw Normal View History

import Service from '@ember/service';
import RSVP from 'rsvp';
2019-03-13 00:04:16 +00:00
import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
import { findAll, render, settled } from '@ember/test-helpers';
import hbs from 'htmlbars-inline-precompile';
import PromiseObject from 'nomad-ui/utils/classes/promise-object';
import { componentA11yAudit } from 'nomad-ui/tests/helpers/a11y-audit';
2019-03-13 00:04:16 +00:00
module('Integration | Component | app breadcrumbs', function(hooks) {
setupRenderingTest(hooks);
hooks.beforeEach(function() {
const mockBreadcrumbs = Service.extend({
init() {
this._super(...arguments);
this.breadcrumbs = [];
},
});
2019-03-13 00:04:16 +00:00
this.owner.register('service:breadcrumbs', mockBreadcrumbs);
this.breadcrumbs = this.owner.lookup('service:breadcrumbs');
});
2019-03-13 00:04:16 +00:00
const commonCrumbs = [{ label: 'One', args: ['one'] }, { label: 'Two', args: ['two'] }];
2019-03-13 00:04:16 +00:00
const template = hbs`
<ul><AppBreadcrumbs /></ul>
2019-03-13 00:04:16 +00:00
`;
2019-03-13 00:04:16 +00:00
test('breadcrumbs comes from the breadcrumbs service', async function(assert) {
this.breadcrumbs.set('breadcrumbs', commonCrumbs);
2019-03-13 00:04:16 +00:00
await render(template);
assert.equal(
2019-03-13 00:04:16 +00:00
findAll('[data-test-breadcrumb]').length,
commonCrumbs.length,
'The number of crumbs matches the crumbs from the service'
);
});
2019-03-13 00:04:16 +00:00
test('every breadcrumb is rendered correctly', async function(assert) {
this.breadcrumbs.set('breadcrumbs', commonCrumbs);
2019-03-13 00:04:16 +00:00
await render(template);
2019-03-13 00:04:16 +00:00
const renderedCrumbs = findAll('[data-test-breadcrumb]');
2019-03-13 00:04:16 +00:00
renderedCrumbs.forEach((crumb, index) => {
assert.equal(
crumb.textContent.trim(),
commonCrumbs[index].label,
`Crumb ${index} is ${commonCrumbs[index].label}`
);
});
});
test('when breadcrumbs are pending promises, an ellipsis is rendered', async function(assert) {
let resolvePromise;
const promise = new RSVP.Promise(resolve => {
resolvePromise = resolve;
});
2019-03-13 00:04:16 +00:00
this.breadcrumbs.set('breadcrumbs', [
{ label: 'One', args: ['one'] },
PromiseObject.create({ promise }),
{ label: 'Three', args: ['three'] },
]);
await render(template);
assert.equal(
findAll('[data-test-breadcrumb]')[1].textContent.trim(),
2019-03-13 00:04:16 +00:00
'…',
'Promise breadcrumb is in a loading state'
);
2019-03-13 00:04:16 +00:00
await componentA11yAudit(this.element, assert);
2019-03-13 00:04:16 +00:00
resolvePromise({ label: 'Two', args: ['two'] });
return settled().then(() => {
assert.equal(
findAll('[data-test-breadcrumb]')[1].textContent.trim(),
'Two',
'Promise breadcrumb has resolved and now renders Two'
);
});
});
});