open-nomad/ui/tests/integration/components/single-select-dropdown-test.js

86 lines
2.6 KiB
JavaScript
Raw Normal View History

import { findAll, find, render } from '@ember/test-helpers';
import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
2021-12-28 16:08:12 +00:00
import {
selectChoose,
clickTrigger,
} from 'ember-power-select/test-support/helpers';
import sinon from 'sinon';
import hbs from 'htmlbars-inline-precompile';
import { componentA11yAudit } from 'nomad-ui/tests/helpers/a11y-audit';
2021-12-28 14:45:20 +00:00
module('Integration | Component | single-select dropdown', function (hooks) {
setupRenderingTest(hooks);
const commonProperties = () => ({
label: 'Type',
selection: 'nomad',
options: [
{ key: 'consul', label: 'Consul' },
{ key: 'nomad', label: 'Nomad' },
{ key: 'terraform', label: 'Terraform' },
{ key: 'packer', label: 'Packer' },
{ key: 'vagrant', label: 'Vagrant' },
{ key: 'vault', label: 'Vault' },
],
onSelect: sinon.spy(),
});
const commonTemplate = hbs`
<SingleSelectDropdown
@label={{this.label}}
@options={{this.options}}
@selection={{this.selection}}
@onSelect={{this.onSelect}} />
`;
2021-12-28 14:45:20 +00:00
test('component shows label and selection in the trigger', async function (assert) {
const props = commonProperties();
this.setProperties(props);
await render(commonTemplate);
2021-12-28 16:08:12 +00:00
assert.ok(
find('.ember-power-select-trigger').textContent.includes(props.label)
);
assert.ok(
find('.ember-power-select-trigger').textContent.includes(
props.options.findBy('key', props.selection).label
)
);
assert.notOk(find('[data-test-dropdown-options]'));
await componentA11yAudit(this.element, assert);
});
2021-12-28 14:45:20 +00:00
test('all options are shown in the dropdown', async function (assert) {
const props = commonProperties();
this.setProperties(props);
await render(commonTemplate);
await clickTrigger('[data-test-single-select-dropdown]');
assert.equal(
findAll('.ember-power-select-option').length,
props.options.length,
'All options are shown'
);
findAll('.ember-power-select-option').forEach((optionEl, index) => {
assert.equal(
optionEl.querySelector('.dropdown-label').textContent.trim(),
props.options[index].label
);
});
});
2021-12-28 14:45:20 +00:00
test('selecting an option calls `onSelect` with the key for the selected option', async function (assert) {
const props = commonProperties();
this.setProperties(props);
await render(commonTemplate);
const option = props.options.findBy('key', 'terraform');
await selectChoose('[data-test-single-select-dropdown]', option.label);
assert.ok(props.onSelect.calledWith(option.key));
});
});