<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: David Moore</title>
    <description>The latest articles on DEV Community by David Moore (@davidimoore).</description>
    <link>https://dev.to/davidimoore</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F87809%2Ff6c3179c-1c3b-4340-a2a6-ef11991ca4a0.jpeg</url>
      <title>DEV Community: David Moore</title>
      <link>https://dev.to/davidimoore</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/davidimoore"/>
    <language>en</language>
    <item>
      <title>Using Storybook as both a visual library and test code</title>
      <dc:creator>David Moore</dc:creator>
      <pubDate>Sun, 29 Jul 2018 23:05:55 +0000</pubDate>
      <link>https://dev.to/davidimoore/using-storybook-as-both-a-visual-library-and-test-code-3cb4</link>
      <guid>https://dev.to/davidimoore/using-storybook-as-both-a-visual-library-and-test-code-3cb4</guid>
      <description>&lt;p&gt;After creating a React component and testing it, you still need to see what it looks like. But you may not have a view or&lt;br&gt;&lt;br&gt;
parent component ready to render it with.&lt;/p&gt;

&lt;p&gt;In my previous &lt;a href="https://dev.to/davidimoore/using-fixtures-for-testing-a-reactredux-app-with-jest--enzyme-3hd0"&gt;article&lt;/a&gt; I created a redux connected &lt;code&gt;UserListContainer&lt;/code&gt; that fetched users from a remote source. I also created a &lt;code&gt;UserList&lt;/code&gt; presentational component to render the fetched users.&lt;br&gt;&lt;br&gt;
Additionally I generated jest &lt;a href="https://jestjs.io/docs/en/snapshot-testing#snapshot-testing-with-jest" rel="noopener noreferrer"&gt;snapshots&lt;/a&gt; for our&lt;br&gt;&lt;br&gt;
components.&lt;/p&gt;

&lt;p&gt;Lastly I used reusable data or fixtures in my tests.&lt;/p&gt;

&lt;p&gt;Let’s say you want to render the &lt;code&gt;UserList&lt;/code&gt; component (and its container) in another component like &lt;code&gt;UserListView&lt;/code&gt;. But &lt;code&gt;UserListView&lt;/code&gt; isn’t complete.&lt;br&gt;&lt;br&gt;
What if you simply want to see what &lt;code&gt;UserList&lt;/code&gt; looks like before adding it anywhere else in your app temporarily?&lt;/p&gt;

&lt;p&gt;This is where &lt;a href="https://github.com/storybooks/storybook" rel="noopener noreferrer"&gt;Storybook&lt;/a&gt; provides a really practical solution. Storybook lets you view your components in isolation. So we could see our &lt;code&gt;UserList&lt;/code&gt; with some sample users.&lt;/p&gt;

&lt;p&gt;In this article I will do the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Add Storybook to the app&lt;/li&gt;
&lt;li&gt;Create stories for my &lt;code&gt;UserList&lt;/code&gt; component&lt;/li&gt;
&lt;li&gt;Create snapshots from those stories&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you want to follow along do the following to checkout this blog’s code example “start” branch:&lt;br&gt;&lt;br&gt;
&lt;code&gt;git clone -b start git@github.com:davidimoore/blog-article-2.git&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;To get the completed repo&lt;br&gt;&lt;br&gt;
&lt;code&gt;git clone git@github.com:davidimoore/blog-article-2.git&lt;/code&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Step 1 – Add Storybook
&lt;/h3&gt;

