adjoe Engineers’ Blog
 /  Frontend  /  Storybook 7.0
decorative image in green with blue elements
Frontend

Documenting & Testing React Dashboard Components with Storybook 7.0

In my last article, I explained how to write stories for both simple components – like buttons – and more complex state-based components – like filters and dropdowns – with the help of Storybook.

In this article, I’ll show you how to document your own components with Storybook’s Autodocs feature and MDX. I will also give you a small introduction to Chromatic’s visual testing capability. 

But first, let’s talk about Storybook 7.0.

What Is Storybook 7.0?

The Storybook team released a new version at the beginning of April – Storybook 7.0. This version introduces a lot of changes regarding how you write and document stories. If you want to upgrade, the Storybook team provides you with a clear step-by-step tutorial on how to do so. It also gives you the option of migrating your components to the new story format CSF3.

If you don’t migrate your components after upgrading, they will still function in the old CSF2 format. However, you will see deprecated warnings.

In order to make sure this article is up to date with the Storybook 7.0 changes, I’ll work with the new documentation changes introduced in the update. This means you’ll need this version of Storybook to fully utilize these features.

How Do I Document My Components?

There are two ways to document your components in Storybook. You can use the Autodocs feature to utilize prop commenting and automatically generate documentation for your components. Or you can use MDX to customize the documentation page of your stories.

Let’s use our Button component from my last article:

interface IButtonProps {
 variant: 'text' | 'outlined';
 onClick: () => void;
 children: React.ReactNode;
}
export const MyButton: FC<IButtonProps> = ({ variant, onClick, children }) => (
 <Button variant={variant} onClick={onClick}>
   {children}
 </Button>
);

This is the corresponding story file that utilizes the new CSF3 format:

const meta: Meta<typeof MyButton> = {
 title: 'MyButton',
 component: MyButton,
 render: args => <MyButton {...args}>My Button</MyButton>,
};


export default meta;
type Story = StoryObj<typeof MyButton>;


export const Outlined: Story = {
 args: {
   variant: 'outlined',
 },
};


export const Text: Story = {
 args: { variant: 'text' },
};

When migrating to Storybook 7.0, during the automatic migration steps, the Autodocs feature will automatically be enabled by adding the following code to the main.js Storybook file.

ocs: {
   autodocs: true
 }

This will create the new docs substory for every component’s story.

Screenshot of the Docs Substory for the MyButtons Story
Screenshot of the Documentation of the MyButton Component

The Autodocs feature will generate the documentation story and show you all the stories created and the component’s properties on one page.

With the help of prop commenting, you can enhance the props’ descriptions.

interface IButtonProps {
 /** The variant of the button. Outlined Buttons do have a border around the child. */
 variant: 'text' | 'outlined';
 /** The onClick function that will be executed when clicking on the button */
 onClick: () => void;
 children: React.ReactNode;
}
 /** Default Button Component for interaction. */
export const MyButton: FC<IButtonProps> = ({ variant, onClick, children }) => (
 <Button variant={variant} onClick={onClick}>
   {children}
 </Button>
);

As you can see, I’ve added comments above the properties in the TypeScript interface, as well as above the component itself. These comments will automatically be added to the documentation.

Screenshot of the added comments in the storybook documentation.

You can also add comments above the substories in the story file to give them a subheading.

You can use MDX to fully customize your stories or documentation. MDX utilizes a mix of Markdown and JSX so that you can generate your own stories and documentation layouts. One common use for MDX is to create overviews of icons, typography, or color options in your project. 

To create new MDX pages, create a new .stories.mdx file first. When using VSCode, I recommend installing the MDX plugin, which supports highlighting for the file. Every MDX file needs the meta tag for Storybook to render it correctly. The Storybook framework comes with some tags that you can import via @storybook/blocks.

{/* MuiIcons.stories.mdx */}


import {Meta} from '@storybook/blocks'


<Meta title="MuiIcons" />

As an example, we can create a small list of icons from MUI.

{/* MuiIcons.stories.mdx */}


import { Meta, IconGallery, IconItem } from '@storybook/blocks'
import { ArrowBack, ArrowForward, ArrowUpward, ArrowDownward } from '@mui/icons-material';


<Meta title="MuiIcons" />


# Mui Icons


<IconGallery>
 <IconItem name="ArrowBackIcon">
   <ArrowBack />
 </IconItem>
 <IconItem name="ArrowForwardIcon">
   <ArrowForward />
 </IconItem>
 <IconItem name="ArrowUpIcon">
   <ArrowUpward />
 </IconItem>
 <IconItem name="ArrowDownIcon">
   <ArrowDownward />
 </IconItem>
</IconGallery>
Screenshot of custom story for mui icons.

How Do I Test My Components with Storybook?

When it comes to our Storybook setup for the dashboard project we’re working on at adjoe, we publish our stories on successful deployment to our production servers.

We have enabled UI tests (which you can do in the Manage section of the Chromatic project options) that will be run automatically on the Chromatic project when a new Storybook build is pushed. These UI tests will create snapshots of each story created and compare it to the latest accepted baseline version of the component. 

When the test detects changes in the visual snapshot, or detects new components, it will set the status of the build to “Review.”

This means the build is not automatically accepted as the latest Storybook. A developer needs to manually review the change and either accept it or deny it.
If all changes are accepted, the build is marked as successful and is then the new baseline. All components of the latest accepted build will be used as the baseline for future UI tests. The permanent links to the library and Storybook will subsequently use this build.

