-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
246 additions
and
129 deletions.
There are no files selected for viewing
This file contains 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 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,80 @@ | ||
# @testing-library/svelte/core | ||
|
||
Do you want to build your own Svelte testing library? You may want to use our rendering core, which abstracts away differences in Svelte versions to provide a simple API to render Svelte components into the document and clean them up afterwards | ||
|
||
<!-- START doctoc generated TOC please keep comment here to allow auto update --> | ||
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE --> | ||
|
||
- [Usage](#usage) | ||
- [API](#api) | ||
- [`prepareDocument`](#preparedocument) | ||
- [`mount`](#mount) | ||
- [`cleanup`](#cleanup) | ||
|
||
<!-- END doctoc generated TOC please keep comment here to allow auto update --> | ||
|
||
## Usage | ||
|
||
```ts | ||
import { prepareDocument, mount, cleanup } from '@testing-library/svelte/core' | ||
import MyCoolComponent from './my-cool-component.svelte' | ||
|
||
const { baseElement, target, options } = prepareDocument({ awesome: true }) | ||
const { component, unmount, rerender } = mount(MyCoolComponent, options) | ||
|
||
// later | ||
cleanup() | ||
``` | ||
|
||
## API | ||
|
||
### `prepareDocument` | ||
|
||
Validate options and prepare document elements for rendering. | ||
|
||
```ts | ||
const { baseElement, target, options } = prepareDocument(propsOrOptions, renderOptions) | ||
``` | ||
|
||
| Argument | Type | Description | | ||
| ---------------- | ---------------------------------------- | --------------------------------------------------------------------- | | ||
| `propsOrOptions` | `Props` or partial [component options][] | The component's props, or options to pass to Svelte's client-side API | | ||
| `renderOptions` | `{ baseElement?: HTMLElement }` | customize `baseElement`; will be `document.body` if unspecified | | ||
|
||
| Result | Type | Description | | ||
| ------------- | --------------------- | -------------------------------------------------------------------- | | ||
| `baseElement` | `HTMLElement` | The base element, `document.body` by default | | ||
| `target` | `HTMLElement` | The component's `target` element, a `<div>` by default | | ||
| `options` | [component options][] | Validated and normalized Svelte options to pass to `renderComponent` | | ||
|
||
[component options]: https://svelte.dev/docs/client-side-component-api | ||
|
||
### `mount` | ||
|
||
Mount a Svelte component into the document. | ||
|
||
```ts | ||
const { component, unmount, rerender } = mount(Component, options) | ||
``` | ||
|
||
| Argument | Type | Description | | ||
| ----------- | --------------------- | ---------------------------- | | ||
| `Component` | [Svelte component][] | An imported Svelte component | | ||
| `options` | [component options][] | Svelte component options | | ||
|
||
| Result | Type | Description | | ||
| ----------- | ------------------------------------------ | -------------------------------------------------- | | ||
| `component` | [component instance][] | The component instance | | ||
| `unmount` | `() => void` | Unmount the component from the document | | ||
| `rerender` | `(props: Partial<Props>) => Promise<void>` | Update the component's props and wait for rerender | | ||
|
||
[Svelte component]: https://svelte.dev/docs/svelte-components | ||
[component instance]: https://svelte.dev/docs/client-side-component-api | ||
|
||
### `cleanup` | ||
|
||
Cleanup rendered components and added elements. Call this when your tests are over. | ||
|
||
```ts | ||
cleanup() | ||
``` |
This file contains 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,30 @@ | ||
/** @type {Map<unknown, () => void} */ | ||
const itemsToClean = new Map() | ||
|
||
/** Register an item for later cleanup. */ | ||
const addItemToCleanup = (item, onCleanup) => { | ||
itemsToClean.set(item, onCleanup) | ||
} | ||
|
||
/** Remove an individual item from cleanup without running its cleanup handler. */ | ||
const removeItemFromCleanup = (item) => { | ||
itemsToClean.delete(item) | ||
} | ||
|
||
/** Clean up an individual item. */ | ||
const cleanupItem = (item) => { | ||
const handleCleanup = itemsToClean.get(item) | ||
handleCleanup?.() | ||
itemsToClean.delete(item) | ||
} | ||
|
||
/** Clean up all components and elements added to the document. */ | ||
const cleanup = () => { | ||
for (const handleCleanup of itemsToClean.values()) { | ||
handleCleanup() | ||
} | ||
|
||
itemsToClean.clear() | ||
} | ||
|
||
export { addItemToCleanup, cleanup, cleanupItem, removeItemFromCleanup } |
This file contains 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 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 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 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,75 @@ | ||
import { tick } from 'svelte' | ||
|
||
import { addItemToCleanup, removeItemFromCleanup } from './cleanup.js' | ||
import * as LegacySvelte from './legacy.js' | ||
import * as ModernSvelte from './modern.svelte.js' | ||
import { validateOptions } from './validate-options.js' | ||
|
||
const { mountComponent, allowedOptions } = ModernSvelte.IS_MODERN_SVELTE | ||
? ModernSvelte | ||
: LegacySvelte | ||
|
||
/** | ||
* Validate options and prepare document elements for rendering. | ||
* | ||
* @template {import('svelte').SvelteComponent} C | ||
* @param {import('svelte').ComponentProps<C> | Partial<import('svelte').ComponentConstructorOptions<import('svelte').ComponentProps<C>>>} propsOrOptions | ||
* @param {{ baseElement?: HTMLElement }} renderOptions | ||
* @returns {{ | ||
* baseElement: HTMLElement | ||
* target: HTMLElement | ||
* options: import('svelte').ComponentConstructorOptions<import('svelte').ComponentProps<C>> | ||
* }} | ||
*/ | ||
const prepareDocument = (propsOrOptions = {}, renderOptions = {}) => { | ||
const options = validateOptions(allowedOptions, propsOrOptions) | ||
|
||
const baseElement = | ||
renderOptions.baseElement ?? options.target ?? document.body | ||
|
||
const target = | ||
options.target ?? baseElement.appendChild(document.createElement('div')) | ||
|
||
addItemToCleanup(target, () => { | ||
if (target.parentNode === document.body) { | ||
document.body.removeChild(target) | ||
} | ||
}) | ||
|
||
return { baseElement, target, options: { ...options, target } } | ||
} | ||
|
||
/** | ||
* Render a Svelte component into the document. | ||
* | ||
* @template {import('svelte').SvelteComponent} C | ||
* @param {import('svelte').ComponentType<C>} Component | ||
* @param {import('svelte').ComponentConstructorOptions<import('svelte').ComponentProps<C>>} options | ||
* @returns {{ | ||
* component: C | ||
* rerender: (props: Partial<import('svelte').ComponentProps<C>>) => Promise<void> | ||
* unmount: () => void | ||
* }} | ||
*/ | ||
const mount = (Component, options = {}) => { | ||
const { component, unmountComponent, updateProps } = mountComponent( | ||
Component, | ||
options | ||
) | ||
|
||
const unmount = () => { | ||
unmountComponent() | ||
removeItemFromCleanup(component) | ||
} | ||
|
||
const rerender = async (props) => { | ||
updateProps(props) | ||
await tick() | ||
} | ||
|
||
addItemToCleanup(component, unmount) | ||
|
||
return { component, unmount, rerender } | ||
} | ||
|
||
export { mount, prepareDocument } |
This file contains 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 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
Oops, something went wrong.