1
1
import {
2
- fireEvent as dtlFireEvent ,
2
+ fireEvent as baseFireEvent ,
3
3
getQueriesForElement ,
4
4
prettyDOM ,
5
5
} from '@testing-library/dom'
6
6
import { tick } from 'svelte'
7
7
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'
15
9
16
10
const targetCache = new Set ( )
17
11
const componentCache = new Set ( )
18
12
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
+ */
19
59
const render = ( Component , options = { } , renderOptions = { } ) => {
20
60
options = validateOptions ( options )
21
61
@@ -62,6 +102,7 @@ const render = (Component, options = {}, renderOptions = {}) => {
62
102
}
63
103
}
64
104
105
+ /** Remove a component from the component cache. */
65
106
const cleanupComponent = ( component ) => {
66
107
const inCache = componentCache . delete ( component )
67
108
@@ -70,6 +111,7 @@ const cleanupComponent = (component) => {
70
111
}
71
112
}
72
113
114
+ /** Remove a target element from the target cache. */
73
115
const cleanupTarget = ( target ) => {
74
116
const inCache = targetCache . delete ( target )
75
117
@@ -78,30 +120,55 @@ const cleanupTarget = (target) => {
78
120
}
79
121
}
80
122
123
+ /** Unmount all components and remove elements added to `<body>`. */
81
124
const cleanup = ( ) => {
82
125
componentCache . forEach ( cleanupComponent )
83
126
targetCache . forEach ( cleanupTarget )
84
127
}
85
128
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
+ */
86
135
const act = async ( fn ) => {
87
136
if ( fn ) {
88
137
await fn ( )
89
138
}
90
139
return tick ( )
91
140
}
92
141
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
+ */
93
160
const fireEvent = async ( ...args ) => {
94
- const event = dtlFireEvent ( ...args )
161
+ const event = baseFireEvent ( ...args )
95
162
await tick ( )
96
163
return event
97
164
}
98
165
99
- Object . keys ( dtlFireEvent ) . forEach ( ( key ) => {
166
+ Object . keys ( baseFireEvent ) . forEach ( ( key ) => {
100
167
fireEvent [ key ] = async ( ...args ) => {
101
- const event = dtlFireEvent [ key ] ( ...args )
168
+ const event = baseFireEvent [ key ] ( ...args )
102
169
await tick ( )
103
170
return event
104
171
}
105
172
} )
106
173
107
- export { act , cleanup , fireEvent , render , UnknownSvelteOptionsError }
174
+ export { act , cleanup , fireEvent , render }
0 commit comments