&lt;p&gt;First let’s install storybook. For this project I’m installing it globally.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If you are using &lt;code&gt;npm&lt;/code&gt; do &lt;code&gt;npm install -g @storybook/cli&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;If you are using &lt;code&gt;yarn&lt;/code&gt; do &lt;code&gt;yarn global add @storybook/cli&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;You will also need to install &lt;a href="https://facebook.github.io/watchman/docs/install.html" rel="noopener noreferrer"&gt;watchman&lt;/a&gt; for macs and homebrew &lt;code&gt;brew install watchman&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Next lets add Storybook to our project. In the root of the project run &lt;code&gt;getstorybook&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In our &lt;code&gt;package.json&lt;/code&gt; file in the &lt;code&gt;scripts&lt;/code&gt; section we should see the following has been added:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  "scripts": {
    ...
    "storybook": "start-storybook -p 9009 -s public",
    "build-storybook": "build-storybook -s public"
  },

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run &lt;code&gt;yarn storybook&lt;/code&gt; and then go to &lt;code&gt;http://localhost:9009&lt;/code&gt; in your browser. You should see the following:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://dmohq.com/wp-content/uploads/2018/07/storybook-welcome-screen-e1532906435668.png" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fdmohq.com%2Fwp-content%2Fuploads%2F2018%2F07%2Fstorybook-welcome-screen-800x424.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Storybook also added 2 directories&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;.storybook contains &lt;code&gt;addons.js&lt;/code&gt; and &lt;code&gt;config.js&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;stories which has some example code.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Step 2 – Create a UserList story
&lt;/h3&gt;

&lt;p&gt;Let’s add a &lt;code&gt;UserList&lt;/code&gt; story when we have users passed into the component. We can use the same fixture file from our&lt;br&gt;&lt;br&gt;
we imported in &lt;code&gt;__test__ /components/UserList.test.jsx&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// src/stories/UserList.jsx

import React from "react";
import { storiesOf } from "@storybook/react";

import UserList from "components/UserList";
import reducedUsers from " __fixtures__ /reducedUsers";

storiesOf("UserList", module).add("with users", () =&amp;gt; (
  &amp;lt;UserList users={reducedUsers} /&amp;gt;
));

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We also will need to update our &lt;code&gt;src/stories/index.js&lt;/code&gt; file to require our new story. Let’s remove the Welcome story and only load our new &lt;code&gt;UserList&lt;/code&gt; story.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;require("./UserList");

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Storybook web page should auto reload and we should see:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://dmohq.com/wp-content/uploads/2018/07/user-list-story.png" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fdmohq.com%2Fwp-content%2Fuploads%2F2018%2F07%2Fuser-list-story-800x399.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It’s not an artistic achievement but we now have an idea of what our table looks like without having to render it in&lt;br&gt;&lt;br&gt;
our app.&lt;/p&gt;
&lt;h3&gt;
  
  
  Step 3 – Create storybook generated snapshots with StoryShots
&lt;/h3&gt;

&lt;p&gt;Instead of generating our snapshots with our tests we can also generate them with what our stories render. It’s arugably preferable because you are capturing a component based on what it looks like and not simply what it’s programitic structure is. To do that let’s add StoryShots.&lt;/p&gt;

&lt;p&gt;Either &lt;code&gt;npm install --save-dev @storybook/addon-storyshots&lt;/code&gt; or &lt;code&gt;yarn add -D @storybook/addon-storyshots&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Next we create a test to run Storyshots&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// src/ __tests__ /Storyshots.test.js

import initStoryshots from '@storybook/addon-storyshots';

initStoryshots();

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next run our tests and a new snapshot will be created in our &lt;code&gt;__tests__ / __snapshots__&lt;/code&gt; directory. At this point you could delete the original snapshot for the &lt;code&gt;UserStory&lt;/code&gt; component since it renders an identical structure.&lt;/p&gt;

&lt;h3&gt;
  
  
  Summary
&lt;/h3&gt;

&lt;p&gt;Storybook is a great way to view your components in an isolated sandbox. With Storyshots that same view can generate a snapshot test whenever you run your test suite.&lt;/p&gt;

