@storybook/sveltekit 11.0.0-beta.1 → 11.0.0-beta.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/_browser-chunks/chunk-3HHKZM2E.js +55 -0
- package/dist/_browser-chunks/chunk-VAS652SN.js +88 -0
- package/dist/_node-chunks/{chunk-N7N5BJW4.js → chunk-FG74A7QO.js} +8 -8
- package/dist/chunk-56nWP1O9.d.ts +43 -0
- package/dist/chunk-B5frDvvZ.d.ts +64 -0
- package/dist/index.d.ts +3 -3
- package/dist/index.js +2 -1
- package/dist/mocks/app/forms.d.ts +32 -0
- package/dist/mocks/app/forms.js +74 -14
- package/dist/mocks/app/navigation.d.ts +61 -0
- package/dist/mocks/app/navigation.js +63 -60
- package/dist/mocks/docs-isolation.d.ts +22 -0
- package/dist/mocks/docs-isolation.js +62 -0
- package/dist/node/index.d.ts +1 -1
- package/dist/node/index.js +6 -6
- package/dist/preset.js +11 -10
- package/dist/preview.js +2 -1
- package/dist/vite-plugin.js +7 -7
- package/package.json +20 -8
- package/static/app-state-mock.svelte.d.ts +39 -0
- package/static/app-state-mock.svelte.js +63 -33
- package/template/stories_svelte-kit-skeleton-ts/modules/Forms.svelte +18 -3
- package/template/stories_svelte-kit-skeleton-ts/modules/Hrefs.svelte +8 -0
- package/template/stories_svelte-kit-skeleton-ts/modules/State.svelte +3 -3
- package/template/stories_svelte-kit-skeleton-ts/modules/docs-page-isolated-mocks.stories.js +26 -0
- package/template/stories_svelte-kit-skeleton-ts/modules/docs-page-shared-mocks.stories.js +28 -0
- package/template/stories_svelte-kit-skeleton-ts/modules/docs-page-user.svelte +5 -0
- package/template/stories_svelte-kit-skeleton-ts/modules/forms.stories.js +88 -14
- package/template/stories_svelte-kit-skeleton-ts/modules/hrefs.stories.js +42 -33
- package/template/stories_svelte-kit-skeleton-ts/modules/navigation.stories.js +52 -111
- package/template/stories_svelte-kit-skeleton-ts/modules/state.stories.js +45 -63
- package/dist/_browser-chunks/chunk-EB5JBDJD.js +0 -34
- package/dist/chunk-Curl48fh.d.ts +0 -120
- package/static/MockProvider.svelte +0 -123
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
// src/errors.ts
|
|
2
|
+
import { StorybookError } from "storybook/internal/preview-errors";
|
|
3
|
+
var CATEGORY = "FRAMEWORK_SVELTEKIT", SvelteKitMockParametersRemovedError = class extends StorybookError {
|
|
4
|
+
constructor(data) {
|
|
5
|
+
super({
|
|
6
|
+
name: "SvelteKitMockParametersRemovedError",
|
|
7
|
+
category: CATEGORY,
|
|
8
|
+
code: 1,
|
|
9
|
+
documentation: [
|
|
10
|
+
"https://github.com/storybookjs/storybook/blob/next/MIGRATION.md#sveltekit-sveltekit_experimental-parameters-removed",
|
|
11
|
+
"https://storybook.js.org/docs/get-started/frameworks/sveltekit#mocking-modules"
|
|
12
|
+
],
|
|
13
|
+
message: [
|
|
14
|
+
`\`parameters.sveltekit_experimental\` was removed in Storybook 11, and this story still sets it: ${data.keys.map((key) => `\`${key}\``).join(", ")}.`,
|
|
15
|
+
"",
|
|
16
|
+
"Mock SvelteKit modules in `beforeEach` instead. Import `goto` and the other functions from `$app/navigation`, set `page` from `$app/state`, and use `link` and `setAfterNavigate` from `@storybook/sveltekit/navigation.mock` and `formAction` from `@storybook/sveltekit/forms.mock`. The migration guide maps every key to its replacement, and `npx storybook automigrate sveltekit-mocking-before-each` rewrites them for you."
|
|
17
|
+
].join(`
|
|
18
|
+
`)
|
|
19
|
+
});
|
|
20
|
+
this.data = data;
|
|
21
|
+
}
|
|
22
|
+
}, SvelteKitMocksSharedOnDocsPageError = class extends StorybookError {
|
|
23
|
+
constructor() {
|
|
24
|
+
super({
|
|
25
|
+
name: "SvelteKitMocksSharedOnDocsPageError",
|
|
26
|
+
category: CATEGORY,
|
|
27
|
+
code: 2,
|
|
28
|
+
documentation: "https://storybook.js.org/docs/get-started/frameworks/sveltekit#docs-pages",
|
|
29
|
+
// Short lines: a docs page shows this in a box that doesn't wrap them
|
|
30
|
+
message: [
|
|
31
|
+
"Inline stories can't set up SvelteKit mocks.",
|
|
32
|
+
"",
|
|
33
|
+
"To fix this, render the stories of this file in iframes. Add this",
|
|
34
|
+
"to the meta (`defineMeta()` or the default export):",
|
|
35
|
+
"",
|
|
36
|
+
" parameters: { docs: { story: { inline: false } } }",
|
|
37
|
+
"",
|
|
38
|
+
"Inline stories render in one document, so they share the mocks of",
|
|
39
|
+
"`$app/state`, `$app/navigation` and `$app/forms`. Every story would",
|
|
40
|
+
"show the setup of the story that ran last, so Storybook stops here.",
|
|
41
|
+
"In an iframe, each story has its own mocks.",
|
|
42
|
+
"",
|
|
43
|
+
"A story sets up the mocks when its `beforeEach` sets `page`,",
|
|
44
|
+
"`navigating` or `updated`, calls `setAfterNavigate()`, or",
|
|
45
|
+
"configures a mock function such as `goto` or `formAction`."
|
|
46
|
+
].join(`
|
|
47
|
+
`)
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
export {
|
|
53
|
+
SvelteKitMockParametersRemovedError,
|
|
54
|
+
SvelteKitMocksSharedOnDocsPageError
|
|
55
|
+
};
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import {
|
|
2
|
+
SvelteKitMockParametersRemovedError
|
|
3
|
+
} from "./chunk-3HHKZM2E.js";
|
|
4
|
+
import {
|
|
5
|
+
__export
|
|
6
|
+
} from "./chunk-4BE7D4DS.js";
|
|
7
|
+
|
|
8
|
+
// src/preview.ts
|
|
9
|
+
var preview_exports = {};
|
|
10
|
+
__export(preview_exports, {
|
|
11
|
+
beforeEach: () => beforeEach,
|
|
12
|
+
decorators: () => decorators
|
|
13
|
+
});
|
|
14
|
+
import {
|
|
15
|
+
pageDefaultsChanged,
|
|
16
|
+
resetAppState
|
|
17
|
+
} from "@storybook/sveltekit/internal/mocks/app/state.svelte.js";
|
|
18
|
+
import {
|
|
19
|
+
beginDocsStory,
|
|
20
|
+
endDocsSetup,
|
|
21
|
+
resetMocks
|
|
22
|
+
} from "@storybook/sveltekit/internal/mocks/docs-isolation";
|
|
23
|
+
import { setAfterNavigate } from "@storybook/sveltekit/navigation.mock";
|
|
24
|
+
|
|
25
|
+
// src/mocks/link-clicks.ts
|
|
26
|
+
import { link } from "@storybook/sveltekit/navigation.mock";
|
|
27
|
+
var sameWindowTargets = ["", "_self", "_parent", "_top"], onClick = (event) => {
|
|
28
|
+
if (event.defaultPrevented || event.button !== 0 || event.metaKey || event.ctrlKey || event.shiftKey || event.altKey)
|
|
29
|
+
return;
|
|
30
|
+
let anchor = event.composedPath().find((element) => element instanceof Element && element.localName === "a"), href = anchor?.getAttribute("href");
|
|
31
|
+
if (!anchor || href === null || href === void 0)
|
|
32
|
+
return;
|
|
33
|
+
let target = anchor.getAttribute("target")?.toLowerCase() ?? "";
|
|
34
|
+
!sameWindowTargets.includes(target) || anchor.hasAttribute("download") || (event.preventDefault(), link(href, event));
|
|
35
|
+
};
|
|
36
|
+
function interceptLinkClicks({
|
|
37
|
+
canvasElement,
|
|
38
|
+
viewMode
|
|
39
|
+
}) {
|
|
40
|
+
let onStoryClick = (event) => {
|
|
41
|
+
viewMode === "docs" && (!canvasElement || !event.composedPath().includes(canvasElement)) || onClick(event);
|
|
42
|
+
}, ownerDocument = canvasElement?.ownerDocument ?? globalThis.document;
|
|
43
|
+
return ownerDocument.addEventListener("click", onStoryClick), () => ownerDocument.removeEventListener("click", onStoryClick);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// src/preview.ts
|
|
47
|
+
var keyPaths = (value, depth = 0) => depth >= 2 || typeof value != "object" || value === null || Array.isArray(value) ? [] : Object.entries(value).flatMap(([key, child]) => {
|
|
48
|
+
let childPaths = key === "hrefs" ? [] : keyPaths(child, depth + 1);
|
|
49
|
+
return childPaths.length > 0 ? childPaths.map((path) => `${key}.${path}`) : [key];
|
|
50
|
+
}), renders = /* @__PURE__ */ new WeakMap(), beforeEach = ({
|
|
51
|
+
id,
|
|
52
|
+
parameters,
|
|
53
|
+
canvasElement,
|
|
54
|
+
viewMode,
|
|
55
|
+
abortSignal
|
|
56
|
+
}) => {
|
|
57
|
+
if (parameters?.sveltekit_experimental !== void 0) {
|
|
58
|
+
let keys = keyPaths(parameters.sveltekit_experimental);
|
|
59
|
+
throw new SvelteKitMockParametersRemovedError({
|
|
60
|
+
keys: keys.length > 0 ? keys : ["sveltekit_experimental"]
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
let endDocsStory = viewMode === "docs" ? beginDocsStory(id, canvasElement, pageDefaultsChanged()) : void 0;
|
|
64
|
+
resetMocks(() => {
|
|
65
|
+
resetAppState(), setAfterNavigate({});
|
|
66
|
+
});
|
|
67
|
+
let stopLinkClicks = interceptLinkClicks({ canvasElement, viewMode }), cleanup = () => {
|
|
68
|
+
stopLinkClicks(), endDocsStory?.();
|
|
69
|
+
}, render = { mounted: !1 };
|
|
70
|
+
return canvasElement && renders.set(canvasElement, render), abortSignal?.addEventListener(
|
|
71
|
+
"abort",
|
|
72
|
+
() => {
|
|
73
|
+
render.mounted || cleanup();
|
|
74
|
+
},
|
|
75
|
+
{ once: !0 }
|
|
76
|
+
), cleanup;
|
|
77
|
+
}, decorators = [
|
|
78
|
+
(Story, { canvasElement, viewMode }) => {
|
|
79
|
+
let render = renders.get(canvasElement);
|
|
80
|
+
return render && (render.mounted = !0), viewMode === "docs" && endDocsSetup(canvasElement, pageDefaultsChanged()), Story();
|
|
81
|
+
}
|
|
82
|
+
];
|
|
83
|
+
|
|
84
|
+
export {
|
|
85
|
+
beforeEach,
|
|
86
|
+
decorators,
|
|
87
|
+
preview_exports
|
|
88
|
+
};
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
1
|
+
import CJS_COMPAT_NODE_URL_z6vcx631om8 from 'node:url';
|
|
2
|
+
import CJS_COMPAT_NODE_PATH_z6vcx631om8 from 'node:path';
|
|
3
|
+
import CJS_COMPAT_NODE_MODULE_z6vcx631om8 from "node:module";
|
|
4
4
|
|
|
5
|
-
var __filename =
|
|
6
|
-
var __dirname =
|
|
7
|
-
var require =
|
|
5
|
+
var __filename = CJS_COMPAT_NODE_URL_z6vcx631om8.fileURLToPath(import.meta.url);
|
|
6
|
+
var __dirname = CJS_COMPAT_NODE_PATH_z6vcx631om8.dirname(__filename);
|
|
7
|
+
var require = CJS_COMPAT_NODE_MODULE_z6vcx631om8.createRequire(import.meta.url);
|
|
8
8
|
|
|
9
9
|
// ------------------------------------------------------------
|
|
10
10
|
// end of CJS compatibility banner, injected by Storybook's esbuild configuration
|
|
@@ -17,8 +17,8 @@ function mockSveltekitModules() {
|
|
|
17
17
|
config: () => ({
|
|
18
18
|
resolve: {
|
|
19
19
|
alias: {
|
|
20
|
-
"$app/forms": "@storybook/sveltekit/
|
|
21
|
-
"$app/navigation": "@storybook/sveltekit/
|
|
20
|
+
"$app/forms": "@storybook/sveltekit/forms.mock",
|
|
21
|
+
"$app/navigation": "@storybook/sveltekit/navigation.mock",
|
|
22
22
|
"$app/state": "@storybook/sveltekit/internal/mocks/app/state.svelte.js"
|
|
23
23
|
}
|
|
24
24
|
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { CompatibleString, StorybookConfig } from "storybook/internal/types";
|
|
2
|
+
import { BuilderOptions, StorybookConfigVite } from "@storybook/builder-vite";
|
|
3
|
+
|
|
4
|
+
//#region code/frameworks/sveltekit/.dts-emit/code/frameworks/sveltekit/src/types.d.ts
|
|
5
|
+
type FrameworkName = CompatibleString<'@storybook/sveltekit'>;
|
|
6
|
+
type BuilderName = CompatibleString<'@storybook/builder-vite'>;
|
|
7
|
+
type FrameworkOptions = {
|
|
8
|
+
builder?: BuilderOptions;
|
|
9
|
+
/**
|
|
10
|
+
* Enable or disable automatic documentation generation for component properties, events, and
|
|
11
|
+
* slots. When disabled, Storybook will skip the docgen processing step during build, which can
|
|
12
|
+
* improve build performance.
|
|
13
|
+
*
|
|
14
|
+
* @deprecated Builder docgen is removed in Storybook 12. Use `features.docgenServer`.
|
|
15
|
+
* @default true
|
|
16
|
+
*/
|
|
17
|
+
docgen?: boolean;
|
|
18
|
+
};
|
|
19
|
+
type StorybookConfigFramework = {
|
|
20
|
+
framework: FrameworkName | {
|
|
21
|
+
name: FrameworkName;
|
|
22
|
+
options: FrameworkOptions;
|
|
23
|
+
};
|
|
24
|
+
core?: StorybookConfig['core'] & {
|
|
25
|
+
builder?: BuilderName | {
|
|
26
|
+
name: BuilderName;
|
|
27
|
+
options: BuilderOptions;
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
type StorybookConfig$1 = Omit<StorybookConfig, keyof StorybookConfigVite | keyof StorybookConfigFramework> & StorybookConfigVite & StorybookConfigFramework;
|
|
32
|
+
interface SvelteKitTypes {
|
|
33
|
+
parameters: {
|
|
34
|
+
/**
|
|
35
|
+
* Removed in Storybook 11. Mock the SvelteKit modules in `beforeEach` instead.
|
|
36
|
+
*
|
|
37
|
+
* @deprecated See https://storybook.js.org/docs/get-started/frameworks/sveltekit#mocking-modules
|
|
38
|
+
*/
|
|
39
|
+
sveltekit_experimental?: never;
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
//#endregion
|
|
43
|
+
export { StorybookConfig$1 as n, SvelteKitTypes as r, FrameworkOptions as t };
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
//#region code/frameworks/sveltekit/.dts-emit/code/frameworks/sveltekit/src/mocks/types.d.ts
|
|
2
|
+
type NavigationTarget = {
|
|
3
|
+
params?: Record<string, string> | null;
|
|
4
|
+
route?: {
|
|
5
|
+
id: string | null;
|
|
6
|
+
};
|
|
7
|
+
url: URL;
|
|
8
|
+
};
|
|
9
|
+
/** The argument of an `afterNavigate` callback. */
|
|
10
|
+
type AfterNavigate = {
|
|
11
|
+
type: 'enter' | 'form' | 'link' | 'goto' | 'popstate';
|
|
12
|
+
from: NavigationTarget | null;
|
|
13
|
+
to: NavigationTarget | null;
|
|
14
|
+
shallow: boolean;
|
|
15
|
+
willUnload: false;
|
|
16
|
+
delta?: number;
|
|
17
|
+
complete: Promise<void>;
|
|
18
|
+
};
|
|
19
|
+
/** The result of a form action, which `formAction` resolves to. */
|
|
20
|
+
type ActionResult<Success extends Record<string, unknown> | undefined = Record<string, any>, Failure extends Record<string, unknown> | undefined = Record<string, any>> = {
|
|
21
|
+
type: 'success';
|
|
22
|
+
status: number;
|
|
23
|
+
data?: Success;
|
|
24
|
+
location?: string;
|
|
25
|
+
} | {
|
|
26
|
+
type: 'failure';
|
|
27
|
+
status: number;
|
|
28
|
+
data?: Failure;
|
|
29
|
+
location?: string;
|
|
30
|
+
} | {
|
|
31
|
+
type: 'redirect';
|
|
32
|
+
status: number;
|
|
33
|
+
location: string;
|
|
34
|
+
} | {
|
|
35
|
+
type: 'error';
|
|
36
|
+
status?: number;
|
|
37
|
+
error: any;
|
|
38
|
+
location?: string;
|
|
39
|
+
};
|
|
40
|
+
/** What `formAction` receives: the form submission that SvelteKit would send to the server. */
|
|
41
|
+
type FormSubmission = {
|
|
42
|
+
action: URL;
|
|
43
|
+
formData: FormData;
|
|
44
|
+
formElement: HTMLFormElement;
|
|
45
|
+
submitter: HTMLElement | null;
|
|
46
|
+
};
|
|
47
|
+
type UpdateOptions = {
|
|
48
|
+
reset?: boolean;
|
|
49
|
+
refreshAll?: boolean; /** @deprecated Use `refreshAll` instead. */
|
|
50
|
+
invalidateAll?: boolean;
|
|
51
|
+
};
|
|
52
|
+
type SubmitFunction = (input: FormSubmission & {
|
|
53
|
+
controller: AbortController;
|
|
54
|
+
cancel: () => void;
|
|
55
|
+
}) => void | SubmitCallback | Promise<void | SubmitCallback>;
|
|
56
|
+
type SubmitCallback = (opts: {
|
|
57
|
+
formData: FormData;
|
|
58
|
+
formElement: HTMLFormElement;
|
|
59
|
+
action: URL;
|
|
60
|
+
result: ActionResult;
|
|
61
|
+
update: (options?: UpdateOptions) => Promise<void>;
|
|
62
|
+
}) => void | Promise<void>;
|
|
63
|
+
//#endregion
|
|
64
|
+
export { SubmitFunction as i, AfterNavigate as n, FormSubmission as r, ActionResult as t };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { n as StorybookConfig, r as SvelteKitTypes, t as FrameworkOptions } from "./chunk-56nWP1O9.js";
|
|
2
2
|
import { AddonTypes, InferTypes, PreviewAddonEntry } from "storybook/internal/csf";
|
|
3
3
|
import { NamedOrDefaultProjectAnnotations, NormalizedProjectAnnotations, ProjectAnnotations } from "storybook/internal/types";
|
|
4
4
|
import { SveltePreview, SvelteRenderer, SvelteTypes } from "@storybook/svelte";
|
|
@@ -29,11 +29,11 @@ declare function setProjectAnnotations(projectAnnotations: NamedOrDefaultProject
|
|
|
29
29
|
//#region code/frameworks/sveltekit/.dts-emit/code/frameworks/sveltekit/src/index.d.ts
|
|
30
30
|
/**
|
|
31
31
|
* Define the preview of a SvelteKit project, with the `preview.meta()` and `meta.story()`
|
|
32
|
-
* factories. It includes the
|
|
32
|
+
* factories. It includes the mocks of the SvelteKit modules.
|
|
33
33
|
*/
|
|
34
34
|
declare function definePreview<Addons extends PreviewAddonEntry[] = []>(preview: {
|
|
35
35
|
addons?: Addons;
|
|
36
36
|
} & ProjectAnnotations<SvelteTypes & SvelteKitTypes & InferTypes<Addons>>): SvelteKitPreview<InferTypes<Addons>>;
|
|
37
37
|
interface SvelteKitPreview<T extends AddonTypes> extends SveltePreview<SvelteKitTypes & T> {}
|
|
38
38
|
//#endregion
|
|
39
|
-
export { FrameworkOptions,
|
|
39
|
+
export { FrameworkOptions, StorybookConfig, SvelteKitPreview, SvelteKitTypes, definePreview, setProjectAnnotations };
|
package/dist/index.js
CHANGED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { i as SubmitFunction, r as FormSubmission, t as ActionResult } from "../../chunk-B5frDvvZ.js";
|
|
2
|
+
|
|
3
|
+
//#region code/frameworks/sveltekit/.dts-emit/code/frameworks/sveltekit/src/mocks/app/forms.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Stands in for the server's form action. A form with `use:enhance` calls it on submit, and its
|
|
6
|
+
* result drives the rest of the submit flow. Resolves `{ type: 'success', status: 200 }` by
|
|
7
|
+
* default; set another result in `beforeEach`.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
*
|
|
11
|
+
* ```ts
|
|
12
|
+
* import { formAction } from '@storybook/sveltekit/forms.mock';
|
|
13
|
+
*
|
|
14
|
+
* export const InvalidEmail = {
|
|
15
|
+
* beforeEach() {
|
|
16
|
+
* formAction.mockResolvedValue({ type: 'failure', status: 400, data: { error: 'Invalid email' } });
|
|
17
|
+
* },
|
|
18
|
+
* };
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
declare const formAction: import("storybook/test").Mock<(submission: FormSubmission) => Promise<ActionResult>>;
|
|
22
|
+
/**
|
|
23
|
+
* Applies a form action result to the page, like SvelteKit: a redirect calls `goto`, an error sets
|
|
24
|
+
* `page.error`, and other results set `page.form` and `page.status`.
|
|
25
|
+
*/
|
|
26
|
+
declare const applyAction: import("storybook/test").Mock<(result: ActionResult) => Promise<undefined>>;
|
|
27
|
+
declare const deserialize: import("storybook/test").Mock<(result: string) => ActionResult | undefined>;
|
|
28
|
+
declare const enhance: import("storybook/test").Mock<(formElement: HTMLFormElement, submit?: SubmitFunction) => {
|
|
29
|
+
destroy(): void;
|
|
30
|
+
}>;
|
|
31
|
+
//#endregion
|
|
32
|
+
export { applyAction, deserialize, enhance, formAction };
|
package/dist/mocks/app/forms.js
CHANGED
|
@@ -1,26 +1,86 @@
|
|
|
1
1
|
import "../../_browser-chunks/chunk-4BE7D4DS.js";
|
|
2
2
|
|
|
3
3
|
// src/mocks/app/forms.ts
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
4
|
+
import { page } from "@storybook/sveltekit/internal/mocks/app/state.svelte.js";
|
|
5
|
+
import { goto, refreshAll } from "@storybook/sveltekit/navigation.mock";
|
|
6
|
+
import { noteConfiguration } from "@storybook/sveltekit/internal/mocks/docs-isolation";
|
|
7
|
+
import { fn } from "storybook/test";
|
|
8
|
+
var formAction = fn(async () => ({
|
|
9
|
+
type: "success",
|
|
10
|
+
status: 200
|
|
11
|
+
})).mockName("$app/forms::formAction"), applyAction = fn(async (result) => {
|
|
12
|
+
switch (result.type) {
|
|
13
|
+
case "redirect":
|
|
14
|
+
await goto(result.location);
|
|
15
|
+
return;
|
|
16
|
+
case "error":
|
|
17
|
+
page.error = result.error, page.status = result.status ?? 500;
|
|
18
|
+
return;
|
|
19
|
+
case "success":
|
|
20
|
+
case "failure":
|
|
21
|
+
page.form = result.data, page.status = result.status;
|
|
22
|
+
return;
|
|
23
|
+
default:
|
|
24
|
+
return result;
|
|
25
|
+
}
|
|
26
|
+
}).mockName("$app/forms::applyAction"), deserialize = fn(
|
|
27
|
+
() => {
|
|
28
|
+
}
|
|
29
|
+
).mockName("$app/forms::deserialize"), clone = (element) => HTMLElement.prototype.cloneNode.call(element), enhance = fn((formElement, submit = () => {
|
|
30
|
+
}) => {
|
|
31
|
+
let controllers = /* @__PURE__ */ new Set(), destroyed = !1, fallback = async (signal, result, options = {}) => {
|
|
32
|
+
if (signal.aborted || destroyed)
|
|
33
|
+
return;
|
|
34
|
+
let { reset = !0 } = options, shouldRefreshAll = options.refreshAll ?? options.invalidateAll ?? result.type === "success";
|
|
35
|
+
result.type === "success" && reset && HTMLFormElement.prototype.reset.call(formElement), shouldRefreshAll && result.type !== "redirect" && await refreshAll(), !signal.aborted && !destroyed && await applyAction(result);
|
|
36
|
+
}, handleSubmit = async (event) => {
|
|
37
|
+
let submitter = event.submitter;
|
|
38
|
+
if ((submitter?.hasAttribute("formmethod") ? submitter.formMethod : clone(formElement).method) !== "post")
|
|
39
|
+
return;
|
|
40
|
+
event.preventDefault();
|
|
41
|
+
let action = new URL(
|
|
42
|
+
submitter?.hasAttribute("formaction") ? submitter.formAction : clone(formElement).action
|
|
43
|
+
), formData = new FormData(formElement, submitter), controller = new AbortController();
|
|
44
|
+
controllers.add(controller);
|
|
45
|
+
try {
|
|
46
|
+
await submitWith(controller, { action, formData, formElement, submitter });
|
|
47
|
+
} finally {
|
|
48
|
+
controllers.delete(controller);
|
|
49
|
+
}
|
|
50
|
+
}, submitWith = async (controller, submission) => {
|
|
51
|
+
let { signal } = controller, cancelled = !1, callback = await submit({
|
|
52
|
+
...submission,
|
|
53
|
+
cancel: () => {
|
|
54
|
+
cancelled = !0;
|
|
55
|
+
},
|
|
56
|
+
controller
|
|
57
|
+
}) ?? (({ result: result2 }) => fallback(signal, result2));
|
|
58
|
+
if (cancelled || signal.aborted)
|
|
59
|
+
return;
|
|
60
|
+
let result;
|
|
61
|
+
try {
|
|
62
|
+
result = await formAction(submission);
|
|
63
|
+
} catch (error) {
|
|
64
|
+
result = { type: "error", status: 500, error };
|
|
65
|
+
}
|
|
66
|
+
signal.aborted || await callback({
|
|
67
|
+
action: submission.action,
|
|
68
|
+
formData: submission.formData,
|
|
69
|
+
formElement: submission.formElement,
|
|
70
|
+
result,
|
|
71
|
+
update: (options) => fallback(signal, result, options)
|
|
9
72
|
});
|
|
10
|
-
window.dispatchEvent(event);
|
|
11
73
|
};
|
|
12
|
-
return
|
|
74
|
+
return HTMLFormElement.prototype.addEventListener.call(formElement, "submit", handleSubmit), {
|
|
13
75
|
destroy() {
|
|
14
|
-
|
|
76
|
+
destroyed = !0, controllers.forEach((controller) => controller.abort()), HTMLFormElement.prototype.removeEventListener.call(formElement, "submit", handleSubmit);
|
|
15
77
|
}
|
|
16
78
|
};
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
}
|
|
20
|
-
function deserialize() {
|
|
21
|
-
}
|
|
79
|
+
});
|
|
80
|
+
[formAction, applyAction, deserialize, enhance].forEach(noteConfiguration);
|
|
22
81
|
export {
|
|
23
82
|
applyAction,
|
|
24
83
|
deserialize,
|
|
25
|
-
enhance
|
|
84
|
+
enhance,
|
|
85
|
+
formAction
|
|
26
86
|
};
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { n as AfterNavigate } from "../../chunk-B5frDvvZ.js";
|
|
2
|
+
|
|
3
|
+
//#region code/frameworks/sveltekit/.dts-emit/code/frameworks/sveltekit/src/mocks/app/navigation.d.ts
|
|
4
|
+
type Url = string | URL;
|
|
5
|
+
declare const goto: import("storybook/test").Mock<(url: Url, opts?: Record<string, unknown>) => Promise<void>>;
|
|
6
|
+
declare const invalidate: import("storybook/test").Mock<(resource: Url | ((url: URL) => boolean)) => Promise<void>>;
|
|
7
|
+
declare const invalidateAll: import("storybook/test").Mock<() => Promise<void>>;
|
|
8
|
+
declare const refreshAll: import("storybook/test").Mock<(opts?: {
|
|
9
|
+
includeLoadFunctions?: boolean;
|
|
10
|
+
}) => Promise<void>>;
|
|
11
|
+
declare const preloadData: import("storybook/test").Mock<(href: string) => Promise<{
|
|
12
|
+
type: 'loaded';
|
|
13
|
+
status: number;
|
|
14
|
+
data: Record<string, unknown>;
|
|
15
|
+
}>>;
|
|
16
|
+
declare const preloadCode: import("storybook/test").Mock<(pathname: string) => Promise<void>>;
|
|
17
|
+
declare const pushState: import("storybook/test").Mock<(url: Url, state: Record<string, unknown>) => void>;
|
|
18
|
+
declare const replaceState: import("storybook/test").Mock<(url: Url, state: Record<string, unknown>) => void>;
|
|
19
|
+
declare const disableScrollHandling: import("storybook/test").Mock<() => void>;
|
|
20
|
+
declare const beforeNavigate: import("storybook/test").Mock<(callback: (navigation: unknown) => void) => void>;
|
|
21
|
+
declare const onNavigate: import("storybook/test").Mock<(callback: (navigation: unknown) => unknown) => void>;
|
|
22
|
+
declare const snapshot: import("storybook/test").Mock<(options: {
|
|
23
|
+
capture: () => unknown;
|
|
24
|
+
restore: (value: unknown) => void;
|
|
25
|
+
}) => void>;
|
|
26
|
+
/**
|
|
27
|
+
* Sets the navigation that `afterNavigate` callbacks receive when the component mounts. It merges
|
|
28
|
+
* `navigation` into the default, an `enter` navigation from nowhere, like a fresh page load. Call it
|
|
29
|
+
* in `beforeEach`; the framework resets it before every story.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
*
|
|
33
|
+
* ```ts
|
|
34
|
+
* import { setAfterNavigate } from '@storybook/sveltekit/navigation.mock';
|
|
35
|
+
*
|
|
36
|
+
* export const FromSearch = {
|
|
37
|
+
* beforeEach() {
|
|
38
|
+
* setAfterNavigate({ type: 'link', from: { url: new URL('http://localhost/search?q=shoes') } });
|
|
39
|
+
* },
|
|
40
|
+
* };
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
declare function setAfterNavigate(navigation: Partial<AfterNavigate>): void;
|
|
44
|
+
declare const afterNavigate: import("storybook/test").Mock<(callback: (navigation: AfterNavigate) => void) => void>;
|
|
45
|
+
/**
|
|
46
|
+
* Called when a link in the story is clicked, instead of navigating the preview. SvelteKit's router
|
|
47
|
+
* handles link clicks without `goto`, so they have their own spy. Assert on it in `play`, or give it
|
|
48
|
+
* an implementation in `beforeEach`.
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
*
|
|
52
|
+
* ```ts
|
|
53
|
+
* import { link } from '@storybook/sveltekit/navigation.mock';
|
|
54
|
+
*
|
|
55
|
+
* await userEvent.click(canvas.getByRole('link', { name: 'Settings' }));
|
|
56
|
+
* await expect(link).toHaveBeenCalledWith('/settings', expect.anything());
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
declare const link: import("storybook/test").Mock<(href: string, event: MouseEvent) => void>;
|
|
60
|
+
//#endregion
|
|
61
|
+
export { afterNavigate, beforeNavigate, disableScrollHandling, goto, invalidate, invalidateAll, link, onNavigate, preloadCode, preloadData, pushState, refreshAll, replaceState, setAfterNavigate, snapshot };
|
|
@@ -1,68 +1,70 @@
|
|
|
1
1
|
import "../../_browser-chunks/chunk-4BE7D4DS.js";
|
|
2
2
|
|
|
3
3
|
// src/mocks/app/navigation.ts
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
4
|
+
import { onMount } from "svelte";
|
|
5
|
+
import { noteConfiguration, noteMockSet } from "@storybook/sveltekit/internal/mocks/docs-isolation";
|
|
6
|
+
import { fn } from "storybook/test";
|
|
7
|
+
var goto = fn(
|
|
8
|
+
async () => {
|
|
9
|
+
}
|
|
10
|
+
).mockName("$app/navigation::goto"), invalidate = fn(
|
|
11
|
+
async () => {
|
|
12
|
+
}
|
|
13
|
+
).mockName("$app/navigation::invalidate"), invalidateAll = fn(async () => {
|
|
14
|
+
}).mockName("$app/navigation::invalidateAll"), refreshAll = fn(
|
|
15
|
+
async () => {
|
|
16
|
+
}
|
|
17
|
+
).mockName("$app/navigation::refreshAll"), preloadData = fn(async () => ({ type: "loaded", status: 200, data: {} })).mockName("$app/navigation::preloadData"), preloadCode = fn(async () => {
|
|
18
|
+
}).mockName(
|
|
19
|
+
"$app/navigation::preloadCode"
|
|
20
|
+
), pushState = fn(() => {
|
|
21
|
+
}).mockName(
|
|
22
|
+
"$app/navigation::pushState"
|
|
23
|
+
), replaceState = fn(
|
|
24
|
+
() => {
|
|
25
|
+
}
|
|
26
|
+
).mockName("$app/navigation::replaceState"), disableScrollHandling = fn(() => {
|
|
27
|
+
}).mockName(
|
|
28
|
+
"$app/navigation::disableScrollHandling"
|
|
29
|
+
), beforeNavigate = fn(() => {
|
|
30
|
+
}), onNavigate = fn(() => {
|
|
31
|
+
}), snapshot = fn(() => {
|
|
32
|
+
}), defaultAfterNavigate = () => ({
|
|
33
|
+
type: "enter",
|
|
34
|
+
from: null,
|
|
35
|
+
to: null,
|
|
36
|
+
shallow: !1,
|
|
37
|
+
willUnload: !1,
|
|
38
|
+
complete: Promise.resolve()
|
|
39
|
+
}), afterNavigateNavigation = defaultAfterNavigate();
|
|
40
|
+
function setAfterNavigate(navigation) {
|
|
41
|
+
noteMockSet(), afterNavigateNavigation = { ...defaultAfterNavigate(), ...navigation };
|
|
13
42
|
}
|
|
14
|
-
|
|
15
|
-
let
|
|
43
|
+
var afterNavigate = fn((callback) => {
|
|
44
|
+
let navigation = afterNavigateNavigation;
|
|
16
45
|
onMount(() => {
|
|
17
|
-
|
|
18
|
-
type: "enter",
|
|
19
|
-
shallow: !1,
|
|
20
|
-
from: null,
|
|
21
|
-
to: null,
|
|
22
|
-
willUnload: !1,
|
|
23
|
-
complete: Promise.resolve(),
|
|
24
|
-
...argument
|
|
25
|
-
});
|
|
26
|
-
});
|
|
27
|
-
}
|
|
28
|
-
function onNavigate() {
|
|
29
|
-
}
|
|
30
|
-
function beforeNavigate() {
|
|
31
|
-
}
|
|
32
|
-
function disableScrollHandling() {
|
|
33
|
-
}
|
|
34
|
-
async function invalidate(...args) {
|
|
35
|
-
let event = new CustomEvent("storybook:invalidate", {
|
|
36
|
-
detail: args
|
|
46
|
+
callback(navigation);
|
|
37
47
|
});
|
|
38
|
-
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
window.dispatchEvent(event);
|
|
59
|
-
}
|
|
60
|
-
async function replaceState(...args) {
|
|
61
|
-
let event = new CustomEvent("storybook:replaceState", {
|
|
62
|
-
detail: args
|
|
63
|
-
});
|
|
64
|
-
window.dispatchEvent(event);
|
|
65
|
-
}
|
|
48
|
+
}), link = fn(() => {
|
|
49
|
+
}).mockName(
|
|
50
|
+
"$app/navigation::link"
|
|
51
|
+
);
|
|
52
|
+
[
|
|
53
|
+
goto,
|
|
54
|
+
invalidate,
|
|
55
|
+
invalidateAll,
|
|
56
|
+
refreshAll,
|
|
57
|
+
preloadData,
|
|
58
|
+
preloadCode,
|
|
59
|
+
pushState,
|
|
60
|
+
replaceState,
|
|
61
|
+
disableScrollHandling,
|
|
62
|
+
beforeNavigate,
|
|
63
|
+
onNavigate,
|
|
64
|
+
snapshot,
|
|
65
|
+
afterNavigate,
|
|
66
|
+
link
|
|
67
|
+
].forEach(noteConfiguration);
|
|
66
68
|
export {
|
|
67
69
|
afterNavigate,
|
|
68
70
|
beforeNavigate,
|
|
@@ -70,12 +72,13 @@ export {
|
|
|
70
72
|
goto,
|
|
71
73
|
invalidate,
|
|
72
74
|
invalidateAll,
|
|
75
|
+
link,
|
|
73
76
|
onNavigate,
|
|
74
77
|
preloadCode,
|
|
75
78
|
preloadData,
|
|
76
79
|
pushState,
|
|
77
80
|
refreshAll,
|
|
78
81
|
replaceState,
|
|
79
|
-
|
|
82
|
+
setAfterNavigate,
|
|
80
83
|
snapshot
|
|
81
84
|
};
|