Skip to content

Commit

Permalink
add template stories for sveltekit prerelease
Browse files Browse the repository at this point in the history
  • Loading branch information
JReinhold committed Dec 6, 2023
1 parent 32a8d30 commit 7192c90
Show file tree
Hide file tree
Showing 10 changed files with 386 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<script lang="ts">
/**
* @component Button TypeScript
* @wrapper
*/
import { global as globalThis } from '@storybook/global';
// @ts-ignore
const Button = globalThis.Components?.Button;
/**
* Rounds the button
*/
export let primary: boolean = false;
/**
* Displays the count
*/
export let count: number = 0;
/**
* Button text
* @slot
*/
export let text: string = 'You clicked';
function handleClick(_event: MouseEvent) {
count += 1;
}
</script>

<h1>Button TypeScript</h1>

<Button {primary} on:click on:click={handleClick} label="{text}: {count}" />

<p>A little text to show this is a view.</p>
<p>If we need to test components in a Svelte environment, for instance to test slot behaviour,</p>
<p>then wrapping the component up in a view</p>
<p>made just for the story is the simplest way to achieve this.</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script>
import { enhance } from '$app/forms';
</script>

<form use:enhance>
<button>enhance</button>
</form>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<ul>
<li><a href="/basic-href">Link to <code>/basic-href</code></a></li>
<li>
<a href="/deep/nested/link?with=true&multiple-params=200#and-an-id"
>Link to <code>/deep/nested/link?with=true&multiple-params=200#and-an-id</code></a
>
</li>
</ul>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<script>
import { goto, invalidate, invalidateAll, afterNavigate } from '$app/navigation';
export let afterNavigateFn;
afterNavigate(afterNavigateFn);
</script>

<button
on:click={() => {
goto('/storybook-goto');
}}>goto</button
>

<button
on:click={() => {
invalidate('/storybook-invalidate');
}}>invalidate</button
>

<button
on:click={() => {
invalidateAll();
}}>invalidateAll</button
>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<script>
import { page, navigating, updated, getStores } from '$app/stores';
let { navigating: navigatingStore, page: pageStore, updated: updatedStore } = getStores();
updated.check();
</script>

<p>Directly importing</p>
<pre>{JSON.stringify($page, null, 2)}</pre>
<pre>{JSON.stringify($navigating, null, 2)}</pre>
<pre>{JSON.stringify($updated, null, 2)}</pre>

<p>With getStores</p>
<pre>{JSON.stringify($pageStore, null, 2)}</pre>
<pre>{JSON.stringify($navigatingStore, null, 2)}</pre>
<pre>{JSON.stringify($updatedStore, null, 2)}</pre>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { expect, fn, within } from '@storybook/test';
import Forms from './Forms.svelte';

export default {
title: 'stories/sveltekit/modules/forms',
component: Forms,
tags: ['autodocs'],
};

const enhance = fn();

export const Enhance = {
async play({ canvasElement }) {
const canvas = within(canvasElement);
const button = canvas.getByText('enhance');
button.click();
expect(enhance).toHaveBeenCalled();
},
parameters: {
sveltekit_experimental: {
forms: {
enhance,
},
},
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { expect, fn, within } from '@storybook/test';
import Hrefs from './Hrefs.svelte';

export default {
title: 'stories/sveltekit/modules/hrefs',
component: Hrefs,
tags: ['autodocs'],
};

export const DefaultActions = {
async play({ canvasElement }) {
const canvas = within(canvasElement);
// eslint-disable-next-line no-undef
const initialUrl = window.location.toString();

const basicHref = canvas.getByText('/basic-href');
basicHref.click();

const complexHref = canvas.getByText(
'/deep/nested/link?with=true&multiple-params=200#and-an-id'
);
complexHref.click();

// eslint-disable-next-line no-undef
const finalUrl = window.location.toString();
expect(finalUrl).toBe(initialUrl);
},
};

const basicStringMatch = fn();
const noMatch = fn();
const exactStringMatch = fn();
const regexMatch = fn();

export const Callbacks = {
parameters: {
sveltekit_experimental: {
hrefs: {
'/basic-href': basicStringMatch,
'/basic': noMatch,
'/deep/nested/link?with=true&multiple-params=200#and-an-id': exactStringMatch,
'nested/link\\?with': { callback: regexMatch, asRegex: true },
},
},
},
play: async (ctx) => {
await DefaultActions.play(ctx);
expect(basicStringMatch).toHaveBeenCalledTimes(1);
expect(noMatch).not.toHaveBeenCalled();
expect(exactStringMatch).toHaveBeenCalledTimes(1);
expect(regexMatch).toHaveBeenCalledTimes(1);
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { expect, fn, within } from '@storybook/test';
import Navigation from './Navigation.svelte';

export default {
title: 'stories/sveltekit/modules/navigation',
component: Navigation,
tags: ['autodocs'],
};

const goto = fn();

export const Goto = {
async play({ canvasElement }) {
const canvas = within(canvasElement);
const button = canvas.getByText('goto');
button.click();
expect(goto).toHaveBeenCalledWith('/storybook-goto');
},
parameters: {
sveltekit_experimental: {
navigation: {
goto,
},
},
},
};

export const DefaultActions = {};

const invalidate = fn();

export const Invalidate = {
async play({ canvasElement }) {
const canvas = within(canvasElement);
const button = canvas.getByText('invalidate', { exact: true });
button.click();
expect(invalidate).toHaveBeenCalledWith('/storybook-invalidate');
},
parameters: {
sveltekit_experimental: {
navigation: {
invalidate,
},
},
},
};

const invalidateAll = fn();

export const InvalidateAll = {
async play({ canvasElement }) {
const canvas = within(canvasElement);
const button = canvas.getByText('invalidateAll');
button.click();
expect(invalidateAll).toHaveBeenCalledWith();
},
parameters: {
sveltekit_experimental: {
navigation: {
invalidateAll,
},
},
},
};

const afterNavigateFn = fn();

export const AfterNavigate = {
async play() {
expect(afterNavigateFn).toHaveBeenCalledWith({ test: 'passed' });
},
args: {
afterNavigateFn,
},
parameters: {
sveltekit_experimental: {
navigation: {
afterNavigate: {
test: 'passed',
},
},
},
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import Stores from './Stores.svelte';

export default {
title: 'stories/sveltekit/modules/stores',
component: Stores,
tags: ['autodocs'],
};

export const AllUndefined = {};

export const PageStore = {
parameters: {
sveltekit_experimental: {
stores: {
page: {
data: {
test: 'passed',
},
},
},
},
},
};

export const NavigatingStore = {
parameters: {
sveltekit_experimental: {
stores: {
navigating: {
route: {
id: '/storybook',
},
},
},
},
},
};

export const UpdatedStore = {
parameters: {
sveltekit_experimental: {
stores: {
updated: true,
},
},
},
};

export const PageAndNavigatingStore = {
parameters: {
sveltekit_experimental: {
stores: {
page: {
data: {
test: 'passed',
},
},
navigating: {
route: {
id: '/storybook',
},
},
},
},
},
};

export const PageAndUpdatedStore = {
parameters: {
sveltekit_experimental: {
stores: {
page: {
data: {
test: 'passed',
},
},
updated: true,
},
},
},
};

export const NavigatingAndUpdatedStore = {
parameters: {
sveltekit_experimental: {
stores: {
navigating: {
route: {
id: '/storybook',
},
},
updated: true,
},
},
},
};

export const AllThreeStores = {
parameters: {
sveltekit_experimental: {
stores: {
page: {
data: {
test: 'passed',
},
},
navigating: {
route: {
id: '/storybook',
},
},
updated: true,
},
},
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import ButtonTypescript from './ButtonTypeScript.svelte';

export default {
title: 'stories/renderers/svelte/ts-docs',
component: ButtonTypescript,
args: {
primary: true,
},
tags: ['autodocs'],
};

export const Primary = {};

0 comments on commit 7192c90

Please sign in to comment.