Skip to content

Commit

Permalink
Use originalStoryFunction as that exists in all renderers
Browse files Browse the repository at this point in the history
  • Loading branch information
tmeasday committed Jun 1, 2023
1 parent 9068e31 commit 6822e4d
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions code/lib/store/template/stories/decorators.stories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { global as globalThis } from '@storybook/global';
import type {
ArgsStoryFn,
PartialStoryFn,
PlayFunctionContext,
StoryContext,
} from '@storybook/types';
import { within } from '@storybook/testing-library';
import { expect } from '@storybook/jest';
import { useEffect } from '@storybook/preview-api';
import { STORY_ARGS_UPDATED, UPDATE_STORY_ARGS, RESET_STORY_ARGS } from '@storybook/core-events';

export default {
component: globalThis.Components.Pre,
parameters: { useProjectDecorator: true },
decorators: [
(storyFn: PartialStoryFn, context: StoryContext) =>
storyFn({ args: { ...context.args, text: `component ${context.args['text']}` } }),
],
};

export const Inheritance = {
decorators: [
(storyFn: PartialStoryFn, context: StoryContext) =>
storyFn({ args: { ...context.args, text: `story ${context.args['text']}` } }),
],
args: {
text: 'starting',
},
play: async ({ canvasElement }: PlayFunctionContext<any>) => {
const canvas = within(canvasElement);
await expect(canvas.getByTestId('pre').innerText).toEqual('story component project starting');
},
};

export const Hooks = {
decorators: [
// decorator that uses hooks
(storyFn: PartialStoryFn, context: StoryContext) => {
useEffect(() => {});
return storyFn({ args: { ...context.args, text: `story ${context.args['text']}` } });
},
// conditional decorator, runs before the above
(storyFn: PartialStoryFn, context: StoryContext) =>
context.args.condition
? storyFn()
: (context.originalStoryFn as ArgsStoryFn)(context.args, context),
],
args: {
text: 'text',
condition: true,
},
play: async ({ id, args }: PlayFunctionContext<any>) => {
const channel = globalThis.__STORYBOOK_ADDONS_CHANNEL__;
await channel.emit(RESET_STORY_ARGS, { storyId: id });
await new Promise((resolve) => channel.once(STORY_ARGS_UPDATED, resolve));

await channel.emit(UPDATE_STORY_ARGS, {
storyId: id,
updatedArgs: { condition: !args.condition },
});
await new Promise((resolve) => channel.once(STORY_ARGS_UPDATED, resolve));
},
};

0 comments on commit 6822e4d

Please sign in to comment.