2021-12-28 16:08:12 +00:00
|
|
|
import {
|
|
|
|
findAll,
|
|
|
|
find,
|
|
|
|
click,
|
|
|
|
focus,
|
|
|
|
render,
|
|
|
|
triggerKeyEvent,
|
|
|
|
} from '@ember/test-helpers';
|
2019-03-13 00:04:16 +00:00
|
|
|
import { module, test } from 'qunit';
|
|
|
|
import { setupRenderingTest } from 'ember-qunit';
|
2019-01-16 01:16:18 +00:00
|
|
|
import sinon from 'sinon';
|
|
|
|
import hbs from 'htmlbars-inline-precompile';
|
2020-08-25 15:56:02 +00:00
|
|
|
import { componentA11yAudit } from 'nomad-ui/tests/helpers/a11y-audit';
|
2019-01-16 01:16:18 +00:00
|
|
|
|
|
|
|
const TAB = 9;
|
|
|
|
const ESC = 27;
|
|
|
|
const SPACE = 32;
|
|
|
|
const ARROW_UP = 38;
|
|
|
|
const ARROW_DOWN = 40;
|
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
module('Integration | Component | multi-select dropdown', function (hooks) {
|
2019-03-13 00:04:16 +00:00
|
|
|
setupRenderingTest(hooks);
|
|
|
|
|
|
|
|
const commonProperties = () => ({
|
|
|
|
label: 'This is the dropdown label',
|
|
|
|
selection: [],
|
|
|
|
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(),
|
|
|
|
});
|
2019-01-16 01:16:18 +00:00
|
|
|
|
2019-03-13 00:04:16 +00:00
|
|
|
const commonTemplate = hbs`
|
2020-07-09 17:30:11 +00:00
|
|
|
<MultiSelectDropdown
|
2021-04-29 20:00:59 +00:00
|
|
|
@label={{this.label}}
|
|
|
|
@options={{this.options}}
|
|
|
|
@selection={{this.selection}}
|
|
|
|
@onSelect={{this.onSelect}} />
|
2019-03-13 00:04:16 +00:00
|
|
|
`;
|
2019-01-16 01:16:18 +00:00
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
test('component is initially closed', async function (assert) {
|
2021-12-28 19:30:38 +00:00
|
|
|
assert.expect(4);
|
|
|
|
|
2019-03-13 00:04:16 +00:00
|
|
|
const props = commonProperties();
|
|
|
|
this.setProperties(props);
|
|
|
|
await render(commonTemplate);
|
|
|
|
|
|
|
|
assert.ok(find('.dropdown-trigger'), 'Trigger is shown');
|
|
|
|
assert.equal(
|
|
|
|
find('[data-test-dropdown-trigger]').textContent.trim(),
|
|
|
|
props.label,
|
|
|
|
'Trigger is appropriately labeled'
|
|
|
|
);
|
2021-12-28 16:08:12 +00:00
|
|
|
assert.notOk(
|
|
|
|
find('[data-test-dropdown-options]'),
|
|
|
|
'Options are not rendered'
|
|
|
|
);
|
2020-08-25 15:56:02 +00:00
|
|
|
|
|
|
|
await componentA11yAudit(this.element, assert);
|
2019-03-13 00:04:16 +00:00
|
|
|
});
|
2019-01-16 01:16:18 +00:00
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
test('component opens the options dropdown when clicked', async function (assert) {
|
2021-12-28 19:30:38 +00:00
|
|
|
assert.expect(3);
|
|
|
|
|
2019-03-13 00:04:16 +00:00
|
|
|
const props = commonProperties();
|
|
|
|
this.setProperties(props);
|
|
|
|
await render(commonTemplate);
|
|
|
|
|
2019-07-23 19:40:32 +00:00
|
|
|
await click('[data-test-dropdown-trigger]');
|
|
|
|
|
2021-12-28 16:08:12 +00:00
|
|
|
await assert.ok(
|
|
|
|
find('[data-test-dropdown-options]'),
|
|
|
|
'Options are shown now'
|
|
|
|
);
|
2020-08-25 15:56:02 +00:00
|
|
|
await componentA11yAudit(this.element, assert);
|
|
|
|
|
2019-07-23 19:40:32 +00:00
|
|
|
await click('[data-test-dropdown-trigger]');
|
|
|
|
|
2021-12-28 16:08:12 +00:00
|
|
|
assert.notOk(
|
|
|
|
find('[data-test-dropdown-options]'),
|
|
|
|
'Options are hidden after clicking again'
|
|
|
|
);
|
2019-03-13 00:04:16 +00:00
|
|
|
});
|
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
test('all options are shown in the options dropdown, each with a checkbox input', async function (assert) {
|
2021-12-28 19:30:38 +00:00
|
|
|
assert.expect(13);
|
|
|
|
|
2019-03-13 00:04:16 +00:00
|
|
|
const props = commonProperties();
|
|
|
|
this.setProperties(props);
|
|
|
|
await render(commonTemplate);
|
|
|
|
|
2019-07-23 19:40:32 +00:00
|
|
|
await click('[data-test-dropdown-trigger]');
|
2019-03-13 00:04:16 +00:00
|
|
|
|
2019-07-23 19:40:32 +00:00
|
|
|
assert.equal(
|
|
|
|
findAll('[data-test-dropdown-option]').length,
|
|
|
|
props.options.length,
|
|
|
|
'All options are shown'
|
|
|
|
);
|
|
|
|
findAll('[data-test-dropdown-option]').forEach((optionEl, index) => {
|
|
|
|
const label = props.options[index].label;
|
2021-12-28 16:08:12 +00:00
|
|
|
assert.equal(
|
|
|
|
optionEl.textContent.trim(),
|
|
|
|
label,
|
|
|
|
`Correct label for ${label}`
|
|
|
|
);
|
|
|
|
assert.ok(
|
|
|
|
optionEl.querySelector('input[type="checkbox"]'),
|
|
|
|
'Option contains a checkbox'
|
|
|
|
);
|
2019-01-16 01:16:18 +00:00
|
|
|
});
|
2019-03-13 00:04:16 +00:00
|
|
|
});
|
2019-01-16 01:16:18 +00:00
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
test('onSelect gets called when an option is clicked', async function (assert) {
|
2019-03-13 00:04:16 +00:00
|
|
|
const props = commonProperties();
|
|
|
|
this.setProperties(props);
|
|
|
|
await render(commonTemplate);
|
|
|
|
|
2019-07-23 19:40:32 +00:00
|
|
|
await click('[data-test-dropdown-trigger]');
|
|
|
|
await click('[data-test-dropdown-option] label');
|
|
|
|
|
|
|
|
assert.ok(props.onSelect.called, 'onSelect was called');
|
|
|
|
const newSelection = props.onSelect.getCall(0).args[0];
|
|
|
|
assert.deepEqual(
|
|
|
|
newSelection,
|
|
|
|
[props.options[0].key],
|
|
|
|
'onSelect was called with the first option key'
|
|
|
|
);
|
2019-03-13 00:04:16 +00:00
|
|
|
});
|
2019-01-16 01:16:18 +00:00
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
test('the component trigger shows the selection count when there is a selection', async function (assert) {
|
2021-12-28 19:30:38 +00:00
|
|
|
assert.expect(4);
|
|
|
|
|
2019-03-13 00:04:16 +00:00
|
|
|
const props = commonProperties();
|
|
|
|
props.selection = [props.options[0].key, props.options[1].key];
|
|
|
|
this.setProperties(props);
|
|
|
|
await render(commonTemplate);
|
2019-01-16 01:16:18 +00:00
|
|
|
|
2019-07-23 19:40:32 +00:00
|
|
|
assert.ok(
|
|
|
|
find('[data-test-dropdown-trigger] [data-test-dropdown-count]'),
|
|
|
|
'The count is shown'
|
|
|
|
);
|
2019-01-16 01:16:18 +00:00
|
|
|
assert.equal(
|
2021-12-28 16:08:12 +00:00
|
|
|
find('[data-test-dropdown-trigger] [data-test-dropdown-count]')
|
|
|
|
.textContent,
|
2019-03-13 00:04:16 +00:00
|
|
|
props.selection.length,
|
|
|
|
'The count is accurate'
|
2019-01-16 01:16:18 +00:00
|
|
|
);
|
|
|
|
|
2020-08-25 15:56:02 +00:00
|
|
|
await componentA11yAudit(this.element, assert);
|
|
|
|
|
2019-07-23 19:40:32 +00:00
|
|
|
await this.set('selection', []);
|
2019-03-13 00:04:16 +00:00
|
|
|
|
2019-07-23 19:40:32 +00:00
|
|
|
assert.notOk(
|
|
|
|
find('[data-test-dropdown-trigger] [data-test-dropdown-count]'),
|
|
|
|
'The count is no longer shown when the selection is empty'
|
|
|
|
);
|
2019-03-13 00:04:16 +00:00
|
|
|
});
|
2019-01-16 01:16:18 +00:00
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
test('pressing DOWN when the trigger has focus opens the options list', async function (assert) {
|
2019-03-13 00:04:16 +00:00
|
|
|
const props = commonProperties();
|
|
|
|
this.setProperties(props);
|
|
|
|
await render(commonTemplate);
|
|
|
|
|
2019-07-23 19:40:32 +00:00
|
|
|
await focus('[data-test-dropdown-trigger]');
|
2021-12-28 16:08:12 +00:00
|
|
|
assert.notOk(
|
|
|
|
find('[data-test-dropdown-options]'),
|
|
|
|
'Options are not shown on focus'
|
|
|
|
);
|
2022-03-08 17:28:36 +00:00
|
|
|
await triggerKeyEvent('[data-test-dropdown-trigger]', 'keyup', ARROW_DOWN);
|
2019-03-13 00:04:16 +00:00
|
|
|
assert.ok(find('[data-test-dropdown-options]'), 'Options are now shown');
|
|
|
|
assert.equal(
|
|
|
|
document.activeElement,
|
|
|
|
find('[data-test-dropdown-trigger]'),
|
|
|
|
'The dropdown trigger maintains focus'
|
2019-01-16 01:16:18 +00:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
test('pressing DOWN when the trigger has focus and the options list is open focuses the first option', async function (assert) {
|
2019-03-13 00:04:16 +00:00
|
|
|
const props = commonProperties();
|
|
|
|
this.setProperties(props);
|
|
|
|
await render(commonTemplate);
|
2019-01-16 01:16:18 +00:00
|
|
|
|
2019-07-23 19:40:32 +00:00
|
|
|
await focus('[data-test-dropdown-trigger]');
|
2022-03-08 17:28:36 +00:00
|
|
|
await triggerKeyEvent('[data-test-dropdown-trigger]', 'keyup', ARROW_DOWN);
|
|
|
|
await triggerKeyEvent('[data-test-dropdown-trigger]', 'keyup', ARROW_DOWN);
|
2019-03-13 00:04:16 +00:00
|
|
|
assert.equal(
|
|
|
|
document.activeElement,
|
|
|
|
find('[data-test-dropdown-option]'),
|
|
|
|
'The first option now has focus'
|
|
|
|
);
|
|
|
|
});
|
2019-01-16 01:16:18 +00:00
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
test('pressing TAB when the trigger has focus and the options list is open focuses the first option', async function (assert) {
|
2019-03-13 00:04:16 +00:00
|
|
|
const props = commonProperties();
|
|
|
|
this.setProperties(props);
|
|
|
|
await render(commonTemplate);
|
2019-01-16 01:16:18 +00:00
|
|
|
|
2019-07-23 19:40:32 +00:00
|
|
|
await focus('[data-test-dropdown-trigger]');
|
2022-03-08 17:28:36 +00:00
|
|
|
await triggerKeyEvent('[data-test-dropdown-trigger]', 'keyup', ARROW_DOWN);
|
|
|
|
await triggerKeyEvent('[data-test-dropdown-trigger]', 'keyup', TAB);
|
2019-03-13 00:04:16 +00:00
|
|
|
assert.equal(
|
|
|
|
document.activeElement,
|
|
|
|
find('[data-test-dropdown-option]'),
|
|
|
|
'The first option now has focus'
|
|
|
|
);
|
|
|
|
});
|
2019-01-16 01:16:18 +00:00
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
test('pressing UP when the first list option is focused does nothing', async function (assert) {
|
2019-03-13 00:04:16 +00:00
|
|
|
const props = commonProperties();
|
|
|
|
this.setProperties(props);
|
|
|
|
await render(commonTemplate);
|
2019-01-16 01:16:18 +00:00
|
|
|
|
2019-07-23 19:40:32 +00:00
|
|
|
await click('[data-test-dropdown-trigger]');
|
2019-01-16 01:16:18 +00:00
|
|
|
|
2019-07-23 19:40:32 +00:00
|
|
|
await focus('[data-test-dropdown-option]');
|
2022-03-08 17:28:36 +00:00
|
|
|
await triggerKeyEvent('[data-test-dropdown-option]', 'keyup', ARROW_UP);
|
2019-03-13 00:04:16 +00:00
|
|
|
assert.equal(
|
|
|
|
document.activeElement,
|
|
|
|
find('[data-test-dropdown-option]'),
|
|
|
|
'The first option maintains focus'
|
|
|
|
);
|
|
|
|
});
|
2019-01-16 01:16:18 +00:00
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
test('pressing DOWN when the a list option is focused moves focus to the next list option', async function (assert) {
|
2019-03-13 00:04:16 +00:00
|
|
|
const props = commonProperties();
|
|
|
|
this.setProperties(props);
|
|
|
|
await render(commonTemplate);
|
2019-01-16 01:16:18 +00:00
|
|
|
|
2019-07-23 19:40:32 +00:00
|
|
|
await click('[data-test-dropdown-trigger]');
|
2019-01-16 01:16:18 +00:00
|
|
|
|
2019-07-23 19:40:32 +00:00
|
|
|
await focus('[data-test-dropdown-option]');
|
2022-03-08 17:28:36 +00:00
|
|
|
await triggerKeyEvent('[data-test-dropdown-option]', 'keyup', ARROW_DOWN);
|
2019-03-13 00:04:16 +00:00
|
|
|
assert.equal(
|
|
|
|
document.activeElement,
|
|
|
|
findAll('[data-test-dropdown-option]')[1],
|
|
|
|
'The second option has focus'
|
|
|
|
);
|
2019-01-16 01:16:18 +00:00
|
|
|
});
|
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
test('pressing DOWN when the last list option has focus does nothing', async function (assert) {
|
2021-12-28 19:30:38 +00:00
|
|
|
assert.expect(6);
|
|
|
|
|
2019-03-13 00:04:16 +00:00
|
|
|
const props = commonProperties();
|
|
|
|
this.setProperties(props);
|
|
|
|
await render(commonTemplate);
|
|
|
|
|
2019-07-23 19:40:32 +00:00
|
|
|
await click('[data-test-dropdown-trigger]');
|
2019-03-13 00:04:16 +00:00
|
|
|
|
2019-07-23 19:40:32 +00:00
|
|
|
await focus('[data-test-dropdown-option]');
|
2019-03-13 00:04:16 +00:00
|
|
|
const optionEls = findAll('[data-test-dropdown-option]');
|
|
|
|
const lastIndex = optionEls.length - 1;
|
2019-07-23 19:40:32 +00:00
|
|
|
|
|
|
|
for (const [index, option] of optionEls.entries()) {
|
2022-03-08 17:28:36 +00:00
|
|
|
await triggerKeyEvent(option, 'keyup', ARROW_DOWN);
|
2019-07-23 19:40:32 +00:00
|
|
|
|
2019-03-13 00:04:16 +00:00
|
|
|
if (index < lastIndex) {
|
2021-12-28 19:30:38 +00:00
|
|
|
/* eslint-disable-next-line qunit/no-conditional-assertions */
|
2021-12-28 16:08:12 +00:00
|
|
|
assert.equal(
|
|
|
|
document.activeElement,
|
|
|
|
optionEls[index + 1],
|
|
|
|
`Option ${index + 1} has focus`
|
|
|
|
);
|
2019-03-13 00:04:16 +00:00
|
|
|
}
|
2019-07-23 19:40:32 +00:00
|
|
|
}
|
2019-01-16 01:16:18 +00:00
|
|
|
|
2022-03-08 17:28:36 +00:00
|
|
|
await triggerKeyEvent(optionEls[lastIndex], 'keyup', ARROW_DOWN);
|
2019-07-23 19:40:32 +00:00
|
|
|
assert.equal(
|
|
|
|
document.activeElement,
|
|
|
|
optionEls[lastIndex],
|
|
|
|
`Option ${lastIndex} still has focus`
|
|
|
|
);
|
2019-03-13 00:04:16 +00:00
|
|
|
});
|
2019-01-16 01:16:18 +00:00
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
test('onSelect gets called when pressing SPACE when a list option is focused', async function (assert) {
|
2019-03-13 00:04:16 +00:00
|
|
|
const props = commonProperties();
|
|
|
|
this.setProperties(props);
|
|
|
|
await render(commonTemplate);
|
2019-01-16 01:16:18 +00:00
|
|
|
|
2019-07-23 19:40:32 +00:00
|
|
|
await click('[data-test-dropdown-trigger]');
|
2019-01-16 01:16:18 +00:00
|
|
|
|
2019-07-23 19:40:32 +00:00
|
|
|
await focus('[data-test-dropdown-option]');
|
2022-03-08 17:28:36 +00:00
|
|
|
await triggerKeyEvent('[data-test-dropdown-option]', 'keyup', SPACE);
|
2019-03-13 00:04:16 +00:00
|
|
|
|
|
|
|
assert.ok(props.onSelect.called, 'onSelect was called');
|
|
|
|
const newSelection = props.onSelect.getCall(0).args[0];
|
|
|
|
assert.deepEqual(
|
|
|
|
newSelection,
|
|
|
|
[props.options[0].key],
|
|
|
|
'onSelect was called with the first option key'
|
|
|
|
);
|
|
|
|
});
|
2019-01-16 01:16:18 +00:00
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
test('list options have a zero tabindex and are therefore sequentially navigable', async function (assert) {
|
2021-12-28 19:30:38 +00:00
|
|
|
assert.expect(6);
|
|
|
|
|
2019-03-13 00:04:16 +00:00
|
|
|
const props = commonProperties();
|
|
|
|
this.setProperties(props);
|
|
|
|
await render(commonTemplate);
|
2019-01-16 01:16:18 +00:00
|
|
|
|
2019-07-23 19:40:32 +00:00
|
|
|
await click('[data-test-dropdown-trigger]');
|
2019-01-16 01:16:18 +00:00
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
findAll('[data-test-dropdown-option]').forEach((option) => {
|
2021-12-28 16:08:12 +00:00
|
|
|
assert.equal(
|
|
|
|
parseInt(option.getAttribute('tabindex'), 10),
|
|
|
|
0,
|
|
|
|
'tabindex is zero'
|
|
|
|
);
|
2019-03-13 00:04:16 +00:00
|
|
|
});
|
2019-01-16 01:16:18 +00:00
|
|
|
});
|
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
test('the checkboxes inside list options have a negative tabindex and are therefore not sequentially navigable', async function (assert) {
|
2021-12-28 19:30:38 +00:00
|
|
|
assert.expect(6);
|
|
|
|
|
2019-03-13 00:04:16 +00:00
|
|
|
const props = commonProperties();
|
|
|
|
this.setProperties(props);
|
|
|
|
await render(commonTemplate);
|
2019-01-16 01:16:18 +00:00
|
|
|
|
2019-07-23 19:40:32 +00:00
|
|
|
await click('[data-test-dropdown-trigger]');
|
2019-01-16 01:16:18 +00:00
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
findAll('[data-test-dropdown-option]').forEach((option) => {
|
2019-03-13 00:04:16 +00:00
|
|
|
assert.ok(
|
2021-12-28 16:08:12 +00:00
|
|
|
parseInt(
|
|
|
|
option
|
|
|
|
.querySelector('input[type="checkbox"]')
|
|
|
|
.getAttribute('tabindex'),
|
|
|
|
10
|
|
|
|
) < 0,
|
2019-03-13 00:04:16 +00:00
|
|
|
'tabindex is a negative value'
|
|
|
|
);
|
|
|
|
});
|
2019-01-16 01:16:18 +00:00
|
|
|
});
|
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
test('pressing ESC when the options list is open closes the list and returns focus to the dropdown trigger', async function (assert) {
|
2019-03-13 00:04:16 +00:00
|
|
|
const props = commonProperties();
|
|
|
|
this.setProperties(props);
|
|
|
|
await render(commonTemplate);
|
2019-01-23 00:29:09 +00:00
|
|
|
|
2019-07-23 19:40:32 +00:00
|
|
|
await focus('[data-test-dropdown-trigger]');
|
2022-03-08 17:28:36 +00:00
|
|
|
await triggerKeyEvent('[data-test-dropdown-trigger]', 'keyup', ARROW_DOWN);
|
|
|
|
await triggerKeyEvent('[data-test-dropdown-trigger]', 'keyup', ARROW_DOWN);
|
|
|
|
await triggerKeyEvent('[data-test-dropdown-option]', 'keyup', ESC);
|
2019-01-23 00:29:09 +00:00
|
|
|
|
2021-12-28 16:08:12 +00:00
|
|
|
assert.notOk(
|
|
|
|
find('[data-test-dropdown-options]'),
|
|
|
|
'The options list is hidden once more'
|
|
|
|
);
|
2019-03-13 00:04:16 +00:00
|
|
|
assert.equal(
|
|
|
|
document.activeElement,
|
|
|
|
find('[data-test-dropdown-trigger]'),
|
|
|
|
'The trigger has focus'
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
test('when there are no list options, an empty message is shown', async function (assert) {
|
2021-12-28 19:30:38 +00:00
|
|
|
assert.expect(4);
|
|
|
|
|
2019-03-13 00:04:16 +00:00
|
|
|
const props = commonProperties();
|
|
|
|
props.options = [];
|
|
|
|
this.setProperties(props);
|
|
|
|
await render(commonTemplate);
|
|
|
|
|
2019-07-23 19:40:32 +00:00
|
|
|
await click('[data-test-dropdown-trigger]');
|
2021-12-28 16:08:12 +00:00
|
|
|
assert.ok(
|
|
|
|
find('[data-test-dropdown-options]'),
|
|
|
|
'The dropdown is still shown'
|
|
|
|
);
|
2019-03-13 00:04:16 +00:00
|
|
|
assert.ok(find('[data-test-dropdown-empty]'), 'The empty state is shown');
|
|
|
|
assert.notOk(find('[data-test-dropdown-option]'), 'No options are shown');
|
2020-08-25 15:56:02 +00:00
|
|
|
await componentA11yAudit(this.element, assert);
|
2019-03-13 00:04:16 +00:00
|
|
|
});
|
2019-01-23 00:29:09 +00:00
|
|
|
});
|