|
| 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 | + - [`setup`](#setup) |
| 13 | + - [`mount`](#mount) |
| 14 | + - [`cleanup`](#cleanup) |
| 15 | + - [`addCleanupTask`](#addcleanuptask) |
| 16 | + - [`removeCleanupTask`](#removecleanuptask) |
| 17 | + - [Utility types](#utility-types) |
| 18 | + |
| 19 | +## Example Usage |
| 20 | + |
| 21 | +```ts |
| 22 | +import { beforeEach } from 'vitest' |
| 23 | +import { cleanup, mount, setup } from '@testing-library/svelte-core' |
| 24 | +import type { |
| 25 | + Component, |
| 26 | + ComponentType, |
| 27 | + ComponentOptions, |
| 28 | + Props, |
| 29 | + Exports, |
| 30 | +} from '@testing-library/svelte-core/types' |
| 31 | + |
| 32 | +import { bindQueries, type Screen } from './bring-your-own-queries.js' |
| 33 | + |
| 34 | +beforeEach(() => { |
| 35 | + cleanup() |
| 36 | +}) |
| 37 | + |
| 38 | +export interface RenderResult<C extends Component> { |
| 39 | + screen: Screen |
| 40 | + component: Exports<C> |
| 41 | + target: HTMLElement |
| 42 | + rerender: (props: Partial<Props<C>>) => Promise<void> |
| 43 | + unmount: () => void |
| 44 | +} |
| 45 | + |
| 46 | +export const render = <C extends Component>( |
| 47 | + component: ComponentType<C>, |
| 48 | + options: ComponentOptions<C> |
| 49 | +): RenderResult<C> => { |
| 50 | + const { baseElement, target, mountOptions } = setup(options) |
| 51 | + const { component, unmount, rerender } = mount(component, mountOptions) |
| 52 | + const screen = bindQueries(baseElement) |
| 53 | + |
| 54 | + return { screen, component, target, rerender, unmount } |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +## API |
| 59 | + |
| 60 | +### `setup` |
| 61 | + |
| 62 | +Validate options and prepare document elements for rendering. |
| 63 | + |
| 64 | +```ts |
| 65 | +const { baseElement, target, mountOptions } = setup(options, renderOptions) |
| 66 | +``` |
| 67 | + |
| 68 | +| Argument | Type | Description | |
| 69 | +| ------------------ | ---------------------------------------- | ---------------------------------------------- | |
| 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 `target` element | `<div>` appended to `document.body` | |
| 77 | +| `mountOptions` | [component options][] | Validated Svelte options to pass to `mount` | `{ target, props: {} }` | |
| 78 | + |
| 79 | +[component options]: https://svelte.dev/docs/client-side-component-api |
| 80 | + |
| 81 | +### `mount` |
| 82 | + |
| 83 | +Mount a Svelte component into the document. |
| 84 | + |
| 85 | +```ts |
| 86 | +const { component, unmount, rerender } = mount(Component, options) |
| 87 | +``` |
| 88 | + |
| 89 | +| Argument | Type | Description | |
| 90 | +| ----------- | --------------------- | ---------------------------- | |
| 91 | +| `Component` | [Svelte component][] | An imported Svelte component | |
| 92 | +| `options` | [component options][] | Svelte component options | |
| 93 | + |
| 94 | +| Result | Type | Description | |
| 95 | +| ----------- | ------------------------------------------ | -------------------------------------------------- | |
| 96 | +| `component` | [component instance][] | The component instance | |
| 97 | +| `unmount` | `() => void` | Unmount the component from the document | |
| 98 | +| `rerender` | `(props: Partial<Props>) => Promise<void>` | Update the component's props and wait for rerender | |
| 99 | + |
| 100 | +[Svelte component]: https://svelte.dev/docs/svelte-components |
| 101 | +[component instance]: https://svelte.dev/docs/client-side-component-api |
| 102 | + |
| 103 | +### `cleanup` |
| 104 | + |
| 105 | +Cleanup rendered components and added elements. Call this when your tests are |
| 106 | +over. |
| 107 | + |
| 108 | +```ts |
| 109 | +cleanup() |
| 110 | +``` |
| 111 | + |
| 112 | +### `addCleanupTask` |
| 113 | + |
| 114 | +Add a custom cleanup task to be called with `cleanup()` |
| 115 | + |
| 116 | +```ts |
| 117 | +addCleanupTask(() => { |
| 118 | + // ...reset something |
| 119 | +}) |
| 120 | +``` |
| 121 | + |
| 122 | +### `removeCleanupTask` |
| 123 | + |
| 124 | +Remove a cleanup task from `cleanup()`. Useful if a cleanup task can only be run |
| 125 | +once and may be run outside of `cleanup` |
| 126 | + |
| 127 | +```ts |
| 128 | +const customCleanup = () => { |
| 129 | + // ...reset something |
| 130 | +} |
| 131 | + |
| 132 | +addCleanupTask(customCleanup) |
| 133 | + |
| 134 | +const manuallyCleanupEarly = () => { |
| 135 | + customCleanup() |
| 136 | + removeCleanupTask(customCleanup) |
| 137 | +} |
| 138 | +``` |
| 139 | + |
| 140 | +### Utility types |
| 141 | + |
| 142 | +This module exports various utility types from |
| 143 | +`@testing-library/svelte-core/types`. They adapt to whatever Svelte version is |
| 144 | +installed, and can be used to get type signatures for imported components, |
| 145 | +props, events, etc. |
| 146 | + |
| 147 | +See [`./types.d.ts`](./types.d.ts) for the full list of types available. |
0 commit comments