generated from storybookjs/addon-kit
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Finish feature of extracting raw source of Story
source.code
... and added tests!
- Loading branch information
Showing
9 changed files
with
475 additions
and
144 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,249 @@ | ||
import { describe, it } from 'vitest'; | ||
|
||
import { getStoryChildrenRawSource } from './children.js'; | ||
|
||
import { getSvelteAST } from '../../ast.js'; | ||
import { extractSvelteASTNodes } from '../../extract/svelte/nodes.js'; | ||
|
||
describe(getStoryChildrenRawSource.name, () => { | ||
describe('When a `<Story />` is a self-closing tag...', () => { | ||
it('works when `children` attribute was provided with a reference to snippet at the root of fragment', async ({ | ||
expect, | ||
}) => { | ||
const code = ` | ||
<script context="module"> | ||
import { defineMeta } from "@storybook/addon-svelte-csf"; | ||
import SampleComponent from "./SampleComponent.svelte"; | ||
const { Story } = defineMeta({ | ||
component: SampleComponent, | ||
}); | ||
</script> | ||
{#snippet template(args)} | ||
<SomeComponent {...args} /> | ||
{/snippet} | ||
<Story name="Default" children={template} /> | ||
`; | ||
const ast = getSvelteAST({ code }); | ||
const svelteASTNodes = await extractSvelteASTNodes({ ast }); | ||
const { storyComponents } = svelteASTNodes; | ||
const component = storyComponents[0].component; | ||
const rawSource = getStoryChildrenRawSource({ | ||
component, | ||
svelteASTNodes, | ||
originalCode: code, | ||
}); | ||
|
||
expect(rawSource).toBe('<SomeComponent {...args} />'); | ||
}); | ||
|
||
it('works when `setTemplate` was used correctly in the instance tag', async ({ expect }) => { | ||
const code = ` | ||
<script context="module"> | ||
import { defineMeta, setTemplate } from "@storybook/addon-svelte-csf"; | ||
import SampleComponent from "./SampleComponent.svelte"; | ||
const { Story } = defineMeta({ | ||
component: SampleComponent, | ||
}); | ||
</script> | ||
<script> | ||
setTemplate(template); | ||
</script> | ||
{#snippet template(args)} | ||
<SomeComponent {...args} /> | ||
{/snippet} | ||
<Story name="Default" /> | ||
`; | ||
const ast = getSvelteAST({ code }); | ||
const svelteASTNodes = await extractSvelteASTNodes({ ast }); | ||
const { storyComponents } = svelteASTNodes; | ||
const component = storyComponents[0].component; | ||
const rawSource = getStoryChildrenRawSource({ | ||
component, | ||
svelteASTNodes, | ||
originalCode: code, | ||
}); | ||
|
||
expect(rawSource).toBe('<SomeComponent {...args} />'); | ||
}); | ||
|
||
it('works implicit `children` attribute takes precedence over `setTemplate`', async ({ | ||
expect, | ||
}) => { | ||
const code = ` | ||
<script context="module"> | ||
import { defineMeta, setTemplate } from "@storybook/addon-svelte-csf"; | ||
import SampleComponent from "./SampleComponent.svelte"; | ||
const { Story } = defineMeta({ | ||
component: SampleComponent, | ||
}); | ||
</script> | ||
<script> | ||
setTemplate(template); | ||
</script> | ||
{#snippet templateForSetTemplate(args)} | ||
<SomeComponent wins="setTemplate" {...args} /> | ||
{/snippet} | ||
{#snippet templateForChildren(args)} | ||
<SomeComponent wins="childrenAttribute" {...args} /> | ||
{/snippet} | ||
<Story name="Default" children={templateForChildren} /> | ||
`; | ||
const ast = getSvelteAST({ code }); | ||
const svelteASTNodes = await extractSvelteASTNodes({ ast }); | ||
const { storyComponents } = svelteASTNodes; | ||
const component = storyComponents[0].component; | ||
const rawSource = getStoryChildrenRawSource({ | ||
component, | ||
svelteASTNodes, | ||
originalCode: code, | ||
}); | ||
|
||
expect(rawSource).toBe(`<SomeComponent wins="childrenAttribute" {...args} />`); | ||
}); | ||
|
||
it('works when no `setTemplate`, no `children` attribute, just a story', async ({ expect }) => { | ||
const code = ` | ||
<script context="module"> | ||
import { defineMeta } from "@storybook/addon-svelte-csf"; | ||
import SampleComponent from "./SampleComponent.svelte"; | ||
const { Story } = defineMeta({ | ||
component: SampleComponent, | ||
}); | ||
</script> | ||
<Story name="Default" /> | ||
`; | ||
const ast = getSvelteAST({ code }); | ||
const svelteASTNodes = await extractSvelteASTNodes({ ast }); | ||
const { storyComponents } = svelteASTNodes; | ||
const component = storyComponents[0].component; | ||
const rawSource = getStoryChildrenRawSource({ | ||
component, | ||
svelteASTNodes, | ||
originalCode: code, | ||
}); | ||
|
||
expect(rawSource).toBe(`<SampleComponent {...args} />`); | ||
}); | ||
}); | ||
|
||
describe('When a `<Story />` is NOT a self-closing tag...', () => { | ||
it('works when a static children content provided', async ({ expect }) => { | ||
const code = ` | ||
<script context="module"> | ||
import { defineMeta } from "@storybook/addon-svelte-csf"; | ||
import SampleComponent from "./SampleComponent.svelte"; | ||
const { Story } = defineMeta({ | ||
component: SampleComponent, | ||
}); | ||
</script> | ||
<Story name="Default"> | ||
<SomeComponent foo="bar" /> | ||
</Story> | ||
`; | ||
const ast = getSvelteAST({ code }); | ||
const svelteASTNodes = await extractSvelteASTNodes({ ast }); | ||
const { storyComponents } = svelteASTNodes; | ||
const component = storyComponents[0].component; | ||
const rawSource = getStoryChildrenRawSource({ | ||
component, | ||
svelteASTNodes, | ||
originalCode: code, | ||
}); | ||
|
||
expect(rawSource).toBe(`<SomeComponent foo="bar" />`); | ||
}); | ||
|
||
it("works when a `children` svelte's snippet block used inside", async ({ expect }) => { | ||
const code = ` | ||
<script context="module"> | ||
import { defineMeta } from "@storybook/addon-svelte-csf"; | ||
import SampleComponent from "./SampleComponent.svelte"; | ||
const { Story } = defineMeta({ | ||
component: SampleComponent, | ||
}); | ||
</script> | ||
<Story name="Default"> | ||
{#snippet children(args)} | ||
<SomeComponent {...args} /> | ||
{/snippet} | ||
</Story> | ||
`; | ||
const ast = getSvelteAST({ code }); | ||
const svelteASTNodes = await extractSvelteASTNodes({ ast }); | ||
const { storyComponents } = svelteASTNodes; | ||
const component = storyComponents[0].component; | ||
const rawSource = getStoryChildrenRawSource({ | ||
component, | ||
svelteASTNodes, | ||
originalCode: code, | ||
}); | ||
|
||
expect(rawSource).toBe(`<SomeComponent {...args} />`); | ||
}); | ||
|
||
it("inner `<Story>`'s children content takes precedence over `setTemplate`", async ({ | ||
expect, | ||
}) => { | ||
const code = ` | ||
<script context="module"> | ||
import { defineMeta, setTemplate } from "@storybook/addon-svelte-csf"; | ||
import SampleComponent from "./SampleComponent.svelte"; | ||
const { Story } = defineMeta({ | ||
component: SampleComponent, | ||
}); | ||
</script> | ||
<script> | ||
setTemplate(template); | ||
</script> | ||
{#snippet templateForSetTemplate(args)} | ||
<SomeComponent wins="setTemplate" {...args} /> | ||
{/snippet} | ||
<Story name="Default"> | ||
{#snippet children(args)} | ||
<SomeComponent wins="children" {...args} /> | ||
{/snippet} | ||
</Story> | ||
`; | ||
const ast = getSvelteAST({ code }); | ||
const svelteASTNodes = await extractSvelteASTNodes({ ast }); | ||
const { storyComponents } = svelteASTNodes; | ||
const component = storyComponents[0].component; | ||
const rawSource = getStoryChildrenRawSource({ | ||
component, | ||
svelteASTNodes, | ||
originalCode: code, | ||
}); | ||
|
||
expect(rawSource).toBe(`<SomeComponent wins="children" {...args} />`); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.