If any change under review is denied (it does not matter if one or more are denied), the build is marked as failed. It will not be used as the new baseline, and new pushes of denied components will automatically be set to review, even if there are no changes to them.

Screenshot of chromatics review page with unreviewed changes.

At adjoe, we are currently thinking of utilizing Storybook’s UI test feature in addition to unit tests as an alternative to snapshots. You can set up merge requests in a way that they cannot be merged if the UI test is failing. This could either be due to the build being under review or a component being denied.

You can achieve this in various ways based on if you are using a linked or unlinked project in Chromatic. Unlinked projects are used by self-hosted GitLab repositories or enterprise providers. They cannot utilize direct merge request integrations with Chromatic, but have to use their CI settings to block merges.

Summarizing Storybook

Now you should have the tools to implement Storybook in your own React projects and utilize its visualization capabilities, documentation, and testing features to improve your daily development workflow.

If you want to learn more about Storybook, you can find a lot more information with examples on the official website. Especially, about the new Storybook 7.0 update.

I hope my articles about Storybook gave you a good introduction to the key features of Storybook’s framework. You now should have some useful examples to help you start working with it in your own projects. Thanks for reading!

Data Science Lead (Playtime Team) (f/m/d)

  • adjoe
  • Playtime Data Science
  • Full-time
adjoe is a leading mobile ad platform developing cutting-edge advertising and monetization solutions that take its app partners’ business to the next level. Part of the applike group ecosystem, adjoe is home to an advanced tech stack, powerful financial backing from Bertelsmann, and a highly motivated workforce to be reckoned with.

Meet Your Team: Playtime

Playtime is a time- and event-based ad unit that continuously rewards users with in-app currency – for the time they spend and events completed while playing mobile games. We connect advertisers to 200+ million Playtime users and serve 2bn requests per day at low latency. We ensure that all parties involved have a positive experience. Advertisers get more users for their apps. Monetizers earn revenue for users on their platforms. Users play fun games while simultaneously getting rewarded. Our data science team powers the engine that distributes our ads. They solve multiple tasks such as developing algorithms to provide the most relevant ads for users, predicting user interests and inclinations, and dynamically adjusting pricing based on these predictions.
What you will do:
  • Work closely with Supply & Demand Tech and Product teams to provide DS models which will simultaneously take into account and balance between advertisers’ & monetizers’ goals, as well as the end users’ experience.
  • Build, maintain & develop new and existing models (classifications, recommendations, etc.).
  • Be hands-on by working on data science tasks on your own (at least 50% of the working time).
  • Grow and manage a data science team: hire new team members, organize routines in the team, set up guidelines (code style, documentation, best practices).
  • Be a mentor and train data scientists in the team to make them better in analysis, programming, communication, and planning.
  • Be the owner of critical data science documentation (experiments, implementation details) and act as an advocate for data-related topics in the company and become the go to person in your area of expertise.
  • Contribute to the company’s strategic planning. Work on a long-term roadmap and quarterly OKRs.
  • Analyze business data to identify opportunities for improvement, and then lead projects that help the company to grow substantially.
  • Who you are:
  • You have a tech degree (mathematics, physics, computer science, or similar). Alternatively 5 years of professional experience in data science.
  • You have 2+ years’ of experience of leading and/or mentoring people (hiring, doing regular 1-1s, creating career plans, doing pair programming, etc.).
  • You have a strong knowledge of Python, R, Scala, Julia or similar programming languages for Data Science.
  • You have the ability to understand business needs and build solutions that help the business & product. 
  • You have experience developing deep learning models and have applied them already in a production environment with large amount of traffic (>1 million predictions per day)
  • You have excellent analytical skills and for every problem you see more than just one solution.
  • You have experience drilling into large amounts of data coming from various sources – including AWS Athena, Kafka, Spark, S3, MySQL.
  • You are able to dive deep into mathematical foundations and explain complex topics in a simple way.
  • You are an excellent communicator and collaborator. You can communicate clearly in written and spoken form. You can also tailor your communication to the specific needs of the audience, be it data scientists, developers, leads, or members of the business teams.
  • Plus: Tech & Infrastructure knowledge: Airflow, hosting models, deploying models, etc.
  • Plus: Experience in the AdTech industry.
  • Fuel for the Journey: Benefits to Support Your Ambitions
  • Invest in Your Future: Regular feedback and our development program support your growth, helping you expand your skill set and achieve your career goals.
  • Easy Arrival to adjoe: From signing to settling in Hamburg, we’ve got you covered. Need a visa? No problem. Ready to build your new life and career at (company name) in Hamburg? We support every ambition—from learning German to a relocation bonus that helps you settle in and make Hamburg feel like home.
  • Live Your Best Life, at Work and Beyond: We work in a hybrid setup with 3 core office days, plus flexible working hours. Enjoy 30 vacation days, 3 weeks of remote work per year, and free access to an in-house gym with lots of different fitness classes and mental health support through our Employee Assistance Program (EAP).
  • Thrive Where You Work: Enjoy the Alster lake view from our central office with top-notch equipment, fun open spaces, and a large variety of snacks and drinks.
  • Join the Community! Participate in regular team and company events, including hackathons and social gatherings. We work together, and we celebrate together, too.
  • Build our signature product

    See vacancies