mirror of
https://github.com/storybookjs/storybook.git
synced 2025-04-09 00:19:13 +08:00
fix types and tests
This commit is contained in:
parent
80ccd471c3
commit
f151fef10c
@ -1,3 +1,4 @@
|
|||||||
|
// @ts-expect-error this is just a mock file
|
||||||
import { config } from '#.storybook/preview';
|
import { config } from '#.storybook/preview';
|
||||||
|
|
||||||
const meta = config.meta({
|
const meta = config.meta({
|
||||||
|
@ -17,7 +17,9 @@ const meta = config.meta({
|
|||||||
argTypes: {
|
argTypes: {
|
||||||
backgroundColor: { control: 'color' },
|
backgroundColor: { control: 'color' },
|
||||||
},
|
},
|
||||||
args: {},
|
args: {
|
||||||
|
children: 'Children coming from meta args',
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
export const CSF2Secondary = meta.story({
|
export const CSF2Secondary = meta.story({
|
||||||
@ -61,7 +63,7 @@ export const CSF2StoryWithLocale = meta.story({
|
|||||||
});
|
});
|
||||||
|
|
||||||
export const CSF2StoryWithParamsAndDecorator = meta.story({
|
export const CSF2StoryWithParamsAndDecorator = meta.story({
|
||||||
render: (args: any) => {
|
render: (args) => {
|
||||||
return <Button {...args} />;
|
return <Button {...args} />;
|
||||||
},
|
},
|
||||||
args: {
|
args: {
|
||||||
@ -87,7 +89,7 @@ export const CSF3Button = meta.story({
|
|||||||
|
|
||||||
export const CSF3ButtonWithRender = meta.story({
|
export const CSF3ButtonWithRender = meta.story({
|
||||||
...CSF3Button,
|
...CSF3Button,
|
||||||
render: (args: any) => (
|
render: (args) => (
|
||||||
<div>
|
<div>
|
||||||
<p data-testid="custom-render">I am a custom render function</p>
|
<p data-testid="custom-render">I am a custom render function</p>
|
||||||
<Button {...args} />
|
<Button {...args} />
|
||||||
@ -139,6 +141,7 @@ export const CSF3InputFieldFilled = meta.story({
|
|||||||
const mockFn = fn();
|
const mockFn = fn();
|
||||||
export const LoaderStory = meta.story({
|
export const LoaderStory = meta.story({
|
||||||
args: {
|
args: {
|
||||||
|
// @ts-expect-error TODO: add a way to provide custom args/argTypes
|
||||||
mockFn,
|
mockFn,
|
||||||
},
|
},
|
||||||
loaders: [
|
loaders: [
|
||||||
@ -165,13 +168,16 @@ export const LoaderStory = meta.story({
|
|||||||
|
|
||||||
export const MountInPlayFunction = meta.story({
|
export const MountInPlayFunction = meta.story({
|
||||||
args: {
|
args: {
|
||||||
|
// @ts-expect-error TODO: add a way to provide custom args/argTypes
|
||||||
mockFn: fn(),
|
mockFn: fn(),
|
||||||
},
|
},
|
||||||
play: async ({ args, mount, context }: any) => {
|
play: async ({ args, mount, context }) => {
|
||||||
// equivalent of loaders
|
// equivalent of loaders
|
||||||
const loadedData = await Promise.resolve('loaded data');
|
const loadedData = await Promise.resolve('loaded data');
|
||||||
|
// @ts-expect-error TODO: add a way to provide custom args/argTypes
|
||||||
mocked(args.mockFn).mockReturnValueOnce('mockFn return value');
|
mocked(args.mockFn).mockReturnValueOnce('mockFn return value');
|
||||||
// equivalent of render
|
// equivalent of render
|
||||||
|
// @ts-expect-error TODO: add a way to provide custom args/argTypes
|
||||||
const data = args.mockFn('render');
|
const data = args.mockFn('render');
|
||||||
// TODO refactor this in the mount args PR
|
// TODO refactor this in the mount args PR
|
||||||
context.originalStoryFn = () => (
|
context.originalStoryFn = () => (
|
||||||
@ -183,6 +189,7 @@ export const MountInPlayFunction = meta.story({
|
|||||||
await mount();
|
await mount();
|
||||||
|
|
||||||
// equivalent of play
|
// equivalent of play
|
||||||
|
// @ts-expect-error TODO: add a way to provide custom args/argTypes
|
||||||
expect(args.mockFn).toHaveBeenCalledWith('render');
|
expect(args.mockFn).toHaveBeenCalledWith('render');
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
@ -195,13 +202,16 @@ export const MountInPlayFunctionThrow = meta.story({
|
|||||||
|
|
||||||
export const WithActionArg = meta.story({
|
export const WithActionArg = meta.story({
|
||||||
args: {
|
args: {
|
||||||
|
// @ts-expect-error TODO: add a way to provide custom args/argTypes
|
||||||
someActionArg: action('some-action-arg'),
|
someActionArg: action('some-action-arg'),
|
||||||
},
|
},
|
||||||
render: (args: any) => {
|
render: (args) => {
|
||||||
|
// @ts-expect-error TODO: add a way to provide custom args/argTypes
|
||||||
args.someActionArg('in render');
|
args.someActionArg('in render');
|
||||||
return (
|
return (
|
||||||
<button
|
<button
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
|
// @ts-expect-error TODO: add a way to provide custom args/argTypes
|
||||||
args.someActionArg('on click');
|
args.someActionArg('on click');
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
@ -216,6 +226,7 @@ export const WithActionArg = meta.story({
|
|||||||
|
|
||||||
export const WithActionArgType = meta.story({
|
export const WithActionArgType = meta.story({
|
||||||
argTypes: {
|
argTypes: {
|
||||||
|
// @ts-expect-error TODO: add a way to provide custom args/argTypes
|
||||||
someActionArg: {
|
someActionArg: {
|
||||||
action: true,
|
action: true,
|
||||||
},
|
},
|
||||||
|
@ -51,7 +51,9 @@ exports[`Renders CSF3ButtonWithRender story 1`] = `
|
|||||||
<button
|
<button
|
||||||
class="storybook-button storybook-button--medium storybook-button--secondary"
|
class="storybook-button storybook-button--medium storybook-button--secondary"
|
||||||
type="button"
|
type="button"
|
||||||
/>
|
>
|
||||||
|
Children coming from meta args
|
||||||
|
</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</body>
|
</body>
|
||||||
|
@ -28,8 +28,9 @@ const HooksStory = composeStory(
|
|||||||
const projectAnnotations = setProjectAnnotations([]);
|
const projectAnnotations = setProjectAnnotations([]);
|
||||||
|
|
||||||
// example with composeStories, returns an object with all stories composed with args/decorators
|
// example with composeStories, returns an object with all stories composed with args/decorators
|
||||||
const { CSF3Primary, LoaderStory, MountInPlayFunction, MountInPlayFunctionThrow } =
|
// @ts-expect-error TODO: add a way to provide custom args/argTypes
|
||||||
composeStories(ButtonStories);
|
// eslint-disable-next-line prettier/prettier
|
||||||
|
const { CSF3Primary, LoaderStory, MountInPlayFunction, MountInPlayFunctionThrow } = composeStories(ButtonStories);
|
||||||
const { ThrowsError } = composeStories(ComponentWithErrorStories);
|
const { ThrowsError } = composeStories(ComponentWithErrorStories);
|
||||||
|
|
||||||
beforeAll(async () => {
|
beforeAll(async () => {
|
||||||
@ -83,8 +84,8 @@ describe('renders', () => {
|
|||||||
expect(screen.getByTestId('loaded-data').textContent).toEqual('loaded data');
|
expect(screen.getByTestId('loaded-data').textContent).toEqual('loaded data');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should throw an error in play function', () => {
|
it('should throw an error in play function', async () => {
|
||||||
expect(() => MountInPlayFunctionThrow.run()).rejects.toThrowError('Error thrown in play');
|
await expect(() => MountInPlayFunctionThrow.run()).rejects.toThrowError('Error thrown in play');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should call and compose loaders data', async () => {
|
it('should call and compose loaders data', async () => {
|
||||||
@ -136,6 +137,8 @@ describe('projectAnnotations', () => {
|
|||||||
ButtonStories.CSF3Primary.meta.annotations,
|
ButtonStories.CSF3Primary.meta.annotations,
|
||||||
addonActionsPreview as ProjectAnnotations<ReactRenderer>
|
addonActionsPreview as ProjectAnnotations<ReactRenderer>
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// @ts-expect-error TODO: add a way to provide custom args/argTypes
|
||||||
expect(Story.args.someActionArg).toHaveProperty('isAction', true);
|
expect(Story.args.someActionArg).toHaveProperty('isAction', true);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@ -189,7 +192,9 @@ describe('CSF3', () => {
|
|||||||
const input = screen.getByTestId('input') as HTMLInputElement;
|
const input = screen.getByTestId('input') as HTMLInputElement;
|
||||||
expect(input.value).toEqual('Hello world!');
|
expect(input.value).toEqual('Hello world!');
|
||||||
} finally {
|
} finally {
|
||||||
document.body.removeChild(divElement);
|
if (divElement) {
|
||||||
|
document.body.removeChild(divElement);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -237,7 +242,9 @@ describe('ComposeStories types', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// @ts-expect-error TODO: fix the types for this
|
||||||
const testCases = Object.values(composeStories(ButtonStories)).map(
|
const testCases = Object.values(composeStories(ButtonStories)).map(
|
||||||
|
// @ts-expect-error TODO: fix the types for this
|
||||||
(Story) => [Story.storyName, Story] as [string, typeof Story]
|
(Story) => [Story.storyName, Story] as [string, typeof Story]
|
||||||
);
|
);
|
||||||
it.each(testCases)('Renders %s story', async (_storyName, Story) => {
|
it.each(testCases)('Renders %s story', async (_storyName, Story) => {
|
||||||
@ -245,6 +252,7 @@ it.each(testCases)('Renders %s story', async (_storyName, Story) => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// @ts-expect-error TODO: fix the types for this
|
||||||
await Story.run();
|
await Story.run();
|
||||||
expect(document.body).toMatchSnapshot();
|
expect(document.body).toMatchSnapshot();
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user