Skip to content

Commit 67bbafa

Browse files
committed
patch from #284 for svelte 5
1 parent c0ff791 commit 67bbafa

File tree

2 files changed

+15
-22
lines changed

2 files changed

+15
-22
lines changed

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
"contributors:generate": "all-contributors generate"
5252
},
5353
"peerDependencies": {
54-
"svelte": "^3 || ^4"
54+
"svelte": "^3 || ^4 || ^5"
5555
},
5656
"dependencies": {
5757
"@testing-library/dom": "^9.3.1"
@@ -77,7 +77,7 @@
7777
"lint-staged": "^13.2.3",
7878
"npm-run-all": "^4.1.5",
7979
"prettier": "^3.0.0",
80-
"svelte": "^4.0.1",
80+
"svelte": "^5",
8181
"vite": "^4.3.9",
8282
"vitest": "^0.33.0"
8383
},

src/pure.js

+13-20
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import {
22
fireEvent as dtlFireEvent,
33
getQueriesForElement,
4-
prettyDOM
4+
prettyDOM,
55
} from '@testing-library/dom'
6-
import { tick } from 'svelte'
6+
import { tick, createRoot } from 'svelte'
77

88
const containerCache = new Set()
99
const componentCache = new Set()
@@ -14,13 +14,13 @@ const svelteComponentOptions = [
1414
'props',
1515
'hydrate',
1616
'intro',
17-
'context'
17+
'context',
1818
]
1919

2020
const render = (
2121
Component,
2222
{ target, ...options } = {},
23-
{ container, queries } = {}
23+
{ container, queries } = {},
2424
) => {
2525
container = container || document.body
2626
target = target || container.appendChild(document.createElement('div'))
@@ -29,13 +29,13 @@ const render = (
2929

3030
const checkProps = (options) => {
3131
const isProps = !Object.keys(options).some((option) =>
32-
svelteComponentOptions.includes(option)
32+
svelteComponentOptions.includes(option),
3333
)
3434

3535
// Check if any props and Svelte options were accidentally mixed.
3636
if (!isProps) {
3737
const unrecognizedOptions = Object.keys(options).filter(
38-
(option) => !svelteComponentOptions.includes(option)
38+
(option) => !svelteComponentOptions.includes(option),
3939
)
4040

4141
if (unrecognizedOptions.length > 0) {
@@ -54,42 +54,35 @@ const render = (
5454
return { props: options }
5555
}
5656

57-
let component = new ComponentConstructor({
57+
let component = createRoot(ComponentConstructor, {
5858
target,
59-
...checkProps(options)
59+
...checkProps(options),
60+
ondestroy: () => componentCache.delete(component),
6061
})
6162

6263
containerCache.add({ container, target, component })
6364
componentCache.add(component)
6465

65-
component.$$.on_destroy.push(() => {
66-
componentCache.delete(component)
67-
})
68-
6966
return {
7067
container,
7168
component,
7269
debug: (el = container) => console.log(prettyDOM(el)),
7370
rerender: (options) => {
7471
if (componentCache.has(component)) component.$destroy()
7572

76-
// eslint-disable-next-line no-new
77-
component = new ComponentConstructor({
73+
component = createRoot(ComponentConstructor, {
7874
target,
79-
...checkProps(options)
75+
...checkProps(options),
76+
ondestroy: () => componentCache.delete(component),
8077
})
8178

8279
containerCache.add({ container, target, component })
8380
componentCache.add(component)
84-
85-
component.$$.on_destroy.push(() => {
86-
componentCache.delete(component)
87-
})
8881
},
8982
unmount: () => {
9083
if (componentCache.has(component)) component.$destroy()
9184
},
92-
...getQueriesForElement(container, queries)
85+
...getQueriesForElement(container, queries),
9386
}
9487
}
9588

0 commit comments

Comments
 (0)