open-nomad/ui/tests/integration/components/multi-select-dropdown-test.js
Michael Klein e096a0a5ab
Upgrade Ember and friends 3.28 (#12215)
* chore: upgrade forward compatible packages

* chore: v3.20.2...v3.24.0

* chore: silence string prototype extension deprecation

* refact: don't test clicking disabled button job-list

Recent test-helper upgrades will guard against clicking disabled buttons
as this is not something that real users can do. We need to change our
tests accordingly.

* fix: await async test helper `expectError`

We have to await this async test function otherwise the test's
rendering context will be torn down before we run assertions
against it.

* fix: don't try to click disabled two-step-button

Recent test-helper updates prohibit clicking disabled buttons. We need
to adapt the tests accordingly.

* fix: recommendation-accordion

Use up-to-date semantics for handling list-accordion closing
in recommendation-accordion.

* fixes toggling recommendation-accordion toggle.

* fix: simple-unless linting error application.hbs

There's no reason to use unless here - we can use if instead.

* fix: no-quoteless-attributes recommendation accordion

* fix: no-quoteless-attributes recommendation-chart

* fix: allow `unless` - global-header.hbs

This is a valid use of unless in our opinion.

* fix: allow unless in job-diff

This is not a great use for unless but we don't want to change this
behavior atm.

* fix: no-attrs-in-components list-pager

There is no need to use this.attrs in classic components. When we
will convert to glimmer we will use `@`-instead.

* fix: simple-unless job/definition

We can convert to a simple if here.

* fix: allow inline-styles stats-box component

To make linter happy.

* fix: disable no-action and no-invalid-interactive

Will be adressed in follow-up PRs.

* chore: update ember-classic-decorator to latest

* chore: upgrade ember-can to latest

* chore: upgrade ember-composable-helpers to latest

* chore: upgrade ember-concurrency

* fix: recomputation deprecation `Trigger`

schedule `do` on actions queue to work around recomputation deprecation
when triggering Trigger on `did-insert`.

* chore: upgrade ember-cli-string-helpers

* chore: upgrade ember-copy

* chore: upgrade ember-data-model-fragments

* chore: upgrade ember-deprecation-workflow

* chore: upgrade ember-inline-svg

* chore: upgrade ember-modifier

* chore: upgrade ember-truth-helpers

* chore: upgrade ember-moment & ember-cli-moment-shim

* chore: upgrade ember-power-select

* chore: upgrade ember-responsive

* chore: upgrade ember-sinon

* chore: upgrade ember-cli-mirage

For now we will stay on 2.2 - upgrades > 2.3 break the build.

* chore: upgrade 3.24.0 to 3.28.5

* fix: add missing classic decorators on adapters

* fix: missing classic decorators to serializers

* fix: don't reopen Ember.Object anymore

* fix: remove unused useNativeEvents

ember-cli-page-objects doesn't provide this method anymore

* fix: add missing attributeBindings for test-selectors

ember-test-selectors doesn't provides automatic bindings for
data-test-* attributes anymore.

* fix: classic decorator for application serializer test

* fix: remove `removeContext` from tests.

It is unneeded and ember-cli-page-objects doesn't provides
this method anymore.

* fix: remove deprecations `run.*`-invocations

* fix: `collapseWhitespace` in optimize test

* fix: make sure to load async relationship before access

* fix: dependent keys for relationship computeds

We need to add `*.isFulfilled` as dependent keys for computeds that
access async relationships.

* fix: `computed.read`-invocations use `read` instead

* chore: prettify templates

* fix: use map instead of mapBy ember-cli-page-object

Doesn't work with updated ember-cli-page-object anymore.

* fix: remove remaining deprecated `run.*`-calls

* chore: add more deprecations deprecation-workflow

* fix: `implicit-injection`-deprecation

All routes that add watchers will need to inject the store-service
as the store service is internally used in watchers.

* fix: more implicit injection deprecations

* chore: silence implicit-injection deprecation

We can tackle the deprecation when we find the time.

* fix: new linting errors after upgrade

* fix: remove merge conflicts prettierignore

* chore: upgrade to run node 12.22 when building binaries
2022-03-08 12:28:36 -05:00

377 lines
11 KiB
JavaScript

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