11import {
2- fireEvent as dtlFireEvent ,
2+ fireEvent as baseFireEvent ,
33 getQueriesForElement ,
44 prettyDOM ,
55} from '@testing-library/dom'
@@ -16,6 +16,52 @@ import {
1616const targetCache = new Set ( )
1717const componentCache = new Set ( )
1818
19+ /**
20+ * Customize how Svelte renders the component.
21+ *
22+ * @template {import('svelte').SvelteComponent} C
23+ * @typedef {import('svelte').ComponentProps<C> | Partial<import('svelte').ComponentConstructorOptions<import('svelte').ComponentProps<C>>> } SvelteComponentOptions
24+ */
25+
26+ /**
27+ * Customize how Testing Library sets up the document and binds queries.
28+ *
29+ * @template {import('@testing-library/dom').Queries } [Q=typeof import('@testing-library/dom').queries]
30+ * @typedef {{
31+ * baseElement?: HTMLElement
32+ * queries?: Q
33+ * }} RenderOptions
34+ */
35+
36+ /**
37+ * The rendered component and bound testing functions.
38+ *
39+ * @template {import('svelte').SvelteComponent} C
40+ * @template {import('@testing-library/dom').Queries } [Q=typeof import('@testing-library/dom').queries]
41+ *
42+ * @typedef {{
43+ * container: HTMLElement
44+ * baseElement: HTMLElement
45+ * component: C
46+ * debug: (el?: HTMLElement | DocumentFragment) => void
47+ * rerender: (props: Partial<import('svelte').ComponentProps<C>>) => Promise<void>
48+ * unmount: () => void
49+ * } & {
50+ * [P in keyof Q]: import('@testing -library/dom').BoundFunction<Q[P]>
51+ * }} RenderResult
52+ */
53+
54+ /**
55+ * Render a component into the document.
56+ *
57+ * @template {import('svelte').SvelteComponent} C
58+ * @template {import('@testing-library/dom').Queries } [Q=typeof import('@testing-library/dom').queries]
59+ *
60+ * @param {import('svelte').ComponentType<C> } Component - The component to render.
61+ * @param {SvelteComponentOptions<C> } options - Customize how Svelte renders the component.
62+ * @param {RenderOptions<Q> } renderOptions - Customize how Testing Library sets up the document and binds queries.
63+ * @returns {RenderResult<C, Q> } The rendered component and bound testing functions.
64+ */
1965const render = ( Component , options = { } , renderOptions = { } ) => {
2066 options = validateOptions ( options )
2167
@@ -62,6 +108,7 @@ const render = (Component, options = {}, renderOptions = {}) => {
62108 }
63109}
64110
111+ /** Remove a component from the component cache. */
65112const cleanupComponent = ( component ) => {
66113 const inCache = componentCache . delete ( component )
67114
@@ -70,6 +117,7 @@ const cleanupComponent = (component) => {
70117 }
71118}
72119
120+ /** Remove a target element from the target cache. */
73121const cleanupTarget = ( target ) => {
74122 const inCache = targetCache . delete ( target )
75123
@@ -78,27 +126,52 @@ const cleanupTarget = (target) => {
78126 }
79127}
80128
129+ /** Unmount all components and remove elements added to `<body>`. */
81130const cleanup = ( ) => {
82131 componentCache . forEach ( cleanupComponent )
83132 targetCache . forEach ( cleanupTarget )
84133}
85134
135+ /**
136+ * Call a function and wait for Svelte to flush pending changes.
137+ *
138+ * @param {() => unknown } [fn] - A function, which may be `async`, to call before flushing updates.
139+ * @returns {Promise<void> }
140+ */
86141const act = async ( fn ) => {
87142 if ( fn ) {
88143 await fn ( )
89144 }
90145 return tick ( )
91146}
92147
148+ /**
149+ * @typedef {(...args: Parameters<import('@testing-library/dom').FireFunction>) => Promise<ReturnType<import('@testing-library/dom').FireFunction>> } FireFunction
150+ */
151+
152+ /**
153+ * @typedef {{
154+ * [K in import('@testing -library/dom').EventType]: (...args: Parameters<import('@testing-library/dom').FireObject[K]>) => Promise<ReturnType<import('@testing-library/dom').FireObject[K]>>
155+ * }} FireObject
156+ */
157+
158+ /**
159+ * Fire an event on an element.
160+ *
161+ * Consider using `@testing-library/user-event` instead, if possible.
162+ * @see https://testing-library.com/docs/user-event/intro/
163+ *
164+ * @type {FireFunction & FireObject }
165+ */
93166const fireEvent = async ( ...args ) => {
94- const event = dtlFireEvent ( ...args )
167+ const event = baseFireEvent ( ...args )
95168 await tick ( )
96169 return event
97170}
98171
99- Object . keys ( dtlFireEvent ) . forEach ( ( key ) => {
172+ Object . keys ( baseFireEvent ) . forEach ( ( key ) => {
100173 fireEvent [ key ] = async ( ...args ) => {
101- const event = dtlFireEvent [ key ] ( ...args )
174+ const event = baseFireEvent [ key ] ( ...args )
102175 await tick ( )
103176 return event
104177 }
0 commit comments