<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Anna Maria Gruber on Medium]]></title>
        <description><![CDATA[Stories by Anna Maria Gruber on Medium]]></description>
        <link>https://medium.com/@futuranna?source=rss-15738fcda2dc------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*DWqpFDyX0oCuTkS_.jpeg</url>
            <title>Stories by Anna Maria Gruber on Medium</title>
            <link>https://medium.com/@futuranna?source=rss-15738fcda2dc------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 05:30:37 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@futuranna/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Why I created chai-redux]]></title>
            <link>https://medium.com/netscape/why-i-created-chai-redux-9704563fedef?source=rss-15738fcda2dc------2</link>
            <guid isPermaLink="false">https://medium.com/p/9704563fedef</guid>
            <category><![CDATA[chai]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[redux]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[mocha]]></category>
            <dc:creator><![CDATA[Anna Maria Gruber]]></dc:creator>
            <pubDate>Mon, 24 Apr 2017 09:46:07 GMT</pubDate>
            <atom:updated>2018-02-23T20:59:32.491Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vkAZx3OzAu2HQ5R8_UwJrw.png" /></figure><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FQN6zOYROOnY%3Ffeature%3Doembed&amp;url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DQN6zOYROOnY&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FQN6zOYROOnY%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/f1cc7fa056e99860b36a8e5d0e7e872e/href">https://medium.com/media/f1cc7fa056e99860b36a8e5d0e7e872e/href</a></iframe><h3>Why I created <a href="https://github.com/ScaCap/chai-redux">chai-redux</a></h3><p>I have been using redux for more than a year now — and I am a big fangirl. <br>I like that a reducer is a pure function. That it has no side-effects. <strong>And I love that a reducer is easily testable.</strong></p><p>But for me testing the reducer is only half the truth. I prefer writing an integration test too, which covers:</p><ul><li>async actions using a middleware</li><li>creating a redux store by wiring everything together.</li></ul><p>However writing integration tests with async actions has always been painful for me. I somehow always managed to test it, but often at the cost of readability.</p><p>That’s why I have decided that writing such a test should be as simple as:</p><pre>const store = chai.createReduxStore({ <em>reducer, middleware: thunk </em>});<br>expect(store).to.have.eventually<br>    .dispatched(<strong>&#39;FETCH&#39;</strong>)<br>    .then.dispatched({ <strong>type</strong>: <strong>&#39;SUCCESSFUL&#39;</strong>, name})<br>    .notify(<em>done</em>);</pre><p>But I am getting ahead of myself. Let’s start at the beginning.</p><h3>Let’s create a redux store.</h3><p>We will create a redux store which will fetch data asynchronously from an API. <br>Our store will support four states:</p><ul><li>initial</li><li>loading</li><li>success</li><li>failure</li></ul><p>And listen to three actions:</p><ul><li>FETCH → will set state loading</li><li>SUCCESSFUL → will set state success</li><li>FAILED → will set state failure.</li></ul><h4>We will start with a reducer.</h4><p>A reducer is a pure function. A synchronous action can be dispatched which updates the state immediately.</p><pre>const<strong> </strong>INITIAL_STATE = {loading: false, loaded: false, error: false, name: null};<br><br><strong>export default </strong>(state = INITIAL_STATE, action) =&gt; {<br>    <strong>switch </strong>(action.type) {<br>        <strong>case &#39;FETCH&#39;</strong>:<br>            <strong>return </strong>{<br>                ...INITIAL_STATE,<br>                <strong>loading</strong>: <strong>true<br>            </strong>};<br>        <strong>case &#39;SUCCESSFUL&#39;</strong>:<br>            <strong>return </strong>{<br>                ...INITIAL_STATE,<br>                <strong>loaded</strong>: <strong>true</strong>,<br>                <strong>name</strong>: action.name<br>            };<br>        <strong>case &#39;FAILED&#39;</strong>:<br>            <strong>return </strong>{<br>                ...INITIAL_STATE,<br>                <strong>error</strong>: <strong>true<br>            </strong>};<br>    }<br>    <strong>return </strong>state;<br>};</pre><p>This reducer will handle fetching data from an API. It will update the state, when fetching data is triggered and once the server has responded. <br>The process of fetching data <strong>will dispatch a series of actions, causing the state to update multiple times.</strong></p><p>This process will look like this:</p><ol><li>Our reducer is notified when fetching data was triggered.</li><li>Our reducer is notified when data was successfully fetched.</li></ol><pre>it(<strong>&#39;should successfully fetch data from API&#39;</strong>, () =&gt; {<br>    store.dispatch({<strong>type</strong>: <strong>&#39;FETCH&#39;</strong>});<br>    expect(store.getState().<strong>loading</strong>).to.be.true;<br>    store.dispatch({<br>        <strong>type</strong>: <strong>&#39;SUCCESSFUL&#39;</strong>,<br>        <strong>name</strong>: <strong>&#39;redux&#39;</strong><br>    });<br>    expect(store.getState().<strong>loaded</strong>).to.be.true;<br>    expect(store.getState().<strong>details</strong>).to.<strong>eql</strong>({<strong>name</strong>: <strong>&#39;redux&#39;</strong>});<br>});</pre><p>The test above shows how our process/flow will look like.</p><p>We will implement this flow using redux-thunk middleware and fetch to fetch the data asynchronously. Our action will dispatch a sync action when it is triggered and one when fetching data was successful or failed.</p><pre>import &#39;isomorphic-fetch&#39;;</pre><pre>export const <em>fetchData </em>= () =&gt; (dispatch) =&gt; {<br>    <strong>dispatch(</strong>{type: &#39;<strong>FETCH</strong>&#39;}<strong>)</strong>;<br>    fetch(&#39;https://registry.npmjs.org/redux/latest&#39;)<br>        .then(response =&gt; response.json())<br>        .then(({name}) =&gt; {<br>            <strong>dispatch(</strong>{type: &#39;<strong>SUCCESSFUL</strong>&#39;, name}<strong>);</strong><br>        })<br>        .catch(() =&gt; {<br>            <strong>dispatch(</strong>{type: &#39;<strong>FAILED</strong>&#39;}<strong>);</strong><br>        });<br>};</pre><p>Now we can setup our store. Wiring everything together.</p><pre>import { createStore, <em>applyMiddleware </em>} from &#39;redux&#39;;<br>import reducer, { fetchData } from &#39;./reducer&#39;;</pre><pre>const globalStore = createStore(reducer, <em>applyMiddleware(thunk)</em>);<br>globalStore.dispatch(fetchData());</pre><p>We have successfully implemented all component’s of our redux store.</p><h4>Let’s write some tests, which…</h4><ol><li>test if all states are updated correctly.</li><li>verify our async action behaves as described above.</li><li>describe the process.</li><li>allow other developers to understand how to setup the redux store.</li></ol><p>For 1. we will create plain unit tests without <a href="https://github.com/ScaCap/chai-redux">chai-redux</a>. There is no need to re-invent anything. Testing a state update is one of THE advantages of redux.</p><pre><strong>it(&#39;should have initial state&#39;</strong>, () =&gt; {<br>    const nextState = <em>reducer</em>(<em>undefined</em>, {type: &#39;@@INIT&#39;});<br>    <strong>expect(</strong>nextState<strong>).to.eql(</strong>{loading: false, loaded: false, error: false, name: null}<strong>);</strong><br>}<strong>);</strong></pre><pre><strong>it(&#39;should set loading&#39;, </strong>() =&gt; {<br>    const nextState = <em>reducer</em>({loading: false}, {type: &#39;FETCH&#39;});<br>    <strong>expect(</strong>nextState.loading<strong>).to.be.true;</strong><br>}<strong>);</strong></pre><pre>it(&#39;should set loaded and details if action is SUCCESSFUL&#39;, () =&gt; {<br>    const nextState = <em>reducer</em>({loading: true}, {<br>        type: &#39;SUCCESSFUL&#39;,<br>        name: &#39;redux&#39;<br>    });<br>    <strong>expect(</strong>nextState.loaded<strong>).to.be.true;<br>    expect(</strong>nextState.name<strong>).to.eql(</strong>&#39;redux&#39;<strong>);</strong><br>});</pre><p>For 2. to 4. we will use chai-redux.</p><h4>chai-redux tests</h4><p>Now it’s time to setup a (chai-)redux store and write more tests.</p><pre>import chai, {expect} from &#39;chai&#39;;<br><strong>import <em>chaiRedux </em>from &#39;chai-redux&#39;;</strong><br>import <em>reducer </em>from &#39;./reducer&#39;;<br><br>chai.<strong>use</strong>(<em>chaiRedux</em>);<br>// ...<br>const<strong> </strong>store = chai.<strong>createReduxStore</strong>({<strong><em>reducer</em></strong>});</pre><p>A chai-redux store can be setup with a reducer(s) and optionally an initialState and middleware(s). chai-redux plugin includes following assertions:</p><ul><li>.state(), .state.like()</li><li>.dispatched()</li><li>.eventually &amp; .notify()</li><li>then.state(), then.dispatched()</li></ul><h4>state</h4><p>will check if store’s state history includes given state. <br>We can use it to test our store’s initial state.</p><pre>expect(store).to.have<br>  .<strong>state</strong>({loading: false, loaded: false, error: false, name: null});</pre><pre>// partial comparison<br>expect(store).to.have.<strong>state.like</strong>({ loaded: false, name: null});</pre><h4>dispatched</h4><p>Next we verify that our async action behaves as described above.</p><pre>const<strong> </strong>store = chai.createReduxStore({<em>reducer, middleware: thunk</em>});<br><em>//when<br></em>store.dispatch(<em>fetchData</em>());<br><em>//then<br></em>expect(store).to.have.<strong>dispatched</strong>(&#39;FETCH&#39;);</pre><p>What is happening here:</p><ul><li>We setup an store using redux-thunk middleware.</li><li>We dispatch our async action.</li><li>We verify <strong>FETCH</strong> action has been dispatched.</li></ul><p><strong>Note:</strong> We just described how our reducer needs to be set up and this helps other developers understanding it.</p><h4>eventually / notify</h4><p>However our async action will not dispatch its actions immediately. .eventually will wait until an action is dispatched and .notify chai.</p><pre>// then<br>expect(store).to.<strong>eventually</strong>.have<br>    .dispatched(&#39;FETCH&#39;)<br>    .<strong>notify</strong>(done);</pre><h4>then</h4><p>Let’s add the success action to our test. We expect it to be dispatched right after FETCH.</p><pre>expect(store).to.eventually.have<br>    .dispatched(&#39;FETCH&#39;)<br>    <strong>.then.dispatched(</strong>{type: &#39;SUCCESSFUL&#39;, name: &#39;redux&#39;}<strong>)</strong><br>    .notify(done);</pre><p><strong>We have successfully tested our redux store. We verified all state changes are done correctly…</strong></p><pre>const store = chai.<strong>createReduxStore</strong>({<strong>reducer</strong>, <strong>middleware</strong>: [thunk]});<br>// when<br>store.dispatch(fetchData());<br>// then<br>expect(store).to.<strong>eventually</strong>.have<br>    .<strong>dispatched</strong>(&#39;FETCH&#39;)<br>    .<strong>then.dispatched</strong>(<br>        {type: &#39;SUCCESSFUL&#39;, name: &#39;redux&#39;})<br>    .<strong>notify</strong>(done);</pre><p><strong>… and we have tested our async action behaves as expected.</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9704563fedef" width="1" height="1" alt=""><hr><p><a href="https://medium.com/netscape/why-i-created-chai-redux-9704563fedef">Why I created chai-redux</a> was originally published in <a href="https://medium.com/netscape">Netscape</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>