Skip to content

Commit 21f164f

Browse files
committed
added more focus-management integration tests for Modal and Flyout components
1 parent 9852f6f commit 21f164f

File tree

2 files changed

+115
-1
lines changed

2 files changed

+115
-1
lines changed

showcase/tests/integration/components/hds/flyout/index-test.js

+57
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,63 @@ module('Integration | Component | hds/flyout/index', function (hooks) {
160160
assert.dom('button.hds-flyout__dismiss').isFocused();
161161
});
162162

163+
test('it returns focus to the element that initiated the open event, if is still in the DOM', async function (assert) {
164+
await render(
165+
hbs`<button id="test-button" type="button" {{on "click" (set this "showFlyout" true)}}>open flyout</button>
166+
{{#if this.showFlyout}}
167+
<Hds::Flyout id="test-flyout" as |M|>
168+
<M.Header>Title</M.Header>
169+
</Hds::Flyout>
170+
{{/if}}
171+
`
172+
);
173+
await click('#test-button');
174+
assert.true(this.showFlyout);
175+
await click('button.hds-flyout__dismiss');
176+
assert.dom('#test-button').isFocused();
177+
});
178+
179+
// not sure how to reach the `body` element, it says "body is not a valid root element"
180+
skip('it returns focus to the `body` element, if the one that initiated the open event not anymore in the DOM', async function (assert) {
181+
await render(
182+
hbs`<Hds::Dropdown as |D|>
183+
<D.ToggleButton id="test-toggle" @text="open flyout" />
184+
<D.Interactive id="test-interactive" {{on "click" (set this "showFlyout" true)}}>open flyout</D.Interactive>
185+
</Hds::Dropdown>
186+
{{#if this.showFlyout}}
187+
<Hds::Flyout id="test-flyout" as |M|>
188+
<M.Header>Title</M.Header>
189+
</Hds::Flyout>
190+
{{/if}}
191+
`
192+
);
193+
await click('#test-toggle');
194+
await click('#test-interactive');
195+
assert.true(this.showFlyout);
196+
await click('button.hds-flyout__dismiss');
197+
assert.dom('body', 'body').isFocused();
198+
});
199+
200+
test('it returns focus to a specific element if provided via`@returnFocusTo`', async function (assert) {
201+
await render(
202+
hbs`<Hds::Dropdown as |D|>
203+
<D.ToggleButton id="test-toggle" @text="open flyout" />
204+
<D.Interactive id="test-interactive" {{on "click" (set this "showFlyout" true)}}>open flyout</D.Interactive>
205+
</Hds::Dropdown>
206+
{{#if this.showFlyout}}
207+
<Hds::Flyout id="test-flyout" @returnFocusTo="test-toggle" as |M|>
208+
<M.Header>Title</M.Header>
209+
</Hds::Flyout>
210+
{{/if}}
211+
`
212+
);
213+
await click('#test-toggle');
214+
await click('#test-interactive');
215+
assert.true(this.showFlyout);
216+
await click('button.hds-flyout__dismiss');
217+
assert.dom('#test-toggle').isFocused();
218+
});
219+
163220
// CALLBACKS
164221

165222
test('it should call `onOpen` function if provided', async function (assert) {

showcase/tests/integration/components/hds/modal/index-test.js

+58-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* SPDX-License-Identifier: MPL-2.0
44
*/
55

6-
import { module, test } from 'qunit';
6+
import { module, test, skip } from 'qunit';
77
import { setupRenderingTest } from 'ember-qunit';
88
import {
99
click,
@@ -194,6 +194,63 @@ module('Integration | Component | hds/modal/index', function (hooks) {
194194
assert.dom('button.hds-modal__dismiss').isFocused();
195195
});
196196

197+
test('it returns focus to the element that initiated the open event, if is still in the DOM', async function (assert) {
198+
await render(
199+
hbs`<button id="test-button" type="button" {{on "click" (set this "showModal" true)}}>open modal</button>
200+
{{#if this.showModal}}
201+
<Hds::Modal id="test-modal" as |M|>
202+
<M.Header>Title</M.Header>
203+
</Hds::Modal>
204+
{{/if}}
205+
`
206+
);
207+
await click('#test-button');
208+
assert.true(this.showModal);
209+
await click('button.hds-modal__dismiss');
210+
assert.dom('#test-button').isFocused();
211+
});
212+
213+
// not sure how to reach the `body` element, it says "body is not a valid root element"
214+
skip('it returns focus to the `body` element, if the one that initiated the open event not anymore in the DOM', async function (assert) {
215+
await render(
216+
hbs`<Hds::Dropdown as |D|>
217+
<D.ToggleButton id="test-toggle" @text="open modal" />
218+
<D.Interactive id="test-interactive" {{on "click" (set this "showModal" true)}}>open modal</D.Interactive>
219+
</Hds::Dropdown>
220+
{{#if this.showModal}}
221+
<Hds::Modal id="test-modal" as |M|>
222+
<M.Header>Title</M.Header>
223+
</Hds::Modal>
224+
{{/if}}
225+
`
226+
);
227+
await click('#test-toggle');
228+
await click('#test-interactive');
229+
assert.true(this.showModal);
230+
await click('button.hds-modal__dismiss');
231+
assert.dom('body', 'body').isFocused();
232+
});
233+
234+
test('it returns focus to a specific element if provided via`@returnFocusTo`', async function (assert) {
235+
await render(
236+
hbs`<Hds::Dropdown as |D|>
237+
<D.ToggleButton id="test-toggle" @text="open modal" />
238+
<D.Interactive id="test-interactive" {{on "click" (set this "showModal" true)}}>open modal</D.Interactive>
239+
</Hds::Dropdown>
240+
{{#if this.showModal}}
241+
<Hds::Modal id="test-modal" @returnFocusTo="test-toggle" as |M|>
242+
<M.Header>Title</M.Header>
243+
</Hds::Modal>
244+
{{/if}}
245+
`
246+
);
247+
await click('#test-toggle');
248+
await click('#test-interactive');
249+
assert.true(this.showModal);
250+
await click('button.hds-modal__dismiss');
251+
assert.dom('#test-toggle').isFocused();
252+
});
253+
197254
// CALLBACKS
198255

199256
test('it should call `onOpen` function if provided', async function (assert) {

0 commit comments

Comments
 (0)