1
1
import {
2
2
fireEvent as dtlFireEvent ,
3
3
getQueriesForElement ,
4
- prettyDOM
4
+ prettyDOM ,
5
5
} from '@testing-library/dom'
6
- import { tick } from 'svelte'
6
+ import { tick , createRoot } from 'svelte'
7
7
8
8
const containerCache = new Set ( )
9
9
const componentCache = new Set ( )
@@ -14,13 +14,13 @@ const svelteComponentOptions = [
14
14
'props' ,
15
15
'hydrate' ,
16
16
'intro' ,
17
- 'context'
17
+ 'context' ,
18
18
]
19
19
20
20
const render = (
21
21
Component ,
22
22
{ target, ...options } = { } ,
23
- { container, queries } = { }
23
+ { container, queries } = { } ,
24
24
) => {
25
25
container = container || document . body
26
26
target = target || container . appendChild ( document . createElement ( 'div' ) )
@@ -29,13 +29,13 @@ const render = (
29
29
30
30
const checkProps = ( options ) => {
31
31
const isProps = ! Object . keys ( options ) . some ( ( option ) =>
32
- svelteComponentOptions . includes ( option )
32
+ svelteComponentOptions . includes ( option ) ,
33
33
)
34
34
35
35
// Check if any props and Svelte options were accidentally mixed.
36
36
if ( ! isProps ) {
37
37
const unrecognizedOptions = Object . keys ( options ) . filter (
38
- ( option ) => ! svelteComponentOptions . includes ( option )
38
+ ( option ) => ! svelteComponentOptions . includes ( option ) ,
39
39
)
40
40
41
41
if ( unrecognizedOptions . length > 0 ) {
@@ -54,42 +54,35 @@ const render = (
54
54
return { props : options }
55
55
}
56
56
57
- let component = new ComponentConstructor ( {
57
+ let component = createRoot ( ComponentConstructor , {
58
58
target,
59
- ...checkProps ( options )
59
+ ...checkProps ( options ) ,
60
+ ondestroy : ( ) => componentCache . delete ( component ) ,
60
61
} )
61
62
62
63
containerCache . add ( { container, target, component } )
63
64
componentCache . add ( component )
64
65
65
- component . $$ . on_destroy . push ( ( ) => {
66
- componentCache . delete ( component )
67
- } )
68
-
69
66
return {
70
67
container,
71
68
component,
72
69
debug : ( el = container ) => console . log ( prettyDOM ( el ) ) ,
73
70
rerender : ( options ) => {
74
71
if ( componentCache . has ( component ) ) component . $destroy ( )
75
72
76
- // eslint-disable-next-line no-new
77
- component = new ComponentConstructor ( {
73
+ component = createRoot ( ComponentConstructor , {
78
74
target,
79
- ...checkProps ( options )
75
+ ...checkProps ( options ) ,
76
+ ondestroy : ( ) => componentCache . delete ( component ) ,
80
77
} )
81
78
82
79
containerCache . add ( { container, target, component } )
83
80
componentCache . add ( component )
84
-
85
- component . $$ . on_destroy . push ( ( ) => {
86
- componentCache . delete ( component )
87
- } )
88
81
} ,
89
82
unmount : ( ) => {
90
83
if ( componentCache . has ( component ) ) component . $destroy ( )
91
84
} ,
92
- ...getQueriesForElement ( container , queries )
85
+ ...getQueriesForElement ( container , queries ) ,
93
86
}
94
87
}
95
88
0 commit comments