-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathsvelte5.svelte.js
49 lines (38 loc) · 1.13 KB
/
svelte5.svelte.js
1
2
3
4
5
6
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
44
45
46
47
48
49
/** eslint-global: $state */
import { mount, unmount } from 'svelte'
import { SvelteTestingLibrary } from './pure.js'
class Svelte5TestingLibrary extends SvelteTestingLibrary {
svelteComponentOptions = [
'target',
'props',
'events',
'context',
'intro',
'recover',
]
propsByComponent = new Map()
renderComponent(ComponentConstructor, componentOptions) {
const props = $state(componentOptions.props ?? {})
const component = mount(ComponentConstructor, {
...componentOptions,
props,
})
this.componentCache.add(component)
this.propsByComponent.set(component, props)
return component
}
rerenderComponent(component, nextProps) {
const prevProps = this.propsByComponent.get(component)
Object.assign(prevProps, nextProps)
}
cleanupComponent(component) {
const inCache = this.componentCache.delete(component)
this.propsByComponent.delete(component)
if (inCache) {
unmount(component)
}
}
}
const instance = new Svelte5TestingLibrary()
export const render = instance.render.bind(instance)
export const cleanup = instance.cleanup.bind(instance)