<?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 Gerald Yeo on Medium]]></title>
        <description><![CDATA[Stories by Gerald Yeo on Medium]]></description>
        <link>https://medium.com/@yeojz?source=rss-26e000c5f633------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*pRnbpSNvdOFJQvuQ9qKvMQ.png</url>
            <title>Stories by Gerald Yeo on Medium</title>
            <link>https://medium.com/@yeojz?source=rss-26e000c5f633------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 04:23:56 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@yeojz/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[Redux Thunk — skipping mocks using withExtraArgument]]></title>
            <link>https://medium.com/@yeojz/redux-thunk-skipping-mocks-using-withextraargument-513d38d38554?source=rss-26e000c5f633------2</link>
            <guid isPermaLink="false">https://medium.com/p/513d38d38554</guid>
            <category><![CDATA[redux]]></category>
            <category><![CDATA[redux-thunk]]></category>
            <category><![CDATA[testing]]></category>
            <dc:creator><![CDATA[Gerald Yeo]]></dc:creator>
            <pubDate>Tue, 14 Nov 2017 14:09:21 GMT</pubDate>
            <atom:updated>2017-11-14T14:12:19.679Z</atom:updated>
            <content:encoded><![CDATA[<blockquote><strong><em>tl;dr: </em></strong><em>Do take a look at redux-thunk’s </em>withExtraArgument. <em>It may help further de-couple common dependencies for simpler test code.</em></blockquote><p>If you have been using Redux for a period of time, you will have had to pair it up with redux-thunk or something similar to handle asynchronous actions at some point of time.</p><p>A common use case for thunks is to allow an action creator to make API calls. Your code would probably look something like the following snippet:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ce6f8555fe1ca84882d9e7204ea6a261/href">https://medium.com/media/ce6f8555fe1ca84882d9e7204ea6a261/href</a></iframe><p>However, if you want to test this code, you would probably have to resort to using mocks on your dependencies. As such, your corresponding spec/test file would probably look something like the following:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d37aae851b5c4f0a5c7c5133c1fc4a8e/href">https://medium.com/media/d37aae851b5c4f0a5c7c5133c1fc4a8e/href</a></iframe><p>What if we want to further de-couple this? Why not pass api as an argument of the thunk function instead? That is exactly what “withExtraArgument” method of redux-thunk, which introduced in v2.1.0, can help you achieve.</p><blockquote>Early users of redux-thunk v1.x may have overlooked the addition of “withExtraArgument” , which was introduced since v2.1.0.</blockquote><p>This was something I have totally missed for the longest of time until a recent project. With this, your action creator will look something like this instead:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b233ae6a555130927344d06a73ca14cd/href">https://medium.com/media/b233ae6a555130927344d06a73ca14cd/href</a></iframe><p>As such, the corresponding test code would probably look something like the following:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8bbc1d9d413c8de2f769e9159ed77771/href">https://medium.com/media/8bbc1d9d413c8de2f769e9159ed77771/href</a></iframe><p>Since api (or some other dependency) is now an argument as part of your thunk function, you can easily pass in your stubs or test methods without having to resort full mocks.</p><blockquote>This might seem like a trivial change. However, when you are trying to mock “imports/requires” that are non-objects, the above alternative helps to simplify testing by a margin.</blockquote><p>While jest, used in the snippets above, provides module mocking out of the box, the same cannot be said with other testing frameworks. You may have to use other libraries like rewire , mockery or even babel-plugin-rewire to transpile your code, resulting in a more complicated the test code.</p><p>With that said, your mileage and benefit may vary. Usage of “<em>withExtraArguments</em>” may not applicable in all scenarios due to existing codebase or code-splitting/bundling requirements, but it is something worth keeping in the back of the head while setting up the next project / refactoring codes.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=513d38d38554" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>