<?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: Mark J. Lehman</title>
    <description>The latest articles on DEV Community by Mark J. Lehman (@supremebeing7).</description>
    <link>https://dev.to/supremebeing7</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%2F12796%2F49f5552c-4e08-4f6e-946d-c2eb79143a00.jpeg</url>
      <title>DEV Community: Mark J. Lehman</title>
      <link>https://dev.to/supremebeing7</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/supremebeing7"/>
    <language>en</language>
    <item>
      <title>Don't Surrender to Extraneous React Re-Renders</title>
      <dc:creator>Mark J. Lehman</dc:creator>
      <pubDate>Thu, 07 Nov 2019 19:29:21 +0000</pubDate>
      <link>https://dev.to/supremebeing7/don-t-surrender-to-extraneous-re-renders-31f0</link>
      <guid>https://dev.to/supremebeing7/don-t-surrender-to-extraneous-re-renders-31f0</guid>
      <description>&lt;p&gt;After learning about and monkeying around with this fantastic React tool &lt;a href="https://github.com/welldone-software/why-did-you-render"&gt;&lt;code&gt;why-did-you-render&lt;/code&gt;&lt;/a&gt; for about a week, I realized there was a lot that I didn't realize or understand about how React determines when to re-render a component. Here are 6 of the most helpful things I learned during this adventure.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Use React.memo for pure functional components
&lt;/h2&gt;

&lt;p&gt;With &lt;a href="https://reactjs.org/docs/hooks-intro.html"&gt;React hooks&lt;/a&gt;, it's easier than ever to use functional components rather than class components. Larger and/or more complex components can be written as functions instead of classes. However, vanilla functional components re-render with every change to props, and when dealing with a large or complex component, that might not be necessary. &lt;/p&gt;

&lt;p&gt;Enter &lt;a href="https://reactjs.org/docs/react-api.html#reactmemo"&gt;&lt;code&gt;React.memo&lt;/code&gt;&lt;/a&gt;. This makes a functional component behave similar to extending &lt;code&gt;React.PureComponent&lt;/code&gt; -- namely, that it will do a shallow comparison of props on any prop change, and only re-render if previous props shallowly equal new props.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Pass in a comparison function for doing deep compares
&lt;/h2&gt;

&lt;p&gt;Shallow comparison might not do the trick though. After all, maybe one of the props is an array of strings. If that array is generated on the fly somehow, for example by taking something from state and using &lt;code&gt;map&lt;/code&gt; or &lt;code&gt;filter&lt;/code&gt; to get only certain ones, even if the array contents hasn't changed, the new prop will be a new array, so &lt;code&gt;prevProps.arrayProp === this.props.arrayProp&lt;/code&gt; will be false, and the component will re-render unnecessarily.&lt;/p&gt;

&lt;p&gt;Luckily, &lt;code&gt;React.memo&lt;/code&gt; takes a second argument that will be used to compare the props. So if there are limited props that can be compared deeply, that can avoid some unnecessary re-renders. A few notes about this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The docs say this is not guaranteed to prevent re-renders. However, anecdotally I have noticed fewer re-renders using this approach. &lt;/li&gt;
&lt;li&gt;Depending on how large or "heavy" the component is, and depending on how complex the props are, it's a good idea to determine whether it will be more performant to re-render or do a deep compare.&lt;/li&gt;
&lt;li&gt;This is more or less analogous to the &lt;code&gt;shouldComponentUpdate&lt;/code&gt; lifecycle method on &lt;code&gt;React.Component&lt;/code&gt;, only in reverse (e.g. if &lt;code&gt;shouldComponentUpdate&lt;/code&gt; returned true, the component would re-render; whereas if this passed-in function &lt;code&gt;areEqual&lt;/code&gt; returns true, the component &lt;em&gt;does not&lt;/em&gt; re-render.)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  3. Only update state if it has changed
&lt;/h2&gt;

