Skip to content
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

Open
wants to merge 2 commits into
base: bc/461
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module.exports = {
setupFilesAfterEnv: [
'<rootDir>/tests/setupTestingLibrary.js',
],
testEnvironment: 'jsdom',
testEnvironment: '@happy-dom/jest-environment',
transformIgnorePatterns: [
'node_modules/(?!(@react-ui-org))',
],
Expand Down
1,990 changes: 819 additions & 1,171 deletions package-lock.json

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,15 @@
"@babel/preset-env": "^7.22.14",
"@babel/preset-react": "^7.22.5",
"@babel/register": "^7.22.5",
"@happy-dom/jest-environment": "^14.12.0",
"@svgr/webpack": "^8.1.0",
"@testing-library/jest-dom": "^6.1.2",
"@testing-library/react": "^14.0.0",
"@testing-library/user-event": "^14.4.3",
"@testing-library/jest-dom": "^6.4.5",
"@testing-library/react": "^15.0.7",
"@testing-library/user-event": "^14.5.2",
"@visionappscz/eslint-config-visionapps": "^1.7.0",
"@visionappscz/stylelint-config": "^3.0.0",
"autoprefixer": "^10.4.15",
"babel-jest": "^29.6.4",
"babel-jest": "^29.7.0",
"babel-loader": "^9.1.3",
"core-js": "^3.32.1",
"css-loader": "^6.10.0",
Expand All @@ -85,8 +86,7 @@
"eslint-plugin-react": "^7.33.2",
"eslint-plugin-react-hooks": "^4.6.0",
"identity-obj-proxy": "^3.0.0",
"jest": "^29.6.4",
"jest-environment-jsdom": "^29.6.4",
Copy link
Member

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 🙂.

"jest": "^29.7.0",
"markdownlint-cli2": "~0.9.2",
"mini-css-extract-plugin": "^2.7.6",
"postcss": "^8.4.29",
Expand Down
20 changes: 16 additions & 4 deletions src/components/Grid/__tests__/Grid.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// 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.
// The following tests must use `rootElement.outerHTML.includes()` to test presence of CSS variables in the DOM
// because the `toHaveStyle` matcher does not support reading CSS variables via the `var()` function.

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 },
Expand All @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Object.entries(responsiveSpacingStyles('row-gap')).forEach(([ccsAttribute, cssValue]) => {
expect(rootElement.outerHTML.includes(`${ccsAttribute}: ${cssValue}`)).toBeTruthy();
Object.entries(responsiveSpacingStyles('row-gap')).forEach(([cssAttribute, cssValue]) => {
expect(rootElement.outerHTML.includes(`${cssAttribute}: ${cssValue}`)).toBeTruthy();

});
},
],
[
{ 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 },
Expand Down
1 change: 1 addition & 0 deletions src/components/Modal/Modal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export const Modal = ({
autoFocus,
childrenWrapperRef,
primaryButtonRef,
closeButtonRef,
);

useModalScrollPrevention(preventScrollUnderneath);
Expand Down
53 changes: 17 additions & 36 deletions src/components/Modal/__tests__/Modal.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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((
Expand All @@ -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 = '';
});

Expand All @@ -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'),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could still be getByRole('dialog'), couldn't it?

(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((
Expand All @@ -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((
Expand All @@ -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((
Expand All @@ -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((
Expand All @@ -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((
Expand All @@ -176,7 +157,7 @@ describe.skip('functionality', () => {
</Modal>
));

await action();
await userEvent.keyboard('{Escape}');
expect(spy).not.toHaveBeenCalled();
});

Expand Down
2 changes: 1 addition & 1 deletion src/components/Modal/__tests__/ModalTitle.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('rendering', () => {
level: 1,
},
(rootElement) => {
expect(rootElement).toContainHTML('<h1');
expect(rootElement.tagName).toEqual('H1');
},
],
])('renders with props: "%s"', (testedProps, assert) => {
Expand Down
8 changes: 8 additions & 0 deletions src/components/Modal/_hooks/useModalFocus.js
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

obrazek

Could you check this please?

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
autoFocus,
childrenWrapperRef,
primaryButtonRef,
closeButtonRef,
) => {
useEffect(
() => {
Expand Down Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The 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'
Expand Down Expand Up @@ -110,7 +118,7 @@

return () => window.document.removeEventListener('keydown', keyPressHandler, false);
},
[

Check failure on line 121 in src/components/Modal/_hooks/useModalFocus.js

View workflow job for this annotation

GitHub Actions / Lint

React Hook useEffect has a missing dependency: 'closeButtonRef'. Either include it or remove the dependency array
autoFocus,
childrenWrapperRef,
primaryButtonRef,
Expand Down
8 changes: 4 additions & 4 deletions src/components/Radio/__tests__/Radio.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ describe('rendering', () => {
[
{ options: mandatoryProps.options },
(rootElement) => {
expect(within(rootElement).getByLabelText('option 1')).not.toHaveAttribute('checked');
expect(within(rootElement).getByLabelText('option 2')).not.toHaveAttribute('checked');
expect(within(rootElement).getByLabelText('option 1')).not.toBeChecked();
expect(within(rootElement).getByLabelText('option 2')).not.toBeChecked();
expect(within(rootElement).getByLabelText('option 2')).toBeDisabled();
},
],
Expand All @@ -91,14 +91,14 @@ describe('rendering', () => {
onChange: () => {},
value: 'option2',
},
(rootElement) => expect(within(rootElement).getByLabelText('option 2')).toHaveAttribute('checked'),
(rootElement) => expect(within(rootElement).getByLabelText('option 2')).toBeChecked(),
],
[
{
onChange: () => {},
value: 1,
},
(rootElement) => expect(within(rootElement).getByLabelText('option 1')).toHaveAttribute('checked'),
(rootElement) => expect(within(rootElement).getByLabelText('option 1')).toBeChecked(),
],
])('renders with props: "%s"', (testedProps, assert) => {
const dom = render((
Expand Down
2 changes: 1 addition & 1 deletion tests/propTests/tagPropTest.js
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'),
],
];
Loading