How to use boundStoryFunction method in storybook-root

Best JavaScript code snippet using storybook-root

decorators.ts

Source:decorators.ts Github

copy

Full Screen

1import type {2 DecoratorFunction,3 StoryContext,4 StoryContextUpdate,5 PartialStoryFn,6 LegacyStoryFn,7 AnyFramework,8} from '@storybook/csf';9export function decorateStory<TFramework extends AnyFramework>(10 storyFn: LegacyStoryFn<TFramework>,11 decorator: DecoratorFunction<TFramework>,12 bindWithContext: (storyFn: LegacyStoryFn<TFramework>) => PartialStoryFn<TFramework>13): LegacyStoryFn<TFramework> {14 // Bind the partially decorated storyFn so that when it is called it always knows about the story context,15 // no matter what it is passed directly. This is because we cannot guarantee a decorator will16 // pass the context down to the next decorated story in the chain.17 const boundStoryFunction = bindWithContext(storyFn);18 return (context) => decorator(boundStoryFunction, context);19}20type ContextStore<TFramework extends AnyFramework> = { value?: StoryContext<TFramework> };21/**22 * Currently StoryContextUpdates are allowed to have any key in the type.23 * However, you cannot overwrite any of the build-it "static" keys.24 *25 * @param inputContextUpdate StoryContextUpdate26 * @returns StoryContextUpdate27 */28export function sanitizeStoryContextUpdate({29 componentId,30 title,31 kind,32 id,33 name,34 story,35 parameters,36 initialArgs,37 argTypes,38 ...update39}: StoryContextUpdate = {}): StoryContextUpdate {40 return update;41}42export function defaultDecorateStory<TFramework extends AnyFramework>(43 storyFn: LegacyStoryFn<TFramework>,44 decorators: DecoratorFunction<TFramework>[]45): LegacyStoryFn<TFramework> {46 // We use a trick to avoid recreating the bound story function inside `decorateStory`.47 // Instead we pass it a context "getter", which is defined once (at "decoration time")48 // The getter reads a variable which is scoped to this call of `decorateStory`49 // (ie to this story), so there is no possibility of overlap.50 // This will break if you call the same story twice interleaved51 // (React might do it if you rendered the same story twice in the one ReactDom.render call, for instance)52 const contextStore: ContextStore<TFramework> = {};53 /**54 * When you call the story function inside a decorator, e.g.:55 *56 * ```jsx57 * <div>{storyFn({ foo: 'bar' })}</div>58 * ```59 *60 * This will override the `foo` property on the `innerContext`, which gets61 * merged in with the default context62 */63 const bindWithContext =64 (decoratedStoryFn: LegacyStoryFn<TFramework>): PartialStoryFn<TFramework> =>65 (update) => {66 contextStore.value = {67 ...contextStore.value,68 ...sanitizeStoryContextUpdate(update),69 };70 return decoratedStoryFn(contextStore.value);71 };72 const decoratedWithContextStore = decorators.reduce(73 (story, decorator) => decorateStory(story, decorator, bindWithContext),74 storyFn75 );76 return (context) => {77 contextStore.value = context;78 return decoratedWithContextStore(context); // Pass the context directly into the first decorator79 };...

Full Screen

Full Screen

decorateStory.ts

Source:decorateStory.ts Github

copy

Full Screen

1import {2 StoryContext,3 StoryContextUpdate,4 PartialStoryFn,5 LegacyStoryFn,6 DecoratorFunction,7} from './types';8const defaultContext: StoryContext = {9 id: 'unspecified',10 name: 'unspecified',11 kind: 'unspecified',12 parameters: {},13 args: {},14 argTypes: {},15 globals: {},16};17/**18 * When you call the story function inside a decorator, e.g.:19 *20 * ```jsx21 * <div>{storyFn({ foo: 'bar' })}</div>22 * ```23 *24 * This will override the `foo` property on the `innerContext`, which gets25 * merged in with the default context26 */27const bindWithContext =28 (storyFn: LegacyStoryFn, getStoryContext: () => StoryContext): PartialStoryFn =>29 // (NOTE: You cannot override the parameters key, it is fixed)30 ({ id, name, kind, parameters, ...contextUpdate }: StoryContextUpdate = {}) =>31 storyFn({ ...getStoryContext(), ...contextUpdate });32export const decorateStory = (33 storyFn: LegacyStoryFn,34 decorator: DecoratorFunction,35 getStoryContext: () => StoryContext36): LegacyStoryFn => {37 // Bind the partially decorated storyFn so that when it is called it always knows about the story context,38 // no matter what it is passed directly. This is because we cannot guarantee a decorator will39 // pass the context down to the next decorated story in the chain.40 const boundStoryFunction = bindWithContext(storyFn, getStoryContext);41 return (context: StoryContext) => decorator(boundStoryFunction, context);42};43export const defaultDecorateStory = (44 storyFn: LegacyStoryFn,45 decorators: DecoratorFunction[]46): LegacyStoryFn => {47 // We use a trick to avoid recreating the bound story function inside `decorateStory`.48 // Instead we pass it a context "getter", which is defined once (at "decoration time")49 // The getter reads a variable which is scoped to this call of `decorateStory`50 // (ie to this story), so there is no possibility of overlap.51 // This will break if you call the same story twice interleaved52 // (React might do it if you rendered the same story twice in the one ReactDom.render call, for instance)53 let contextStore: StoryContext;54 const decoratedWithContextStore = decorators.reduce(55 (story: LegacyStoryFn, decorator) => decorateStory(story, decorator, () => contextStore),56 storyFn57 );58 return (context = defaultContext) => {59 contextStore = context;60 return decoratedWithContextStore(context); // Pass the context directly into the first decorator61 };...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1import { boundStoryFunction } from 'storybook-root';2import { storiesOf } from '@storybook/react';3import { action } from '@storybook/addon-actions';4import { linkTo } from '@storybook/addon-links';5import { Welcome } from '@storybook/react/demo';6storiesOf('Welcome', module).add('to Storybook', () => <Welcome showApp={linkTo('Button')} />);7storiesOf('Button', module)8 .add('with text', () => <Button onClick={action('clicked')}>Hello Button</Button>)9 .add('with some emoji', () => (10 <Button onClick={action('clicked')}>11 ));12storiesOf('Test', module)13 .add('with text', () => <Test onClick={action('clicked')}>Hello Test</Test>)14 .add('with some emoji', () => (15 <Test onClick={action('clicked')}>16 ));17import { configure } from '@storybook/react';18const req = require.context('../src', true, /\.stories\.js$/);19function loadStories() {20 req.keys().forEach(filename => req(filename));21}22const boundStoryFunction = configure(loadStories, module);23export { boundStoryFunction };24import { boundStoryFunction } from 'storybook-root';25import { configure } from '@storybook/react';26import { setOptions } from '@storybook/addon-options';27setOptions({

Full Screen

Using AI Code Generation

copy

Full Screen

1import { boundStoryFunction } from 'storybook-root-decorator';2import { storiesOf } from '@storybook/react';3import { withKnobs, text, boolean } from '@storybook/addon-knobs';4import { withInfo } from '@storybook/addon-info';5import { withReadme } from 'storybook-readme';6import Readme from './README.md';7import { Component } from './Component';8const stories = storiesOf('Component', module);9stories.addDecorator(withKnobs);10stories.addDecorator(withInfo);11stories.addDecorator(withReadme(Readme));12stories.add('Default', boundStoryFunction((storyFn, context) => {13 const props = {14 title: text('Title', 'Default title'),15 content: text('Content', 'Default content'),16 isActive: boolean('isActive', true),17 };18 return storyFn(context, props);19}));20import React from 'react';21import PropTypes from 'prop-types';22export const Component = ({ title, content, isActive }) => (23 <h1>{title}</h1>24 <p>{content}</p>25 <p>{isActive ? 'Active' : 'Inactive'}</p>26);27Component.propTypes = {28};29![Component/Default](./__screenshots__/Component-Default.png)30import { Component } from 'component-name';31<Component title="Example title" content="Example content" isActive={false} />

Full Screen

Using AI Code Generation

copy

Full Screen

1import { configure } from '@storybook/react';2import { addDecorator } from '@storybook/react';3import { withContexts } from '@storybook/addon-contexts/react';4import { contexts } from './contexts';5addDecorator(withContexts(contexts));6const req = require.context('../src', true, /.stories.tsx$/);7function loadStories() {8 req.keys().forEach(filename => req(filename));9}10configure(loadStories, module);11import { contexts, Parameters } from '@storybook/addon-contexts';12import { create } from '@storybook/theming';13import { addons } from '@storybook/addons';14export const parameters = {15 backgrounds: {16 {17 },18 {19 },20 },21 options: {22 theme: create({23 }),24 },25};26export const globalTypes = {27 theme: {28 toolbar: {29 },30 },31};32 (Story, context) => {33 const { theme } = context.globals;34 return (35 <div style={{ backgroundColor: theme === 'light' ? 'white' : 'black' }}>36 );37 },38];39 {40 {41 props: {42 },43 },44 {45 props: {46 },47 },48 options: {49 },50 },51];

