|
| 1 | +/** |
| 2 | + * Component rendering core, with support for Svelte 3, 4, and 5 |
| 3 | + */ |
| 4 | +import * as Svelte from 'svelte' |
| 5 | + |
| 6 | +import { addCleanupTask, removeCleanupTask } from './cleanup.js' |
| 7 | +import { createProps } from './props.svelte.js' |
| 8 | + |
| 9 | +/** Whether we're using Svelte >= 5. */ |
| 10 | +const IS_MODERN_SVELTE = typeof Svelte.mount === 'function' |
| 11 | + |
| 12 | +/** Allowed options to the `mount` call or legacy component constructor. */ |
| 13 | +const ALLOWED_MOUNT_OPTIONS = IS_MODERN_SVELTE |
| 14 | + ? ['target', 'anchor', 'props', 'events', 'context', 'intro'] |
| 15 | + : ['target', 'accessors', 'anchor', 'props', 'hydrate', 'intro', 'context'] |
| 16 | + |
| 17 | +/** Mount a modern Svelte 5 component into the DOM. */ |
| 18 | +const mountModern = (Component, options) => { |
| 19 | + const [props, updateProps] = createProps(options.props) |
| 20 | + const component = Svelte.mount(Component, { ...options, props }) |
| 21 | + |
| 22 | + /** Remove the component from the DOM. */ |
| 23 | + const unmount = () => { |
| 24 | + Svelte.flushSync(() => Svelte.unmount(component)) |
| 25 | + removeCleanupTask(unmount) |
| 26 | + } |
| 27 | + |
| 28 | + /** Update the component's props. */ |
| 29 | + const rerender = (nextProps) => { |
| 30 | + Svelte.flushSync(() => updateProps(nextProps)) |
| 31 | + } |
| 32 | + |
| 33 | + addCleanupTask(unmount) |
| 34 | + Svelte.flushSync() |
| 35 | + |
| 36 | + return { component, unmount, rerender } |
| 37 | +} |
| 38 | + |
| 39 | +/** Mount a legacy Svelte 3 or 4 component into the DOM. */ |
| 40 | +const mountLegacy = (Component, options) => { |
| 41 | + const component = new Component(options) |
| 42 | + |
| 43 | + /** Remove the component from the DOM. */ |
| 44 | + const unmount = () => { |
| 45 | + component.$destroy() |
| 46 | + removeCleanupTask(unmount) |
| 47 | + } |
| 48 | + |
| 49 | + /** Update the component's props. */ |
| 50 | + const rerender = (nextProps) => { |
| 51 | + component.$set(nextProps) |
| 52 | + } |
| 53 | + |
| 54 | + // This `$$.on_destroy` listener is included for strict backwards compatibility |
| 55 | + // with previous versions of `@testing-library/svelte`. |
| 56 | + // It's unnecessary and will be removed in a future major version. |
| 57 | + component.$$.on_destroy.push(() => { |
| 58 | + removeCleanupTask(unmount) |
| 59 | + }) |
| 60 | + |
| 61 | + addCleanupTask(unmount) |
| 62 | + |
| 63 | + return { component, unmount, rerender } |
| 64 | +} |
| 65 | + |
| 66 | +/** The mount method in use. */ |
| 67 | +const mountComponent = IS_MODERN_SVELTE ? mountModern : mountLegacy |
| 68 | + |
| 69 | +/** |
| 70 | + * Render a Svelte component into the document. |
| 71 | + * |
| 72 | + * @template {import('./types.js').Component} C |
| 73 | + * @param {import('./types.js').ComponentType<C>} Component |
| 74 | + * @param {import('./types.js').MountOptions<C>} options |
| 75 | + * @returns {{ |
| 76 | + * component: C |
| 77 | + * unmount: () => void |
| 78 | + * rerender: (props: Partial<import('./types.js').Props<C>>) => Promise<void> |
| 79 | + * }} |
| 80 | + */ |
| 81 | +const mount = (Component, options = {}) => { |
| 82 | + const { component, unmount, rerender } = mountComponent(Component, options) |
| 83 | + |
| 84 | + return { |
| 85 | + component, |
| 86 | + unmount, |
| 87 | + rerender: async (props) => { |
| 88 | + rerender(props) |
| 89 | + // Await the next tick for Svelte 4, which cannot flush changes synchronously |
| 90 | + await Svelte.tick() |
| 91 | + }, |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +export { ALLOWED_MOUNT_OPTIONS, mount } |
0 commit comments