We've concentrated on building UIs from the bottom up; starting small and adding complexity. Doing so has allowed us to develop each component in isolation, figure out its data needs, and play with it in Storybook. All without needing to stand up a server or build out screens!
In this chapter we continue to increase the sophistication by combining components in a screen and developing that screen in Storybook.
As our app is very simple, the screen we’ll build is pretty trivial, simply wrapping the TaskList
component (which supplies its own data via Redux) in some layout and pulling a top-level error
field out of redux (let's assume we'll set that field if we have some problem connecting to our server). Create InboxScreen.js
in your components
folder:
// src/components/InboxScreen.js
import React from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import TaskList from './TaskList';
export function PureInboxScreen({ error }) {
if (error) {
return (
<div className="page lists-show">
<div className="wrapper-message">
<span className="icon-face-sad" />
<div className="title-message">Oh no!</div>
<div className="subtitle-message">Something went wrong</div>
</div>
</div>
);
}
return (
<div className="page lists-show">
<nav>
<h1 className="title-page">
<span className="title-wrapper">Taskbox</span>
</h1>
</nav>
<TaskList />
</div>
);
}
PureInboxScreen.propTypes = {
/** The error message */
error: PropTypes.string,
};
PureInboxScreen.defaultProps = {
error: null,
};
export default connect(({ error }) => ({ error }))(PureInboxScreen);
We also change the App
component to render the InboxScreen
(eventually we would use a router to choose the correct screen, but let's not worry about that here):
// src/App.js
import React from 'react';
import { Provider } from 'react-redux';
import store from './lib/redux';
import InboxScreen from './components/InboxScreen';
import './index.css';
function App() {
return (
<Provider store={store}>
<InboxScreen />
</Provider>
);
}
export default App;
However, where things get interesting is in rendering the story in Storybook.
As we saw previously, the TaskList
component is a container that renders the PureTaskList
presentational component. By definition container components cannot be simply rendered in isolation; they expect to be passed some context or to connect to a service. What this means is that to render a container in Storybook, we must mock (i.e. provide a pretend version) the context or service it requires.
When placing the TaskList
into Storybook, we were able to dodge this issue by simply rendering the PureTaskList
and avoiding the container. We'll do something similar and render the PureInboxScreen
in Storybook also.
However, for the PureInboxScreen
we have a problem because although the PureInboxScreen
itself is presentational, its child, the TaskList
, is not. In a sense the PureInboxScreen
has been polluted by “container-ness”. So when we setup our stories in InboxScreen.stories.js
:
// src/components/InboxScreen.stories.js
import React from 'react';
import { PureInboxScreen } from './InboxScreen';
export default {
component: PureInboxScreen,
title: 'InboxScreen',
};
const Template = args => <PureInboxScreen {...args} />;
export const Default = Template.bind({});
export const Error = Template.bind({});
Error.args = {
error: 'Something',
};
We see that although the error
story works just fine, we have an issue in the default
story, because the TaskList
has no Redux store to connect to. (You also would encounter similar problems when trying to test the PureInboxScreen
with a unit test).
One way to sidestep this problem is to never render container components anywhere in your app except at the highest level and instead pass all data-requirements down the component hierarchy.
However, developers will inevitably need to render containers further down the component hierarchy. If we want to render most or all of the app in Storybook (we do!), we need a solution to this issue.
The good news is that it is easy to supply a Redux store to the InboxScreen
in a story! We can just use a mocked version of the Redux store provided in a decorator:
// src/components/InboxScreen.stories.js
import React from 'react';
import { Provider } from 'react-redux';
import { action } from '@storybook/addon-actions';
import { PureInboxScreen } from './InboxScreen';
import * as TaskListStories from './TaskList.stories';
// A super-simple mock of a redux store
const store = {
getState: () => {
return {
tasks: TaskListStories.Default.args.tasks,
};
},
subscribe: () => 0,
dispatch: action('dispatch'),
};
export default {
component: PureInboxScreen,
decorators: [story => <Provider store={store}>{story()}</Provider>],
title: 'InboxScreen',
};
const Template = args => <PureInboxScreen {...args} />;
export const Default = Template.bind({});
export const Error = Template.bind({});
Error.args = {
error: 'Something',
};
Similar approaches exist to provide mocked context for other data libraries, such as Apollo, Relay and others.
Cycling through states in Storybook makes it easy to test we’ve done this correctly:
We started from the bottom with Task
, then progressed to TaskList
, now we’re here with a whole screen UI. Our InboxScreen
accommodates a nested container component and includes accompanying stories.
Component-Driven Development allows you to gradually expand complexity as you move up the component hierarchy. Among the benefits are a more focused development process and increased coverage of all possible UI permutations. In short, CDD helps you build higher-quality and more complex user interfaces.
We’re not done yet - the job doesn't end when the UI is built. We also need to ensure that it remains durable over time.