@testing-library/svelte 5.1.0 → 5.2.0-next.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +65 -74
- package/package.json +27 -22
- package/src/core/index.js +27 -0
- package/src/core/legacy.js +46 -0
- package/src/core/modern.svelte.js +50 -0
- package/src/core/validate-options.js +39 -0
- package/src/index.js +5 -4
- package/src/pure.js +146 -129
- package/types/core/index.d.ts +8 -0
- package/types/core/index.d.ts.map +1 -0
- package/types/core/legacy.d.ts +19 -0
- package/types/core/legacy.d.ts.map +1 -0
- package/types/core/modern.svelte.d.ts +15 -0
- package/types/core/modern.svelte.d.ts.map +1 -0
- package/types/core/validate-options.d.ts +5 -0
- package/types/core/validate-options.d.ts.map +1 -0
- package/types/index.d.ts +5 -82
- package/types/index.d.ts.map +1 -0
- package/types/pure.d.ts +95 -0
- package/types/pure.d.ts.map +1 -0
- package/types/vite.d.ts +5 -12
- package/types/vite.d.ts.map +1 -0
- package/types/vitest.d.ts +2 -0
- package/types/vitest.d.ts.map +1 -0
- package/src/__tests__/__snapshots__/auto-cleanup-skip.test.js.snap +0 -3
- package/src/__tests__/_vitest-setup.js +0 -1
- package/src/__tests__/act.test.js +0 -33
- package/src/__tests__/auto-cleanup-skip.test.js +0 -23
- package/src/__tests__/auto-cleanup.test.js +0 -31
- package/src/__tests__/cleanup.test.js +0 -35
- package/src/__tests__/context.test.js +0 -14
- package/src/__tests__/debug.test.js +0 -18
- package/src/__tests__/events.test.js +0 -32
- package/src/__tests__/fixtures/Comp.svelte +0 -17
- package/src/__tests__/fixtures/Context.svelte +0 -7
- package/src/__tests__/fixtures/Mounter.svelte +0 -19
- package/src/__tests__/fixtures/Simple.svelte +0 -7
- package/src/__tests__/fixtures/Transitioner.svelte +0 -18
- package/src/__tests__/mount.test.js +0 -33
- package/src/__tests__/multi-base.test.js +0 -42
- package/src/__tests__/render.test.js +0 -85
- package/src/__tests__/rerender.test.js +0 -50
- package/src/__tests__/transition.test.js +0 -31
- package/src/__tests__/utils.js +0 -7
- package/src/svelte5-index.js +0 -23
- package/src/svelte5.js +0 -30
- package/types/types.test-d.ts +0 -65
package/src/pure.js
CHANGED
|
@@ -1,157 +1,174 @@
|
|
|
1
1
|
import {
|
|
2
|
-
fireEvent as
|
|
2
|
+
fireEvent as baseFireEvent,
|
|
3
3
|
getQueriesForElement,
|
|
4
4
|
prettyDOM,
|
|
5
5
|
} from '@testing-library/dom'
|
|
6
|
-
import
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
6
|
+
import { tick } from 'svelte'
|
|
7
|
+
|
|
8
|
+
import { mount, unmount, updateProps, validateOptions } from './core/index.js'
|
|
9
|
+
|
|
10
|
+
const targetCache = new Set()
|
|
11
|
+
const componentCache = new Set()
|
|
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
|
+
*/
|
|
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)
|
|
71
|
+
|
|
72
|
+
const component = mount(
|
|
73
|
+
Component.default ?? Component,
|
|
74
|
+
{ ...options, target },
|
|
75
|
+
cleanupComponent
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
componentCache.add(component)
|
|
79
|
+
|
|
80
|
+
return {
|
|
81
|
+
baseElement,
|
|
82
|
+
component,
|
|
83
|
+
container: target,
|
|
84
|
+
debug: (el = baseElement) => {
|
|
85
|
+
console.log(prettyDOM(el))
|
|
86
|
+
},
|
|
87
|
+
rerender: async (props) => {
|
|
88
|
+
if (props.props) {
|
|
89
|
+
console.warn(
|
|
90
|
+
'rerender({ props: {...} }) deprecated, use rerender({...}) instead'
|
|
91
|
+
)
|
|
92
|
+
props = props.props
|
|
44
93
|
}
|
|
45
94
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
componentOptions = this.checkProps(componentOptions)
|
|
54
|
-
|
|
55
|
-
const baseElement =
|
|
56
|
-
renderOptions.baseElement ?? componentOptions.target ?? document.body
|
|
57
|
-
|
|
58
|
-
const target =
|
|
59
|
-
componentOptions.target ??
|
|
60
|
-
baseElement.appendChild(document.createElement('div'))
|
|
61
|
-
|
|
62
|
-
this.targetCache.add(target)
|
|
63
|
-
|
|
64
|
-
const ComponentConstructor = Component.default || Component
|
|
65
|
-
|
|
66
|
-
const component = this.renderComponent(ComponentConstructor, {
|
|
67
|
-
...componentOptions,
|
|
68
|
-
target,
|
|
69
|
-
})
|
|
70
|
-
|
|
71
|
-
return {
|
|
72
|
-
baseElement,
|
|
73
|
-
component,
|
|
74
|
-
container: target,
|
|
75
|
-
debug: (el = baseElement) => console.log(prettyDOM(el)),
|
|
76
|
-
rerender: async (props) => {
|
|
77
|
-
if (props.props) {
|
|
78
|
-
console.warn(
|
|
79
|
-
'rerender({ props: {...} }) deprecated, use rerender({...}) instead'
|
|
80
|
-
)
|
|
81
|
-
props = props.props
|
|
82
|
-
}
|
|
83
|
-
component.$set(props)
|
|
84
|
-
await Svelte.tick()
|
|
85
|
-
},
|
|
86
|
-
unmount: () => {
|
|
87
|
-
this.cleanupComponent(component)
|
|
88
|
-
},
|
|
89
|
-
...getQueriesForElement(baseElement, renderOptions.queries),
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
renderComponent(ComponentConstructor, componentOptions) {
|
|
94
|
-
if (IS_SVELTE_5) {
|
|
95
|
-
throw new Error('for Svelte 5, use `@testing-library/svelte/svelte5`')
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
const component = new ComponentConstructor(componentOptions)
|
|
99
|
-
|
|
100
|
-
this.componentCache.add(component)
|
|
101
|
-
|
|
102
|
-
// TODO(mcous, 2024-02-11): remove this behavior in the next major version
|
|
103
|
-
component.$$.on_destroy.push(() => {
|
|
104
|
-
this.componentCache.delete(component)
|
|
105
|
-
})
|
|
106
|
-
|
|
107
|
-
return component
|
|
95
|
+
updateProps(component, props)
|
|
96
|
+
await tick()
|
|
97
|
+
},
|
|
98
|
+
unmount: () => {
|
|
99
|
+
cleanupComponent(component)
|
|
100
|
+
},
|
|
101
|
+
...queries,
|
|
108
102
|
}
|
|
103
|
+
}
|
|
109
104
|
|
|
110
|
-
|
|
111
|
-
|
|
105
|
+
/** Remove a component from the component cache. */
|
|
106
|
+
const cleanupComponent = (component) => {
|
|
107
|
+
const inCache = componentCache.delete(component)
|
|
112
108
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
}
|
|
109
|
+
if (inCache) {
|
|
110
|
+
unmount(component)
|
|
116
111
|
}
|
|
112
|
+
}
|
|
117
113
|
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
if (inCache && target.parentNode === document.body) {
|
|
122
|
-
document.body.removeChild(target)
|
|
123
|
-
}
|
|
124
|
-
}
|
|
114
|
+
/** Remove a target element from the target cache. */
|
|
115
|
+
const cleanupTarget = (target) => {
|
|
116
|
+
const inCache = targetCache.delete(target)
|
|
125
117
|
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
this.targetCache.forEach(this.cleanupTarget.bind(this))
|
|
118
|
+
if (inCache && target.parentNode === document.body) {
|
|
119
|
+
document.body.removeChild(target)
|
|
129
120
|
}
|
|
130
121
|
}
|
|
131
122
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
123
|
+
/** Unmount all components and remove elements added to `<body>`. */
|
|
124
|
+
const cleanup = () => {
|
|
125
|
+
componentCache.forEach(cleanupComponent)
|
|
126
|
+
targetCache.forEach(cleanupTarget)
|
|
127
|
+
}
|
|
137
128
|
|
|
138
|
-
|
|
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
|
+
*/
|
|
135
|
+
const act = async (fn) => {
|
|
139
136
|
if (fn) {
|
|
140
137
|
await fn()
|
|
141
138
|
}
|
|
142
|
-
return
|
|
139
|
+
return tick()
|
|
143
140
|
}
|
|
144
141
|
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
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
|
+
*/
|
|
160
|
+
const fireEvent = async (...args) => {
|
|
161
|
+
const event = baseFireEvent(...args)
|
|
162
|
+
await tick()
|
|
148
163
|
return event
|
|
149
164
|
}
|
|
150
165
|
|
|
151
|
-
Object.keys(
|
|
166
|
+
Object.keys(baseFireEvent).forEach((key) => {
|
|
152
167
|
fireEvent[key] = async (...args) => {
|
|
153
|
-
const event =
|
|
154
|
-
await
|
|
168
|
+
const event = baseFireEvent[key](...args)
|
|
169
|
+
await tick()
|
|
155
170
|
return event
|
|
156
171
|
}
|
|
157
172
|
})
|
|
173
|
+
|
|
174
|
+
export { act, cleanup, fireEvent, render }
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export const mount: (Component: any, options: any, onDestroy: any) => any;
|
|
2
|
+
import { UnknownSvelteOptionsError } from './validate-options.js';
|
|
3
|
+
export const unmount: (component: any) => void;
|
|
4
|
+
export const updateProps: (component: any, nextProps: any) => void;
|
|
5
|
+
/** Validate component options. */
|
|
6
|
+
export const validateOptions: (options: any) => any;
|
|
7
|
+
export { UnknownSvelteOptionsError };
|
|
8
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/core/index.js"],"names":[],"mappings":";0CAYO,uBAAuB;;;AAK9B,kCAAkC;AAClC,oDAA6D"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Legacy rendering core for svelte-testing-library.
|
|
3
|
+
*
|
|
4
|
+
* Supports Svelte <= 4.
|
|
5
|
+
*/
|
|
6
|
+
/** Allowed options for the component constructor. */
|
|
7
|
+
export const allowedOptions: string[];
|
|
8
|
+
/**
|
|
9
|
+
* Mount the component into the DOM.
|
|
10
|
+
*
|
|
11
|
+
* The `onDestroy` callback is included for strict backwards compatibility
|
|
12
|
+
* with previous versions of this library. It's mostly unnecessary logic.
|
|
13
|
+
*/
|
|
14
|
+
export function mount(Component: any, options: any, onDestroy: any): any;
|
|
15
|
+
/** Remove the component from the DOM. */
|
|
16
|
+
export function unmount(component: any): void;
|
|
17
|
+
/** Update the component's props. */
|
|
18
|
+
export function updateProps(component: any, nextProps: any): void;
|
|
19
|
+
//# sourceMappingURL=legacy.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"legacy.d.ts","sourceRoot":"","sources":["../../src/core/legacy.js"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,qDAAqD;AACrD,sCAQC;AAED;;;;;GAKG;AACH,yEAUC;AAED,yCAAyC;AACzC,8CAEC;AAED,oCAAoC;AACpC,kEAEC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/** Allowed options to the `mount` call. */
|
|
2
|
+
export const allowedOptions: string[];
|
|
3
|
+
/** Whether we're using Svelte >= 5. */
|
|
4
|
+
export const IS_MODERN_SVELTE: boolean;
|
|
5
|
+
/** Mount the component into the DOM. */
|
|
6
|
+
export function mount(Component: any, options: any): any;
|
|
7
|
+
/** Remove the component from the DOM. */
|
|
8
|
+
export function unmount(component: any): void;
|
|
9
|
+
/**
|
|
10
|
+
* Update the component's props.
|
|
11
|
+
*
|
|
12
|
+
* Relies on the `$state` signal added in `mount`.
|
|
13
|
+
*/
|
|
14
|
+
export function updateProps(component: any, nextProps: any): void;
|
|
15
|
+
//# sourceMappingURL=modern.svelte.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"modern.svelte.d.ts","sourceRoot":"","sources":["../../src/core/modern.svelte.js"],"names":[],"mappings":"AAaA,2CAA2C;AAC3C,sCAOC;AAXD,uCAAuC;AACvC,uCAA2D;AAY3D,wCAAwC;AACxC,yDAOC;AAED,yCAAyC;AACzC,8CAGC;AAED;;;;GAIG;AACH,kEAGC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validate-options.d.ts","sourceRoot":"","sources":["../../src/core/validate-options.js"],"names":[],"mappings":"AAiBA,kFAmBC;AApCD;IACE,sDAaC;CACF"}
|
package/types/index.d.ts
CHANGED
|
@@ -1,82 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
BoundFunction,
|
|
7
|
-
EventType,
|
|
8
|
-
Queries,
|
|
9
|
-
queries,
|
|
10
|
-
} from '@testing-library/dom'
|
|
11
|
-
import {
|
|
12
|
-
ComponentConstructorOptions,
|
|
13
|
-
ComponentProps,
|
|
14
|
-
SvelteComponent,
|
|
15
|
-
} from 'svelte'
|
|
16
|
-
|
|
17
|
-
export * from '@testing-library/dom'
|
|
18
|
-
|
|
19
|
-
type SvelteComponentOptions<C extends SvelteComponent> =
|
|
20
|
-
| ComponentProps<C>
|
|
21
|
-
| Partial<ComponentConstructorOptions<ComponentProps<C>>>
|
|
22
|
-
|
|
23
|
-
type Constructor<T> = new (...args: any[]) => T
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Render a Component into the Document.
|
|
27
|
-
*/
|
|
28
|
-
export type RenderResult<
|
|
29
|
-
C extends SvelteComponent,
|
|
30
|
-
Q extends Queries = typeof queries,
|
|
31
|
-
> = {
|
|
32
|
-
container: HTMLElement
|
|
33
|
-
baseElement: HTMLElement
|
|
34
|
-
component: C
|
|
35
|
-
debug: (el?: HTMLElement | DocumentFragment) => void
|
|
36
|
-
rerender: (props: Partial<ComponentProps<C>>) => Promise<void>
|
|
37
|
-
unmount: () => void
|
|
38
|
-
} & { [P in keyof Q]: BoundFunction<Q[P]> }
|
|
39
|
-
|
|
40
|
-
export interface RenderOptions<Q extends Queries = typeof queries> {
|
|
41
|
-
baseElement?: HTMLElement
|
|
42
|
-
queries?: Q
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export function render<
|
|
46
|
-
C extends SvelteComponent,
|
|
47
|
-
Q extends Queries = typeof queries,
|
|
48
|
-
>(
|
|
49
|
-
component: Constructor<C>,
|
|
50
|
-
componentOptions?: SvelteComponentOptions<C>,
|
|
51
|
-
renderOptions?: RenderOptions<Q>
|
|
52
|
-
): RenderResult<C, Q>
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Unmounts trees that were mounted with render.
|
|
56
|
-
*/
|
|
57
|
-
export function cleanup(): void
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* Fires DOM events on an element provided by @testing-library/dom. Since Svelte needs to flush
|
|
61
|
-
* pending state changes via `tick`, these methods have been override and now return a promise.
|
|
62
|
-
*/
|
|
63
|
-
export type FireFunction = (
|
|
64
|
-
element: Document | Element | Window,
|
|
65
|
-
event: Event
|
|
66
|
-
) => Promise<boolean>
|
|
67
|
-
|
|
68
|
-
export type FireObject = {
|
|
69
|
-
[K in EventType]: (
|
|
70
|
-
element: Document | Element | Window,
|
|
71
|
-
options?: {}
|
|
72
|
-
) => Promise<boolean>
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
export const fireEvent: FireFunction & FireObject
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* Calls a function and notifies Svelte to flush any pending state changes.
|
|
79
|
-
*
|
|
80
|
-
* If the function returns a Promise, that Promise will be resolved first.
|
|
81
|
-
*/
|
|
82
|
-
export function act(fn?: () => unknown): Promise<void>
|
|
1
|
+
export * from "@testing-library/dom";
|
|
2
|
+
export * from "./pure.js";
|
|
3
|
+
export { UnknownSvelteOptionsError } from "./core/index.js";
|
|
4
|
+
export { fireEvent } from "./pure.js";
|
|
5
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.js"],"names":[],"mappings":""}
|
package/types/pure.d.ts
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Customize how Svelte renders the component.
|
|
3
|
+
*/
|
|
4
|
+
export type SvelteComponentOptions<C extends import("svelte").SvelteComponent> = import("svelte").ComponentProps<C> | Partial<import("svelte").ComponentConstructorOptions<import("svelte").ComponentProps<C>>>;
|
|
5
|
+
/**
|
|
6
|
+
* Customize how Testing Library sets up the document and binds queries.
|
|
7
|
+
*/
|
|
8
|
+
export type RenderOptions<Q extends import("@testing-library/dom").Queries = typeof import("@testing-library/dom/types/queries.js")> = {
|
|
9
|
+
baseElement?: HTMLElement;
|
|
10
|
+
queries?: Q;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* The rendered component and bound testing functions.
|
|
14
|
+
*/
|
|
15
|
+
export type RenderResult<C extends import("svelte").SvelteComponent, Q extends import("@testing-library/dom").Queries = typeof import("@testing-library/dom/types/queries.js")> = {
|
|
16
|
+
container: HTMLElement;
|
|
17
|
+
baseElement: HTMLElement;
|
|
18
|
+
component: C;
|
|
19
|
+
debug: (el?: HTMLElement | DocumentFragment) => void;
|
|
20
|
+
rerender: (props: Partial<import("svelte").ComponentProps<C>>) => Promise<void>;
|
|
21
|
+
unmount: () => void;
|
|
22
|
+
} & { [P in keyof Q]: import("@testing-library/dom").BoundFunction<Q[P]>; };
|
|
23
|
+
export type FireFunction = (...args: Parameters<import("@testing-library/dom").FireFunction>) => Promise<ReturnType<import("@testing-library/dom").FireFunction>>;
|
|
24
|
+
export type FireObject = { [K in import("@testing-library/dom").EventType]: (...args: Parameters<import("@testing-library/dom").FireObject[K]>) => Promise<ReturnType<import("@testing-library/dom").FireObject[K]>>; };
|
|
25
|
+
/**
|
|
26
|
+
* Call a function and wait for Svelte to flush pending changes.
|
|
27
|
+
*
|
|
28
|
+
* @param {() => unknown} [fn] - A function, which may be `async`, to call before flushing updates.
|
|
29
|
+
* @returns {Promise<void>}
|
|
30
|
+
*/
|
|
31
|
+
export function act(fn?: (() => unknown) | undefined): Promise<void>;
|
|
32
|
+
/** Unmount all components and remove elements added to `<body>`. */
|
|
33
|
+
export function cleanup(): void;
|
|
34
|
+
/**
|
|
35
|
+
* @typedef {(...args: Parameters<import('@testing-library/dom').FireFunction>) => Promise<ReturnType<import('@testing-library/dom').FireFunction>>} FireFunction
|
|
36
|
+
*/
|
|
37
|
+
/**
|
|
38
|
+
* @typedef {{
|
|
39
|
+
* [K in import('@testing-library/dom').EventType]: (...args: Parameters<import('@testing-library/dom').FireObject[K]>) => Promise<ReturnType<import('@testing-library/dom').FireObject[K]>>
|
|
40
|
+
* }} FireObject
|
|
41
|
+
*/
|
|
42
|
+
/**
|
|
43
|
+
* Fire an event on an element.
|
|
44
|
+
*
|
|
45
|
+
* Consider using `@testing-library/user-event` instead, if possible.
|
|
46
|
+
* @see https://testing-library.com/docs/user-event/intro/
|
|
47
|
+
*
|
|
48
|
+
* @type {FireFunction & FireObject}
|
|
49
|
+
*/
|
|
50
|
+
export const fireEvent: FireFunction & FireObject;
|
|
51
|
+
/**
|
|
52
|
+
* Customize how Svelte renders the component.
|
|
53
|
+
*
|
|
54
|
+
* @template {import('svelte').SvelteComponent} C
|
|
55
|
+
* @typedef {import('svelte').ComponentProps<C> | Partial<import('svelte').ComponentConstructorOptions<import('svelte').ComponentProps<C>>>} SvelteComponentOptions
|
|
56
|
+
*/
|
|
57
|
+
/**
|
|
58
|
+
* Customize how Testing Library sets up the document and binds queries.
|
|
59
|
+
*
|
|
60
|
+
* @template {import('@testing-library/dom').Queries} [Q=typeof import('@testing-library/dom').queries]
|
|
61
|
+
* @typedef {{
|
|
62
|
+
* baseElement?: HTMLElement
|
|
63
|
+
* queries?: Q
|
|
64
|
+
* }} RenderOptions
|
|
65
|
+
*/
|
|
66
|
+
/**
|
|
67
|
+
* The rendered component and bound testing functions.
|
|
68
|
+
*
|
|
69
|
+
* @template {import('svelte').SvelteComponent} C
|
|
70
|
+
* @template {import('@testing-library/dom').Queries} [Q=typeof import('@testing-library/dom').queries]
|
|
71
|
+
*
|
|
72
|
+
* @typedef {{
|
|
73
|
+
* container: HTMLElement
|
|
74
|
+
* baseElement: HTMLElement
|
|
75
|
+
* component: C
|
|
76
|
+
* debug: (el?: HTMLElement | DocumentFragment) => void
|
|
77
|
+
* rerender: (props: Partial<import('svelte').ComponentProps<C>>) => Promise<void>
|
|
78
|
+
* unmount: () => void
|
|
79
|
+
* } & {
|
|
80
|
+
* [P in keyof Q]: import('@testing-library/dom').BoundFunction<Q[P]>
|
|
81
|
+
* }} RenderResult
|
|
82
|
+
*/
|
|
83
|
+
/**
|
|
84
|
+
* Render a component into the document.
|
|
85
|
+
*
|
|
86
|
+
* @template {import('svelte').SvelteComponent} C
|
|
87
|
+
* @template {import('@testing-library/dom').Queries} [Q=typeof import('@testing-library/dom').queries]
|
|
88
|
+
*
|
|
89
|
+
* @param {import('svelte').ComponentType<C>} Component - The component to render.
|
|
90
|
+
* @param {SvelteComponentOptions<C>} options - Customize how Svelte renders the component.
|
|
91
|
+
* @param {RenderOptions<Q>} renderOptions - Customize how Testing Library sets up the document and binds queries.
|
|
92
|
+
* @returns {RenderResult<C, Q>} The rendered component and bound testing functions.
|
|
93
|
+
*/
|
|
94
|
+
export function render<C extends import("svelte").SvelteComponent, Q extends import("@testing-library/dom").Queries = typeof import("@testing-library/dom/types/queries.js")>(Component: import("svelte").ComponentType<C>, options?: SvelteComponentOptions<C>, renderOptions?: RenderOptions<Q>): RenderResult<C, Q>;
|
|
95
|
+
//# sourceMappingURL=pure.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pure.d.ts","sourceRoot":"","sources":["../src/pure.js"],"names":[],"mappings":";;;mCAegD,CAAC,SAApC,OAAQ,QAAQ,EAAE,eAAgB,IAClC,OAAO,QAAQ,EAAE,cAAc,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,OAAO,QAAQ,EAAE,2BAA2B,CAAC,OAAO,QAAQ,EAAE,cAAc,CAAC,CAAC,CAAC,CAAC,CAAC;;;;0BAMpF,CAAC,SAA3C,OAAQ,sBAAsB,EAAE,OAAQ,6DACxC;IACZ,WAAe,CAAC,EAAE,WAAW,CAAA;IAC7B,OAAW,CAAC,EAAE,CAAC,CAAA;CACZ;;;;yBAM4C,CAAC,SAApC,OAAQ,QAAQ,EAAE,eAAgB,EACQ,CAAC,SAA3C,OAAQ,sBAAsB,EAAE,OAAQ,6DAExC;IACZ,SAAa,EAAE,WAAW,CAAA;IAC1B,WAAe,EAAE,WAAW,CAAA;IAC5B,SAAa,EAAE,CAAC,CAAA;IAChB,KAAS,EAAE,CAAC,EAAE,CAAC,EAAE,WAAW,GAAG,gBAAgB,KAAK,IAAI,CAAA;IACxD,QAAY,EAAE,CAAC,KAAK,EAAE,OAAO,CAAC,OAAO,QAAQ,EAAE,cAAc,CAAC,CAAC,CAAC,CAAC,KAAK,OAAO,CAAC,IAAI,CAAC,CAAA;IACnF,OAAW,EAAE,MAAM,IAAI,CAAA;CACpB,GAAG,GACD,CAAY,IAAP,MAAM,CAAC,GAAG,OAAO,sBAAsB,EAAE,aAAa,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GACnE;2BAkGS,CAAC,GAAG,IAAI,EAAE,UAAU,CAAC,OAAO,sBAAsB,EAAE,YAAY,CAAC,KAAK,OAAO,CAAC,UAAU,CAAC,OAAO,sBAAsB,EAAE,YAAY,CAAC,CAAC;yBAItI,GACP,CAA6C,IAAxC,OAAO,sBAAsB,EAAE,SAAS,GAAG,CAAC,GAAG,IAAI,EAAE,UAAU,CAAC,OAAO,sBAAsB,EAAE,UAAU,CAAC,CAAC,CAAC,CAAC,KAAK,OAAO,CAAC,UAAU,CAAC,OAAO,sBAAsB,EAAE,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC,GAC1L;AApBJ;;;;;GAKG;AACH,gCAHiB,OAAO,gBACX,OAAO,CAAC,IAAI,CAAC,CAOzB;AAjBD,oEAAoE;AACpE,gCAGC;AAeD;;GAEG;AAEH;;;;GAIG;AAEH;;;;;;;GAOG;AACH,wBAFU,YAAY,GAAG,UAAU,CAMlC;AAvJD;;;;;GAKG;AAEH;;;;;;;;GAQG;AAEH;;;;;;;;;;;;;;;;GAgBG;AAEH;;;;;;;;;;GAUG;AACH,uBARgD,CAAC,SAApC,OAAQ,QAAQ,EAAE,eAAgB,EACQ,CAAC,SAA3C,OAAQ,sBAAsB,EAAE,OAAQ,sEAE1C,OAAO,QAAQ,EAAE,aAAa,CAAC,CAAC,CAAC,YACjC,sBAAsB,CAAC,CAAC,CAAC,kBACzB,aAAa,CAAC,CAAC,CAAC,GACd,YAAY,CAAC,CAAC,EAAE,CAAC,CAAC,CA8C9B"}
|
package/types/vite.d.ts
CHANGED
|
@@ -1,12 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
* Ensures Svelte is imported correctly in tests
|
|
7
|
-
* and that the DOM is cleaned up after each test.
|
|
8
|
-
*/
|
|
9
|
-
export function svelteTesting(options?: {
|
|
10
|
-
resolveBrowser?: boolean
|
|
11
|
-
autoCleanup?: boolean
|
|
12
|
-
}): Plugin
|
|
1
|
+
export function svelteTesting({ resolveBrowser, autoCleanup, }?: {
|
|
2
|
+
resolveBrowser?: boolean;
|
|
3
|
+
autoCleanup?: boolean;
|
|
4
|
+
}): import("vite").Plugin;
|
|
5
|
+
//# sourceMappingURL=vite.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vite.d.ts","sourceRoot":"","sources":["../src/vite.js"],"names":[],"mappings":"AAYO,iEAHI;IAAC,cAAc,CAAC,EAAE,OAAO,CAAC;IAAC,WAAW,CAAC,EAAE,OAAO,CAAA;CAAC,GAC/C,OAAO,MAAM,EAAE,MAAM,CAoBhC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vitest.d.ts","sourceRoot":"","sources":["../src/vitest.js"],"names":[],"mappings":""}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import '@testing-library/jest-dom/vitest'
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { setTimeout } from 'node:timers/promises'
|
|
2
|
-
|
|
3
|
-
import { act, render } from '@testing-library/svelte'
|
|
4
|
-
import { describe, expect, test } from 'vitest'
|
|
5
|
-
|
|
6
|
-
import Comp from './fixtures/Comp.svelte'
|
|
7
|
-
|
|
8
|
-
describe('act', () => {
|
|
9
|
-
test('state updates are flushed', async () => {
|
|
10
|
-
const { getByText } = render(Comp)
|
|
11
|
-
const button = getByText('Button')
|
|
12
|
-
|
|
13
|
-
expect(button).toHaveTextContent('Button')
|
|
14
|
-
|
|
15
|
-
await act(() => {
|
|
16
|
-
button.click()
|
|
17
|
-
})
|
|
18
|
-
|
|
19
|
-
expect(button).toHaveTextContent('Button Clicked')
|
|
20
|
-
})
|
|
21
|
-
|
|
22
|
-
test('accepts async functions', async () => {
|
|
23
|
-
const { getByText } = render(Comp)
|
|
24
|
-
const button = getByText('Button')
|
|
25
|
-
|
|
26
|
-
await act(async () => {
|
|
27
|
-
await setTimeout(100)
|
|
28
|
-
button.click()
|
|
29
|
-
})
|
|
30
|
-
|
|
31
|
-
expect(button).toHaveTextContent('Button Clicked')
|
|
32
|
-
})
|
|
33
|
-
})
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { beforeAll, describe, expect, test } from 'vitest'
|
|
2
|
-
|
|
3
|
-
import Comp from './fixtures/Comp.svelte'
|
|
4
|
-
|
|
5
|
-
describe('auto-cleanup-skip', () => {
|
|
6
|
-
let render
|
|
7
|
-
|
|
8
|
-
beforeAll(async () => {
|
|
9
|
-
process.env.STL_SKIP_AUTO_CLEANUP = 'true'
|
|
10
|
-
const stl = await import('@testing-library/svelte')
|
|
11
|
-
render = stl.render
|
|
12
|
-
})
|
|
13
|
-
|
|
14
|
-
// This one verifies that if STL_SKIP_AUTO_CLEANUP is set
|
|
15
|
-
// then we DON'T auto-wire up the afterEach for folks
|
|
16
|
-
test('first', () => {
|
|
17
|
-
render(Comp, { props: { name: 'world' } })
|
|
18
|
-
})
|
|
19
|
-
|
|
20
|
-
test('second', () => {
|
|
21
|
-
expect(document.body.innerHTML).toMatchSnapshot()
|
|
22
|
-
})
|
|
23
|
-
})
|