open-nomad/ui/tests/integration/components/popover-menu-test.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

128 lines
3.9 KiB
JavaScript
Raw Normal View History

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
import { click, render } from '@ember/test-helpers';
2019-12-03 06:16:56 +00:00
import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
import hbs from 'htmlbars-inline-precompile';
import { componentA11yAudit } from 'nomad-ui/tests/helpers/a11y-audit';
2019-12-03 06:16:56 +00:00
import { create } from 'ember-cli-page-object';
import popoverMenuPageObject from 'nomad-ui/tests/pages/components/popover-menu';
const PopoverMenu = create(popoverMenuPageObject());
2021-12-28 14:45:20 +00:00
module('Integration | Component | popover-menu', function (hooks) {
2019-12-03 06:16:56 +00:00
setupRenderingTest(hooks);
2021-12-28 14:45:20 +00:00
const commonProperties = (overrides) =>
2019-12-03 06:16:56 +00:00
Object.assign(
{
triggerClass: '',
label: 'Trigger Label',
},
overrides
);
const commonTemplate = hbs`
<PopoverMenu
@isOpen={{isOpen}}
@label={{label}}
@triggerClass={{triggerClass}} as |m|>
2019-12-03 06:16:56 +00:00
<h1>This is a heading</h1>
<label>This is an input: <input id="mock-input-for-test" type="text" /></label>
<button id="mock-button-for-test" type="button" onclick={{action m.actions.close}}>Close Button</button>
</PopoverMenu>
2019-12-03 06:16:56 +00:00
`;
2021-12-28 14:45:20 +00:00
test('presents as a button with a chevron-down icon', async function (assert) {
assert.expect(5);
2019-12-03 06:16:56 +00:00
const props = commonProperties();
this.setProperties(props);
await render(commonTemplate);
2019-12-03 06:16:56 +00:00
assert.ok(PopoverMenu.isPresent);
assert.ok(PopoverMenu.labelHasIcon);
assert.notOk(PopoverMenu.menu.isOpen);
assert.equal(PopoverMenu.label, props.label);
await componentA11yAudit(this.element, assert);
2019-12-03 06:16:56 +00:00
});
2021-12-28 14:45:20 +00:00
test('clicking the trigger button toggles the popover menu', async function (assert) {
assert.expect(3);
2019-12-03 06:16:56 +00:00
const props = commonProperties();
this.setProperties(props);
await render(commonTemplate);
2019-12-03 06:16:56 +00:00
assert.notOk(PopoverMenu.menu.isOpen);
await PopoverMenu.toggle();
assert.ok(PopoverMenu.menu.isOpen);
await componentA11yAudit(this.element, assert);
2019-12-03 06:16:56 +00:00
});
2021-12-28 14:45:20 +00:00
test('the trigger gets the triggerClass prop assigned as a class', async function (assert) {
2019-12-03 06:16:56 +00:00
const specialClass = 'is-special';
const props = commonProperties({ triggerClass: specialClass });
this.setProperties(props);
await render(commonTemplate);
2019-12-03 06:16:56 +00:00
2019-12-13 02:35:48 +00:00
assert.dom('[data-test-popover-trigger]').hasClass('is-special');
2019-12-03 06:16:56 +00:00
});
2021-12-28 14:45:20 +00:00
test('pressing DOWN ARROW when the trigger is focused opens the popover menu', async function (assert) {
2019-12-03 06:16:56 +00:00
const props = commonProperties();
this.setProperties(props);
await render(commonTemplate);
2019-12-03 06:16:56 +00:00
assert.notOk(PopoverMenu.menu.isOpen);
await PopoverMenu.focus();
await PopoverMenu.downArrow();
assert.ok(PopoverMenu.menu.isOpen);
});
2021-12-28 14:45:20 +00:00
test('pressing TAB when the trigger button is focused and the menu is open focuses the first focusable element in the popover menu', async function (assert) {
2019-12-03 06:16:56 +00:00
const props = commonProperties();
this.setProperties(props);
await render(commonTemplate);
2019-12-03 06:16:56 +00:00
await PopoverMenu.focus();
await PopoverMenu.downArrow();
2019-12-13 02:35:48 +00:00
assert.dom('[data-test-popover-trigger]').isFocused();
2019-12-03 06:16:56 +00:00
await PopoverMenu.focusNext();
2019-12-13 02:35:48 +00:00
assert.dom('#mock-input-for-test').isFocused();
2019-12-03 06:16:56 +00:00
});
2021-12-28 14:45:20 +00:00
test('pressing ESC when the popover menu is open closes the menu and returns focus to the trigger button', async function (assert) {
2019-12-03 06:16:56 +00:00
const props = commonProperties();
this.setProperties(props);
await render(commonTemplate);
2019-12-03 06:16:56 +00:00
await PopoverMenu.toggle();
assert.ok(PopoverMenu.menu.isOpen);
await PopoverMenu.esc();
assert.notOk(PopoverMenu.menu.isOpen);
});
2021-12-28 14:45:20 +00:00
test('the ember-basic-dropdown object is yielded as context, including the close action', async function (assert) {
2019-12-03 06:16:56 +00:00
const props = commonProperties();
this.setProperties(props);
await render(commonTemplate);
2019-12-03 06:16:56 +00:00
await PopoverMenu.toggle();
assert.ok(PopoverMenu.menu.isOpen);
await click('#mock-button-for-test');
assert.notOk(PopoverMenu.menu.isOpen);
});
});