Repository navigation
test(design-system): add jsdom consumer test scenarios [AR-117427] #927
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
Open
mmurawski-dn
wants to merge
7
commits into
drivenets:main
Choose a base branch
from
mmurawski-dn:claude/AR-117427-jsdom-consumer-tests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+827
−37
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
94f3d48
test(design-system): add jsdom consumer test scenarios [AR-117427]
claude 3eb7666
test(design-system): run story hooks and flush effects in jsdom scena…
claude 939cd01
fix(design-system): render without matchMedia and scrollIntoView in j…
mmurawski-dn 79b04f6
test(design-system): fail jsdom scenarios on console output and cover…
mmurawski-dn 81b1c59
test(design-system): document jsdom stubs instead of guarding compone…
mmurawski-dn bcf78c3
docs(design-system): move jsdom testing setup to guidelines page [AR-…
mmurawski-dn 0189546
test(design-system): run jsdom scenarios on jsdom 26 and catch unmoun…
mmurawski-dn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| import { Meta } from '@storybook/addon-docs/blocks'; | ||
|
|
||
| <Meta title="Guidelines/Testing" /> | ||
|
|
||
| # Testing with JSDOM | ||
|
|
||
| Components render and respond to user events in JSDOM-based test runners (Vitest or Jest with `jsdom`). JSDOM lacks a | ||
| few browser APIs the components use, so add these stubs to your test setup file: | ||
|
|
||
| ```typescript | ||
| // vitest.setup.ts or jest.setup.ts | ||
| window.matchMedia = (query: string) => ({ | ||
| matches: false, | ||
| media: query, | ||
| onchange: null, | ||
| addEventListener: () => {}, | ||
| removeEventListener: () => {}, | ||
| addListener: () => {}, | ||
| removeListener: () => {}, | ||
| dispatchEvent: () => false, | ||
| }); | ||
|
|
||
| globalThis.ResizeObserver = class { | ||
| observe() {} | ||
| unobserve() {} | ||
| disconnect() {} | ||
| }; | ||
|
|
||
| Element.prototype.scrollTo = () => {}; | ||
| Element.prototype.scrollIntoView = () => {}; | ||
| ``` | ||
|
|
||
| Without them: | ||
|
|
||
| - Components with responsive props (`DsButton`, `DsStack`, `DsGrid`, `DsDrawer`, `DsPanel` and anything built on | ||
| them, such as `DsDatePicker` and `DsTable`) throw `window.matchMedia is not a function` on render. | ||
| - Opening `DsSelect`, `DsAutocomplete` or another positioned overlay throws `ResizeObserver is not defined`, and so do | ||
| components that measure their content, such as `DsTable`. | ||
| - `DsSelect` never commits the chosen option, and `DsTimePicker` throws when it opens (`scrollTo`). | ||
| - `DsCommentThread` throws on render (`scrollIntoView`). | ||
|
|
||
| ## Older JSDOM versions | ||
|
|
||
| Jest 29 and 30 ship jsdom 20 and 26. | ||
|
|
||
| - Before jsdom 30 there is no `CSS.escape`. Keyboard navigation in `DsAutocomplete` and `DsRadioGroup`, and | ||
| `DsTable` column sizing, call it and throw `CSS is not defined`. `@testing-library/jest-dom` installs a polyfill; | ||
| without it, install `css.escape` and import it in your setup file. | ||
| - Before jsdom 27, highlighted option text is split, so `DsAutocomplete` options are named "Ban ana" instead of | ||
| "Banana". Match on text content instead | ||
| (`getByRole('option', { name: (_name, element) => element.textContent.includes('Banana') })`), or use jsdom 27 or | ||
| later. | ||
|
|
||
| ## CSS imports in Jest | ||
|
|
||
| If a test imports `@drivenets/design-system/index.min.css`, map CSS files to a stub in Jest | ||
| (`moduleNameMapper: { '\\.css$': '<rootDir>/style-stub.js' }`). Vitest handles CSS imports on its own. | ||
|
|
||
| ## Known limitations | ||
|
|
||
| - JSDOM does no layout. Elements have no size, so a `DsTable` with `virtualized` set renders no data rows. Leave | ||
| `virtualized` off in tests that check rows. | ||
| - Truncation (tooltips on truncated text), overlay positioning and scroll behavior can't be asserted. Cover them in a | ||
| real browser (for example Vitest browser mode or Playwright). | ||
| - The `matchMedia` stub never matches, so responsive props resolve to the `md` breakpoint. Return `matches: true` to | ||
| test `lg`. | ||
| - The `DsSlider` thumb appears one tick after render, so find it with `findByRole('slider')`. | ||
| - `DsCodeInput` logs JSDOM's "Not implemented: HTMLCanvasElement's getContext()" message. It is harmless. | ||
|
mmurawski-dn marked this conversation as resolved.
Resolved
|
||
|
|
||
| ## Writing tests | ||
|
|
||
| Query components by role and accessible name (`getByRole('combobox')`, `getByRole('dialog', { name: 'Title' })`) and | ||
| drive them with `@testing-library/user-event`. The design system runs its own scenarios with exactly this setup, on | ||
| jsdom 26 and the latest jsdom, in [`tests/jsdom`](https://github.com/drivenets/design-system/tree/main/packages/design-system/tests/jsdom). | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.