Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
xeho91 committed Jul 21, 2024
1 parent b48bfe4 commit fdebf5c
Showing 1 changed file with 63 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import pkg from '@storybook/addon-svelte-csf/package.json' with { type: 'json' };
import type { Comment, Component } from 'svelte/compiler';
import { print } from 'svelte-ast-print';
import { describe, it } from 'vitest';
Expand All @@ -11,7 +10,7 @@ describe(transformComponentMetaToDefineMeta.name, () => {
it('works with a simple example', async ({ expect }) => {
const code = `
<script context="module">
import { Meta } from "${pkg.name}";
import { Meta } from "@storybook/addon-svelte-csf";
</script>
<Meta title="Atoms/Button" component={Button} />
Expand All @@ -32,7 +31,7 @@ describe(transformComponentMetaToDefineMeta.name, () => {
it('leading comments are included', async ({ expect }) => {
const code = `
<script context="module">
import { Meta } from "${pkg.name}";
import { Meta } from "@storybook/addon-svelte-csf";
</script>
<!-- This is a description for the **Button** component stories. -->
Expand All @@ -54,4 +53,65 @@ describe(transformComponentMetaToDefineMeta.name, () => {
"/** This is a description for the **Button** component stories. */ const { Story } = defineMeta({ title: "Atoms/Button", component: Button });"
`);
});

it('supports <Meta> parameters with functions', async ({ expect }) => {
const code = `
<script context="module">
import { Meta } from "@storybook/addon-svelte-csf";
import WithParameters from './WithParameters.svelte';
</script>
<Meta component={WithParameters} parameters={{
docs: {
source: {
transform: (code) => {
return code + 'transformed';
}
}
}
}} />
`;
const component = await parseAndExtractSvelteNode<Component>(code, 'Component');

expect(
print(
transformComponentMetaToDefineMeta({ component })
)
).toMatchInlineSnapshot(`
"const { Story } = defineMeta({
component: WithParameters,
parameters: {
docs: {
source: {
transform: (code) => {
return code + 'transformed';
}
}
}
}
});"
`);
});

it('supports <Meta> with parameters being referenced to variable in the instance tag', async ({ expect }) => {
const code = `
<script>
const parameters = { foo: 'bar' };
</script>
<Meta component={WithParameters} parameters={{ ...parameters, baz: 'yes'}} />
`;
const component = await parseAndExtractSvelteNode<Component>(code, 'Component');

expect(
print(
transformComponentMetaToDefineMeta({ component })
)
).toMatchInlineSnapshot(`
"const { Story } = defineMeta({
component: WithParameters,
parameters: { ...parameters, baz: 'yes' }
});"
`);
});
});

0 comments on commit fdebf5c

Please sign in to comment.