11import {
2- fireEvent as dtlFireEvent ,
2+ fireEvent as baseFireEvent ,
33 getQueriesForElement ,
44 prettyDOM ,
55} from '@testing-library/dom'
66import { tick } from 'svelte'
77
8- import {
9- mount ,
10- UnknownSvelteOptionsError ,
11- unmount ,
12- updateProps ,
13- validateOptions ,
14- } from './core/index.js'
8+ import { mount , unmount , updateProps , validateOptions } from './core/index.js'
159
1610const targetCache = new Set ( )
1711const componentCache = new Set ( )
1812
13+ /**
14+ * Customize how Svelte renders the component.
15+ *
16+ * @template {import('svelte').SvelteComponent} C
17+ * @typedef {import('svelte').ComponentProps<C> | Partial<import('svelte').ComponentConstructorOptions<import('svelte').ComponentProps<C>>> } SvelteComponentOptions
18+ */
19+
20+ /**
21+ * Customize how Testing Library sets up the document and binds queries.
22+ *
23+ * @template {import('@testing-library/dom').Queries } [Q=typeof import('@testing-library/dom').queries]
24+ * @typedef {{
25+ * baseElement?: HTMLElement
26+ * queries?: Q
27+ * }} RenderOptions
28+ */
29+
30+ /**
31+ * The rendered component and bound testing functions.
32+ *
33+ * @template {import('svelte').SvelteComponent} C
34+ * @template {import('@testing-library/dom').Queries } [Q=typeof import('@testing-library/dom').queries]
35+ *
36+ * @typedef {{
37+ * container: HTMLElement
38+ * baseElement: HTMLElement
39+ * component: C
40+ * debug: (el?: HTMLElement | DocumentFragment) => void
41+ * rerender: (props: Partial<import('svelte').ComponentProps<C>>) => Promise<void>
42+ * unmount: () => void
43+ * } & {
44+ * [P in keyof Q]: import('@testing -library/dom').BoundFunction<Q[P]>
45+ * }} RenderResult
46+ */
47+
48+ /**
49+ * Render a component into the document.
50+ *
51+ * @template {import('svelte').SvelteComponent} C
52+ * @template {import('@testing-library/dom').Queries } [Q=typeof import('@testing-library/dom').queries]
53+ *
54+ * @param {import('svelte').ComponentType<C> } Component - The component to render.
55+ * @param {SvelteComponentOptions<C> } options - Customize how Svelte renders the component.
56+ * @param {RenderOptions<Q> } renderOptions - Customize how Testing Library sets up the document and binds queries.
57+ * @returns {RenderResult<C, Q> } The rendered component and bound testing functions.
58+ */
1959const render = ( Component , options = { } , renderOptions = { } ) => {
2060 options = validateOptions ( options )
2161
@@ -62,6 +102,7 @@ const render = (Component, options = {}, renderOptions = {}) => {
62102 }
63103}
64104
105+ /** Remove a component from the component cache. */
65106const cleanupComponent = ( component ) => {
66107 const inCache = componentCache . delete ( component )
67108
@@ -70,6 +111,7 @@ const cleanupComponent = (component) => {
70111 }
71112}
72113
114+ /** Remove a target element from the target cache. */
73115const cleanupTarget = ( target ) => {
74116 const inCache = targetCache . delete ( target )
75117
@@ -78,30 +120,55 @@ const cleanupTarget = (target) => {
78120 }
79121}
80122
123+ /** Unmount all components and remove elements added to `<body>`. */
81124const cleanup = ( ) => {
82125 componentCache . forEach ( cleanupComponent )
83126 targetCache . forEach ( cleanupTarget )
84127}
85128
129+ /**
130+ * Call a function and wait for Svelte to flush pending changes.
131+ *
132+ * @param {() => unknown } [fn] - A function, which may be `async`, to call before flushing updates.
133+ * @returns {Promise<void> }
134+ */
86135const act = async ( fn ) => {
87136 if ( fn ) {
88137 await fn ( )
89138 }
90139 return tick ( )
91140}
92141
142+ /**
143+ * @typedef {(...args: Parameters<import('@testing-library/dom').FireFunction>) => Promise<ReturnType<import('@testing-library/dom').FireFunction>> } FireFunction
144+ */
145+
146+ /**
147+ * @typedef {{
148+ * [K in import('@testing -library/dom').EventType]: (...args: Parameters<import('@testing-library/dom').FireObject[K]>) => Promise<ReturnType<import('@testing-library/dom').FireObject[K]>>
149+ * }} FireObject
150+ */
151+
152+ /**
153+ * Fire an event on an element.
154+ *
155+ * Consider using `@testing-library/user-event` instead, if possible.
156+ * @see https://testing-library.com/docs/user-event/intro/
157+ *
158+ * @type {FireFunction & FireObject }
159+ */
93160const fireEvent = async ( ...args ) => {
94- const event = dtlFireEvent ( ...args )
161+ const event = baseFireEvent ( ...args )
95162 await tick ( )
96163 return event
97164}
98165
99- Object . keys ( dtlFireEvent ) . forEach ( ( key ) => {
166+ Object . keys ( baseFireEvent ) . forEach ( ( key ) => {
100167 fireEvent [ key ] = async ( ...args ) => {
101- const event = dtlFireEvent [ key ] ( ...args )
168+ const event = baseFireEvent [ key ] ( ...args )
102169 await tick ( )
103170 return event
104171 }
105172} )
106173
107- export { act , cleanup , fireEvent , render , UnknownSvelteOptionsError }
174+ export { act , cleanup , fireEvent , render }
0 commit comments