-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] Replace jsdom
with happy-dom
testing environment to enable Modal
tests (#461)
#545
base: bc/461
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -84,13 +84,19 @@ describe('rendering', () => { | |||||||||
{ children: <div>content text</div> }, | ||||||||||
(rootElement) => expect(within(rootElement).getByText('content text')), | ||||||||||
], | ||||||||||
// Two following tests must used rootElement.outerHTML.includes() to test presence of CSS variables in the DOM, | ||||||||||
// because the `toHaveStyle` matcher does not support CSS variables with var() function. | ||||||||||
Comment on lines
+87
to
+88
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
It's twice in this document. |
||||||||||
[ | ||||||||||
{ columnGap: responsiveSpacingBreakpoints }, | ||||||||||
(rootElement) => expect(rootElement).toHaveStyle(responsiveSpacingStyles('column-gap')), | ||||||||||
(rootElement) => { | ||||||||||
Object.entries(responsiveSpacingStyles('column-gap')).forEach(([ccsAttribute, cssValue]) => { | ||||||||||
expect(rootElement.outerHTML.includes(`${ccsAttribute}: ${cssValue}`)).toBeTruthy(); | ||||||||||
}); | ||||||||||
}, | ||||||||||
], | ||||||||||
[ | ||||||||||
{ columnGap: 0 }, | ||||||||||
(rootElement) => expect(rootElement).toHaveStyle({ '--rui-local-column-gap-xs': 'var(--rui-dimension-space-0)' }), | ||||||||||
(rootElement) => expect(rootElement.outerHTML.includes('--rui-local-column-gap-xs: var(--rui-dimension-space-0)')).toBeTruthy(), | ||||||||||
], | ||||||||||
[ | ||||||||||
{ columns: responsiveBreakpoints }, | ||||||||||
|
@@ -116,13 +122,19 @@ describe('rendering', () => { | |||||||||
{ justifyItems: 'placeholder' }, | ||||||||||
(rootElement) => expect(rootElement).toHaveStyle({ '--rui-local-justify-items-xs': 'placeholder' }), | ||||||||||
], | ||||||||||
// Two following tests must used rootElement.outerHTML.includes() to test presence of CSS variables in the DOM, | ||||||||||
// because the `toHaveStyle` matcher does not support CSS variables with var() function. | ||||||||||
[ | ||||||||||
{ rowGap: responsiveSpacingBreakpoints }, | ||||||||||
(rootElement) => expect(rootElement).toHaveStyle(responsiveSpacingStyles('row-gap')), | ||||||||||
(rootElement) => { | ||||||||||
Object.entries(responsiveSpacingStyles('row-gap')).forEach(([ccsAttribute, cssValue]) => { | ||||||||||
expect(rootElement.outerHTML.includes(`${ccsAttribute}: ${cssValue}`)).toBeTruthy(); | ||||||||||
Comment on lines
+130
to
+131
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
}); | ||||||||||
}, | ||||||||||
], | ||||||||||
[ | ||||||||||
{ rowGap: 0 }, | ||||||||||
(rootElement) => expect(rootElement).toHaveStyle({ '--rui-local-row-gap-xs': 'var(--rui-dimension-space-0)' }), | ||||||||||
(rootElement) => expect(rootElement.outerHTML.includes('--rui-local-row-gap-xs: var(--rui-dimension-space-0)')).toBeTruthy(), | ||||||||||
], | ||||||||||
[ | ||||||||||
{ rows: responsiveBreakpoints }, | ||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,10 +14,7 @@ import { ModalContent } from '../ModalContent'; | |
import { ModalFooter } from '../ModalFooter'; | ||
import { ModalHeader } from '../ModalHeader'; | ||
|
||
// Test suites skipped due tu missing implementation of HTMLDialogElement in jsdom | ||
// See https://github.com/jsdom/jsdom/issues/3294 | ||
|
||
describe.skip('rendering', () => { | ||
describe('rendering', () => { | ||
it('renders with "portalId" props', () => { | ||
document.body.innerHTML = '<div id="portal-id" />'; | ||
render(( | ||
|
@@ -29,7 +26,7 @@ describe.skip('rendering', () => { | |
</Modal> | ||
)); | ||
|
||
expect(screen.getByTestId('portal-id').firstChild.firstChild).toHaveAttribute('id', 'id'); | ||
expect(screen.getByTestId('portal-id').firstChild).toHaveAttribute('id', 'id'); | ||
document.body.innerHTML = ''; | ||
}); | ||
|
||
|
@@ -40,31 +37,27 @@ describe.skip('rendering', () => { | |
], | ||
[ | ||
{ position: 'top' }, | ||
(rootElement) => expect(within(rootElement).getByRole('presentation')).toHaveClass('isRootPositionTop'), | ||
], | ||
[ | ||
{ position: 'center' }, | ||
(rootElement) => expect(within(rootElement).getByRole('presentation')).toHaveClass('isRootPositionCenter'), | ||
(rootElement) => expect(rootElement).toHaveClass('isRootPositionTop'), | ||
], | ||
[ | ||
{ size: 'small' }, | ||
(rootElement) => expect(within(rootElement).getByRole('presentation')).toHaveClass('isRootSizeSmall'), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It could still be |
||
(rootElement) => expect(rootElement).toHaveClass('isRootSizeSmall'), | ||
], | ||
[ | ||
{ size: 'medium' }, | ||
(rootElement) => expect(within(rootElement).getByRole('presentation')).toHaveClass('isRootSizeMedium'), | ||
(rootElement) => expect(rootElement).toHaveClass('isRootSizeMedium'), | ||
], | ||
[ | ||
{ size: 'large' }, | ||
(rootElement) => expect(within(rootElement).getByRole('presentation')).toHaveClass('isRootSizeLarge'), | ||
(rootElement) => expect(rootElement).toHaveClass('isRootSizeLarge'), | ||
], | ||
[ | ||
{ size: 'fullscreen' }, | ||
(rootElement) => expect(within(rootElement).getByRole('presentation')).toHaveClass('isRootSizeFullscreen'), | ||
(rootElement) => expect(rootElement).toHaveClass('isRootSizeFullscreen'), | ||
], | ||
[ | ||
{ size: 'auto' }, | ||
(rootElement) => expect(within(rootElement).getByRole('presentation')).toHaveClass('isRootSizeAuto'), | ||
(rootElement) => expect(rootElement).toHaveClass('isRootSizeAuto'), | ||
], | ||
])('renders with props: "%s"', (testedProps, assert) => { | ||
const dom = render(( | ||
|
@@ -77,11 +70,8 @@ describe.skip('rendering', () => { | |
}); | ||
}); | ||
|
||
describe.skip('functionality', () => { | ||
it.each([ | ||
() => userEvent.keyboard('{Escape}'), | ||
() => userEvent.click(screen.getByTestId('id').parentNode), | ||
])('call close modal using `closeButtonRef` (%#)', async (action) => { | ||
describe('functionality', () => { | ||
it('call close modal using `closeButtonRef` (%#)', async () => { | ||
const spy = jest.fn(); | ||
const ref = React.createRef(); | ||
render(( | ||
|
@@ -100,14 +90,11 @@ describe.skip('functionality', () => { | |
</Modal> | ||
)); | ||
|
||
await action(); | ||
await userEvent.keyboard('{Escape}'); | ||
expect(spy).toHaveBeenCalled(); | ||
}); | ||
|
||
it.each([ | ||
() => userEvent.keyboard('{Escape}'), | ||
() => userEvent.click(screen.getByTestId('id').parentNode), | ||
])('do not call close modal using `closeButtonRef` when button is disabled (%#)', async (action) => { | ||
it('do not call close modal using `closeButtonRef` when button is disabled (%#)', async () => { | ||
const spy = jest.fn(); | ||
const ref = React.createRef(); | ||
render(( | ||
|
@@ -127,14 +114,11 @@ describe.skip('functionality', () => { | |
</Modal> | ||
)); | ||
|
||
await action(); | ||
await userEvent.keyboard('{Escape}'); | ||
expect(spy).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it.each([ | ||
() => userEvent.keyboard('{Escape}'), | ||
() => userEvent.click(screen.getByTestId('id').parentNode), | ||
])('call close modal using `closeButtonRef` and `ModalCloseButton` (%#)', async (action) => { | ||
it('call close modal using `closeButtonRef` and `ModalCloseButton` (%#)', async () => { | ||
const spy = jest.fn(); | ||
const ref = React.createRef(); | ||
render(( | ||
|
@@ -151,14 +135,11 @@ describe.skip('functionality', () => { | |
</Modal> | ||
)); | ||
|
||
await action(); | ||
await userEvent.keyboard('{Escape}'); | ||
expect(spy).toHaveBeenCalled(); | ||
}); | ||
|
||
it.each([ | ||
() => userEvent.keyboard('{Escape}'), | ||
() => userEvent.click(screen.getByTestId('id').parentNode), | ||
])('do not call close modal using `closeButtonRef` and `ModalCloseButton` when button is disabled (%#)', async (action) => { | ||
it('do not call close modal using `closeButtonRef` and `ModalCloseButton` when button is disabled (%#)', async () => { | ||
const spy = jest.fn(); | ||
const ref = React.createRef(); | ||
render(( | ||
|
@@ -176,7 +157,7 @@ describe.skip('functionality', () => { | |
</Modal> | ||
)); | ||
|
||
await action(); | ||
await userEvent.keyboard('{Escape}'); | ||
expect(spy).not.toHaveBeenCalled(); | ||
}); | ||
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
autoFocus, | ||
childrenWrapperRef, | ||
primaryButtonRef, | ||
closeButtonRef, | ||
) => { | ||
useEffect( | ||
() => { | ||
|
@@ -52,6 +53,13 @@ | |
}; | ||
|
||
const keyPressHandler = (e) => { | ||
// While <dialog> component uses built-in browser dialog functionality for closing the dialog, we preserve this | ||
// functionality due to lack of support for <dialog> in jsdom and happy-dom testing environments. | ||
Comment on lines
+56
to
+57
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we have any jsdom/happy-dom issues to link to so we can remove this in future? |
||
if (e.key === 'Escape' && closeButtonRef?.current != null) { | ||
closeButtonRef.current.click(); | ||
return; | ||
} | ||
|
||
if ( | ||
e.key === 'Enter' | ||
&& e.target.nodeName !== 'BUTTON' | ||
|
@@ -110,7 +118,7 @@ | |
|
||
return () => window.document.removeEventListener('keydown', keyPressHandler, false); | ||
}, | ||
[ | ||
autoFocus, | ||
childrenWrapperRef, | ||
primaryButtonRef, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
export const tagPropTest = [ | ||
[ | ||
{ tag: 'section' }, | ||
(rootElement) => expect(rootElement).toContainHTML('<section'), | ||
(rootElement) => expect(rootElement.tagName).toEqual('SECTION'), | ||
], | ||
]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 for not forgetting to remove this 🙂.