</description>
      <category>jest</category>
      <category>react</category>
      <category>storybook</category>
    </item>
    <item>
      <title>Using fixtures for testing a React/Redux app (with Jest &amp; Enzyme)</title>
      <dc:creator>David Moore</dc:creator>
      <pubDate>Fri, 27 Jul 2018 22:53:45 +0000</pubDate>
      <link>https://dev.to/davidimoore/using-fixtures-for-testing-a-reactredux-app-with-jest--enzyme-3hd0</link>
      <guid>https://dev.to/davidimoore/using-fixtures-for-testing-a-reactredux-app-with-jest--enzyme-3hd0</guid>
      <description>&lt;p&gt;I love testing because it helps me understand better the code I write. One particular problem it solves is how I expect data that I’m fetching to render. Since the same data often gets passed around to multiple functions, I find using fixtures a to be a really useful way to confirm that everything is working as expected. I’ve put together what I think is a practical demonstration below.&lt;/p&gt;

&lt;p&gt;Let’s assume the following&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We have an endpoint &lt;code&gt;GET /users&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;We want to render a list of users with a response from the endpoint&lt;/li&gt;
&lt;li&gt;We are going to use redux to manage the state of our app&lt;/li&gt;
&lt;li&gt;We want to test all the things (reducer, actions, components and containers) with &lt;a href="https://jestjs.io/"&gt;jest&lt;/a&gt; and &lt;a href="https://github.com/airbnb/enzyme"&gt;enzyme&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You’ll need to have some familiarity with redux including &lt;a href="https://redux.js.org/advanced/async-actions"&gt;async actions&lt;/a&gt; and &lt;a href="https://github.com/reduxjs/redux-thunk"&gt;thunk&lt;/a&gt;.&lt;br&gt;&lt;br&gt;
If you have trouble with the portions of this article that involve redux, the &lt;a href="https://redux.js.org/"&gt;docs&lt;/a&gt; are really well written.&lt;/p&gt;
&lt;h3&gt;
  
  
  Step 1 – Setup
&lt;/h3&gt;

&lt;p&gt;For this post you can either create your own project from scratch or refer to the &lt;a href="https://github.com/davidimoore/blog-article-1"&gt;Github&lt;/a&gt; repo&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Install &lt;a href="https://yarnpkg.com/lang/en/docs/install/#mac-stable"&gt;yarn&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Install &lt;a href="https://github.com/facebook/create-react-app"&gt;create-react-app&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Use create-react-app to &lt;a href="https://github.com/facebook/create-react-app#yarn"&gt;create your app&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Change to the root of your new project and install dependenices
&lt;code&gt;yarn add axios redux redux-thunk&lt;/code&gt;
&lt;code&gt;yarn add -D axios-mock-adapter enzyme enzyme-adapter-react-16 react-test-renderer redux-mock-store&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Create a global setup file &lt;code&gt;src/setupTests.js&lt;/code&gt; and the following enzyme configuration:
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import Enzyme from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';

Enzyme.configure({ adapter: new Adapter() });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;Last we’ll add a .env file in the root of the project and add a couple of environment variables.

&lt;ul&gt;
&lt;li&gt;NODE_PATH – Makes importing files easier.&lt;/li&gt;
&lt;li&gt;REACT_APP_BASE_URL – Since we often use different servers for different environments we want to set the base url
to whatever server we use for development. I’ll be using &lt;code&gt;http://localhost:3001&lt;/code&gt;
&lt;code&gt;NODE_PATH=src/
REACT_APP_BASE_URL=http://localhost:3001&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;
  
  
  Step 2 – Generate a snapshot with static data
&lt;/h3&gt;

&lt;p&gt;In order to fetch and render data in our app we need to answer a couple of questions:&lt;br&gt;&lt;br&gt;
– What data do we get from our endpoint&lt;br&gt;&lt;br&gt;
– How is that data being rendered in our app?&lt;/p&gt;

&lt;p&gt;Our endpoint &lt;code&gt;GET /users&lt;/code&gt; returns an array of users.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[
  {
    "id": 1,
    "first_name": "Diana",
    "last_name": "Prince",
    "email": "dianaprince@flatley.com",
    "nickname": "Wonder Woman",
    "created_at": "2018-07-25T22:18:13.337Z",
    "updated_at": "2018-07-25T22:18:13.337Z"
  },
  {
    "id": 2,
    "first_name": "Bruce",
    "last_name": "Wayne",
    "email": "brucewayne@cummerata.com",
    "nickname": "Batman",
    "created_at": "2018-07-25T22:18:13.340Z",
    "updated_at": "2018-07-25T22:18:13.340Z"
  }
]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let’s create component with static values we want to render based on some of the data in the response:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// src/components/UserList.jsx

