diff --git a/examples/Button.stories.svelte b/examples/Button.stories.svelte index 5c37cd97..0b546afb 100644 --- a/examples/Button.stories.svelte +++ b/examples/Button.stories.svelte @@ -7,6 +7,7 @@ } from '@storybook/addon-svelte-csf'; import { fn } from '@storybook/test'; + import type { Snippet } from 'svelte' import Button from './components/Button.svelte'; const onclickFn = fn().mockName('onclick'); @@ -20,6 +21,7 @@ tags: ['autodocs'], args: { onclick: onclickFn, + children: 'Click me' as unknown as Snippet<[]> }, argTypes: { backgroundColor: { control: 'color' }, @@ -37,7 +39,7 @@ {#snippet template(args: Args, context: StoryContext)} - + {/snippet} diff --git a/src/runtime/create-runtime-stories.ts b/src/runtime/create-runtime-stories.ts index 89947402..bfddb5b0 100644 --- a/src/runtime/create-runtime-stories.ts +++ b/src/runtime/create-runtime-stories.ts @@ -68,7 +68,22 @@ export const createRuntimeStories = (Stories: Component, meta: Meta) => { * The 'play' function should be delegated to the real play Story function * in order to be run into the component scope. */ - storyObj.play = (storyContext) => { + function params(fn) { + return ( + fn + .toString() + .match(/[^(]*\(([^)]*)/) + ?.slice(1) ?? [] + ); + } + const isMounting = + params(storyObj.play).filter((p) => /\{.*,?\s*mount\s*,?.*\}/.test(p)).length != 0; + storyObj.play = isMounting + ? function ({ mount }) { + return playDelegator(arguments[0]); + } + : playDelegator; + function playDelegator(storyContext) { const delegate = storyContext.playFunction?.__play; if (delegate) { @@ -77,7 +92,7 @@ export const createRuntimeStories = (Stories: Component, meta: Meta) => { // @ts-expect-error WARN: It should not affect user perspective- the problem lies in this addon's type `SvelteRenderer` missing type constrains or default generic parameter type return play(storyContext); - }; + } } stories[exportName] = storyObj; diff --git a/tests/stories/Mountable.stories.svelte b/tests/stories/Mountable.stories.svelte new file mode 100644 index 00000000..66197201 --- /dev/null +++ b/tests/stories/Mountable.stories.svelte @@ -0,0 +1,55 @@ + + + + { + const canvas = within(canvasElement); + expect(canvas.getByText('Mountable')).toBeInTheDocument(); + }} +> + + + { + console.log('Pre'); + await mount(); + console.log('Post'); + }} +> + + + { + const canvas = within(canvasElement); + + // passing test does no show pre mount, so we test after mount + const preMountLabel = canvas.queryByText('Mountable'); + + await mount(); + + expect(preMountLabel).toBeNull(); // Here so shows in Component Test panel + + expect(canvas.getByText('Mountable')).toBeInTheDocument(); + expect(args.onMounted).toHaveBeenCalled(); + }} +> diff --git a/tests/stories/Mountable.svelte b/tests/stories/Mountable.svelte new file mode 100644 index 00000000..1a395d6d --- /dev/null +++ b/tests/stories/Mountable.svelte @@ -0,0 +1,20 @@ + + +
+ {text} +