We're hosting a webinar with @stackblitz on designing, testing, documenting, and efficiently utilizing components.
🗓️ Jun 12, 2024
🕔 5pm GMT
🎟️ Register to save a spot: crowdcast.io/c/stackblitz-ch…
We’re very excited to make it easier than ever to get started and stay productive creating stories for your components.
Learn more about interactive story generation in our just-published blog post:
storybook.js.org/blog/intera…
✨ Add new components from the sidebar
(React-only, while we solidify the workflow!)
1. Press the + button
2. Search for your component
3. Select the component to generate a stories file with a basic starting story
4. Change some controls and create new stories from there
💾 Save modifications to existing stories
Doing this in the UI is super handy:
1. Open a story and play around with some values in the Controls tab
2. Press the Save button in the toolbar that appears to save the changes to the current story
🎛️ Create new stories using controls
Easy as 1-2-(there is no 3):
1. Open a story and change some values in the Controls tab
2. Press the New button in the toolbar that appears to create a new story with those values
Interactive story generation: Edit and create stories from Storybook’s UI!
🎛️ Create new stories using controls
💾 Save modifications to existing stories
✨ Add new components from the sidebar
Let’s look at each one…
ALT The Storybook sidebar with a cursor on the + button and the "Add new component" modal with "Avatar" being searched for
One of the best component testing solutions out there is @storybookjs, and they're continuing to innovate by using JS standards-based mocking techniques. storybook.js.org/blog/type-s…
Did you know that typesafe module mocking is available via a standard package.json field? And that it's already supported by Node, Webpack, Vite, and TypeScript?
Hoping we can help pave the way to make this the new norm in testing. 🚀
ALT A meme template with 3 rows. 1: Module mocking in Storybook, with a photo of Vince McMayon sitting in a chair with an intrigued look on his face. 2: It's type-safe, with a close up of Vince's face, now with a shocked, delighted expression. 3: It's built on Node standards, with Vince fully leaning back in his chair, mouth agape
We’re really excited about the capabilities this brings to Storybook, especially component testing.
Read the post for more details and hints at what’s coming next.
storybook.js.org/blog/type-s…
Using that mock in stories looks like this.
🎭 We can modify its behavior for each story using Storybook’s new `beforeEach` hook
📝 The Actions panel will now log whenever the function is called
✅ We can assert on the calls in the play function
ALT A file named Dashboard.stories.ts, where the most relevant code is:
// 👇 Import the mock file explicitly, as that will make
// TypeScript understand that these exports are the mock functions
import { getDashboardLayout } from '#lib/settings.mock'
const meta = {
component: Dashboard,
} satisfies Meta<typeof Dashboard>;
export default meta;
type Story = StoryObj<typeof meta>;
export const Empty: Story = {
beforeEach: () => {
// 👇 Mock return an empty layout
getDashboardLayout.mockReturnValue([]);
},
play: async ({ canvasElement }) => {
const canvas = within(canvasElement);
// 👇 Expect the UI to prompt when the dashboard is empty
await expect(canvas).toHaveTextContent('Configure your dashboard');
// 👇 Assert directly on the mock function that it was called as expected
expect(getDashboardLayout).toHaveBeenCalled();
},
};
That mock file looks something like this. See that `fn` call? That produces a fully type-safe Vitest mock function, with all of its methods, that’s automatically spied in Storybook.
ALT A file named settings.mock.ts, containing the code:
import { fn } from '@storybook/test';
import * as actual from './settings'; // 👈 Import the actual implementation
// 👇 Re-export the actual implementation.
// This catch-all ensures that the exports of the mock file always contains
// all the exports of the original. It is up to the user to override
// individual exports below as appropriate.
export * from './settings';
// 👇 Export a mock function whose default implementation is the actual implementation.
// With a useful mockName, it displays nicely in Storybook's Actions addon
// for debugging.
export const getDashboardLayout = fn(actual.getDashboardLayout)
.mockName('settings::getDashboardLayout');
Storybook's module mocking is based on Subpath Imports, a Node standard supported by much of the JS ecosystem: TypeScript, Webpack, Vite, Vitest, Jest, etc.
The config below will resolve the `#lib/settings` absolute import to the mock file when loaded in Storybook.
ALT A portion of the package.json file, containing the code:
{
"imports": {
"#lib/settings": {
"storybook": "./lib/settings.mock.ts",
"default": "./lib/settings.ts"
},
"#*": [ // fallback for non-mocked absolute imports
"./*",
"./*.ts",
"./*.tsx"
]
}
}
ALT A meme template with 3 rows. 1: Module mocking in Storybook, with a photo of Vince McMayon sitting in a chair with an intrigued look on his face. 2: It's type-safe, with a close up of Vince's face, now with a shocked, delighted expression. 3: It's built on Node standards, with Vince fully leaning back in his chair, mouth agape
Transform your UI development workflow! 🚀
Join experts from @stackblitz and @chromaticcom to discover how to design, test, document, and utilize components efficiently.
🗓️ Jun 12, 2024
🕔 5pm GMT
Registration link in thread ⬇️
ALT Revolutionizing UI Development with Chromatic and StackBlitz. Presented by Tomek Sułkowski and Kyle Gach