-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(svelte5): incorporate Svelte 5 support into main entry point (#375)
- Loading branch information
Showing
20 changed files
with
311 additions
and
244 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
<!-- svelte-ignore options_deprecated_accessors --> | ||
<svelte:options accessors /> | ||
|
||
<script> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<script> | ||
let { name = 'World' } = $props() | ||
let buttonText = $state('Button') | ||
function handleClick() { | ||
buttonText = 'Button Clicked' | ||
} | ||
</script> | ||
|
||
<h1 data-testid="test">Hello {name}!</h1> | ||
|
||
<button onclick={handleClick}>{buttonText}</button> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,4 +16,4 @@ | |
}) | ||
</script> | ||
<button /> | ||
<button></button> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* Rendering core for svelte-testing-library. | ||
* | ||
* Defines how components are added to and removed from the DOM. | ||
* Will switch to legacy, class-based mounting logic | ||
* if it looks like we're in a Svelte <= 4 environment. | ||
*/ | ||
import * as LegacyCore from './legacy.js' | ||
import * as ModernCore from './modern.svelte.js' | ||
import { | ||
createValidateOptions, | ||
UnknownSvelteOptionsError, | ||
} from './validate-options.js' | ||
|
||
const { mount, unmount, updateProps, allowedOptions } = | ||
ModernCore.IS_MODERN_SVELTE ? ModernCore : LegacyCore | ||
|
||
/** Validate component options. */ | ||
const validateOptions = createValidateOptions(allowedOptions) | ||
|
||
export { | ||
mount, | ||
UnknownSvelteOptionsError, | ||
unmount, | ||
updateProps, | ||
validateOptions, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/** | ||
* Legacy rendering core for svelte-testing-library. | ||
* | ||
* Supports Svelte <= 4. | ||
*/ | ||
|
||
/** Allowed options for the component constructor. */ | ||
const allowedOptions = [ | ||
'target', | ||
'accessors', | ||
'anchor', | ||
'props', | ||
'hydrate', | ||
'intro', | ||
'context', | ||
] | ||
|
||
/** | ||
* Mount the component into the DOM. | ||
* | ||
* The `onDestroy` callback is included for strict backwards compatibility | ||
* with previous versions of this library. It's mostly unnecessary logic. | ||
*/ | ||
const mount = (Component, options, onDestroy) => { | ||
const component = new Component(options) | ||
|
||
if (typeof onDestroy === 'function') { | ||
component.$$.on_destroy.push(() => { | ||
onDestroy(component) | ||
}) | ||
} | ||
|
||
return component | ||
} | ||
|
||
/** Remove the component from the DOM. */ | ||
const unmount = (component) => { | ||
component.$destroy() | ||
} | ||
|
||
/** Update the component's props. */ | ||
const updateProps = (component, nextProps) => { | ||
component.$set(nextProps) | ||
} | ||
|
||
export { allowedOptions, mount, unmount, updateProps } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** | ||
* Modern rendering core for svelte-testing-library. | ||
* | ||
* Supports Svelte >= 5. | ||
*/ | ||
import * as Svelte from 'svelte' | ||
|
||
/** Props signals for each rendered component. */ | ||
const propsByComponent = new Map() | ||
|
||
/** Whether we're using Svelte >= 5. */ | ||
const IS_MODERN_SVELTE = typeof Svelte.mount === 'function' | ||
|
||
/** Allowed options to the `mount` call. */ | ||
const allowedOptions = [ | ||
'target', | ||
'anchor', | ||
'props', | ||
'events', | ||
'context', | ||
'intro', | ||
] | ||
|
||
/** Mount the component into the DOM. */ | ||
const mount = (Component, options) => { | ||
const props = $state(options.props ?? {}) | ||
const component = Svelte.mount(Component, { ...options, props }) | ||
|
||
propsByComponent.set(component, props) | ||
|
||
return component | ||
} | ||
|
||
/** Remove the component from the DOM. */ | ||
const unmount = (component) => { | ||
propsByComponent.delete(component) | ||
Svelte.unmount(component) | ||
} | ||
|
||
/** | ||
* Update the component's props. | ||
* | ||
* Relies on the `$state` signal added in `mount`. | ||
*/ | ||
const updateProps = (component, nextProps) => { | ||
const prevProps = propsByComponent.get(component) | ||
Object.assign(prevProps, nextProps) | ||
} | ||
|
||
export { allowedOptions, IS_MODERN_SVELTE, mount, unmount, updateProps } |
Oops, something went wrong.