import React from "react";

const UserList = () =&amp;gt; (
    &amp;lt;table&amp;gt;
      &amp;lt;thead&amp;gt;
        &amp;lt;tr&amp;gt;
          &amp;lt;td&amp;gt;Full Name&amp;lt;/td&amp;gt;
          &amp;lt;td&amp;gt;Email&amp;lt;/td&amp;gt;
          &amp;lt;td&amp;gt;Nickname&amp;lt;/td&amp;gt;
        &amp;lt;/tr&amp;gt;
      &amp;lt;/thead&amp;gt;
      &amp;lt;tbody&amp;gt;
        &amp;lt;tr className="User"&amp;gt;
          &amp;lt;td&amp;gt;Diana Prince&amp;lt;/td&amp;gt;
          &amp;lt;td&amp;gt;dianaprince@flatley.com&amp;lt;/td&amp;gt;
          &amp;lt;td&amp;gt;Wonder Woman&amp;lt;/td&amp;gt;
        &amp;lt;/tr&amp;gt;
        &amp;lt;tr className="User"&amp;gt;
          &amp;lt;td&amp;gt;Bruce Wayne&amp;lt;/td&amp;gt;
          &amp;lt;td&amp;gt;brucewayne@cummerata.com&amp;lt;/td&amp;gt;
          &amp;lt;td&amp;gt;Batman&amp;lt;/td&amp;gt;
        &amp;lt;/tr&amp;gt;
      &amp;lt;/tbody&amp;gt;
    &amp;lt;/table&amp;gt;
  );

export default UserList
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let’s create a a couple of tests. One tells us how many user rows we expect and the second is a snapshot test. Having these test in place early helps guide the refactoring and catches us from making any unwanted changes to the “markup” in our component.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// src/ __tests__ /UserList.test.jsx
import React from "react";
import UserList from "components/UserList";

import renderer from "react-test-renderer";