&lt;p&gt;As you can see, the name of the game in reducing re-renders in general is to avoid props changes. Sometimes that will mean adding a bit more complexity elsewhere. For example, on our team, we like simple cases in reducers, such as this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;DOMAIN/UPDATE_ARRAY_PROP&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;propName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;arrayProp&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;assign&lt;/span&gt;&lt;span class="p"&gt;({},&lt;/span&gt; &lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
      &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;propName&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt; &lt;span class="nx"&gt;arrayProp&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;But, if &lt;code&gt;state[propName]&lt;/code&gt; is deeply equal to &lt;code&gt;arrayProp&lt;/code&gt;, we are reassigning that property even though it isn't actually changing. And as we just learned, reassigning the property, particularly when dealing with array and object props, creates a new array or object which will cause shallow comparisons to fail. &lt;/p&gt;

&lt;p&gt;Instead, we should check if an UPDATE action is actually going to update, or if the updated values are the same as what's currently in state. If they are the same, don't update them and return state as-is to avoid the re-render. The above example, reworked (using &lt;a href="https://lodash.com/docs/4.17.15#isEqual"&gt;&lt;code&gt;lodash/isEqual&lt;/code&gt;&lt;/a&gt;):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;DOMAIN/UPDATE_ARRAY_PROP&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;propName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;arrayProp&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="c1"&gt;// Add this guard!&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isEqual&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;propName&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nx"&gt;arrayProp&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;assign&lt;/span&gt;&lt;span class="p"&gt;({},&lt;/span&gt; &lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
      &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;propName&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt; &lt;span class="nx"&gt;arrayProp&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;To further illustrate this, here's an example updating an object's property. &lt;/p&gt;

&lt;p&gt;With extraneous re-renders:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;DOMAIN/UPDATE_OBJECT_NAME&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;objectName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;newName&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;assign&lt;/span&gt;&lt;span class="p"&gt;({},&lt;/span&gt; &lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;objectName&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;objectName&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; 
        &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;newName&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Optimized:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;DOMAIN/UPDATE_OBJECT_NAME&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;objectName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;newName&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="c1"&gt;// Add this guard!&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;objectName&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;newName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;assign&lt;/span&gt;&lt;span class="p"&gt;({},&lt;/span&gt; &lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;objectName&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;objectName&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; 
        &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;newName&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  4. Avoid data conversion in selectors
&lt;/h2&gt;

&lt;p&gt;Same problem, different symptom. When using selectors, avoid doing any data conversion if possible. This includes using &lt;code&gt;map&lt;/code&gt; and &lt;code&gt;filter&lt;/code&gt;. I have experience with selectors becoming a kind of repository of helper functions that do a lot of &lt;code&gt;map&lt;/code&gt;ping and &lt;code&gt;filter&lt;/code&gt;ing. Using tools like &lt;a href="https://github.com/reduxjs/reselect"&gt;&lt;code&gt;reselect&lt;/code&gt;&lt;/a&gt; can help with this by memoizing the return values of the selectors. &lt;/p&gt;

&lt;p&gt;Even so, some selectors might be better moved to helper functions, imported into the functions, and used to &lt;code&gt;map&lt;/code&gt; or &lt;code&gt;filter&lt;/code&gt; values pulled directly from state. Because a selector that pulls from state and then &lt;code&gt;map&lt;/code&gt;s or &lt;code&gt;filter&lt;/code&gt;s will return a new array and re-render every time, whereas using a helper function in the component would have the component only re-render when that value in state has changed.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Get only what is needed from state
&lt;/h2&gt;

&lt;p&gt;In selectors, fetch only what is needed in the component. For example, if I only want to check the count of some array of objects, I don't want to load the whole array into props, I just load the count for simpler shallow comparison.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. No anonymous functions as props
&lt;/h2&gt;

&lt;p&gt;I have seen and done this many times before realizing it was problematic:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;SomeComponent&lt;/span&gt;
  &lt;span class="nx"&gt;onError&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;BAD&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;
&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Every render of &lt;code&gt;SomeComponent&lt;/code&gt; will compare that function against its previous iteration, and since it's anonymous, it will be effectively a different function each time, resulting in shallow prop comparison failure. &lt;/p&gt;

&lt;p&gt;Instead, define functions outside the component and then pass in the named function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;logError&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;BAD&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;SomeComponent&lt;/span&gt;
  &lt;span class="nx"&gt;onError&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;logError&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;There are also some more complicated and helpful examples in the &lt;a href="https://github.com/welldone-software/why-did-you-render/issues/33"&gt;issue tracker&lt;/a&gt; for why-did-you-render.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Remember that React itself seems generally very performant, so it's important to try not to get bogged down in wiping out all unnecessary re-renders. With small enough components, even if they re-render all the time, it likely won't have noticeable affects on app performance. For me, I choose to focus on the big heavy component re-renders and any low-hanging fruit for the smaller components, and I don't sweat the other stuff.&lt;/p&gt;

&lt;h6&gt;
  
  
  &lt;em&gt;Image credit &lt;a href="https://unsplash.com/@louishansel?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Louis Hansel&lt;/a&gt;&lt;/em&gt;
&lt;/h6&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>performance</category>
      <category>redux</category>
    </item>
    <item>
      <title>Mocking with Jest and React Native</title>
      <dc:creator>Mark J. Lehman</dc:creator>
      <pubDate>Mon, 23 Jul 2018 16:00:00 +0000</pubDate>
      <link>https://dev.to/supremebeing7/mocking-with-jest-and-react-native-3mdn</link>
      <guid>https://dev.to/supremebeing7/mocking-with-jest-and-react-native-3mdn</guid>
      <description>&lt;p&gt;I’ve been building a React Native app and learning testing using &lt;a href="https://jestjs.io/"&gt;Jest&lt;/a&gt;. It has been an interesting journey, and I’m not going to go in-depth on much of my approaches to this test suite, other than to share a few links to &lt;a href="https://medium.com/react-native-training/learning-to-test-react-native-with-jest-part-1-f782c4e30101"&gt;articles&lt;/a&gt; I found helpful.&lt;/p&gt;

&lt;p&gt;Anyway, the meat and potatoes of this post is how to mock with Jest, specifically how I figured out how to mock a popular library called &lt;a href="https://github.com/aksonov/react-native-router-flux"&gt;&lt;code&gt;'react-native-router-flux'&lt;/code&gt;&lt;/a&gt; in my unit tests.&lt;/p&gt;

&lt;p&gt;I was testing an action following &lt;a href="https://pillow.codes/testing-in-react-native-jest-detox-d7b3b79a166a"&gt;the code samples in this post&lt;/a&gt;, and one of my actions routed to a new scene:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Actions.js
import { Actions } from 'react-native-router-flux'
// ...some codes...
Actions.replace('afterLoginScene')

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

&lt;/div&gt;



&lt;p&gt;I don’t feel the need to test that transition while unit testing that specific action, but the test tries to run that code and horks badly. So I want to stub or mock the &lt;code&gt;Actions&lt;/code&gt; somehow so I can test everything else in that action. After lots of trial and error and Googling, a colleague shared with me a &lt;a href="https://stackoverflow.com/a/45007792/3477163"&gt;StackOverflow post&lt;/a&gt; that had the key:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// testHelper.js
export default jest.mock(
  'react-native-router-flux', () =&amp;gt; ({
    Actions: {
      replace: jest.fn(),
      // whatever other Actions you use in your code
    },
  })
)


// Actions.test.js
import 'testHelper.js'
import { Actions } from 'react-native-router-flux'

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

&lt;/div&gt;



&lt;p&gt;Now I can test only that the expected actions are fired:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  const expectedActions = [
    { type: 'LOGIN/SET_LOGIN_ERROR', message: '' },
    { type: 'LOGIN/STORE_SOMETHING', something: 'stuff' },
  ]

  store
    .dispatch(LoginActions.sendLogin(username, password))
    .then(() =&amp;gt; {
      expect(store.getActions()).toEqual(expectedActions)
      done()
    })

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

&lt;/div&gt;






&lt;p&gt;Versions:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"jest": "22.4.4"
"jest-expo": "27.0.1"
"react-native": "0.55.2"
"react-native-router-flux": "4.0.0-beta.32"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
    </item>
  </channel>
</rss>
