question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Usage with Storybook (Component Story Format)

See original GitHub issue

I’m trying to use new Component Story Format of Storybook with Testing library.

The goal is to import a story as an ES6 module without having to redefine its context.

With this component:

@Component({
  selector: 'app-root',
  template: `<input [type]="type">`
})
export class AppComponent {
  type: string;
}

I create 2 stories with Component Story Format to display two types of input.

export default {
  title: 'AppComponent'
};

export const defaultInput = () => ({
  component: AppComponent,
  props: {
    type: 'text'
  }
});

export const rangeInput = () => ({
  component: AppComponent,
  props: {
    type: 'range'
  }
});

With Testing Library, i would expect to be able to test the type from each story without having to define the type in the test. I’m aware my issue comes from the way i use render() as i call the component property of the story and at this point no property is defined for this component. But i can’t find a way to render the component and its context just by calling ‘defaultInput()’ or ‘rangeInput()’. Is there a solution?

describe('AppComponent', () => {
  // not working test : the type is not defined
  it('should render a custom title', async () => {
    const component = await render(defaultInput().component);
    expect(component.fixture.componentInstance.type).toBe('text');
  });
  // working example as the property is defined into the test context
  it('should render a custom title', async () => {
    const component = await render(defaultInput().component, {
      componentProperties: {
        type: 'text'
      }
    });
    expect(component.fixture.componentInstance.type).toBe('text');
  });
  // not working test : the type is not defined
  it('should render a custom title', async () => {
    const component = await render(rangeInput().component);
    expect(component.fixture.componentInstance.type).toBe('range');
  });
  // working example as the property is defined into the test context
  it('should render a custom title', async () => {
    const component = await render(rangeInput().component, {
      componentProperties: {
        type: 'range'
      }
    });
    expect(component.fixture.componentInstance.type).toBe('range');
  });
});

Link to the project on Github.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
shilmancommented, Nov 8, 2020

@lkashef Thanks for the mention.

We’re currently bridging the gap between storybook and React testing library: https://github.com/storybookjs/storybook/issues/10145

I’m sure some of these utilities (e.g. decorator/parameter/arg composition) can be applied to all frameworks.

As for a more natural way to test stories, we’re still figuring that out. Suggestions welcome!

2reactions
geromegrignoncommented, Nov 11, 2019

Thanks a lot, i was thinking to use this way too.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Component Story Format (CSF) - Storybook
Component Story Format (CSF) is the recommended way to write stories. It's an open standard based on ES6 modules that is portable beyond...
Read more >
Component Story Format - WebComponents.dev
Stories are expressed in Storybook's Component Story Format (CSF). WebComponents.dev supports Storybook 6.x improvements. In CSF, stories and component metadata ...
Read more >
What makes up a Story?
Storybook supports Component Story Format (CSF), an open standard based on ES6 modules which was introduced in Storybook 6.0 as the main way ......
Read more >
ComponentDriven/csf: Component Story Format is an ... - GitHub
The Component Story Format is an open standard for component examples based on JavaScript ES6 modules. This enables interoperation between development, testing, ...
Read more >
@storybook/csf - npm
Storybook Component Story Format (CSF) utilities. Latest version: 0.0.1, last published: 3 years ago. Start using @storybook/csf in your ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found