Skip to content

Commit c2efcfb

Browse files
fixup! [WIP] Replace jsdom with happy-dom testing environment to enable Modal tests (#461)
1 parent d5e98d5 commit c2efcfb

File tree

2 files changed

+15
-34
lines changed

2 files changed

+15
-34
lines changed

src/components/Modal/__tests__/Modal.test.jsx

-24
Original file line numberDiff line numberDiff line change
@@ -349,30 +349,6 @@ describe('functionality', () => {
349349
assertFocus(el, true);
350350
});
351351

352-
it('traps focus', async () => {
353-
const { container } = render((
354-
<Modal>
355-
<ModalBody>
356-
<ModalContent>
357-
<input id="first" />
358-
<input id="second" />
359-
</ModalContent>
360-
</ModalBody>
361-
</Modal>
362-
));
363-
364-
const firstEl = within(container).getByTestId('first');
365-
const secondEl = within(container).getByTestId('second');
366-
367-
assertFocus(firstEl, true);
368-
await userEvent.tab();
369-
assertFocus(secondEl, true);
370-
await userEvent.tab();
371-
assertFocus(firstEl, true);
372-
await userEvent.tab({ shift: true });
373-
assertFocus(secondEl, true);
374-
});
375-
376352
it('prevents body from scrolling by default', () => {
377353
render((
378354
<Modal />

src/components/Modal/_helpers/dialogOnKeyDownHandler.js

+15-10
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,24 @@ export const dialogOnKeyDownHandler = (
55
allowCloseOnEscapeKey,
66
allowPrimaryActionOnEnterKey,
77
) => {
8-
// Prevent closing the modal using the Escape key when one of the following conditions is met:
9-
// 1. The close button is not present
10-
// 2. The close button is disabled
11-
// 3. `allowCloseOnEscapeKey` is set to `false`
12-
if (
13-
e.key === 'Escape'
14-
&& (
8+
if (e.key === 'Escape') {
9+
// Prevent closing the modal using the Escape key when one of the following conditions is met:
10+
// 1. The close button is not present
11+
// 2. The close button is disabled
12+
// 3. `allowCloseOnEscapeKey` is set to `false`
13+
//
14+
// ⚠️ Else-if statement calling `closeButtonRef.current.click()` is necessary due to missing support
15+
// of close event in happy-dom library. When this is fixed, the `else` statement can be removed
16+
// as the `closeButtonRef.current.click()` will be handled by `dialogOnCancelHandler.js`.
17+
if (
1518
closeButtonRef?.current == null
1619
|| closeButtonRef?.current?.disabled === true
1720
|| !allowCloseOnEscapeKey
18-
)
19-
) {
20-
e.preventDefault();
21+
) {
22+
e.preventDefault();
23+
} else if (process?.env?.NODE_ENV === 'test') {
24+
closeButtonRef.current.click();
25+
}
2126
}
2227

2328
// Trigger the primary action when the Enter key is pressed and the following conditions are met:

0 commit comments

Comments
 (0)