Skip to content

Commit 4a0c4e4

Browse files
authored
feat(ct): type the mount fixture via story template argument (#41920)
1 parent f39623b commit 4a0c4e4

16 files changed

Lines changed: 406 additions & 739 deletions

File tree

docs/src/test-api/class-fixtures.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,51 @@ test('example test', async ({ page, context }) => {
6969
});
7070
```
7171

72+
## method: Fixtures.mount
73+
* since: v1.62
74+
- returns: <[Locator]>
75+
76+
Mounts a component story and returns a [Locator] pointing to the root element the story was rendered into. Scope your queries from the returned locator: `component.getByRole('button')`, not `page.getByRole('button')`.
77+
78+
A **story** is a small wrapper component that embeds the component under test in one specific scenario: hard-coded props, mock data, providers, recorded callbacks. Stories are rendered by a **gallery** page that you implement and serve at [`property: TestOptions.baseURL`]. The gallery exposes `window.mount(params)` and `window.unmount()` functions that render a story into its root element. Each call to [`method: Fixtures.mount`] navigates to [`property: TestOptions.baseURL`] and calls `window.mount()` with the story id and props, so tests are fully isolated from each other.
79+
80+
**Usage**
81+
82+
```js
83+
test('click should expand', async ({ mount }) => {
84+
const component = await mount('components/Expandable/Stateful');
85+
await component.getByRole('button').click();
86+
await expect(component.getByTestId('expanded')).toHaveValue('true');
87+
});
88+
```
89+
90+
Pass the story type as a template argument to type-check the props:
91+
92+
```js
93+
import type { WithTitle } from './Button.story';
94+
95+
test('renders the title', async ({ mount }) => {
96+
const component = await mount<typeof WithTitle>('Button/WithTitle', { title: 'Hello' });
97+
await expect(component).toContainText('Hello');
98+
});
99+
```
100+
101+
The returned locator is augmented with two methods:
102+
* `update(props)` - re-renders the same story with new props without remounting, preserving component state;
103+
* `unmount()` - unmounts the story.
104+
105+
### param: Fixtures.mount.storyId
106+
* since: v1.62
107+
- `storyId` <[string]>
108+
109+
Identifier of the story to mount, as resolved by the gallery page. Conventionally, the story file path plus the exported story name, for example `'components/Button/Primary'`.
110+
111+
### param: Fixtures.mount.props
112+
* since: v1.62
113+
- `props` ?<[Object]>
114+
115+
Optional plain, serializable props passed to the story.
116+
72117
## property: Fixtures.page
73118
* since: v1.10
74119
- type: <[Page]>

0 commit comments

Comments
 (0)