-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24795 from paoloricciuti/next
SvelteKit: Add experimental page and navigation mocking
- Loading branch information
Showing
29 changed files
with
1,061 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
export function enhance(form: HTMLFormElement) { | ||
const listener = (e: Event) => { | ||
e.preventDefault(); | ||
const event = new CustomEvent('storybook:enhance'); | ||
window.dispatchEvent(event); | ||
}; | ||
form.addEventListener('submit', listener); | ||
return { | ||
destroy() { | ||
form.removeEventListener('submit', listener); | ||
}, | ||
}; | ||
} | ||
|
||
export function applyAction() {} | ||
|
||
export function deserialize() {} |
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,43 @@ | ||
import { getContext, onMount, setContext } from 'svelte'; | ||
|
||
export async function goto(...args: any[]) { | ||
const event = new CustomEvent('storybook:goto', { | ||
detail: args, | ||
}); | ||
window.dispatchEvent(event); | ||
} | ||
|
||
export function setAfterNavigateArgument(afterNavigateArgs: any) { | ||
setContext('after-navigate-args', afterNavigateArgs); | ||
} | ||
|
||
export function afterNavigate(cb: any) { | ||
const argument = getContext('after-navigate-args'); | ||
onMount(() => { | ||
if (cb && cb instanceof Function) { | ||
cb(argument); | ||
} | ||
}); | ||
} | ||
|
||
export function onNavigate() {} | ||
|
||
export function beforeNavigate() {} | ||
|
||
export function disableScrollHandling() {} | ||
|
||
export async function invalidate(...args: any[]) { | ||
const event = new CustomEvent('storybook:invalidate', { | ||
detail: args, | ||
}); | ||
window.dispatchEvent(event); | ||
} | ||
|
||
export async function invalidateAll() { | ||
const event = new CustomEvent('storybook:invalidateAll'); | ||
window.dispatchEvent(event); | ||
} | ||
|
||
export function preloadCode() {} | ||
|
||
export function preloadData() {} |
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,32 @@ | ||
import { getContext, setContext } from 'svelte'; | ||
|
||
function createMockedStore(contextName: string) { | ||
return [ | ||
{ | ||
subscribe(runner: any) { | ||
const page = getContext(contextName); | ||
runner(page); | ||
return () => {}; | ||
}, | ||
}, | ||
(value: unknown) => { | ||
setContext(contextName, value); | ||
}, | ||
] as const; | ||
} | ||
|
||
export const [page, setPage] = createMockedStore('page-ctx'); | ||
export const [navigating, setNavigating] = createMockedStore('navigating-ctx'); | ||
const [updated, setUpdated] = createMockedStore('updated-ctx'); | ||
|
||
(updated as any).check = () => {}; | ||
|
||
export { updated, setUpdated }; | ||
|
||
export function getStores() { | ||
return { | ||
page, | ||
navigating, | ||
updated, | ||
}; | ||
} |
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
17 changes: 17 additions & 0 deletions
17
code/frameworks/sveltekit/src/plugins/mock-sveltekit-stores.ts
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,17 @@ | ||
import { resolve } from 'node:path'; | ||
import { mergeConfig, type Plugin } from 'vite'; | ||
|
||
export function mockSveltekitStores() { | ||
return { | ||
name: 'storybook:sveltekit-mock-stores', | ||
enforce: 'post', | ||
config: (config) => | ||
mergeConfig(config, { | ||
resolve: { | ||
alias: { | ||
$app: resolve(__dirname, '../src/mocks/app/'), | ||
}, | ||
}, | ||
}), | ||
} satisfies Plugin; | ||
} |
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
Oops, something went wrong.