|
| 1 | +# @testing-library/svelte-core |
| 2 | + |
| 3 | +Do you want to build your own Svelte testing library? You may want to use our |
| 4 | +rendering core, which abstracts away differences in Svelte versions to provide a |
| 5 | +simple API to render Svelte components into the document and clean them up |
| 6 | +afterwards |
| 7 | + |
| 8 | +## Table of Contents |
| 9 | + |
| 10 | +- [Example Usage](#example-usage) |
| 11 | +- [API](#api) |
| 12 | + - [`render`](#render) |
| 13 | + - [`setup`](#setup) |
| 14 | + - [`mount`](#mount) |
| 15 | + - [`cleanup`](#cleanup) |
| 16 | + - [`addCleanupTask`](#addcleanuptask) |
| 17 | + - [`removeCleanupTask`](#removecleanuptask) |
| 18 | + - [Utility types](#utility-types) |
| 19 | + |
| 20 | +## Example Usage |
| 21 | + |
| 22 | +```ts |
| 23 | +import { beforeEach } from 'vitest' |
| 24 | +import * as SvelteCore from '@testing-library/svelte-core' |
| 25 | + |
| 26 | +import { bindQueries, type Screen } from './bring-your-own-queries.js' |
| 27 | + |
| 28 | +beforeEach(() => { |
| 29 | + cleanup() |
| 30 | +}) |
| 31 | + |
| 32 | +export interface RenderResult<C extends SvelteCore.Component> { |
| 33 | + screen: Screen |
| 34 | + component: SvelteCore.Exports<C> |
| 35 | + rerender: SvelteCore.Rerender<C> |
| 36 | + unmount: () => void |
| 37 | +} |
| 38 | + |
| 39 | +export const render = <C extends SvelteCore.Component>( |
| 40 | + component: SvelteCore.ComponentImport<C>, |
| 41 | + options: SvelteCore.ComponentOptions<C> |
| 42 | +): RenderResult<C> => { |
| 43 | + const { baseElement, component, unmount, rerender } = SvelteCore.render( |
| 44 | + component, |
| 45 | + options |
| 46 | + ) |
| 47 | + const screen = bindQueries(baseElement) |
| 48 | + |
| 49 | + return { screen, component, rerender, unmount } |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +## API |
| 54 | + |
| 55 | +### `render` |
| 56 | + |
| 57 | +Set up the document and mount a component into that document. |
| 58 | + |
| 59 | +```ts |
| 60 | +const { baseElement, container, component, unmount, rerender } = render( |
| 61 | + Component, |
| 62 | + componentOptions, |
| 63 | + setupOptions |
| 64 | +) |
| 65 | +``` |
| 66 | + |
| 67 | +| Argument | Type | Description | |
| 68 | +| ------------------ | ---------------------------------------- | ---------------------------------------------- | |
| 69 | +| `Component` | [Svelte component][] | An imported Svelte component | |
| 70 | +| `componentOptions` | `Props` or partial [component options][] | Options for how the component will be rendered | |
| 71 | +| `setupOptions` | `{ baseElement?: HTMLElement }` | Optionally override `baseElement` | |
| 72 | + |
| 73 | +| Result | Type | Description | Default | |
| 74 | +| ------------- | ------------------------------------------ | ---------------------------------------- | ----------------------------------- | |
| 75 | +| `baseElement` | `HTMLElement` | The base element | `document.body` | |
| 76 | +| `target` | `HTMLElement` | The component's immediate parent element | `<div>` appended to `document.body` | |
| 77 | +| `component` | [component instance][] | The component instance | N/A | |
| 78 | +| `rerender` | `(props: Partial<Props>) => Promise<void>` | Update the component's props | N/A | |
| 79 | +| `unmount` | `() => void` | Unmount the component from the document | N/A | |
| 80 | + |
| 81 | +> \[!TIP] |
| 82 | +> Calling `render` is equivalent to calling `setup` followed by `mount` |
| 83 | +> |
| 84 | +> ```ts |
| 85 | +> const { baseElement, container, mountOptions } = setup( |
| 86 | +> componentOptions, |
| 87 | +> setupOptions |
| 88 | +> ) |
| 89 | +> const { component, rerender, unmount } = mount(Component, mountOptions) |
| 90 | +> ``` |
| 91 | +
|
| 92 | +### `setup` |
| 93 | +
|
| 94 | +Validate options and prepare document elements for rendering. |
| 95 | +
|
| 96 | +```ts |
| 97 | +const { baseElement, target, mountOptions } = setup(options, renderOptions) |
| 98 | +``` |
| 99 | +
|
| 100 | +| Argument | Type | Description | |
| 101 | +| ------------------ | ---------------------------------------- | ---------------------------------------------- | |
| 102 | +| `componentOptions` | `Props` or partial [component options][] | Options for how the component will be rendered | |
| 103 | +| `setupOptions` | `{ baseElement?: HTMLElement }` | Optionally override `baseElement` | |
| 104 | + |
| 105 | +| Result | Type | Description | Default | |
| 106 | +| -------------- | --------------------- | ------------------------------------------- | ----------------------------------- | |
| 107 | +| `baseElement` | `HTMLElement` | The base element | `document.body` | |
| 108 | +| `container` | `HTMLElement` | The component's immediate parent element | `<div>` appended to `document.body` | |
| 109 | +| `mountOptions` | [component options][] | Validated Svelte options to pass to `mount` | `{ target, props: {} }` | |
| 110 | + |
| 111 | +[component options]: https://svelte.dev/docs/client-side-component-api |
| 112 | + |
| 113 | +### `mount` |
| 114 | + |
| 115 | +Mount a Svelte component into the document. |
| 116 | + |
| 117 | +```ts |
| 118 | +const { component, unmount, rerender } = mount(Component, options) |
| 119 | +``` |
| 120 | + |
| 121 | +| Argument | Type | Description | |
| 122 | +| -------------- | --------------------- | ---------------------------- | |
| 123 | +| `Component` | [Svelte component][] | An imported Svelte component | |
| 124 | +| `mountOptions` | [component options][] | Svelte component options | |
| 125 | + |
| 126 | +| Result | Type | Description | |
| 127 | +| ----------- | ------------------------------------------ | --------------------------------------- | |
| 128 | +| `component` | [component instance][] | The component instance | |
| 129 | +| `unmount` | `() => void` | Unmount the component from the document | |
| 130 | +| `rerender` | `(props: Partial<Props>) => Promise<void>` | Update the component's props | |
| 131 | + |
| 132 | +[Svelte component]: https://svelte.dev/docs/svelte-components |
| 133 | +[component instance]: https://svelte.dev/docs/client-side-component-api |
| 134 | + |
| 135 | +### `cleanup` |
| 136 | + |
| 137 | +Cleanup rendered components and added elements. Call this when your tests are |
| 138 | +over. |
| 139 | + |
| 140 | +```ts |
| 141 | +cleanup() |
| 142 | +``` |
| 143 | + |
| 144 | +### `addCleanupTask` |
| 145 | + |
| 146 | +Add a custom cleanup task to be called with `cleanup()` |
| 147 | + |
| 148 | +```ts |
| 149 | +addCleanupTask(() => { |
| 150 | + // ...reset something |
| 151 | +}) |
| 152 | +``` |
| 153 | + |
| 154 | +### `removeCleanupTask` |
| 155 | + |
| 156 | +Remove a cleanup task from `cleanup()`. Useful if a cleanup task can only be run |
| 157 | +once and may be run outside of `cleanup` |
| 158 | + |
| 159 | +```ts |
| 160 | +const customCleanup = () => { |
| 161 | + // ...reset something |
| 162 | +} |
| 163 | + |
| 164 | +addCleanupTask(customCleanup) |
| 165 | + |
| 166 | +const manuallyCleanupEarly = () => { |
| 167 | + customCleanup() |
| 168 | + removeCleanupTask(customCleanup) |
| 169 | +} |
| 170 | +``` |
| 171 | + |
| 172 | +### Utility types |
| 173 | + |
| 174 | +This module exports various utility types from |
| 175 | +`@testing-library/svelte-core/types`. They adapt to whatever Svelte version is |
| 176 | +installed, and can be used to get type signatures for imported components, |
| 177 | +props, events, etc. |
| 178 | + |
| 179 | +See [`./types.d.ts`](./types.d.ts) for the full list of types available. |
0 commit comments