Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: patch from #284 for svelte 5 #285

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"contributors:generate": "all-contributors generate"
},
"peerDependencies": {
"svelte": "^3 || ^4"
"svelte": "^3 || ^4 || ^5"
},
"dependencies": {
"@testing-library/dom": "^9.3.1"
Expand All @@ -77,7 +77,7 @@
"lint-staged": "^13.2.3",
"npm-run-all": "^4.1.5",
"prettier": "^3.0.0",
"svelte": "^4.0.1",
"svelte": "^5",
"vite": "^4.3.9",
"vitest": "^0.33.0"
},
Expand Down
33 changes: 13 additions & 20 deletions src/pure.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {
fireEvent as dtlFireEvent,
getQueriesForElement,
prettyDOM
prettyDOM,
} from '@testing-library/dom'
import { tick } from 'svelte'
import { tick, createRoot } from 'svelte'

const containerCache = new Set()
const componentCache = new Set()
Expand All @@ -14,13 +14,13 @@ const svelteComponentOptions = [
'props',
'hydrate',
'intro',
'context'
'context',
]

const render = (
Component,
{ target, ...options } = {},
{ container, queries } = {}
{ container, queries } = {},
) => {
container = container || document.body
target = target || container.appendChild(document.createElement('div'))
Expand All @@ -29,13 +29,13 @@ const render = (

const checkProps = (options) => {
const isProps = !Object.keys(options).some((option) =>
svelteComponentOptions.includes(option)
svelteComponentOptions.includes(option),
)

// Check if any props and Svelte options were accidentally mixed.
if (!isProps) {
const unrecognizedOptions = Object.keys(options).filter(
(option) => !svelteComponentOptions.includes(option)
(option) => !svelteComponentOptions.includes(option),
)

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

let component = new ComponentConstructor({
let component = createRoot(ComponentConstructor, {
target,
...checkProps(options)
...checkProps(options),
ondestroy: () => componentCache.delete(component),
})

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

component.$$.on_destroy.push(() => {
componentCache.delete(component)
})

return {
container,
component,
debug: (el = container) => console.log(prettyDOM(el)),
rerender: (options) => {
if (componentCache.has(component)) component.$destroy()

// eslint-disable-next-line no-new
component = new ComponentConstructor({
component = createRoot(ComponentConstructor, {
target,
...checkProps(options)
...checkProps(options),
ondestroy: () => componentCache.delete(component),
})

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

component.$$.on_destroy.push(() => {
componentCache.delete(component)
})
},
unmount: () => {
if (componentCache.has(component)) component.$destroy()
},
...getQueriesForElement(container, queries)
...getQueriesForElement(container, queries),
}
}

Expand Down
Loading