-
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
SvelteKit: Add support for mocking $app/state
#31369
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
Merged
JReinhold
merged 33 commits into
storybookjs:next
from
xeho91:feat/support-mocking-sveltekit-app-state
Sep 24, 2025
Merged
Changes from 5 commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
9f4dc00
chore(frameworks/sveltekit): Add `@sveltejs/kit` as peer/dev
xeho91 c1e6a35
add `state.svelte.ts` with mocks for SvelteKit `$app/state`
xeho91 f6aba2b
add testing stories file for SveltKit `$app/state`
xeho91 8b52a44
Merge branch 'next' into feat/support-mocking-sveltekit-app-state
xeho91 f30d13c
wip
xeho91 7860caa
chore - remove kit from peers
xeho91 ebde853
thanks greptile
xeho91 515b043
update documentation
xeho91 cb2457b
remove `@sveltejs/kit` due to build issues & copy types
xeho91 acd137a
refactor sveltekit mocks to use package references
JReinhold 11f812e
change example.com to location.origin
JReinhold 277dcbc
fix mjs reference
JReinhold 82f287e
fix parameters in stories
xeho91 7af29a2
cleanup unused
xeho91 99f5109
Apply suggestions from code review
xeho91 58dadc8
Apply suggestions from code review
xeho91 5f9bc5f
Improve stories example
xeho91 2a9c6f5
simplify - create setters function & defaults
xeho91 2c95bb4
cleanup - remove type assertion for setters/getters (they're automati…
xeho91 f909bf7
fix invalid mocking of `updated` from `$app/state`
xeho91 1062424
fix `navigating` values not being set to defaults when moving between…
xeho91 b3cf465
fix `setStateUpdated` logic
xeho91 19ebc0c
move `$app/state` set fns to `beforeEach`
xeho91 b800518
improve fn naming
xeho91 a6ac560
Merge branch 'next' into feat/support-mocking-sveltekit-app-state
xeho91 038fb8e
oops
xeho91 25c6d70
Merge branch 'next' of github.com:storybookjs/storybook into feat/sup…
JReinhold 9d316ee
Merge branch 'next' of github.com:storybookjs/storybook into feat/sup…
JReinhold 210036e
fix store mock imports
JReinhold e043fa8
cleanup
JReinhold 91017dc
make url mock static in stories
JReinhold 5ef0ff8
update updated docs
JReinhold e873a2f
optimizeDeps app state mock to ensure vitest doesn't reload
JReinhold File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,60 @@ | ||
| /** | ||
| * Inspired by the the code: | ||
| * {@link https://github.com/sveltejs/kit/blob/main/packages/kit/src/runtime/client/state.svelte.js} | ||
| * | ||
| * The differences: | ||
| * | ||
| * - Legacy Svelte support is not included | ||
| * - Not using classes (internal coding style preference) | ||
| * | ||
| * @module | ||
| */ | ||
|
|
||
| /* eslint-disable prefer-const */ | ||
| import type { Navigation, Page } from '@sveltejs/kit'; | ||
|
|
||
| let pageData = $state.raw<Page['data']>({}); | ||
| let pageForm = $state.raw<Page['form']>(null); | ||
| let pageError = $state.raw<Page['error']>(null); | ||
| let pageParams = $state.raw<Page['params']>({}); | ||
| let pageRoute = $state.raw<Page['route']>({ id: null }); | ||
| let pageState = $state.raw<Page['state']>({}); | ||
| let pageStatus = $state.raw<Page['status']>(-1); | ||
| let pageUrl = $state.raw<Page['url']>(new URL('https://example.com')); | ||
|
|
||
| export let page = { | ||
| data: pageData, | ||
| form: pageForm, | ||
| error: pageError, | ||
| params: pageParams, | ||
| route: pageRoute, | ||
| state: pageState, | ||
| status: pageStatus, | ||
| url: pageUrl, | ||
| } satisfies Page; | ||
|
|
||
| let navigatingFrom = $state.raw<Navigation['from'] | null>(null); | ||
| let navigatingTo = $state.raw<Navigation['from'] | null>(null); | ||
| let navigatingType = $state.raw<Navigation['type'] | null>(null); | ||
| let navigatingWillUnload = $state.raw<Navigation['willUnload'] | null>(null); | ||
| let navigatingDelta = $state.raw<Navigation['delta'] | null>(null); | ||
| let navigatingComplete = $state.raw<Navigation['complete'] | null>(null); | ||
|
|
||
| export let navigating = { | ||
| from: navigatingFrom, | ||
| to: navigatingTo, | ||
| type: navigatingType, | ||
| willUnload: navigatingWillUnload, | ||
| delta: navigatingDelta, | ||
| complete: navigatingComplete, | ||
| }; | ||
|
|
||
| let updatedCurrent = $state.raw(false); | ||
|
|
||
| export let updated = { | ||
| current: updatedCurrent, | ||
| check: () => | ||
| new Promise<boolean>((resolve) => { | ||
| resolve(updatedCurrent); | ||
| }), | ||
xeho91 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }; | ||
This file contains hidden or 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 hidden or 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 hidden or 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
18 changes: 18 additions & 0 deletions
18
code/frameworks/sveltekit/template/stories_svelte-kit-skeleton-ts/modules/State.svelte
This file contains hidden or 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,18 @@ | ||
| <script> | ||
| import { onMount} from "svelte"; | ||
xeho91 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| import { page, navigating, updated } from '$app/state'; | ||
| updated.check(); | ||
| </script> | ||
|
|
||
| <h1>$app/state</h1> | ||
|
|
||
| <h2>page</h2> | ||
| <pre>{JSON.stringify(page, null, 2)}</pre> | ||
|
|
||
| <h2>navigating</h2> | ||
| <pre>{JSON.stringify(navigating, null, 2)}</pre> | ||
|
|
||
| <h2>updated</h2> | ||
| <pre>{JSON.stringify(updated, null, 2)}</pre> | ||
103 changes: 103 additions & 0 deletions
103
code/frameworks/sveltekit/template/stories_svelte-kit-skeleton-ts/modules/state.stories.js
This file contains hidden or 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,103 @@ | ||
| import State from './State.svelte'; | ||
|
|
||
| export default { | ||
| title: 'stories/frameworks/sveltekit/modules/state', | ||
| component: State, | ||
| }; | ||
|
|
||
| export const AllUndefined = {}; | ||
|
|
||
| export const Page = { | ||
| parameters: { | ||
| sveltekit_experimental: { | ||
| state: { | ||
| page: { | ||
| data: { | ||
| test: 'passed', | ||
| }, | ||
| }, | ||
| }, | ||
JReinhold marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| export const Navigating = { | ||
| parameters: { | ||
| sveltekit_experimental: { | ||
| navigating: { | ||
| route: { | ||
| id: '/storybook', | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| export const Updated = { | ||
| parameters: { | ||
| sveltekit_experimental: { | ||
| updated: true, | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| export const PageAndNavigating = { | ||
| parameters: { | ||
| sveltekit_experimental: { | ||
| page: { | ||
| data: { | ||
| test: 'passed', | ||
| }, | ||
| }, | ||
| navigating: { | ||
| route: { | ||
| id: '/storybook', | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| export const PageAndUpdated = { | ||
| parameters: { | ||
| sveltekit_experimental: { | ||
| page: { | ||
| data: { | ||
| test: 'passed', | ||
| }, | ||
| }, | ||
| updated: true, | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| export const NavigatingAndUpdated = { | ||
| parameters: { | ||
| sveltekit_experimental: { | ||
| navigating: { | ||
| route: { | ||
| id: '/storybook', | ||
| }, | ||
| }, | ||
| updated: true, | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| export const AllThree = { | ||
| parameters: { | ||
| sveltekit_experimental: { | ||
| page: { | ||
| data: { | ||
| test: 'passed', | ||
| }, | ||
| }, | ||
| navigating: { | ||
| route: { | ||
| id: '/storybook', | ||
| }, | ||
| }, | ||
| updated: true, | ||
| }, | ||
| }, | ||
| }; | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.