@@ -5,16 +5,13 @@ import {
55} from '@testing-library/dom'
66import { tick } from 'svelte'
77
8- import { mount , unmount , updateProps , validateOptions } from './core/index.js'
9-
10- const targetCache = new Set ( )
11- const componentCache = new Set ( )
8+ import { mount , prepare } from './core/index.js'
129
1310/**
1411 * Customize how Svelte renders the component.
1512 *
1613 * @template {import('./core/types.js').Component} C
17- * @typedef {import('./core/types.js').Props<C> | Partial<import('./core/types.js').MountOptions<C> > } SvelteComponentOptions
14+ * @typedef {import('./core/types.js').PropsOrMountOptions<C > } SvelteComponentOptions
1815 */
1916
2017/**
@@ -52,38 +49,28 @@ const componentCache = new Set()
5249 * @template {import('@testing-library/dom').Queries } [Q=typeof import('@testing-library/dom').queries]
5350 *
5451 * @param {import('./core/types.js').ComponentType<C> } Component - The component to render.
55- * @param {SvelteComponentOptions<C> } options - Customize how Svelte renders the component.
52+ * @param {SvelteComponentOptions<C> } propsOrOptions - Customize how Svelte renders the component.
5653 * @param {RenderOptions<Q> } renderOptions - Customize how Testing Library sets up the document and binds queries.
5754 * @returns {RenderResult<C, Q> } The rendered component and bound testing functions.
5855 */
59- const render = ( Component , options = { } , renderOptions = { } ) => {
60- options = validateOptions ( options )
61-
62- const baseElement =
63- renderOptions . baseElement ?? options . target ?? document . body
64-
65- const queries = getQueriesForElement ( baseElement , renderOptions . queries )
66-
67- const target =
68- options . target ?? baseElement . appendChild ( document . createElement ( 'div' ) )
69-
70- targetCache . add ( target )
56+ const render = ( Component , propsOrOptions = { } , renderOptions = { } ) => {
57+ const { baseElement, target, mountOptions } = prepare (
58+ propsOrOptions ,
59+ renderOptions
60+ )
7161
72- const component = mount (
62+ const { component, unmount , rerender } = mount (
7363 Component . default ?? Component ,
74- { ...options , target } ,
75- cleanupComponent
64+ mountOptions
7665 )
7766
78- componentCache . add ( component )
67+ const queries = getQueriesForElement ( baseElement , renderOptions . queries )
7968
8069 return {
8170 baseElement,
8271 component,
8372 container : target ,
84- debug : ( el = baseElement ) => {
85- console . log ( prettyDOM ( el ) )
86- } ,
73+ debug : ( el = baseElement ) => console . log ( prettyDOM ( el ) ) ,
8774 rerender : async ( props ) => {
8875 if ( props . props ) {
8976 console . warn (
@@ -92,40 +79,13 @@ const render = (Component, options = {}, renderOptions = {}) => {
9279 props = props . props
9380 }
9481
95- updateProps ( component , props )
96- await tick ( )
97- } ,
98- unmount : ( ) => {
99- cleanupComponent ( component )
82+ await rerender ( props )
10083 } ,
84+ unmount,
10185 ...queries ,
10286 }
10387}
10488
105- /** Remove a component from the component cache. */
106- const cleanupComponent = ( component ) => {
107- const inCache = componentCache . delete ( component )
108-
109- if ( inCache ) {
110- unmount ( component )
111- }
112- }
113-
114- /** Remove a target element from the target cache. */
115- const cleanupTarget = ( target ) => {
116- const inCache = targetCache . delete ( target )
117-
118- if ( inCache && target . parentNode === document . body ) {
119- document . body . removeChild ( target )
120- }
121- }
122-
123- /** Unmount all components and remove elements added to `<body>`. */
124- const cleanup = ( ) => {
125- componentCache . forEach ( cleanupComponent )
126- targetCache . forEach ( cleanupTarget )
127- }
128-
12989/**
13090 * Call a function and wait for Svelte to flush pending changes.
13191 *
@@ -171,4 +131,4 @@ Object.keys(baseFireEvent).forEach((key) => {
171131 }
172132} )
173133
174- export { act , cleanup , fireEvent , render }
134+ export { act , fireEvent , render }
0 commit comments