@testing-library/svelte 5.2.8 → 5.2.10
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 +8 -6
- package/package.json +10 -6
- package/src/component-types.d.ts +1 -1
- package/src/core/cleanup.js +32 -0
- package/src/core/index.js +6 -12
- package/src/core/mount.js +95 -0
- package/src/core/props.svelte.js +38 -0
- package/src/core/validate-options.js +7 -5
- package/src/pure.js +15 -55
- package/types/component-types.d.ts +1 -1
- package/types/core/cleanup.d.ts +15 -0
- package/types/core/cleanup.d.ts.map +1 -0
- package/types/core/index.d.ts +3 -6
- package/types/core/index.d.ts.map +1 -1
- package/types/core/mount.d.ts +20 -0
- package/types/core/mount.d.ts.map +1 -0
- package/types/core/props.svelte.d.ts +12 -0
- package/types/core/props.svelte.d.ts.map +1 -0
- package/types/core/validate-options.d.ts +1 -1
- package/types/core/validate-options.d.ts.map +1 -1
- package/types/pure.d.ts +1 -2
- package/types/pure.d.ts.map +1 -1
- package/src/core/legacy.js +0 -46
- package/src/core/modern.svelte.js +0 -51
- package/types/core/legacy.d.ts +0 -19
- package/types/core/legacy.d.ts.map +0 -1
- package/types/core/modern.svelte.d.ts +0 -15
- package/types/core/modern.svelte.d.ts.map +0 -1
package/README.md
CHANGED
|
@@ -12,9 +12,10 @@
|
|
|
12
12
|
|
|
13
13
|
<p>Simple and complete Svelte testing utilities that encourage good testing practices.</p>
|
|
14
14
|
|
|
15
|
-
[**Read The Docs**][stl-docs] | [Edit the docs][stl-docs-repo]
|
|
15
|
+
[**Read The Docs**][stl-docs] | [Edit the docs][stl-docs-repo] | [Examples](./examples)
|
|
16
16
|
|
|
17
17
|
<!-- prettier-ignore-start -->
|
|
18
|
+
|
|
18
19
|
[![Build Status][build-badge]][build]
|
|
19
20
|
[![Code Coverage][coverage-badge]][coverage]
|
|
20
21
|
[![version][version-badge]][package]
|
|
@@ -29,7 +30,9 @@
|
|
|
29
30
|
[![Watch on GitHub][github-watch-badge]][github-watch]
|
|
30
31
|
[![Star on GitHub][github-star-badge]][github-star]
|
|
31
32
|
[![Tweet][twitter-badge]][twitter]
|
|
33
|
+
|
|
32
34
|
<!-- prettier-ignore-end -->
|
|
35
|
+
|
|
33
36
|
</div>
|
|
34
37
|
|
|
35
38
|
<hr />
|
|
@@ -63,9 +66,6 @@
|
|
|
63
66
|
|
|
64
67
|
## Table of Contents
|
|
65
68
|
|
|
66
|
-
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
|
|
67
|
-
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
|
|
68
|
-
|
|
69
69
|
- [The Problem](#the-problem)
|
|
70
70
|
- [This Solution](#this-solution)
|
|
71
71
|
- [Installation](#installation)
|
|
@@ -78,8 +78,6 @@
|
|
|
78
78
|
- [❓ Questions](#-questions)
|
|
79
79
|
- [Contributors](#contributors)
|
|
80
80
|
|
|
81
|
-
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
|
|
82
|
-
|
|
83
81
|
## The Problem
|
|
84
82
|
|
|
85
83
|
You want to write maintainable tests for your [Svelte][svelte] components.
|
|
@@ -217,8 +215,11 @@ instead of filing an issue on GitHub.
|
|
|
217
215
|
Thanks goes to these people ([emoji key][emojis]):
|
|
218
216
|
|
|
219
217
|
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
|
|
218
|
+
|
|
220
219
|
<!-- prettier-ignore-start -->
|
|
220
|
+
|
|
221
221
|
<!-- markdownlint-disable -->
|
|
222
|
+
|
|
222
223
|
<table>
|
|
223
224
|
<tbody>
|
|
224
225
|
<tr>
|
|
@@ -246,6 +247,7 @@ Thanks goes to these people ([emoji key][emojis]):
|
|
|
246
247
|
</table>
|
|
247
248
|
|
|
248
249
|
<!-- markdownlint-restore -->
|
|
250
|
+
|
|
249
251
|
<!-- prettier-ignore-end -->
|
|
250
252
|
|
|
251
253
|
<!-- ALL-CONTRIBUTORS-LIST:END -->
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@testing-library/svelte",
|
|
3
|
-
"version": "5.2.
|
|
3
|
+
"version": "5.2.10",
|
|
4
4
|
"description": "Simple and complete Svelte testing utilities that encourage good testing practices.",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"exports": {
|
|
@@ -52,17 +52,18 @@
|
|
|
52
52
|
"types"
|
|
53
53
|
],
|
|
54
54
|
"scripts": {
|
|
55
|
-
"all": "npm-run-all contributors:generate
|
|
55
|
+
"all": "npm-run-all contributors:generate docs format types build test:vitest:* test:jest test:examples",
|
|
56
56
|
"all:legacy": "npm-run-all types:legacy test:vitest:* test:jest",
|
|
57
|
-
"
|
|
57
|
+
"docs": "remark --output --use remark-toc --use remark-code-import --use unified-prettier README.md examples",
|
|
58
58
|
"lint": "prettier . --check && eslint .",
|
|
59
59
|
"format": "prettier . --write && eslint . --fix",
|
|
60
60
|
"setup": "npm run install:5 && npm run all",
|
|
61
61
|
"test": "vitest run --coverage",
|
|
62
62
|
"test:watch": "vitest",
|
|
63
|
-
"test:vitest:jsdom": "vitest run --coverage --environment jsdom",
|
|
64
|
-
"test:vitest:happy-dom": "vitest run --coverage --environment happy-dom",
|
|
63
|
+
"test:vitest:jsdom": "vitest run tests --coverage --environment jsdom",
|
|
64
|
+
"test:vitest:happy-dom": "vitest run tests --coverage --environment happy-dom",
|
|
65
65
|
"test:jest": "npx --node-options=\"--experimental-vm-modules --no-warnings\" jest --coverage",
|
|
66
|
+
"test:examples": "vitest run examples --coverage",
|
|
66
67
|
"types": "svelte-check",
|
|
67
68
|
"types:legacy": "svelte-check --tsconfig tsconfig.legacy.json",
|
|
68
69
|
"build": "tsc -p tsconfig.build.json && cp src/component-types.d.ts types",
|
|
@@ -98,7 +99,6 @@
|
|
|
98
99
|
"@vitest/coverage-v8": "^3.1.3",
|
|
99
100
|
"@vitest/eslint-plugin": "^1.1.44",
|
|
100
101
|
"all-contributors-cli": "^6.26.1",
|
|
101
|
-
"doctoc": "^2.2.1",
|
|
102
102
|
"eslint": "^9.26.0",
|
|
103
103
|
"eslint-config-prettier": "^10.1.5",
|
|
104
104
|
"eslint-plugin-jest-dom": "^5.5.0",
|
|
@@ -116,12 +116,16 @@
|
|
|
116
116
|
"npm-run-all": "^4.1.5",
|
|
117
117
|
"prettier": "^3.5.3",
|
|
118
118
|
"prettier-plugin-svelte": "^3.3.3",
|
|
119
|
+
"remark-cli": "^12.0.1",
|
|
120
|
+
"remark-code-import": "^1.2.0",
|
|
121
|
+
"remark-toc": "^9.0.0",
|
|
119
122
|
"svelte": "^5.28.2",
|
|
120
123
|
"svelte-check": "^4.1.7",
|
|
121
124
|
"svelte-jester": "^5.0.0",
|
|
122
125
|
"typescript": "^5.8.3",
|
|
123
126
|
"typescript-eslint": "^8.32.0",
|
|
124
127
|
"typescript-svelte-plugin": "^0.3.46",
|
|
128
|
+
"unified-prettier": "^2.0.1",
|
|
125
129
|
"vite": "^6.3.5",
|
|
126
130
|
"vitest": "^3.1.3"
|
|
127
131
|
}
|
package/src/component-types.d.ts
CHANGED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/** @type {Set<() => void>} */
|
|
2
|
+
const cleanupTasks = new Set()
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Register later cleanup task
|
|
6
|
+
*
|
|
7
|
+
* @param {() => void} onCleanup
|
|
8
|
+
*/
|
|
9
|
+
const addCleanupTask = (onCleanup) => {
|
|
10
|
+
cleanupTasks.add(onCleanup)
|
|
11
|
+
return onCleanup
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Remove a cleanup task without running it.
|
|
16
|
+
*
|
|
17
|
+
* @param {() => void} onCleanup
|
|
18
|
+
*/
|
|
19
|
+
const removeCleanupTask = (onCleanup) => {
|
|
20
|
+
cleanupTasks.delete(onCleanup)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** Clean up all components and elements added to the document. */
|
|
24
|
+
const cleanup = () => {
|
|
25
|
+
for (const handleCleanup of cleanupTasks.values()) {
|
|
26
|
+
handleCleanup()
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
cleanupTasks.clear()
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export { addCleanupTask, cleanup, removeCleanupTask }
|
package/src/core/index.js
CHANGED
|
@@ -5,15 +5,9 @@
|
|
|
5
5
|
* Will switch to legacy, class-based mounting logic
|
|
6
6
|
* if it looks like we're in a Svelte <= 4 environment.
|
|
7
7
|
*/
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
/** Validate component options. */
|
|
16
|
-
const validateOptions = createValidateOptions(allowedOptions)
|
|
17
|
-
|
|
18
|
-
export { mount, unmount, updateProps, validateOptions }
|
|
19
|
-
export { UnknownSvelteOptionsError } from './validate-options.js'
|
|
8
|
+
export { addCleanupTask, cleanup } from './cleanup.js'
|
|
9
|
+
export { mount } from './mount.js'
|
|
10
|
+
export {
|
|
11
|
+
UnknownSvelteOptionsError,
|
|
12
|
+
validateOptions,
|
|
13
|
+
} from './validate-options.js'
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Component rendering core, with support for Svelte 3, 4, and 5
|
|
3
|
+
*/
|
|
4
|
+
import * as Svelte from 'svelte'
|
|
5
|
+
|
|
6
|
+
import { addCleanupTask, removeCleanupTask } from './cleanup.js'
|
|
7
|
+
import { createProps } from './props.svelte.js'
|
|
8
|
+
|
|
9
|
+
/** Whether we're using Svelte >= 5. */
|
|
10
|
+
const IS_MODERN_SVELTE = typeof Svelte.mount === 'function'
|
|
11
|
+
|
|
12
|
+
/** Allowed options to the `mount` call or legacy component constructor. */
|
|
13
|
+
const ALLOWED_MOUNT_OPTIONS = IS_MODERN_SVELTE
|
|
14
|
+
? ['target', 'anchor', 'props', 'events', 'context', 'intro']
|
|
15
|
+
: ['target', 'accessors', 'anchor', 'props', 'hydrate', 'intro', 'context']
|
|
16
|
+
|
|
17
|
+
/** Mount a modern Svelte 5 component into the DOM. */
|
|
18
|
+
const mountModern = (Component, options) => {
|
|
19
|
+
const [props, updateProps] = createProps(options.props)
|
|
20
|
+
const component = Svelte.mount(Component, { ...options, props })
|
|
21
|
+
|
|
22
|
+
/** Remove the component from the DOM. */
|
|
23
|
+
const unmount = () => {
|
|
24
|
+
Svelte.flushSync(() => Svelte.unmount(component))
|
|
25
|
+
removeCleanupTask(unmount)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Update the component's props. */
|
|
29
|
+
const rerender = (nextProps) => {
|
|
30
|
+
Svelte.flushSync(() => updateProps(nextProps))
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
addCleanupTask(unmount)
|
|
34
|
+
Svelte.flushSync()
|
|
35
|
+
|
|
36
|
+
return { component, unmount, rerender }
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Mount a legacy Svelte 3 or 4 component into the DOM. */
|
|
40
|
+
const mountLegacy = (Component, options) => {
|
|
41
|
+
const component = new Component(options)
|
|
42
|
+
|
|
43
|
+
/** Remove the component from the DOM. */
|
|
44
|
+
const unmount = () => {
|
|
45
|
+
component.$destroy()
|
|
46
|
+
removeCleanupTask(unmount)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Update the component's props. */
|
|
50
|
+
const rerender = (nextProps) => {
|
|
51
|
+
component.$set(nextProps)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// This `$$.on_destroy` listener is included for strict backwards compatibility
|
|
55
|
+
// with previous versions of `@testing-library/svelte`.
|
|
56
|
+
// It's unnecessary and will be removed in a future major version.
|
|
57
|
+
component.$$.on_destroy.push(() => {
|
|
58
|
+
removeCleanupTask(unmount)
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
addCleanupTask(unmount)
|
|
62
|
+
|
|
63
|
+
return { component, unmount, rerender }
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** The mount method in use. */
|
|
67
|
+
const mountComponent = IS_MODERN_SVELTE ? mountModern : mountLegacy
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Render a Svelte component into the document.
|
|
71
|
+
*
|
|
72
|
+
* @template {import('./types.js').Component} C
|
|
73
|
+
* @param {import('./types.js').ComponentType<C>} Component
|
|
74
|
+
* @param {import('./types.js').MountOptions<C>} options
|
|
75
|
+
* @returns {{
|
|
76
|
+
* component: C
|
|
77
|
+
* unmount: () => void
|
|
78
|
+
* rerender: (props: Partial<import('./types.js').Props<C>>) => Promise<void>
|
|
79
|
+
* }}
|
|
80
|
+
*/
|
|
81
|
+
const mount = (Component, options = {}) => {
|
|
82
|
+
const { component, unmount, rerender } = mountComponent(Component, options)
|
|
83
|
+
|
|
84
|
+
return {
|
|
85
|
+
component,
|
|
86
|
+
unmount,
|
|
87
|
+
rerender: async (props) => {
|
|
88
|
+
rerender(props)
|
|
89
|
+
// Await the next tick for Svelte 4, which cannot flush changes synchronously
|
|
90
|
+
await Svelte.tick()
|
|
91
|
+
},
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export { ALLOWED_MOUNT_OPTIONS, mount }
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Create a shallowly reactive props object.
|
|
3
|
+
*
|
|
4
|
+
* This allows us to update props on `rerender`
|
|
5
|
+
* without turing `props` into a deep set of Proxy objects
|
|
6
|
+
*
|
|
7
|
+
* @template {Record<string, unknown>} Props
|
|
8
|
+
* @param {Props} initialProps
|
|
9
|
+
* @returns {[Props, (nextProps: Partial<Props>) => void]}
|
|
10
|
+
*/
|
|
11
|
+
const createProps = (initialProps) => {
|
|
12
|
+
const targetProps = initialProps ?? {}
|
|
13
|
+
let currentProps = $state.raw(targetProps)
|
|
14
|
+
|
|
15
|
+
const props = new Proxy(targetProps, {
|
|
16
|
+
get(_, key) {
|
|
17
|
+
return currentProps[key]
|
|
18
|
+
},
|
|
19
|
+
set(_, key, value) {
|
|
20
|
+
currentProps[key] = value
|
|
21
|
+
return true
|
|
22
|
+
},
|
|
23
|
+
has(_, key) {
|
|
24
|
+
return Reflect.has(currentProps, key)
|
|
25
|
+
},
|
|
26
|
+
ownKeys() {
|
|
27
|
+
return Reflect.ownKeys(currentProps)
|
|
28
|
+
},
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
const update = (nextProps) => {
|
|
32
|
+
currentProps = { ...currentProps, ...nextProps }
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return [props, update]
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export { createProps }
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { ALLOWED_MOUNT_OPTIONS } from './mount.js'
|
|
2
|
+
|
|
1
3
|
class UnknownSvelteOptionsError extends TypeError {
|
|
2
4
|
constructor(unknownOptions, allowedOptions) {
|
|
3
5
|
super(`Unknown options.
|
|
@@ -15,9 +17,9 @@ class UnknownSvelteOptionsError extends TypeError {
|
|
|
15
17
|
}
|
|
16
18
|
}
|
|
17
19
|
|
|
18
|
-
const
|
|
20
|
+
const validateOptions = (options) => {
|
|
19
21
|
const isProps = !Object.keys(options).some((option) =>
|
|
20
|
-
|
|
22
|
+
ALLOWED_MOUNT_OPTIONS.includes(option)
|
|
21
23
|
)
|
|
22
24
|
|
|
23
25
|
if (isProps) {
|
|
@@ -26,14 +28,14 @@ const createValidateOptions = (allowedOptions) => (options) => {
|
|
|
26
28
|
|
|
27
29
|
// Check if any props and Svelte options were accidentally mixed.
|
|
28
30
|
const unknownOptions = Object.keys(options).filter(
|
|
29
|
-
(option) => !
|
|
31
|
+
(option) => !ALLOWED_MOUNT_OPTIONS.includes(option)
|
|
30
32
|
)
|
|
31
33
|
|
|
32
34
|
if (unknownOptions.length > 0) {
|
|
33
|
-
throw new UnknownSvelteOptionsError(unknownOptions,
|
|
35
|
+
throw new UnknownSvelteOptionsError(unknownOptions, ALLOWED_MOUNT_OPTIONS)
|
|
34
36
|
}
|
|
35
37
|
|
|
36
38
|
return options
|
|
37
39
|
}
|
|
38
40
|
|
|
39
|
-
export {
|
|
41
|
+
export { UnknownSvelteOptionsError, validateOptions }
|
package/src/pure.js
CHANGED
|
@@ -7,10 +7,7 @@ import {
|
|
|
7
7
|
} from '@testing-library/dom'
|
|
8
8
|
import * as Svelte from 'svelte'
|
|
9
9
|
|
|
10
|
-
import {
|
|
11
|
-
|
|
12
|
-
const targetCache = new Set()
|
|
13
|
-
const componentCache = new Set()
|
|
10
|
+
import { addCleanupTask, mount, validateOptions } from './core/index.js'
|
|
14
11
|
|
|
15
12
|
/**
|
|
16
13
|
* Customize how Svelte renders the component.
|
|
@@ -70,16 +67,17 @@ const render = (Component, options = {}, renderOptions = {}) => {
|
|
|
70
67
|
// eslint-disable-next-line unicorn/prefer-dom-node-append
|
|
71
68
|
options.target ?? baseElement.appendChild(document.createElement('div'))
|
|
72
69
|
|
|
73
|
-
|
|
70
|
+
addCleanupTask(() => {
|
|
71
|
+
if (target.parentNode === document.body) {
|
|
72
|
+
target.remove()
|
|
73
|
+
}
|
|
74
|
+
})
|
|
74
75
|
|
|
75
|
-
const component = mount(
|
|
76
|
+
const { component, unmount, rerender } = mount(
|
|
76
77
|
Component.default ?? Component,
|
|
77
|
-
{ ...options, target }
|
|
78
|
-
cleanupComponent
|
|
78
|
+
{ ...options, target }
|
|
79
79
|
)
|
|
80
80
|
|
|
81
|
-
componentCache.add(component)
|
|
82
|
-
|
|
83
81
|
return {
|
|
84
82
|
baseElement,
|
|
85
83
|
component,
|
|
@@ -95,19 +93,13 @@ const render = (Component, options = {}, renderOptions = {}) => {
|
|
|
95
93
|
props = props.props
|
|
96
94
|
}
|
|
97
95
|
|
|
98
|
-
|
|
99
|
-
await Svelte.tick()
|
|
100
|
-
},
|
|
101
|
-
unmount: () => {
|
|
102
|
-
cleanupComponent(component)
|
|
96
|
+
await rerender(props)
|
|
103
97
|
},
|
|
98
|
+
unmount,
|
|
104
99
|
...queries,
|
|
105
100
|
}
|
|
106
101
|
}
|
|
107
102
|
|
|
108
|
-
/** @type {import('@testing-library/dom'.Config | undefined} */
|
|
109
|
-
let originalDTLConfig
|
|
110
|
-
|
|
111
103
|
/**
|
|
112
104
|
* Configure `@testing-library/dom` for usage with Svelte.
|
|
113
105
|
*
|
|
@@ -116,49 +108,16 @@ let originalDTLConfig
|
|
|
116
108
|
* to flush changes to the DOM before proceeding.
|
|
117
109
|
*/
|
|
118
110
|
const setup = () => {
|
|
119
|
-
originalDTLConfig = getDTLConfig()
|
|
111
|
+
const originalDTLConfig = getDTLConfig()
|
|
120
112
|
|
|
121
113
|
configureDTL({
|
|
122
114
|
asyncWrapper: act,
|
|
123
115
|
eventWrapper: Svelte.flushSync ?? ((cb) => cb()),
|
|
124
116
|
})
|
|
125
|
-
}
|
|
126
117
|
|
|
127
|
-
|
|
128
|
-
const cleanupDTL = () => {
|
|
129
|
-
if (originalDTLConfig) {
|
|
118
|
+
addCleanupTask(() => {
|
|
130
119
|
configureDTL(originalDTLConfig)
|
|
131
|
-
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
/** Remove a component from the component cache. */
|
|
136
|
-
const cleanupComponent = (component) => {
|
|
137
|
-
const inCache = componentCache.delete(component)
|
|
138
|
-
|
|
139
|
-
if (inCache) {
|
|
140
|
-
unmount(component)
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
/** Remove a target element from the target cache. */
|
|
145
|
-
const cleanupTarget = (target) => {
|
|
146
|
-
const inCache = targetCache.delete(target)
|
|
147
|
-
|
|
148
|
-
if (inCache && target.parentNode === document.body) {
|
|
149
|
-
target.remove()
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
/** Unmount components, remove elements added to `<body>`, and reset `@testing-library/dom`. */
|
|
154
|
-
const cleanup = () => {
|
|
155
|
-
for (const component of componentCache) {
|
|
156
|
-
cleanupComponent(component)
|
|
157
|
-
}
|
|
158
|
-
for (const target of targetCache) {
|
|
159
|
-
cleanupTarget(target)
|
|
160
|
-
}
|
|
161
|
-
cleanupDTL()
|
|
120
|
+
})
|
|
162
121
|
}
|
|
163
122
|
|
|
164
123
|
/**
|
|
@@ -201,4 +160,5 @@ for (const [key, baseEvent] of Object.entries(baseFireEvent)) {
|
|
|
201
160
|
fireEvent[key] = async (...args) => act(() => baseEvent(...args))
|
|
202
161
|
}
|
|
203
162
|
|
|
204
|
-
export {
|
|
163
|
+
export { cleanup } from './core/index.js'
|
|
164
|
+
export { act, fireEvent, render, setup }
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Register later cleanup task
|
|
3
|
+
*
|
|
4
|
+
* @param {() => void} onCleanup
|
|
5
|
+
*/
|
|
6
|
+
export function addCleanupTask(onCleanup: () => void): () => void;
|
|
7
|
+
/** Clean up all components and elements added to the document. */
|
|
8
|
+
export function cleanup(): void;
|
|
9
|
+
/**
|
|
10
|
+
* Remove a cleanup task without running it.
|
|
11
|
+
*
|
|
12
|
+
* @param {() => void} onCleanup
|
|
13
|
+
*/
|
|
14
|
+
export function removeCleanupTask(onCleanup: () => void): void;
|
|
15
|
+
//# sourceMappingURL=cleanup.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cleanup.d.ts","sourceRoot":"","sources":["../../src/core/cleanup.js"],"names":[],"mappings":"AAGA;;;;GAIG;AACH,0CAFW,MAAM,IAAI,SAAJ,IAAI,CAKpB;AAWD,kEAAkE;AAClE,gCAMC;AAhBD;;;;GAIG;AACH,6CAFW,MAAM,IAAI,QAIpB"}
|
package/types/core/index.d.ts
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
export {
|
|
2
|
-
export
|
|
3
|
-
export
|
|
4
|
-
export const updateProps: (component: any, nextProps: any) => void;
|
|
5
|
-
/** Validate component options. */
|
|
6
|
-
export const validateOptions: (options: any) => any;
|
|
1
|
+
export { mount } from "./mount.js";
|
|
2
|
+
export { addCleanupTask, cleanup } from "./cleanup.js";
|
|
3
|
+
export { UnknownSvelteOptionsError, validateOptions } from "./validate-options.js";
|
|
7
4
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/core/index.js"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/core/index.js"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/** Allowed options to the `mount` call or legacy component constructor. */
|
|
2
|
+
export const ALLOWED_MOUNT_OPTIONS: string[];
|
|
3
|
+
/**
|
|
4
|
+
* Render a Svelte component into the document.
|
|
5
|
+
*
|
|
6
|
+
* @template {import('./types.js').Component} C
|
|
7
|
+
* @param {import('./types.js').ComponentType<C>} Component
|
|
8
|
+
* @param {import('./types.js').MountOptions<C>} options
|
|
9
|
+
* @returns {{
|
|
10
|
+
* component: C
|
|
11
|
+
* unmount: () => void
|
|
12
|
+
* rerender: (props: Partial<import('./types.js').Props<C>>) => Promise<void>
|
|
13
|
+
* }}
|
|
14
|
+
*/
|
|
15
|
+
export function mount<C extends any>(Component: any, options?: any): {
|
|
16
|
+
component: C;
|
|
17
|
+
unmount: () => void;
|
|
18
|
+
rerender: (props: Partial<any>) => Promise<void>;
|
|
19
|
+
};
|
|
20
|
+
//# sourceMappingURL=mount.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mount.d.ts","sourceRoot":"","sources":["../../src/core/mount.js"],"names":[],"mappings":"AAWA,2EAA2E;AAC3E,6CAE6E;AAsD7E;;;;;;;;;;;GAWG;AACH,sBAT8C,CAAC,SAAlC,GAAgC,kCAGhC;IACR,SAAS,EAAE,CAAC,CAAA;IACZ,OAAO,EAAE,MAAM,IAAI,CAAA;IACnB,QAAQ,EAAE,CAAC,KAAK,EAAE,OAAO,CAAC,GAA6B,CAAC,KAAK,OAAO,CAAC,IAAI,CAAC,CAAA;CAC3E,CAcH"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Create a shallowly reactive props object.
|
|
3
|
+
*
|
|
4
|
+
* This allows us to update props on `rerender`
|
|
5
|
+
* without turing `props` into a deep set of Proxy objects
|
|
6
|
+
*
|
|
7
|
+
* @template {Record<string, unknown>} Props
|
|
8
|
+
* @param {Props} initialProps
|
|
9
|
+
* @returns {[Props, (nextProps: Partial<Props>) => void]}
|
|
10
|
+
*/
|
|
11
|
+
export function createProps<Props extends Record<string, unknown>>(initialProps: Props): [Props, (nextProps: Partial<Props>) => void];
|
|
12
|
+
//# sourceMappingURL=props.svelte.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"props.svelte.d.ts","sourceRoot":"","sources":["../../src/core/props.svelte.js"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,4BAJuC,KAAK,SAA9B,MAAM,CAAC,MAAM,EAAE,OAAO,CAAE,gBAC3B,KAAK,GACH,CAAC,KAAK,EAAE,CAAC,SAAS,EAAE,OAAO,CAAC,KAAK,CAAC,KAAK,IAAI,CAAC,CA2BxD"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
export function createValidateOptions(allowedOptions: any): (options: any) => any;
|
|
2
1
|
export class UnknownSvelteOptionsError extends TypeError {
|
|
3
2
|
constructor(unknownOptions: any, allowedOptions: any);
|
|
4
3
|
}
|
|
4
|
+
export function validateOptions(options: any): any;
|
|
5
5
|
//# sourceMappingURL=validate-options.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validate-options.d.ts","sourceRoot":"","sources":["../../src/core/validate-options.js"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"validate-options.d.ts","sourceRoot":"","sources":["../../src/core/validate-options.js"],"names":[],"mappings":"AAEA;IACE,sDAaC;CACF;AAED,mDAmBC"}
|
package/types/pure.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
export { cleanup } from "./core/index.js";
|
|
1
2
|
/**
|
|
2
3
|
* Customize how Svelte renders the component.
|
|
3
4
|
*/
|
|
@@ -30,8 +31,6 @@ export type FireObject = { [K in import("@testing-library/dom").EventType]: (...
|
|
|
30
31
|
* @returns {Promise<T>}
|
|
31
32
|
*/
|
|
32
33
|
export function act<T>(fn?: (() => Promise<T>) | (() => T)): Promise<T>;
|
|
33
|
-
/** Unmount components, remove elements added to `<body>`, and reset `@testing-library/dom`. */
|
|
34
|
-
export function cleanup(): void;
|
|
35
34
|
/**
|
|
36
35
|
* @typedef {(...args: Parameters<import('@testing-library/dom').FireFunction>) => Promise<ReturnType<import('@testing-library/dom').FireFunction>>} FireFunction
|
|
37
36
|
*/
|
package/types/pure.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pure.d.ts","sourceRoot":"","sources":["../src/pure.js"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"pure.d.ts","sourceRoot":"","sources":["../src/pure.js"],"names":[],"mappings":";;;;mCAcwD,CAAC,SAA5C,OAAQ,sBAAsB,EAAE,SAAU,IAC1C,OAAO,sBAAsB,EAAE,KAAK,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,OAAO,sBAAsB,EAAE,YAAY,CAAC,CAAC,CAAC,CAAC;;;;0BAMvD,CAAC,SAA3C,OAAQ,sBAAsB,EAAE,OAAQ,6DACxC;IACR,WAAW,CAAC,EAAE,WAAW,CAAA;IACzB,OAAO,CAAC,EAAE,CAAC,CAAA;CACZ;;;;yBAMoD,CAAC,SAA5C,OAAQ,sBAAsB,EAAE,SAAU,EACA,CAAC,SAA3C,OAAQ,sBAAsB,EAAE,OAAQ,6DAExC;IACR,SAAS,EAAE,WAAW,CAAA;IACtB,WAAW,EAAE,WAAW,CAAA;IACxB,SAAS,EAAE,OAAO,sBAAsB,EAAE,OAAO,CAAC,CAAC,CAAC,CAAA;IACpD,KAAK,EAAE,CAAC,EAAE,CAAC,EAAE,WAAW,GAAG,gBAAgB,KAAK,IAAI,CAAA;IACpD,QAAQ,EAAE,CAAC,KAAK,EAAE,OAAO,CAAC,OAAO,sBAAsB,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,OAAO,CAAC,IAAI,CAAC,CAAA;IACpF,OAAO,EAAE,MAAM,IAAI,CAAA;CACpB,GAAG,GACD,CAAC,IAAI,MAAM,CAAC,GAAG,OAAO,sBAAsB,EAAE,aAAa,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GACnE;2BAgGS,CAAC,GAAG,IAAI,EAAE,UAAU,CAAC,OAAO,sBAAsB,EAAE,YAAY,CAAC,KAAK,OAAO,CAAC,UAAU,CAAC,OAAO,sBAAsB,EAAE,YAAY,CAAC,CAAC;yBAItI,GACP,CAAC,IAAI,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;AAvBJ;;;;;;GAMG;AACH,oBAJa,CAAC,OACH,CAAC,MAAM,OAAO,CAAC,CAAC,CAAC,CAAC,IAAG,MAAM,CAAC,CAAA,GAC1B,OAAO,CAAC,CAAC,CAAC,CAStB;AAED;;GAEG;AAEH;;;;GAIG;AAEH;;;;;;;GAOG;AACH,wBAFU,YAAY,GAAG,UAAU,CAEmC;AAjJtE;;;;;GAKG;AAEH;;;;;;;;GAQG;AAEH;;;;;;;;;;;;;;;;GAgBG;AAEH;;;;;;;;;;GAUG;AACH,uBARwD,CAAC,SAA5C,OAAQ,sBAAsB,EAAE,SAAU,EACA,CAAC,SAA3C,OAAQ,sBAAsB,EAAE,OAAQ,sEAE1C,OAAO,sBAAsB,EAAE,aAAa,CAAC,CAAC,CAAC,YAC/C,sBAAsB,CAAC,CAAC,CAAC,kBACzB,aAAa,CAAC,CAAC,CAAC,GACd,YAAY,CAAC,CAAC,EAAE,CAAC,CAAC,CA6C9B;AAED;;;;;;GAMG;AACH,8BAWC"}
|
package/src/core/legacy.js
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Legacy rendering core for svelte-testing-library.
|
|
3
|
-
*
|
|
4
|
-
* Supports Svelte <= 4.
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
/** Allowed options for the component constructor. */
|
|
8
|
-
const allowedOptions = [
|
|
9
|
-
'target',
|
|
10
|
-
'accessors',
|
|
11
|
-
'anchor',
|
|
12
|
-
'props',
|
|
13
|
-
'hydrate',
|
|
14
|
-
'intro',
|
|
15
|
-
'context',
|
|
16
|
-
]
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Mount the component into the DOM.
|
|
20
|
-
*
|
|
21
|
-
* The `onDestroy` callback is included for strict backwards compatibility
|
|
22
|
-
* with previous versions of this library. It's mostly unnecessary logic.
|
|
23
|
-
*/
|
|
24
|
-
const mount = (Component, options, onDestroy) => {
|
|
25
|
-
const component = new Component(options)
|
|
26
|
-
|
|
27
|
-
if (typeof onDestroy === 'function') {
|
|
28
|
-
component.$$.on_destroy.push(() => {
|
|
29
|
-
onDestroy(component)
|
|
30
|
-
})
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
return component
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/** Remove the component from the DOM. */
|
|
37
|
-
const unmount = (component) => {
|
|
38
|
-
component.$destroy()
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/** Update the component's props. */
|
|
42
|
-
const updateProps = (component, nextProps) => {
|
|
43
|
-
component.$set(nextProps)
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export { allowedOptions, mount, unmount, updateProps }
|
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Modern rendering core for svelte-testing-library.
|
|
3
|
-
*
|
|
4
|
-
* Supports Svelte >= 5.
|
|
5
|
-
*/
|
|
6
|
-
import * as Svelte from 'svelte'
|
|
7
|
-
|
|
8
|
-
/** Props signals for each rendered component. */
|
|
9
|
-
const propsByComponent = new Map()
|
|
10
|
-
|
|
11
|
-
/** Whether we're using Svelte >= 5. */
|
|
12
|
-
const IS_MODERN_SVELTE = typeof Svelte.mount === 'function'
|
|
13
|
-
|
|
14
|
-
/** Allowed options to the `mount` call. */
|
|
15
|
-
const allowedOptions = [
|
|
16
|
-
'target',
|
|
17
|
-
'anchor',
|
|
18
|
-
'props',
|
|
19
|
-
'events',
|
|
20
|
-
'context',
|
|
21
|
-
'intro',
|
|
22
|
-
]
|
|
23
|
-
|
|
24
|
-
/** Mount the component into the DOM. */
|
|
25
|
-
const mount = (Component, options) => {
|
|
26
|
-
const props = $state(options.props ?? {})
|
|
27
|
-
const component = Svelte.mount(Component, { ...options, props })
|
|
28
|
-
|
|
29
|
-
Svelte.flushSync()
|
|
30
|
-
propsByComponent.set(component, props)
|
|
31
|
-
|
|
32
|
-
return component
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/** Remove the component from the DOM. */
|
|
36
|
-
const unmount = (component) => {
|
|
37
|
-
propsByComponent.delete(component)
|
|
38
|
-
Svelte.flushSync(() => Svelte.unmount(component))
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Update the component's props.
|
|
43
|
-
*
|
|
44
|
-
* Relies on the `$state` signal added in `mount`.
|
|
45
|
-
*/
|
|
46
|
-
const updateProps = (component, nextProps) => {
|
|
47
|
-
const prevProps = propsByComponent.get(component)
|
|
48
|
-
Object.assign(prevProps, nextProps)
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
export { allowedOptions, IS_MODERN_SVELTE, mount, unmount, updateProps }
|
package/types/core/legacy.d.ts
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
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
|
|
@@ -1 +0,0 @@
|
|
|
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"}
|
|
@@ -1,15 +0,0 @@
|
|
|
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): Record<string, 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
|
|
@@ -1 +0,0 @@
|
|
|
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,yEAQC;AAED,yCAAyC;AACzC,8CAGC;AAED;;;;GAIG;AACH,kEAGC"}
|