describe("UserList", () =&amp;gt; {
  it("displays a list of users", () =&amp;gt; {        
    const tree = renderer.create(&amp;lt;UserList/&amp;gt;).toJSON();

    expect(tree).toMatchSnapshot();
  });

  it("renders a list of rows with users", () =&amp;gt; {
    const componentWrapper = shallow(&amp;lt;UserList /&amp;gt;);
    const numberOfUserRows = componentWrapper.find("tr.User").length;

    expect(numberOfUserRows).toEqual(2);
    });
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 3 – Create our reducer
&lt;/h3&gt;

&lt;p&gt;Let’s take a step back and conceptualize the data flow and how things will come together.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We’ll fetch some users by dispatching an action. It would be named &lt;code&gt;fetchUsers&lt;/code&gt; or something similiar&lt;/li&gt;
&lt;li&gt;When we receive the users we’ll pass those to a users reducer&lt;/li&gt;
&lt;li&gt;The users reducer will transform the data from the action into an array of users that is “shaped” like the array of users we used in our test&lt;/li&gt;
&lt;li&gt;That array of users will eventually get passed to a &lt;code&gt;UsersList&lt;/code&gt; component to be rendered.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let’s build a test to define our reducers behavior.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// __tests__ /usersReducer.test.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We have two important pieces of data to help us test further:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Our example response&lt;/li&gt;
&lt;li&gt;A users array based on that response we pass to our &lt;code&gt;UserList&lt;/code&gt; component&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Our test wUserListContainer like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import users from "reducers/users";

describe("users reducer", () =&amp;gt; {
  it("handles a RECEIVED_USERS action", () =&amp;gt; {
    const action = {
      type: "RECEIVED_USERS",
      data: [
        {
          id: 1,
          first_name: "Diana",
          last_name: "Prince",
          email: "dianaprince@flatley.com",
          nickname: "Wonder Woman",
          created_at: "2018-07-25T22:18:13.337Z",
          updated_at: "2018-07-25T22:18:13.337Z"
        },
        {
          id: 2,
          first_name: "Bruce",
          last_name: "Wayne",
          email: "brucewayne@cummerata.com",
          nickname: "Batman",
          created_at: "2018-07-25T22:18:13.340Z",
          updated_at: "2018-07-25T22:18:13.340Z"
        }
      ]
    };

    const result = users(null, action);

    expect(result.users).toEqual([
      {
        id: 1,
        first_name: "Diana",
        last_name: "Prince",
        email: "dianaprince@flatley.com",
        nickname: "Wonder Woman"
      },
      {
        id: 2,
        first_name: "Bruce",
        last_name: "Wayne",
        email: "brucewayne@cummerata.com",
        nickname: "Batman"
      }
    ]);
  });
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And our reducer&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// src/reducers/user.js
const initialState = {
  users: []
};

const receivedUsers = (state, data) =&amp;gt; {
  const users = data.map(user =&amp;gt; {
    const { id, first_name, last_name, email, nickname } = user;
    return { id, first_name, last_name, email, nickname };
  });
  return { ...state, users };
};

const users = (state = initialState, action) =&amp;gt; {
  switch (action.type) {
    case "RECEIVED_USERS":
      return receivedUsers(state, action.data);
    default:
      return state;  
  }
};

export default users;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let’s also update our &lt;code&gt;index.js&lt;/code&gt; file to use redux&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// src/index.js

import React from "react";
import ReactDOM from "react-dom";
import { Provider } from "react-redux";
import thunkMiddleware from "redux-thunk";
import { applyMiddleware, combineReducers, createStore } from "redux";

import users from "reducers/users";
import "./index.css";
import App from "./components/App";
import registerServiceWorker from "./registerServiceWorker";

const appReducer = combineReducers({
  users
});

let store = createStore(appReducer, applyMiddleware(thunkMiddleware));

ReactDOM.render(
  &amp;lt;Provider store={store}&amp;gt;
    &amp;lt;App /&amp;gt;
  &amp;lt;/Provider&amp;gt;,
  document.getElementById("root")
);
registerServiceWorker();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 4 Extract fixtures
&lt;/h2&gt;

&lt;p&gt;You might have noticed we are repeating ourselves in our tests&lt;br&gt;&lt;br&gt;
– The &lt;code&gt;UserList&lt;/code&gt; component gets a users array&lt;br&gt;&lt;br&gt;
– The same users array is the result of our reducer test.&lt;/p&gt;

&lt;p&gt;Let’s extract the users array to a fixture.&lt;br&gt;&lt;br&gt;
You can put your fixtures wherever you want, I use a folder like &lt;code&gt;src/ __fixtures__&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// src/ __fixtures__ /reducedUsers.js
const reducedUsers = [
  {
    id: 1,
    first_name: "Diana",
    last_name: "Prince",
    email: "dianaprince@flatley.com",
    nickname: "Wonder Woman"
  },
  {
    id: 2,
    first_name: "Bruce",
    last_name: "Wayne",
    email: "brucewayne@cummerata.com",
    nickname: "Batman"
  }
];

export default reducedUsers;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We are using the response data in our reducer test and we’ll use it in our user actions test later as well. So we should make a fixture for it too.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// src/ __fixtures__ /getUsersResponse.js

const getUsersResponse = [
  {
    id: 1,
    first_name: "Diana",
    last_name: "Prince",
    email: "dianaprince@flatley.com",
    nickname: "Wonder Woman",
    created_at: "2018-07-25T22:18:13.337Z",
    updated_at: "2018-07-25T22:18:13.337Z"
  },
  {
    id: 2,
    first_name: "Bruce",
    last_name: "Wayne",
    email: "brucewayne@cummerata.com",
    nickname: "Batman",
    created_at: "2018-07-25T22:18:13.340Z",
    updated_at: "2018-07-25T22:18:13.340Z"
  }
];

export default getUsersResponse;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Let’s update our reducer test
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import users from "reducers/users";
import reducedUsers from " __fixtures__ /reducedUsers";
import getUsersResponse from " __fixtures__ /getUsersResponse";

describe("users reducer", () =&amp;gt; {
  it("handles a RECEIVED_USERS action", () =&amp;gt; {
    const action = {
      type: "RECEIVED_USERS",
      data: getUsersResponse
    };

    const result = users(null, action);

    expect(result.users).toEqual(reducedUsers);
  });
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Let’s also update our &lt;code&gt;UserList&lt;/code&gt; test. Again this should not require any change to our snapshot test. Simply refactoring shouldn’t render things differently.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from "react";
import { shallow } from "enzyme";
import renderer from "react-test-renderer";

import UserList from "components/UserList";
import reducedUsers from " __fixtures__ /reducedUsers";

describe("UserList", () =&amp;gt; {
  it("renders correctly", () =&amp;gt; {
    const tree = renderer.create(&amp;lt;UserList users={reducedUsers} /&amp;gt;).toJSON();

    expect(tree).toMatchSnapshot();
  });

  it("renders a list of rows with users", () =&amp;gt; {
    const componentWrapper = shallow(&amp;lt;UserList users={reducedUsers} /&amp;gt;);
    const numberOfUserRows = componentWrapper.find("tr.User").length;

    expect(numberOfUserRows).toEqual(2);
  });
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You might be thinking, “but if I change the fixture now I have to update every test that uses it”. That is exactly the point.&lt;br&gt;&lt;br&gt;
If what is returned from the reducer changes it would affect our &lt;code&gt;UserList&lt;/code&gt; component. &lt;em&gt;Our tests might break which informs us we may need to handle changes in the data&lt;/em&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  Step 5 Add redux actions
&lt;/h2&gt;

&lt;p&gt;Our user actions test will make user of our getUsersResponse fixture&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import axios from "axios";
import configureMockStore from "redux-mock-store";
import thunk from "redux-thunk";
import MockAdapter from "axios-mock-adapter";

import { fetchUsers } from "actions/users";
import getUsersResponse from " __fixtures__ /getUsersResponse";

const axiosMock = new MockAdapter(axios);
const middlewares = [thunk];
const mockStore = configureMockStore(middlewares);

describe("actions", () =&amp;gt; {
  afterEach(() =&amp;gt; {
    axiosMock.reset();
  });

  describe("fetchUsers", () =&amp;gt; {
    it("should make an http request for users", () =&amp;gt; {
      const uri = "http://localhost/users.json";
      axiosMock.onGet(uri).reply(200, getUsersResponse);

      const receiveUsersAction = {
        type: "RECEIVED_USERS",
        data: getUsersResponse
      };

      const store = mockStore({ users: [] });

      store.dispatch(fetchUsers(uri)).then(() =&amp;gt; {
        const result = store.getActions();

        expect(result).toMatchObject([receiveUsersAction]);
      });
    });
  });
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And our users actions&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// actions/users.js
import axios from "axios";

const fetchUsers = uri =&amp;gt; {
  return dispatch =&amp;gt;
    axios.get(uri).then(response =&amp;gt; dispatch(receivedUsers(response.data)));
};

const receivedUsers = data =&amp;gt; {
  return {
    type: "RECEIVED_USERS",
    data
  };
};

export { fetchUsers };
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 6 Integrate redux and react
&lt;/h2&gt;

&lt;p&gt;It’s helpful to &lt;a href="https://medium.com/@dan_abramov/smart-and-dumb-components-7ca2f9a7c7d0"&gt;separate&lt;/a&gt; containers for fetching data from components for rendering the fetched data .&lt;br&gt;&lt;br&gt;
So the last major step is to create a &lt;code&gt;UserListContainer&lt;/code&gt; to fetch users and pass the result on to the &lt;code&gt;UsersList&lt;/code&gt; component.&lt;/p&gt;

&lt;p&gt;We’ll import the &lt;code&gt;UserListContainer&lt;/code&gt; instead of the default export which is the&lt;br&gt;&lt;br&gt;
&lt;code&gt;UserListContainer&lt;/code&gt; wrapped with redux. We’ll also mock out our &lt;code&gt;fetchUsers&lt;/code&gt;&lt;br&gt;&lt;br&gt;
function since we don’t want to actually test the endpoint.&lt;/p&gt;

&lt;p&gt;Our example tests will define expected behavior for two scenarios.&lt;br&gt;&lt;br&gt;
– When users were successfully fetched and passed on to the &lt;code&gt;UserList&lt;/code&gt; component&lt;br&gt;&lt;br&gt;
– When the users array is empty&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// __tests__ /UserListContainer.test.js
import React from "react";
import {shallow} from "enzyme";

import {UserListContainer} from "containers/UserListContainer";
import reducedUsers from " __fixtures__ /reducedUsers";

describe("UserListContainer", () =&amp;gt; {
  it("displays the UsersList component when it has fetched users", () =&amp;gt; {

    const props = {
      fetchUsers: jest.fn(),
      users: reducedUsers
    };

    const container = shallow(&amp;lt;UserListContainer {...props} /&amp;gt;);
    const userListComponent = container.find('UserList').length;

    expect(userListComponent).toEqual(1)
  });

  it("does not display the UserList when ther are no users", () =&amp;gt; {
    const props = {
      fetchUsers: jest.fn(),
      users: []
    };

    const container = shallow(&amp;lt;UserListContainer {...props} /&amp;gt;);
    const userListComponentLength = container.find('UserList').length;

    expect(userListComponentLength).toEqual(0)
  });
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally our UserListContainer&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// src/containers/UserListContainer.jsx

import React from "react";
import { connect } from "react-redux";

import UserList from "components/UserList";
import * as userActions from "actions/users";

// REACT_APP_BASE_URL stored in our .env file
const GET_USERS_URL = `${process.env.REACT_APP_BASE_URL}/users.json`;

export class UserListContainer extends React.Component {
  componentDidMount() {
    const { fetchUsers } = this.props;

    fetchUsers(GET_USERS_URL);
  }

  render() {
    const { users } = this.props;
    return users &amp;amp;&amp;amp; users.length &amp;gt; 0 ? (
      &amp;lt;UserList users={users} /&amp;gt;
    ) : (
      &amp;lt;div&amp;gt;No Users!&amp;lt;/div&amp;gt;
    );
  }
}

const mapStateToProps = ({ users }) =&amp;gt; ({ ...users });

export default connect(
  mapStateToProps,
  userActions
)(UserListContainer);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let’s render everything in the App component&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { Component } from 'react';

import logo from 'logo.svg';
import UserListContainer from "containers/UserListContainer";

class App extends Component {
  render() {
    return (
      &amp;lt;div className="App"&amp;gt;
        &amp;lt;header className="App-header"&amp;gt;
          &amp;lt;img src={logo} className="App-logo" alt="logo" /&amp;gt;
          &amp;lt;h1 className="App-title"&amp;gt;Welcome to React&amp;lt;/h1&amp;gt;
        &amp;lt;/header&amp;gt;
        &amp;lt;div&amp;gt;
          &amp;lt;UserListContainer /&amp;gt;
        &amp;lt;/div&amp;gt;
      &amp;lt;/div&amp;gt;
    );
  }
}

export default App;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Summary
&lt;/h3&gt;

&lt;p&gt;There are solid arguments for and against using fixtures in tests. They can become unwieldily and too numerous if overused. I believe there is a place for fixtures in addition to functions that generate data more dynamically, like factories. In a follow up article I’ll continue on with how the same fixtures can be used with &lt;a href="https://github.com/storybooks/storybook"&gt;storybook&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>jest</category>
      <category>react</category>
      <category>redux</category>
    </item>
  </channel>
</rss>