Full Screen

Using AI Code Generation

copy

Full Screen

1import { boundStoryFunction } from 'storybook-root-decorator'2const story = boundStoryFunction(() => <MyComponent />)3export default {4}5export const basic = () => <MyComponent />6import { configure, addDecorator } from '@storybook/react'7import { withKnobs } from '@storybook/addon-knobs'8import { addRootDecorator } from 'storybook-root-decorator'9addRootDecorator(withKnobs)10addDecorator((storyFn, context) => withKnobs(storyFn, context))11configure(require.context('../src', true, /\.stories\.js$/), module)12configure(require.context('../src', true, /\.stories\.js$/), module)13import { configure, addDecorator } from '@storybook/react'14import { withKnobs } from '@storybook/addon-knobs'15import { addRootDecorator } from 'storybook-root-decorator'16addRootDecorator(withKnobs)17addDecorator((storyFn, context) => withKnobs(storyFn, context))18configure(require.context('../src', true, /\.stories\.js$/), module)19configure(require.context('../src', true, /\.stories\.js$/), module)20import { configure, addDecorator } from '@storybook/react'21import { withKnobs } from '@storybook/addon-knobs'22import { addRootDecorator } from 'storybook-root-decor

Full Screen

Using AI Code Generation

copy

Full Screen

1const { boundStoryFunction } = require('storybook-root');2const storyFn = boundStoryFunction('some-story-name');3module.exports = function(storyFn) {4 const story = storyFn();5 return story;6};7### `boundStoryFunction(storyName, [moduleName])`8MIT © [James Kyburz](

Full Screen

Using AI Code Generation

copy

Full Screen

1import { boundStoryFunction } from "@storybook/react";2import { storiesOf } from "@storybook/react";3import { withKnobs, text } from "@storybook/addon-knobs";4storiesOf("Button", module)5 .addDecorator(withKnobs)6 .add("with text", () => {7 const label = text("Label", "Hello Button");8 return <button>{label}</button>;9 });10const getStory = boundStoryFunction(storiesOf("Button", module));11const story = getStory("with text");12console.log(story);13const story = getStory("with text");14const storyElement = story.render();15import { renderStory } from "@storybook/react";16const storyElement = renderStory("Button", "with text");17import { getStorybook } from "@storybook/react";18const allStories = getStorybook();19console.log(allStories);20import { getStorybook } from "@storybook/react";21const allStories = getStorybook();22console.log(allStories);

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run storybook-root automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful