Skip to content

Commit 1df1c7c

Browse files
committed
support for svelte 5
1 parent e10c11a commit 1df1c7c

File tree

2 files changed

+21
-35
lines changed

2 files changed

+21
-35
lines changed

src/pure.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import * as Svelte from 'svelte'
88

99
const IS_SVELTE_5 = /^5\./.test(SVELTE_VERSION)
1010

11-
class SvelteTestingLibrary {
11+
export class SvelteTestingLibrary {
1212
svelteComponentOptions = [
1313
'accessors',
1414
'anchor',
@@ -78,7 +78,7 @@ class SvelteTestingLibrary {
7878
await Svelte.tick()
7979
},
8080
unmount: () => {
81-
cleanupComponent(component)
81+
this.cleanupComponent(component)
8282
},
8383
...getQueriesForElement(container, queries),
8484
}
@@ -122,19 +122,15 @@ class SvelteTestingLibrary {
122122
}
123123

124124
cleanup() {
125-
this.componentCache.forEach(cleanupComponent)
126-
this.targetCache.forEach(cleanupTarget)
125+
this.componentCache.forEach(this.cleanupComponent.bind(this))
126+
this.targetCache.forEach(this.cleanupTarget.bind(this))
127127
}
128128
}
129129

130130
const instance = new SvelteTestingLibrary()
131131

132132
export const render = instance.render.bind(instance)
133133

134-
export const cleanupComponent = instance.cleanupComponent.bind(instance)
135-
136-
const cleanupTarget = instance.cleanupTarget.bind(instance)
137-
138134
export const cleanup = instance.cleanup.bind(instance)
139135

140136
export const act = async (fn) => {

src/svelte5.js

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,31 @@
11
import { createClassComponent } from 'svelte/legacy'
2-
import {
3-
componentCache,
4-
cleanup,
5-
buildCheckProps,
6-
buildRender,
7-
} from './pure.js'
2+
import { SvelteTestingLibrary } from './pure.js'
83

9-
const svelteComponentOptions = [
10-
'target',
11-
'props',
12-
'events',
13-
'context',
14-
'intro',
15-
'recover',
16-
]
4+
class Svelte5TestingLibrary extends SvelteTestingLibrary {
5+
svelteComponentOptions = [
6+
'target',
7+
'props',
8+
'events',
9+
'context',
10+
'intro',
11+
'recover',
12+
]
1713

18-
const checkProps = buildCheckProps(svelteComponentOptions)
19-
20-
const buildRenderComponent =
21-
({ target, ComponentConstructor }) =>
22-
(options) => {
23-
options = { target, ...checkProps(options) }
14+
renderComponent({ target, ComponentConstructor }, options) {
15+
options = { target, ...this.checkProps(options) }
2416

2517
const component = createClassComponent({
2618
component: ComponentConstructor,
2719
...options,
2820
})
2921

30-
componentCache.add(component)
22+
this.componentCache.add(component)
3123

3224
return component
3325
}
26+
}
3427

35-
const render = buildRender(buildRenderComponent)
36-
37-
/* eslint-disable import/export */
38-
39-
import { act, fireEvent } from './pure.js'
28+
const instance = new Svelte5TestingLibrary()
4029

41-
export { render, cleanup, fireEvent, act }
30+
export const render = instance.render.bind(instance)
31+
export const cleanup = instance.cleanup.bind(instance)

0 commit comments

Comments
 (0)