Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test: Add args to mount in react, svelte, and vue renderers #28385

Merged
merged 12 commits into from
Jul 3, 2024
Merged
1 change: 1 addition & 0 deletions code/lib/types/src/modules/csf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export type {
ArgTypes,
ArgTypesEnhancer,
BaseAnnotations,
Canvas,
ComponentAnnotations,
ComponentId,
ComponentTitle,
Expand Down
1 change: 1 addition & 0 deletions code/renderers/react/src/entry-preview.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export const parameters: {} = { renderer: 'react' };
export { render } from './render';
export { renderToCanvas } from './renderToCanvas';
export { mount } from './mount';
11 changes: 11 additions & 0 deletions code/renderers/react/src/mount.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { type StoryContext, type ReactRenderer } from './public-types';
import type { BaseAnnotations } from '@storybook/types';

export const mount: BaseAnnotations<ReactRenderer>['mount'] =
(context: StoryContext) => async (ui) => {
if (ui != null) {
context.originalStoryFn = () => ui;
kasperpeulen marked this conversation as resolved.
Show resolved Hide resolved
}
await context.renderToCanvas();
return context.canvas;
};
2 changes: 1 addition & 1 deletion code/renderers/react/src/public-types.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ it('Infer mock function given to args in meta.', () => {

const Basic: Story = {
play: async ({ args, mount }) => {
const canvas = await mount();
const canvas = await mount(<TestButton {...args} />);
expectTypeOf(canvas).toEqualTypeOf<Canvas>();
expectTypeOf(args.onClick).toEqualTypeOf<Mock<[], void>>();
expectTypeOf(args.onRender).toEqualTypeOf<() => JSX.Element>();
Expand Down
3 changes: 2 additions & 1 deletion code/renderers/react/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import type { ComponentType } from 'react';
import type { WebRenderer } from '@storybook/types';
import type { Canvas, WebRenderer } from '@storybook/types';

export type { RenderContext, StoryContext } from '@storybook/types';

export interface ReactRenderer extends WebRenderer {
component: ComponentType<this['T']>;
storyResult: StoryFnReactReturnType;
mount: (ui?: JSX.Element) => Promise<Canvas>;
}

export interface ShowErrorArgs {
Expand Down
1 change: 1 addition & 0 deletions code/renderers/svelte/src/entry-preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export const parameters: {} = { renderer: 'svelte' };

export { render, renderToCanvas } from './render';
export { decorateStory as applyDecorators } from './decorators';
export { mount } from './mount';
15 changes: 15 additions & 0 deletions code/renderers/svelte/src/mount.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { type StoryContext, type SvelteRenderer } from './public-types';
import { type BaseAnnotations } from '@storybook/types';

export const mount: BaseAnnotations<SvelteRenderer>['mount'] = (context: StoryContext) => {
return async (Component, options) => {
if (Component) {
context.originalStoryFn = () => ({
Component: Component,
props: options?.props ?? options,
});
}
await context.renderToCanvas();
return context.canvas;
};
};
7 changes: 6 additions & 1 deletion code/renderers/svelte/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { StoryContext as StoryContextBase, WebRenderer } from '@storybook/types';
import type { Canvas, StoryContext as StoryContextBase, WebRenderer } from '@storybook/types';
import type { ComponentConstructorOptions, ComponentEvents, SvelteComponent } from 'svelte';

export type StoryContext = StoryContextBase<SvelteRenderer>;
Expand Down Expand Up @@ -37,6 +37,11 @@ export interface SvelteRenderer<C extends SvelteComponent = SvelteComponent> ext
storyResult: this['T'] extends Record<string, any>
? SvelteStoryResult<this['T'], ComponentEvents<C>>
: SvelteStoryResult;

mount: (
Component?: ComponentType,
options?: Record<string, any> & { props: Record<string, any> }
) => Promise<Canvas>;
}

export interface SvelteStoryResult<
Expand Down
1 change: 1 addition & 0 deletions code/renderers/vue3/src/entry-preview.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export const parameters: {} = { renderer: 'vue3' };
export { render, renderToCanvas } from './render';
export { decorateStory as applyDecorators } from './decorateStory';
export { mount } from './mount';
13 changes: 13 additions & 0 deletions code/renderers/vue3/src/mount.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { type StoryContext, type VueRenderer } from './public-types';
import { h } from 'vue';
import { type BaseAnnotations } from '@storybook/types';

export const mount: BaseAnnotations<VueRenderer>['mount'] = (context: StoryContext) => {
return async (Component, options) => {
if (Component) {
context.originalStoryFn = () => () => h(Component, options?.props, options?.slots);
yannbf marked this conversation as resolved.
Show resolved Hide resolved
}
await context.renderToCanvas();
return context.canvas;
};
};
11 changes: 10 additions & 1 deletion code/renderers/vue3/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { type StoryContext as StoryContextBase, type WebRenderer } from '@storybook/types';
import {
type Canvas,
type StoryContext as StoryContextBase,
type WebRenderer,
} from '@storybook/types';
import type { App, ConcreteComponent } from 'vue';

export type { RenderContext } from '@storybook/types';
Expand All @@ -21,4 +25,9 @@ export interface VueRenderer extends WebRenderer {
// Try not omitting, and check the type errros in the test file, if you want to learn more.
component: Omit<ConcreteComponent<this['T']>, 'props'>;
storyResult: StoryFnVueReturnType;

mount: (
Component?: StoryFnVueReturnType,
options?: { props: Record<string, any>; slots: Record<string, any> }
) => Promise<Canvas>;
}