-
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
- Loading branch information
Showing
16 changed files
with
230 additions
and
228 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 |
---|---|---|
|
@@ -13,5 +13,3 @@ | |
<h1 data-testid="test">Hello {name}!</h1> | ||
|
||
<button on:click={handleClick}>{buttonText}</button> | ||
|
||
<style></style> |
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
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,44 @@ | ||
/** | ||
* Legacy rendering core for svelte-testing-library. | ||
* | ||
* Supports Svelte <= 4. See `core.js` for more details. | ||
*/ | ||
|
||
export const LegacyCore = { | ||
/** Allowed options for the component constructor. */ | ||
componentOptions: [ | ||
'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. | ||
*/ | ||
renderComponent: (ComponentConstructor, componentOptions, onDestroy) => { | ||
const component = new ComponentConstructor(componentOptions) | ||
|
||
component.$$.on_destroy.push(() => { | ||
onDestroy(component) | ||
}) | ||
|
||
return component | ||
}, | ||
|
||
/** Update the component's props. */ | ||
updateProps: (component, nextProps) => { | ||
component.$set(nextProps) | ||
}, | ||
|
||
/** Remove the component from the DOM. */ | ||
cleanupComponent: (component) => { | ||
component.$destroy() | ||
}, | ||
} |
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,51 @@ | ||
/** | ||
* 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 Svelte from 'svelte' | ||
|
||
import { LegacyCore } from './core-legacy' | ||
|
||
const IS_MODERN_SVELTE = typeof Svelte.mount === 'function' | ||
|
||
/** Props signals for each rendered component. */ | ||
const propsByComponent = new Map() | ||
|
||
const ModernCore = { | ||
/** Allowed options to the `mount` call. */ | ||
componentOptions: ['target', 'anchor', 'props', 'events', 'context', 'intro'], | ||
|
||
/** Mount the component into the DOM. */ | ||
renderComponent: (ComponentConstructor, componentOptions) => { | ||
const props = $state(componentOptions.props ?? {}) | ||
const component = Svelte.mount(ComponentConstructor, { | ||
...componentOptions, | ||
props, | ||
}) | ||
|
||
propsByComponent.set(component, props) | ||
|
||
return component | ||
}, | ||
|
||
/** | ||
* Update the component's props. | ||
* | ||
* Relies on the `$state` signal added in `renderComponent`. | ||
*/ | ||
updateProps: (component, nextProps) => { | ||
const prevProps = propsByComponent.get(component) | ||
Object.assign(prevProps, nextProps) | ||
}, | ||
|
||
/** Remove the component from the DOM. */ | ||
cleanupComponent: (component) => { | ||
propsByComponent.delete(component) | ||
Svelte.unmount(component) | ||
}, | ||
} | ||
|
||
export const Core = IS_MODERN_SVELTE ? ModernCore : LegacyCore |
Oops, something went wrong.