@storybook/sveltekit 7.6.0-alpha.6 → 7.6.0-alpha.7
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/README.md +77 -3
- package/dist/{index-64fd81fa.d.ts → index-f3de942f.d.ts} +45 -5
- package/dist/index.d.ts +2 -2
- package/dist/preset.d.ts +4 -3
- package/dist/preset.js +1 -1
- package/dist/preview.d.ts +5 -0
- package/dist/preview.js +1 -0
- package/dist/preview.mjs +6 -0
- package/package.json +12 -7
- package/src/mocks/app/forms.ts +17 -0
- package/src/mocks/app/navigation.ts +43 -0
- package/src/mocks/app/stores.ts +32 -0
- package/template/stories_svelte-kit-skeleton-js/forms.stories.js +26 -0
- package/template/stories_svelte-kit-skeleton-js/hrefs.stories.js +53 -0
- package/template/stories_svelte-kit-skeleton-js/navigation.stories.js +82 -0
- package/template/stories_svelte-kit-skeleton-js/stores.stories.js +116 -0
- package/template/stories_svelte-kit-skeleton-ts/forms.stories.js +26 -0
- package/template/stories_svelte-kit-skeleton-ts/hrefs.stories.js +53 -0
- package/template/stories_svelte-kit-skeleton-ts/navigation.stories.js +82 -0
- package/template/stories_svelte-kit-skeleton-ts/stores.stories.js +116 -0
package/README.md
CHANGED
|
@@ -13,6 +13,8 @@ Check out our [Frameworks API](https://storybook.js.org/blog/framework-api/) ann
|
|
|
13
13
|
- [In a project with Storybook](#in-a-project-with-storybook)
|
|
14
14
|
- [Automatic migration](#automatic-migration)
|
|
15
15
|
- [Manual migration](#manual-migration)
|
|
16
|
+
- [How to mock](#how-to-mock)
|
|
17
|
+
- [Mocking links](#mocking-links)
|
|
16
18
|
- [Troubleshooting](#troubleshooting)
|
|
17
19
|
- [Error: `ERR! SyntaxError: Identifier '__esbuild_register_import_meta_url__' has already been declared` when starting Storybook](#error-err-syntaxerror-identifier-__esbuild_register_import_meta_url__-has-already-been-declared-when-starting-storybook)
|
|
18
20
|
- [Error: `Cannot read properties of undefined (reading 'disable_scroll_handling')` in preview](#error-cannot-read-properties-of-undefined-reading-disable_scroll_handling-in-preview)
|
|
@@ -26,10 +28,10 @@ However SvelteKit has some [Kit-specific modules](https://kit.svelte.dev/docs/mo
|
|
|
26
28
|
| **Module** | **Status** | **Note** |
|
|
27
29
|
| ---------------------------------------------------------------------------------- | ---------------------- | ----------------------------------------------------------------------------------------------------------------------------------- |
|
|
28
30
|
| [`$app/environment`](https://kit.svelte.dev/docs/modules#$app-environment) | ✅ Supported | `version` is always empty in Storybook. |
|
|
29
|
-
| [`$app/forms`](https://kit.svelte.dev/docs/modules#$app-forms) |
|
|
30
|
-
| [`$app/navigation`](https://kit.svelte.dev/docs/modules#$app-navigation) |
|
|
31
|
+
| [`$app/forms`](https://kit.svelte.dev/docs/modules#$app-forms) | ✅ Supported | See [How to mock](#how-to-mock) |
|
|
32
|
+
| [`$app/navigation`](https://kit.svelte.dev/docs/modules#$app-navigation) | ✅ Supported | See [How to mock](#how-to-mock) |
|
|
31
33
|
| [`$app/paths`](https://kit.svelte.dev/docs/modules#$app-paths) | ✅ Supported | Requires SvelteKit 1.4.0 or newer |
|
|
32
|
-
| [`$app/stores`](https://kit.svelte.dev/docs/modules#$app-stores) | ✅ Supported |
|
|
34
|
+
| [`$app/stores`](https://kit.svelte.dev/docs/modules#$app-stores) | ✅ Supported | See [How to mock](#how-to-mock) |
|
|
33
35
|
| [`$env/dynamic/private`](https://kit.svelte.dev/docs/modules#$env-dynamic-private) | ⛔ Not supported | They are meant to only be available server-side, and Storybook renders all components on the client. |
|
|
34
36
|
| [`$env/dynamic/public`](https://kit.svelte.dev/docs/modules#$env-dynamic-public) | 🚧 Partially supported | Only supported in development mode. Storybook is built as a static app with no server-side API so cannot dynamically serve content. |
|
|
35
37
|
| [`$env/static/private`](https://kit.svelte.dev/docs/modules#$env-static-private) | ⛔ Not supported | They are meant to only be available server-side, and Storybook renders all components on the client. |
|
|
@@ -100,6 +102,77 @@ yarn remove storybook-builder-vite
|
|
|
100
102
|
yarn remove @storybook/builder-vite
|
|
101
103
|
```
|
|
102
104
|
|
|
105
|
+
## How to mock
|
|
106
|
+
|
|
107
|
+
To mock a SvelteKit import you can set it on `parameters.sveltekit_experimental`:
|
|
108
|
+
|
|
109
|
+
```ts
|
|
110
|
+
export const MyStory = {
|
|
111
|
+
parameters: {
|
|
112
|
+
sveltekit_experimental: {
|
|
113
|
+
stores: {
|
|
114
|
+
page: {
|
|
115
|
+
data: {
|
|
116
|
+
test: 'passed',
|
|
117
|
+
},
|
|
118
|
+
},
|
|
119
|
+
navigating: {
|
|
120
|
+
route: {
|
|
121
|
+
id: '/storybook',
|
|
122
|
+
},
|
|
123
|
+
},
|
|
124
|
+
updated: true,
|
|
125
|
+
},
|
|
126
|
+
},
|
|
127
|
+
},
|
|
128
|
+
};
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
You can add the name of the module you want to mock to `parameters.sveltekit_experimental` (in the example above we are mocking the `stores` module which correspond to `$app/stores`) and then pass the following kind of objects:
|
|
132
|
+
|
|
133
|
+
| Module | Path in parameters | Kind of objects |
|
|
134
|
+
| ------------------------------------------------- | ------------------------------------------------------------ | -------------------------------------------------------------------------------------------------- |
|
|
135
|
+
| `import { page } from "$app/stores"` | `parameters.sveltekit_experimental.stores.page` | A Partial of the page store |
|
|
136
|
+
| `import { navigating } from "$app/stores"` | `parameters.sveltekit_experimental.stores.navigating` | A Partial of the navigating store |
|
|
137
|
+
| `import { updated } from "$app/stores"` | `parameters.sveltekit_experimental.stores.updated` | A boolean representing the value of updated (you can also access `check()` which will be a noop) |
|
|
138
|
+
| `import { goto } from "$app/navigation"` | `parameters.sveltekit_experimental.navigation.goto` | A callback that will be called whenever goto is called |
|
|
139
|
+
| `import { invalidate } from "$app/navigation"` | `parameters.sveltekit_experimental.navigation.invalidate` | A callback that will be called whenever invalidate is called |
|
|
140
|
+
| `import { invalidateAll } from "$app/navigation"` | `parameters.sveltekit_experimental.navigation.invalidateAll` | A callback that will be called whenever invalidateAll is called |
|
|
141
|
+
| `import { afterNavigate } from "$app/navigation"` | `parameters.sveltekit_experimental.navigation.afterNavigate` | An object that will be passed to the afterNavigate function (which will be invoked onMount) called |
|
|
142
|
+
| `import { enhance } from "$app/forms"` | `parameters.sveltekit_experimental.forms.enhance` | A callback that will called when a form with `use:enhance` is submitted |
|
|
143
|
+
|
|
144
|
+
All the other functions are still exported as `noop` from the mocked modules so that your application will still work.
|
|
145
|
+
|
|
146
|
+
### Mocking links
|
|
147
|
+
|
|
148
|
+
The default link-handling behavior (ie. clicking an `<a />` tag with an `href` attribute) is to log an action to the Actions panel.
|
|
149
|
+
|
|
150
|
+
You can override this by setting an object on `parameter.sveltekit_experimental.hrefs`, where the keys are strings representing an href and the values are objects typed as `{ callback: (href, event) => void, asRegex?: boolean }`.
|
|
151
|
+
|
|
152
|
+
If you have an `<a />` tag inside your code with the `href` attribute that matches one or more of the links defined (treated as regex based on the `asRegex` property) the corresponding `callback` will be called.
|
|
153
|
+
|
|
154
|
+
Example:
|
|
155
|
+
|
|
156
|
+
```ts
|
|
157
|
+
export const MyStory = {
|
|
158
|
+
parameters: {
|
|
159
|
+
sveltekit_experimental: {
|
|
160
|
+
hrefs: {
|
|
161
|
+
'/basic-href': (to, event) => {
|
|
162
|
+
console.log(to, event);
|
|
163
|
+
},
|
|
164
|
+
'/root.*': {
|
|
165
|
+
callback: (to, event) => {
|
|
166
|
+
console.log(to, event);
|
|
167
|
+
},
|
|
168
|
+
asRegex: true,
|
|
169
|
+
},
|
|
170
|
+
},
|
|
171
|
+
},
|
|
172
|
+
},
|
|
173
|
+
};
|
|
174
|
+
```
|
|
175
|
+
|
|
103
176
|
## Troubleshooting
|
|
104
177
|
|
|
105
178
|
### Error: `ERR! SyntaxError: Identifier '__esbuild_register_import_meta_url__' has already been declared` when starting Storybook
|
|
@@ -125,3 +198,4 @@ You'll experience this if anything in your story is importing from `$app/forms`
|
|
|
125
198
|
## Acknowledgements
|
|
126
199
|
|
|
127
200
|
Integrating with SvelteKit would not have been possible if it weren't for the fantastic efforts by the Svelte core team - especially [Ben McCann](https://twitter.com/benjaminmccann) - to make integrations with the wider ecosystem possible.
|
|
201
|
+
A big thank you also goes out to [Paolo Ricciuti](https://twitter.com/PaoloRicciuti) for improving the mocking capabilities.
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
+
import { BuilderOptions as BuilderOptions$1, StorybookConfigVite } from '@storybook/builder-vite';
|
|
1
2
|
import { FileSystemCache } from 'file-system-cache';
|
|
2
3
|
import { TransformOptions } from '@babel/core';
|
|
3
4
|
import { Server } from 'http';
|
|
4
|
-
import { BuilderOptions as BuilderOptions$1, StorybookConfigVite } from '@storybook/builder-vite';
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
Matches any [primitive value](https://developer.mozilla.org/en-US/docs/Glossary/Primitive).
|
|
@@ -2873,8 +2873,15 @@ interface TypescriptOptions {
|
|
|
2873
2873
|
* Disable parsing typescript files through babel.
|
|
2874
2874
|
*
|
|
2875
2875
|
* @default `false`
|
|
2876
|
+
* @deprecated use `skipCompiler` instead
|
|
2876
2877
|
*/
|
|
2877
2878
|
skipBabel: boolean;
|
|
2879
|
+
/**
|
|
2880
|
+
* Disable parsing typescript files through compiler.
|
|
2881
|
+
*
|
|
2882
|
+
* @default `false`
|
|
2883
|
+
*/
|
|
2884
|
+
skipCompiler: boolean;
|
|
2878
2885
|
}
|
|
2879
2886
|
type Preset = string | {
|
|
2880
2887
|
name: string;
|
|
@@ -2994,6 +3001,12 @@ interface StorybookConfig$1 {
|
|
|
2994
3001
|
* Apply decorators from preview.js before decorators from addons or frameworks
|
|
2995
3002
|
*/
|
|
2996
3003
|
legacyDecoratorFileOrder?: boolean;
|
|
3004
|
+
/**
|
|
3005
|
+
* Disallow implicit actions during rendering. This will be the default in Storybook 8.
|
|
3006
|
+
*
|
|
3007
|
+
* This will make sure that your story renders the same no matter if docgen is enabled or not.
|
|
3008
|
+
*/
|
|
3009
|
+
disallowImplicitActionsInRenderV8?: boolean;
|
|
2997
3010
|
};
|
|
2998
3011
|
build?: TestBuildConfig;
|
|
2999
3012
|
/**
|
|
@@ -3080,6 +3093,14 @@ type PresetProperty<K, TStorybookConfig = StorybookConfig$1> = TStorybookConfig[
|
|
|
3080
3093
|
type PresetPropertyFn<K, TStorybookConfig = StorybookConfig$1, TOptions = {}> = (config: TStorybookConfig[K extends keyof TStorybookConfig ? K : never], options: Options & TOptions) => TStorybookConfig[K extends keyof TStorybookConfig ? K : never] | Promise<TStorybookConfig[K extends keyof TStorybookConfig ? K : never]>;
|
|
3081
3094
|
type Path = string;
|
|
3082
3095
|
|
|
3096
|
+
declare function enhance(form: HTMLFormElement): {
|
|
3097
|
+
destroy(): void;
|
|
3098
|
+
};
|
|
3099
|
+
|
|
3100
|
+
declare function goto(...args: any[]): Promise<void>;
|
|
3101
|
+
declare function invalidate(...args: any[]): Promise<void>;
|
|
3102
|
+
declare function invalidateAll(): Promise<void>;
|
|
3103
|
+
|
|
3083
3104
|
type FrameworkName = '@storybook/sveltekit';
|
|
3084
3105
|
type BuilderName = '@storybook/builder-vite';
|
|
3085
3106
|
type FrameworkOptions = {
|
|
@@ -3097,9 +3118,28 @@ type StorybookConfigFramework = {
|
|
|
3097
3118
|
};
|
|
3098
3119
|
};
|
|
3099
3120
|
};
|
|
3100
|
-
/**
|
|
3101
|
-
* The interface for Storybook configuration in `main.ts` files.
|
|
3102
|
-
*/
|
|
3103
3121
|
type StorybookConfig = Omit<StorybookConfig$1, keyof StorybookConfigVite | keyof StorybookConfigFramework> & StorybookConfigVite & StorybookConfigFramework;
|
|
3122
|
+
type NormalizedHrefConfig = {
|
|
3123
|
+
callback: (to: string, event: Event) => void;
|
|
3124
|
+
asRegex?: boolean;
|
|
3125
|
+
};
|
|
3126
|
+
type HrefConfig = NormalizedHrefConfig | NormalizedHrefConfig['callback'];
|
|
3127
|
+
type SvelteKitParameters = Partial<{
|
|
3128
|
+
hrefs: Record<string, HrefConfig>;
|
|
3129
|
+
stores: {
|
|
3130
|
+
page: Record<string, any>;
|
|
3131
|
+
navigating: Record<string, any>;
|
|
3132
|
+
updated: boolean;
|
|
3133
|
+
};
|
|
3134
|
+
navigation: {
|
|
3135
|
+
goto: typeof goto;
|
|
3136
|
+
invalidate: typeof invalidate;
|
|
3137
|
+
invalidateAll: typeof invalidateAll;
|
|
3138
|
+
afterNavigate: Record<string, any>;
|
|
3139
|
+
};
|
|
3140
|
+
forms: {
|
|
3141
|
+
enhance: typeof enhance;
|
|
3142
|
+
};
|
|
3143
|
+
}>;
|
|
3104
3144
|
|
|
3105
|
-
export { FrameworkOptions as F, PresetProperty as P, StorybookConfig as S };
|
|
3145
|
+
export { FrameworkOptions as F, HrefConfig as H, NormalizedHrefConfig as N, PresetProperty as P, StorybookConfig as S, SvelteKitParameters as a };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
export { F as FrameworkOptions, S as StorybookConfig } from './index-
|
|
1
|
+
export { F as FrameworkOptions, H as HrefConfig, N as NormalizedHrefConfig, S as StorybookConfig, a as SvelteKitParameters } from './index-f3de942f.js';
|
|
2
|
+
import '@storybook/builder-vite';
|
|
2
3
|
import 'file-system-cache';
|
|
3
4
|
import '@babel/core';
|
|
4
5
|
import 'http';
|
|
5
|
-
import '@storybook/builder-vite';
|
package/dist/preset.d.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import { P as PresetProperty, S as StorybookConfig } from './index-
|
|
1
|
+
import { P as PresetProperty, S as StorybookConfig } from './index-f3de942f.js';
|
|
2
|
+
import '@storybook/builder-vite';
|
|
2
3
|
import 'file-system-cache';
|
|
3
4
|
import '@babel/core';
|
|
4
5
|
import 'http';
|
|
5
|
-
import '@storybook/builder-vite';
|
|
6
6
|
|
|
7
7
|
declare const core: PresetProperty<'core', StorybookConfig>;
|
|
8
|
+
declare const previewAnnotations: StorybookConfig['previewAnnotations'];
|
|
8
9
|
declare const viteFinal: NonNullable<StorybookConfig['viteFinal']>;
|
|
9
10
|
|
|
10
|
-
export { core, viteFinal };
|
|
11
|
+
export { core, previewAnnotations, viteFinal };
|
package/dist/preset.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var __defProp=Object.defineProperty;var __getOwnPropDesc=Object.getOwnPropertyDescriptor;var __getOwnPropNames=Object.getOwnPropertyNames;var __hasOwnProp=Object.prototype.hasOwnProperty;var __export=(target,all)=>{for(var name in all)__defProp(target,name,{get:all[name],enumerable:!0})},__copyProps=(to,from,except,desc)=>{if(from&&typeof from=="object"||typeof from=="function")for(let key of __getOwnPropNames(from))!__hasOwnProp.call(to,key)&&key!==except&&__defProp(to,key,{get:()=>from[key],enumerable:!(desc=__getOwnPropDesc(from,key))||desc.enumerable});return to};var __toCommonJS=mod=>__copyProps(__defProp({},"__esModule",{value:!0}),mod);var preset_exports={};__export(preset_exports,{core:()=>core,viteFinal:()=>viteFinal});module.exports=__toCommonJS(preset_exports);var import_preset=require("@storybook/svelte-vite/preset"),import_builder_vite=require("@storybook/builder-vite"),import_path=require("path");function configOverrides(){return{name:"storybook:sveltekit-overrides",apply:"build",config:()=>({build:{ssr:!1}})}}var getAbsolutePath=input=>(0,import_path.dirname)(require.resolve((0,import_path.join)(input,"package.json"))),core={builder:getAbsolutePath("@storybook/builder-vite"),renderer:getAbsolutePath("@storybook/svelte")},viteFinal=async(config,options)=>{let baseConfig=await(0,import_preset.viteFinal)(config,options),{plugins=[]}=baseConfig;return plugins=(await(0,import_builder_vite.withoutVitePlugins)(plugins,["vite-plugin-sveltekit-compile","vite-plugin-sveltekit-guard"])).concat(configOverrides()),{...baseConfig,plugins}};0&&(module.exports={core,viteFinal});
|
|
1
|
+
"use strict";var __create=Object.create;var __defProp=Object.defineProperty;var __getOwnPropDesc=Object.getOwnPropertyDescriptor;var __getOwnPropNames=Object.getOwnPropertyNames;var __getProtoOf=Object.getPrototypeOf,__hasOwnProp=Object.prototype.hasOwnProperty;var __export=(target,all)=>{for(var name in all)__defProp(target,name,{get:all[name],enumerable:!0})},__copyProps=(to,from,except,desc)=>{if(from&&typeof from=="object"||typeof from=="function")for(let key of __getOwnPropNames(from))!__hasOwnProp.call(to,key)&&key!==except&&__defProp(to,key,{get:()=>from[key],enumerable:!(desc=__getOwnPropDesc(from,key))||desc.enumerable});return to};var __toESM=(mod,isNodeMode,target)=>(target=mod!=null?__create(__getProtoOf(mod)):{},__copyProps(isNodeMode||!mod||!mod.__esModule?__defProp(target,"default",{value:mod,enumerable:!0}):target,mod)),__toCommonJS=mod=>__copyProps(__defProp({},"__esModule",{value:!0}),mod);var preset_exports={};__export(preset_exports,{core:()=>core,previewAnnotations:()=>previewAnnotations,viteFinal:()=>viteFinal});module.exports=__toCommonJS(preset_exports);var import_preset=require("@storybook/svelte-vite/preset"),import_builder_vite=require("@storybook/builder-vite"),import_path=require("path");function configOverrides(){return{name:"storybook:sveltekit-overrides",apply:"build",config:()=>({build:{ssr:!1}})}}var import_node_path=require("path"),import_vite=require("vite");function mockSveltekitStores(){return{name:"storybook:sveltekit-mock-stores",enforce:"post",config:config=>(0,import_vite.mergeConfig)(config,{resolve:{alias:{$app:(0,import_node_path.resolve)(__dirname,"../src/mocks/app/")}}})}}var getAbsolutePath=input=>(0,import_path.dirname)(require.resolve((0,import_path.join)(input,"package.json"))),core={builder:getAbsolutePath("@storybook/builder-vite"),renderer:getAbsolutePath("@storybook/svelte")},previewAnnotations=(entry=[])=>[...entry,(0,import_path.join)((0,import_path.dirname)(require.resolve("@storybook/sveltekit/package.json")),"dist/preview.mjs")],viteFinal=async(config,options)=>{let baseConfig=await(0,import_preset.viteFinal)(config,options),{plugins=[]}=baseConfig;return plugins=(await(0,import_builder_vite.withoutVitePlugins)(plugins,["vite-plugin-sveltekit-compile","vite-plugin-sveltekit-guard"])).concat(configOverrides()).concat(mockSveltekitStores()),{...baseConfig,plugins}};0&&(module.exports={core,previewAnnotations,viteFinal});
|
package/dist/preview.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var __defProp=Object.defineProperty;var __getOwnPropDesc=Object.getOwnPropertyDescriptor;var __getOwnPropNames=Object.getOwnPropertyNames;var __hasOwnProp=Object.prototype.hasOwnProperty;var __export=(target,all)=>{for(var name in all)__defProp(target,name,{get:all[name],enumerable:!0})},__copyProps=(to,from,except,desc)=>{if(from&&typeof from=="object"||typeof from=="function")for(let key of __getOwnPropNames(from))!__hasOwnProp.call(to,key)&&key!==except&&__defProp(to,key,{get:()=>from[key],enumerable:!(desc=__getOwnPropDesc(from,key))||desc.enumerable});return to};var __toCommonJS=mod=>__copyProps(__defProp({},"__esModule",{value:!0}),mod);var preview_exports={};__export(preview_exports,{decorators:()=>decorators});module.exports=__toCommonJS(preview_exports);var import_addon_actions=require("@storybook/addon-actions"),import_svelte3=require("svelte");var import_svelte=require("svelte");function setAfterNavigateArgument(afterNavigateArgs){(0,import_svelte.setContext)("after-navigate-args",afterNavigateArgs)}var import_svelte2=require("svelte");function createMockedStore(contextName){return[{subscribe(runner){let page2=(0,import_svelte2.getContext)(contextName);return runner(page2),()=>{}}},value=>{(0,import_svelte2.setContext)(contextName,value)}]}var[page,setPage]=createMockedStore("page-ctx"),[navigating,setNavigating]=createMockedStore("navigating-ctx"),[updated,setUpdated]=createMockedStore("updated-ctx");updated.check=()=>{};var normalizeHrefConfig=hrefConfig=>typeof hrefConfig=="function"?{callback:hrefConfig,asRegex:!1}:hrefConfig,decorators=[(Story,ctx)=>{var _a,_b,_c,_d,_e;let svelteKitParameters=((_a=ctx.parameters)==null?void 0:_a.sveltekit_experimental)??{};return setPage((_b=svelteKitParameters==null?void 0:svelteKitParameters.stores)==null?void 0:_b.page),setNavigating((_c=svelteKitParameters==null?void 0:svelteKitParameters.stores)==null?void 0:_c.navigating),setUpdated((_d=svelteKitParameters==null?void 0:svelteKitParameters.stores)==null?void 0:_d.updated),setAfterNavigateArgument((_e=svelteKitParameters==null?void 0:svelteKitParameters.navigation)==null?void 0:_e.afterNavigate),(0,import_svelte3.onMount)(()=>{let globalClickListener=e=>{let element=e.composedPath().findLast(el=>el instanceof HTMLElement&&el.tagName==="A");if(element&&element instanceof HTMLAnchorElement){let to=element.getAttribute("href");if(!to)return;e.preventDefault();let defaultActionCallback=()=>(0,import_addon_actions.action)("navigate")(to,e);if(!svelteKitParameters.hrefs){defaultActionCallback();return}let callDefaultCallback=!0;Object.entries(svelteKitParameters.hrefs).forEach(([href,hrefConfig])=>{let{callback,asRegex}=normalizeHrefConfig(hrefConfig);(asRegex?new RegExp(href).test(to):to===href)&&(callDefaultCallback=!1,callback==null||callback(to,e))}),callDefaultCallback&&defaultActionCallback()}};function createListeners(baseModule,functions){let toRemove=[];return functions.forEach(func=>{var _a2;if((_a2=svelteKitParameters[baseModule])!=null&&_a2[func]&&svelteKitParameters[baseModule][func]instanceof Function){let listener=({detail=[]})=>{let args=Array.isArray(detail)?detail:[];svelteKitParameters[baseModule][func](...args)},eventType=`storybook:${func}`;toRemove.push({eventType,listener}),window.addEventListener(eventType,listener)}}),()=>{toRemove.forEach(({eventType,listener})=>{window.removeEventListener(eventType,listener)})}}let removeNavigationListeners=createListeners("navigation",["goto","invalidate","invalidateAll"]),removeFormsListeners=createListeners("forms",["enhance"]);return window.addEventListener("click",globalClickListener),()=>{window.removeEventListener("click",globalClickListener),removeNavigationListeners(),removeFormsListeners()}}),Story()}];0&&(module.exports={decorators});
|
package/dist/preview.mjs
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { action } from '@storybook/addon-actions';
|
|
2
|
+
import { onMount, setContext, getContext } from 'svelte';
|
|
3
|
+
|
|
4
|
+
function setAfterNavigateArgument(afterNavigateArgs){setContext("after-navigate-args",afterNavigateArgs);}function createMockedStore(contextName){return [{subscribe(runner){let page2=getContext(contextName);return runner(page2),()=>{}}},value=>{setContext(contextName,value);}]}var[page,setPage]=createMockedStore("page-ctx"),[navigating,setNavigating]=createMockedStore("navigating-ctx"),[updated,setUpdated]=createMockedStore("updated-ctx");updated.check=()=>{};var normalizeHrefConfig=hrefConfig=>typeof hrefConfig=="function"?{callback:hrefConfig,asRegex:!1}:hrefConfig,decorators=[(Story,ctx)=>{let svelteKitParameters=ctx.parameters?.sveltekit_experimental??{};return setPage(svelteKitParameters?.stores?.page),setNavigating(svelteKitParameters?.stores?.navigating),setUpdated(svelteKitParameters?.stores?.updated),setAfterNavigateArgument(svelteKitParameters?.navigation?.afterNavigate),onMount(()=>{let globalClickListener=e=>{let element=e.composedPath().findLast(el=>el instanceof HTMLElement&&el.tagName==="A");if(element&&element instanceof HTMLAnchorElement){let to=element.getAttribute("href");if(!to)return;e.preventDefault();let defaultActionCallback=()=>action("navigate")(to,e);if(!svelteKitParameters.hrefs){defaultActionCallback();return}let callDefaultCallback=!0;Object.entries(svelteKitParameters.hrefs).forEach(([href,hrefConfig])=>{let{callback,asRegex}=normalizeHrefConfig(hrefConfig);(asRegex?new RegExp(href).test(to):to===href)&&(callDefaultCallback=!1,callback?.(to,e));}),callDefaultCallback&&defaultActionCallback();}};function createListeners(baseModule,functions){let toRemove=[];return functions.forEach(func=>{if(svelteKitParameters[baseModule]?.[func]&&svelteKitParameters[baseModule][func]instanceof Function){let listener=({detail=[]})=>{let args=Array.isArray(detail)?detail:[];svelteKitParameters[baseModule][func](...args);},eventType=`storybook:${func}`;toRemove.push({eventType,listener}),window.addEventListener(eventType,listener);}}),()=>{toRemove.forEach(({eventType,listener})=>{window.removeEventListener(eventType,listener);});}}let removeNavigationListeners=createListeners("navigation",["goto","invalidate","invalidateAll"]),removeFormsListeners=createListeners("forms",["enhance"]);return window.addEventListener("click",globalClickListener),()=>{window.removeEventListener("click",globalClickListener),removeNavigationListeners(),removeFormsListeners();}}),Story()}];
|
|
5
|
+
|
|
6
|
+
export { decorators };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@storybook/sveltekit",
|
|
3
|
-
"version": "7.6.0-alpha.
|
|
3
|
+
"version": "7.6.0-alpha.7",
|
|
4
4
|
"description": "Storybook for SvelteKit",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"storybook",
|
|
@@ -29,6 +29,9 @@
|
|
|
29
29
|
"require": "./dist/index.js",
|
|
30
30
|
"import": "./dist/index.mjs"
|
|
31
31
|
},
|
|
32
|
+
"./dist/preview.mjs": {
|
|
33
|
+
"import": "./dist/preview.mjs"
|
|
34
|
+
},
|
|
32
35
|
"./preset": {
|
|
33
36
|
"types": "./dist/preset.d.ts",
|
|
34
37
|
"require": "./dist/preset.js"
|
|
@@ -43,16 +46,17 @@
|
|
|
43
46
|
"README.md",
|
|
44
47
|
"*.js",
|
|
45
48
|
"*.d.ts",
|
|
46
|
-
"
|
|
49
|
+
"src/mocks/**/*"
|
|
47
50
|
],
|
|
48
51
|
"scripts": {
|
|
49
|
-
"check": "../../../scripts/prepare/check.ts",
|
|
50
|
-
"prep": "../../../scripts/prepare/bundle.ts"
|
|
52
|
+
"check": "node --loader ../../../scripts/node_modules/esbuild-register/loader.js -r ../../../scripts/node_modules/esbuild-register/register.js ../../../scripts/prepare/check.ts",
|
|
53
|
+
"prep": "node --loader ../../../scripts/node_modules/esbuild-register/loader.js -r ../../../scripts/node_modules/esbuild-register/register.js ../../../scripts/prepare/bundle.ts"
|
|
51
54
|
},
|
|
52
55
|
"dependencies": {
|
|
53
|
-
"@storybook/
|
|
54
|
-
"@storybook/
|
|
55
|
-
"@storybook/svelte
|
|
56
|
+
"@storybook/addon-actions": "7.6.0-alpha.7",
|
|
57
|
+
"@storybook/builder-vite": "7.6.0-alpha.7",
|
|
58
|
+
"@storybook/svelte": "7.6.0-alpha.7",
|
|
59
|
+
"@storybook/svelte-vite": "7.6.0-alpha.7"
|
|
56
60
|
},
|
|
57
61
|
"devDependencies": {
|
|
58
62
|
"@types/node": "^18.0.0",
|
|
@@ -72,6 +76,7 @@
|
|
|
72
76
|
"bundler": {
|
|
73
77
|
"entries": [
|
|
74
78
|
"./src/index.ts",
|
|
79
|
+
"./src/preview.ts",
|
|
75
80
|
"./src/preset.ts"
|
|
76
81
|
],
|
|
77
82
|
"platform": "node"
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export function enhance(form: HTMLFormElement) {
|
|
2
|
+
const listener = (e: Event) => {
|
|
3
|
+
e.preventDefault();
|
|
4
|
+
const event = new CustomEvent('storybook:enhance');
|
|
5
|
+
window.dispatchEvent(event);
|
|
6
|
+
};
|
|
7
|
+
form.addEventListener('submit', listener);
|
|
8
|
+
return {
|
|
9
|
+
destroy() {
|
|
10
|
+
form.removeEventListener('submit', listener);
|
|
11
|
+
},
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function applyAction() {}
|
|
16
|
+
|
|
17
|
+
export function deserialize() {}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { getContext, onMount, setContext } from 'svelte';
|
|
2
|
+
|
|
3
|
+
export async function goto(...args: any[]) {
|
|
4
|
+
const event = new CustomEvent('storybook:goto', {
|
|
5
|
+
detail: args,
|
|
6
|
+
});
|
|
7
|
+
window.dispatchEvent(event);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function setAfterNavigateArgument(afterNavigateArgs: any) {
|
|
11
|
+
setContext('after-navigate-args', afterNavigateArgs);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function afterNavigate(cb: any) {
|
|
15
|
+
const argument = getContext('after-navigate-args');
|
|
16
|
+
onMount(() => {
|
|
17
|
+
if (cb && cb instanceof Function) {
|
|
18
|
+
cb(argument);
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function onNavigate() {}
|
|
24
|
+
|
|
25
|
+
export function beforeNavigate() {}
|
|
26
|
+
|
|
27
|
+
export function disableScrollHandling() {}
|
|
28
|
+
|
|
29
|
+
export async function invalidate(...args: any[]) {
|
|
30
|
+
const event = new CustomEvent('storybook:invalidate', {
|
|
31
|
+
detail: args,
|
|
32
|
+
});
|
|
33
|
+
window.dispatchEvent(event);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export async function invalidateAll() {
|
|
37
|
+
const event = new CustomEvent('storybook:invalidateAll');
|
|
38
|
+
window.dispatchEvent(event);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function preloadCode() {}
|
|
42
|
+
|
|
43
|
+
export function preloadData() {}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { getContext, setContext } from 'svelte';
|
|
2
|
+
|
|
3
|
+
function createMockedStore(contextName: string) {
|
|
4
|
+
return [
|
|
5
|
+
{
|
|
6
|
+
subscribe(runner: any) {
|
|
7
|
+
const page = getContext(contextName);
|
|
8
|
+
runner(page);
|
|
9
|
+
return () => {};
|
|
10
|
+
},
|
|
11
|
+
},
|
|
12
|
+
(value: unknown) => {
|
|
13
|
+
setContext(contextName, value);
|
|
14
|
+
},
|
|
15
|
+
] as const;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export const [page, setPage] = createMockedStore('page-ctx');
|
|
19
|
+
export const [navigating, setNavigating] = createMockedStore('navigating-ctx');
|
|
20
|
+
const [updated, setUpdated] = createMockedStore('updated-ctx');
|
|
21
|
+
|
|
22
|
+
(updated as any).check = () => {};
|
|
23
|
+
|
|
24
|
+
export { updated, setUpdated };
|
|
25
|
+
|
|
26
|
+
export function getStores() {
|
|
27
|
+
return {
|
|
28
|
+
page,
|
|
29
|
+
navigating,
|
|
30
|
+
updated,
|
|
31
|
+
};
|
|
32
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { expect, fn, within } from '@storybook/test';
|
|
2
|
+
import Forms from './Forms.svelte';
|
|
3
|
+
|
|
4
|
+
export default {
|
|
5
|
+
title: 'stories/sveltekit/modules/forms',
|
|
6
|
+
component: Forms,
|
|
7
|
+
tags: ['autodocs'],
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
const enhance = fn();
|
|
11
|
+
|
|
12
|
+
export const Enhance = {
|
|
13
|
+
async play({ canvasElement }) {
|
|
14
|
+
const canvas = within(canvasElement);
|
|
15
|
+
const button = canvas.getByText('enhance');
|
|
16
|
+
button.click();
|
|
17
|
+
expect(enhance).toHaveBeenCalled();
|
|
18
|
+
},
|
|
19
|
+
parameters: {
|
|
20
|
+
sveltekit_experimental: {
|
|
21
|
+
forms: {
|
|
22
|
+
enhance,
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
};
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { expect, fn, within } from '@storybook/test';
|
|
2
|
+
import Hrefs from './Hrefs.svelte';
|
|
3
|
+
|
|
4
|
+
export default {
|
|
5
|
+
title: 'stories/sveltekit/modules/hrefs',
|
|
6
|
+
component: Hrefs,
|
|
7
|
+
tags: ['autodocs'],
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export const DefaultActions = {
|
|
11
|
+
async play({ canvasElement }) {
|
|
12
|
+
const canvas = within(canvasElement);
|
|
13
|
+
// eslint-disable-next-line no-undef
|
|
14
|
+
const initialUrl = window.location.toString();
|
|
15
|
+
|
|
16
|
+
const basicHref = canvas.getByText('/basic-href');
|
|
17
|
+
basicHref.click();
|
|
18
|
+
|
|
19
|
+
const complexHref = canvas.getByText(
|
|
20
|
+
'/deep/nested/link?with=true&multiple-params=200#and-an-id'
|
|
21
|
+
);
|
|
22
|
+
complexHref.click();
|
|
23
|
+
|
|
24
|
+
// eslint-disable-next-line no-undef
|
|
25
|
+
const finalUrl = window.location.toString();
|
|
26
|
+
expect(finalUrl).toBe(initialUrl);
|
|
27
|
+
},
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const basicStringMatch = fn();
|
|
31
|
+
const noMatch = fn();
|
|
32
|
+
const exactStringMatch = fn();
|
|
33
|
+
const regexMatch = fn();
|
|
34
|
+
|
|
35
|
+
export const Callbacks = {
|
|
36
|
+
parameters: {
|
|
37
|
+
sveltekit_experimental: {
|
|
38
|
+
hrefs: {
|
|
39
|
+
'/basic-href': basicStringMatch,
|
|
40
|
+
'/basic': noMatch,
|
|
41
|
+
'/deep/nested/link?with=true&multiple-params=200#and-an-id': exactStringMatch,
|
|
42
|
+
'nested/link\\?with': { callback: regexMatch, asRegex: true },
|
|
43
|
+
},
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
play: async (ctx) => {
|
|
47
|
+
await DefaultActions.play(ctx);
|
|
48
|
+
expect(basicStringMatch).toHaveBeenCalledTimes(1);
|
|
49
|
+
expect(noMatch).not.toHaveBeenCalled();
|
|
50
|
+
expect(exactStringMatch).toHaveBeenCalledTimes(1);
|
|
51
|
+
expect(regexMatch).toHaveBeenCalledTimes(1);
|
|
52
|
+
},
|
|
53
|
+
};
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { expect, fn, within } from '@storybook/test';
|
|
2
|
+
import Navigation from './Navigation.svelte';
|
|
3
|
+
|
|
4
|
+
export default {
|
|
5
|
+
title: 'stories/sveltekit/modules/navigation',
|
|
6
|
+
component: Navigation,
|
|
7
|
+
tags: ['autodocs'],
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
const goto = fn();
|
|
11
|
+
|
|
12
|
+
export const Goto = {
|
|
13
|
+
async play({ canvasElement }) {
|
|
14
|
+
const canvas = within(canvasElement);
|
|
15
|
+
const button = canvas.getByText('goto');
|
|
16
|
+
button.click();
|
|
17
|
+
expect(goto).toHaveBeenCalledWith('/storybook');
|
|
18
|
+
},
|
|
19
|
+
parameters: {
|
|
20
|
+
sveltekit_experimental: {
|
|
21
|
+
navigation: {
|
|
22
|
+
goto,
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
const invalidate = fn();
|
|
29
|
+
|
|
30
|
+
export const Invalidate = {
|
|
31
|
+
async play({ canvasElement }) {
|
|
32
|
+
const canvas = within(canvasElement);
|
|
33
|
+
const button = canvas.getByText('invalidate', { exact: true });
|
|
34
|
+
button.click();
|
|
35
|
+
expect(invalidate).toHaveBeenCalledWith('/storybook');
|
|
36
|
+
},
|
|
37
|
+
parameters: {
|
|
38
|
+
sveltekit_experimental: {
|
|
39
|
+
navigation: {
|
|
40
|
+
invalidate,
|
|
41
|
+
},
|
|
42
|
+
},
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const invalidateAll = fn();
|
|
47
|
+
|
|
48
|
+
export const InvalidateAll = {
|
|
49
|
+
async play({ canvasElement }) {
|
|
50
|
+
const canvas = within(canvasElement);
|
|
51
|
+
const button = canvas.getByText('invalidateAll');
|
|
52
|
+
button.click();
|
|
53
|
+
expect(invalidateAll).toHaveBeenCalledWith();
|
|
54
|
+
},
|
|
55
|
+
parameters: {
|
|
56
|
+
sveltekit_experimental: {
|
|
57
|
+
navigation: {
|
|
58
|
+
invalidateAll,
|
|
59
|
+
},
|
|
60
|
+
},
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
const afterNavigateFn = fn();
|
|
65
|
+
|
|
66
|
+
export const AfterNavigate = {
|
|
67
|
+
async play() {
|
|
68
|
+
expect(afterNavigateFn).toHaveBeenCalledWith({ test: 'passed' });
|
|
69
|
+
},
|
|
70
|
+
args: {
|
|
71
|
+
afterNavigateFn,
|
|
72
|
+
},
|
|
73
|
+
parameters: {
|
|
74
|
+
sveltekit_experimental: {
|
|
75
|
+
navigation: {
|
|
76
|
+
afterNavigate: {
|
|
77
|
+
test: 'passed',
|
|
78
|
+
},
|
|
79
|
+
},
|
|
80
|
+
},
|
|
81
|
+
},
|
|
82
|
+
};
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import Stores from './Stores.svelte';
|
|
2
|
+
|
|
3
|
+
export default {
|
|
4
|
+
title: 'stories/sveltekit/modules/stores',
|
|
5
|
+
component: Stores,
|
|
6
|
+
tags: ['autodocs'],
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
export const AllUndefined = {};
|
|
10
|
+
|
|
11
|
+
export const PageStore = {
|
|
12
|
+
parameters: {
|
|
13
|
+
sveltekit_experimental: {
|
|
14
|
+
stores: {
|
|
15
|
+
page: {
|
|
16
|
+
data: {
|
|
17
|
+
test: 'passed',
|
|
18
|
+
},
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
},
|
|
22
|
+
},
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export const NavigatingStore = {
|
|
26
|
+
parameters: {
|
|
27
|
+
sveltekit_experimental: {
|
|
28
|
+
stores: {
|
|
29
|
+
navigating: {
|
|
30
|
+
route: {
|
|
31
|
+
id: '/storybook',
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
},
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export const UpdatedStore = {
|
|
40
|
+
parameters: {
|
|
41
|
+
sveltekit_experimental: {
|
|
42
|
+
stores: {
|
|
43
|
+
updated: true,
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
},
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
export const PageAndNavigatingStore = {
|
|
50
|
+
parameters: {
|
|
51
|
+
sveltekit_experimental: {
|
|
52
|
+
stores: {
|
|
53
|
+
page: {
|
|
54
|
+
data: {
|
|
55
|
+
test: 'passed',
|
|
56
|
+
},
|
|
57
|
+
},
|
|
58
|
+
navigating: {
|
|
59
|
+
route: {
|
|
60
|
+
id: '/storybook',
|
|
61
|
+
},
|
|
62
|
+
},
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
},
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
export const PageAndUpdatedStore = {
|
|
69
|
+
parameters: {
|
|
70
|
+
sveltekit_experimental: {
|
|
71
|
+
stores: {
|
|
72
|
+
page: {
|
|
73
|
+
data: {
|
|
74
|
+
test: 'passed',
|
|
75
|
+
},
|
|
76
|
+
},
|
|
77
|
+
updated: true,
|
|
78
|
+
},
|
|
79
|
+
},
|
|
80
|
+
},
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
export const NavigatingAndUpdatedStore = {
|
|
84
|
+
parameters: {
|
|
85
|
+
sveltekit_experimental: {
|
|
86
|
+
stores: {
|
|
87
|
+
navigating: {
|
|
88
|
+
route: {
|
|
89
|
+
id: '/storybook',
|
|
90
|
+
},
|
|
91
|
+
},
|
|
92
|
+
updated: true,
|
|
93
|
+
},
|
|
94
|
+
},
|
|
95
|
+
},
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
export const AllThreeStores = {
|
|
99
|
+
parameters: {
|
|
100
|
+
sveltekit_experimental: {
|
|
101
|
+
stores: {
|
|
102
|
+
page: {
|
|
103
|
+
data: {
|
|
104
|
+
test: 'passed',
|
|
105
|
+
},
|
|
106
|
+
},
|
|
107
|
+
navigating: {
|
|
108
|
+
route: {
|
|
109
|
+
id: '/storybook',
|
|
110
|
+
},
|
|
111
|
+
},
|
|
112
|
+
updated: true,
|
|
113
|
+
},
|
|
114
|
+
},
|
|
115
|
+
},
|
|
116
|
+
};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { expect, fn, within } from '@storybook/test';
|
|
2
|
+
import Forms from './Forms.svelte';
|
|
3
|
+
|
|
4
|
+
export default {
|
|
5
|
+
title: 'stories/sveltekit/modules/forms',
|
|
6
|
+
component: Forms,
|
|
7
|
+
tags: ['autodocs'],
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
const enhance = fn();
|
|
11
|
+
|
|
12
|
+
export const Enhance = {
|
|
13
|
+
async play({ canvasElement }) {
|
|
14
|
+
const canvas = within(canvasElement);
|
|
15
|
+
const button = canvas.getByText('enhance');
|
|
16
|
+
button.click();
|
|
17
|
+
expect(enhance).toHaveBeenCalled();
|
|
18
|
+
},
|
|
19
|
+
parameters: {
|
|
20
|
+
sveltekit_experimental: {
|
|
21
|
+
forms: {
|
|
22
|
+
enhance,
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
};
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { expect, fn, within } from '@storybook/test';
|
|
2
|
+
import Hrefs from './Hrefs.svelte';
|
|
3
|
+
|
|
4
|
+
export default {
|
|
5
|
+
title: 'stories/sveltekit/modules/hrefs',
|
|
6
|
+
component: Hrefs,
|
|
7
|
+
tags: ['autodocs'],
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export const DefaultActions = {
|
|
11
|
+
async play({ canvasElement }) {
|
|
12
|
+
const canvas = within(canvasElement);
|
|
13
|
+
// eslint-disable-next-line no-undef
|
|
14
|
+
const initialUrl = window.location.toString();
|
|
15
|
+
|
|
16
|
+
const basicHref = canvas.getByText('/basic-href');
|
|
17
|
+
basicHref.click();
|
|
18
|
+
|
|
19
|
+
const complexHref = canvas.getByText(
|
|
20
|
+
'/deep/nested/link?with=true&multiple-params=200#and-an-id'
|
|
21
|
+
);
|
|
22
|
+
complexHref.click();
|
|
23
|
+
|
|
24
|
+
// eslint-disable-next-line no-undef
|
|
25
|
+
const finalUrl = window.location.toString();
|
|
26
|
+
expect(finalUrl).toBe(initialUrl);
|
|
27
|
+
},
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const basicStringMatch = fn();
|
|
31
|
+
const noMatch = fn();
|
|
32
|
+
const exactStringMatch = fn();
|
|
33
|
+
const regexMatch = fn();
|
|
34
|
+
|
|
35
|
+
export const Callbacks = {
|
|
36
|
+
parameters: {
|
|
37
|
+
sveltekit_experimental: {
|
|
38
|
+
hrefs: {
|
|
39
|
+
'/basic-href': basicStringMatch,
|
|
40
|
+
'/basic': noMatch,
|
|
41
|
+
'/deep/nested/link?with=true&multiple-params=200#and-an-id': exactStringMatch,
|
|
42
|
+
'nested/link\\?with': { callback: regexMatch, asRegex: true },
|
|
43
|
+
},
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
play: async (ctx) => {
|
|
47
|
+
await DefaultActions.play(ctx);
|
|
48
|
+
expect(basicStringMatch).toHaveBeenCalledTimes(1);
|
|
49
|
+
expect(noMatch).not.toHaveBeenCalled();
|
|
50
|
+
expect(exactStringMatch).toHaveBeenCalledTimes(1);
|
|
51
|
+
expect(regexMatch).toHaveBeenCalledTimes(1);
|
|
52
|
+
},
|
|
53
|
+
};
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { expect, fn, within } from '@storybook/test';
|
|
2
|
+
import Navigation from './Navigation.svelte';
|
|
3
|
+
|
|
4
|
+
export default {
|
|
5
|
+
title: 'stories/sveltekit/modules/navigation',
|
|
6
|
+
component: Navigation,
|
|
7
|
+
tags: ['autodocs'],
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
const goto = fn();
|
|
11
|
+
|
|
12
|
+
export const Goto = {
|
|
13
|
+
async play({ canvasElement }) {
|
|
14
|
+
const canvas = within(canvasElement);
|
|
15
|
+
const button = canvas.getByText('goto');
|
|
16
|
+
button.click();
|
|
17
|
+
expect(goto).toHaveBeenCalledWith('/storybook');
|
|
18
|
+
},
|
|
19
|
+
parameters: {
|
|
20
|
+
sveltekit_experimental: {
|
|
21
|
+
navigation: {
|
|
22
|
+
goto,
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
const invalidate = fn();
|
|
29
|
+
|
|
30
|
+
export const Invalidate = {
|
|
31
|
+
async play({ canvasElement }) {
|
|
32
|
+
const canvas = within(canvasElement);
|
|
33
|
+
const button = canvas.getByText('invalidate', { exact: true });
|
|
34
|
+
button.click();
|
|
35
|
+
expect(invalidate).toHaveBeenCalledWith('/storybook');
|
|
36
|
+
},
|
|
37
|
+
parameters: {
|
|
38
|
+
sveltekit_experimental: {
|
|
39
|
+
navigation: {
|
|
40
|
+
invalidate,
|
|
41
|
+
},
|
|
42
|
+
},
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const invalidateAll = fn();
|
|
47
|
+
|
|
48
|
+
export const InvalidateAll = {
|
|
49
|
+
async play({ canvasElement }) {
|
|
50
|
+
const canvas = within(canvasElement);
|
|
51
|
+
const button = canvas.getByText('invalidateAll');
|
|
52
|
+
button.click();
|
|
53
|
+
expect(invalidateAll).toHaveBeenCalledWith();
|
|
54
|
+
},
|
|
55
|
+
parameters: {
|
|
56
|
+
sveltekit_experimental: {
|
|
57
|
+
navigation: {
|
|
58
|
+
invalidateAll,
|
|
59
|
+
},
|
|
60
|
+
},
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
const afterNavigateFn = fn();
|
|
65
|
+
|
|
66
|
+
export const AfterNavigate = {
|
|
67
|
+
async play() {
|
|
68
|
+
expect(afterNavigateFn).toHaveBeenCalledWith({ test: 'passed' });
|
|
69
|
+
},
|
|
70
|
+
args: {
|
|
71
|
+
afterNavigateFn,
|
|
72
|
+
},
|
|
73
|
+
parameters: {
|
|
74
|
+
sveltekit_experimental: {
|
|
75
|
+
navigation: {
|
|
76
|
+
afterNavigate: {
|
|
77
|
+
test: 'passed',
|
|
78
|
+
},
|
|
79
|
+
},
|
|
80
|
+
},
|
|
81
|
+
},
|
|
82
|
+
};
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import Stores from './Stores.svelte';
|
|
2
|
+
|
|
3
|
+
export default {
|
|
4
|
+
title: 'stories/sveltekit/modules/stores',
|
|
5
|
+
component: Stores,
|
|
6
|
+
tags: ['autodocs'],
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
export const AllUndefined = {};
|
|
10
|
+
|
|
11
|
+
export const PageStore = {
|
|
12
|
+
parameters: {
|
|
13
|
+
sveltekit_experimental: {
|
|
14
|
+
stores: {
|
|
15
|
+
page: {
|
|
16
|
+
data: {
|
|
17
|
+
test: 'passed',
|
|
18
|
+
},
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
},
|
|
22
|
+
},
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export const NavigatingStore = {
|
|
26
|
+
parameters: {
|
|
27
|
+
sveltekit_experimental: {
|
|
28
|
+
stores: {
|
|
29
|
+
navigating: {
|
|
30
|
+
route: {
|
|
31
|
+
id: '/storybook',
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
},
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export const UpdatedStore = {
|
|
40
|
+
parameters: {
|
|
41
|
+
sveltekit_experimental: {
|
|
42
|
+
stores: {
|
|
43
|
+
updated: true,
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
},
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
export const PageAndNavigatingStore = {
|
|
50
|
+
parameters: {
|
|
51
|
+
sveltekit_experimental: {
|
|
52
|
+
stores: {
|
|
53
|
+
page: {
|
|
54
|
+
data: {
|
|
55
|
+
test: 'passed',
|
|
56
|
+
},
|
|
57
|
+
},
|
|
58
|
+
navigating: {
|
|
59
|
+
route: {
|
|
60
|
+
id: '/storybook',
|
|
61
|
+
},
|
|
62
|
+
},
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
},
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
export const PageAndUpdatedStore = {
|
|
69
|
+
parameters: {
|
|
70
|
+
sveltekit_experimental: {
|
|
71
|
+
stores: {
|
|
72
|
+
page: {
|
|
73
|
+
data: {
|
|
74
|
+
test: 'passed',
|
|
75
|
+
},
|
|
76
|
+
},
|
|
77
|
+
updated: true,
|
|
78
|
+
},
|
|
79
|
+
},
|
|
80
|
+
},
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
export const NavigatingAndUpdatedStore = {
|
|
84
|
+
parameters: {
|
|
85
|
+
sveltekit_experimental: {
|
|
86
|
+
stores: {
|
|
87
|
+
navigating: {
|
|
88
|
+
route: {
|
|
89
|
+
id: '/storybook',
|
|
90
|
+
},
|
|
91
|
+
},
|
|
92
|
+
updated: true,
|
|
93
|
+
},
|
|
94
|
+
},
|
|
95
|
+
},
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
export const AllThreeStores = {
|
|
99
|
+
parameters: {
|
|
100
|
+
sveltekit_experimental: {
|
|
101
|
+
stores: {
|
|
102
|
+
page: {
|
|
103
|
+
data: {
|
|
104
|
+
test: 'passed',
|
|
105
|
+
},
|
|
106
|
+
},
|
|
107
|
+
navigating: {
|
|
108
|
+
route: {
|
|
109
|
+
id: '/storybook',
|
|
110
|
+
},
|
|
111
|
+
},
|
|
112
|
+
updated: true,
|
|
113
|
+
},
|
|
114
|
+
},
|
|
115
|
+
},
|
|
116
|
+
};
|