<?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>Forem: Javier Marquez</title>
    <description>The latest articles on Forem by Javier Marquez (@arqex).</description>
    <link>https://forem.com/arqex</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%2F210039%2F9f401781-6b98-4471-97a0-cde457528c20.jpeg</url>
      <title>Forem: Javier Marquez</title>
      <link>https://forem.com/arqex</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/arqex"/>
    <language>en</language>
    <item>
      <title>Testing with React Testing Library using component instances</title>
      <dc:creator>Javier Marquez</dc:creator>
      <pubDate>Tue, 21 Apr 2020 16:21:51 +0000</pubDate>
      <link>https://forem.com/arqex/testing-with-react-testing-library-using-component-instances-nl6</link>
      <guid>https://forem.com/arqex/testing-with-react-testing-library-using-component-instances-nl6</guid>
      <description>&lt;p&gt;In the last year, I followed the trend in the industry, and I changed the way I test my React components from using &lt;a href="https://github.com/enzymejs/enzyme" rel="noopener noreferrer"&gt;Enzyme&lt;/a&gt; to embrace &lt;a href="https://github.com/testing-library/react-testing-library" rel="noopener noreferrer"&gt;Testing Library&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The change has been for good! Now my tests are now more concise, clear and meaningful. They are especially easier to read because they don't have any code related to the component internals. And I love how one test can cover the interaction among many components, reaching very deep in the component tree.&lt;/p&gt;

&lt;p&gt;But... sometimes I miss Enzyme.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why to test React component instances
&lt;/h2&gt;

&lt;p&gt;When switching to Testing Library, we focus on the UI trying to avoid any contact with the internal implementation of our React components. Our tests become like final users, that know how to detect elements, click on them, type on the inputs... and our app should just work, no matter how things are handled in the background.&lt;/p&gt;

&lt;p&gt;I have also discussed with my workmates about what's the difference with &lt;a href="https://www.selenium.dev/" rel="noopener noreferrer"&gt;Selenium&lt;/a&gt; or &lt;a href="https://www.cypress.io/" rel="noopener noreferrer"&gt;Cypress&lt;/a&gt; tests then? There should be any?&lt;/p&gt;

&lt;p&gt;There are a lot of differences between the end to end tests we create with Selenium/Cypress to check our systems integration, and our deep React testing that should follow the specs without messing with servers or API interaction at all. But I see how there is some overlapping, and I understand people that get confused, especially if we see our React components only as UI.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;React components are more than UI&lt;/strong&gt;, they also contain logic, functionality that sometimes is much harder to test emulating a user clicking buttons, than with a good unit test. &lt;/p&gt;

&lt;h2&gt;
  
  
  Explosion of tests
&lt;/h2&gt;

&lt;p&gt;Before showing an example of why sometimes it's nice to have access to the component instances in our testing, I'll share &lt;a href="https://github.com/arqex/react-dom-instance" rel="noopener noreferrer"&gt;react-dom-instance&lt;/a&gt;, a library to &lt;strong&gt;find the component instances bound to DOM elements&lt;/strong&gt;. It plays really nicely with React Testing Library, without the need of re-installing Enzyme again.&lt;/p&gt;

&lt;p&gt;When creating tests with Testing Library, there is an issue that I stumble upon often. It's about having my logic is in a parent component and a(many) stateful child component(s) that I need to interact with multiple times in order to check if the parent does what I expect.&lt;/p&gt;

&lt;p&gt;Let's say we have this todo list app (credits to &lt;a href="https://codepen.io/hotate17" rel="noopener noreferrer"&gt;Fumie Wada&lt;/a&gt;), rendered by the component &lt;code&gt;&amp;lt;TodoApp /&amp;gt;&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F5fi1r9sgzce6gv2lqb5c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F5fi1r9sgzce6gv2lqb5c.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When we click on the "Create new item" link at the top right, a form is open to let us type a new todo item. The form is rendered by the &lt;code&gt;&amp;lt;CreateForm /&amp;gt;&lt;/code&gt; component:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F0h2j57z3u8e6zcsh85z4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F0h2j57z3u8e6zcsh85z4.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When the user opens the form, type the text of the item, check if it's important, select a color and click on the "Add" button, our &lt;code&gt;TodoApp&lt;/code&gt; component should:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a new todo item with the text typed and the color selected.&lt;/li&gt;
&lt;li&gt;If it was marked as important, the item is also added to another list.&lt;/li&gt;
&lt;li&gt;Send an event to google analytics with the color selected, if it wasn't marked as important 🤷&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In our &lt;code&gt;TodoApp.spec.js&lt;/code&gt; file, we would have a test for this logic that would be something like:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="nf"&gt;it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;should create an important item&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="o"&gt;=&amp;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;queryByTestId&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;render&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;TodoApp&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nx"&gt;fireEvent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;click&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nf"&gt;queryByTestId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;openButton&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="nx"&gt;fireEvent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nf"&gt;queryByTestId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;todoInput&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="na"&gt;target&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Buy some bread&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="nx"&gt;fireEvent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;click&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nf"&gt;queryByTestId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;color_red&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="nx"&gt;fireEvent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;click&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nf"&gt;queryByTestId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;importantCheckbox&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="nx"&gt;fireEvent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;click&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nf"&gt;queryByTestId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;addButton&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="c1"&gt;// We had mocked some actions beforehand&lt;/span&gt;
  &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nx"&gt;createItem&lt;/span&gt; &lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toHaveBeenCalledWith&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;id1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Buy some bread&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;red&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nx"&gt;addToImportant&lt;/span&gt; &lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toHaveBeenCalledWith&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;id1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nx"&gt;trackGAEvent&lt;/span&gt; &lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;not&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toHaveBeenCalled&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;We are testing so mucho up there, and there are many things that could be done better, but just ignore them for the sake of the example. &lt;/p&gt;

&lt;p&gt;Let's focus on how the color that we clicked in a child component, it's the color that we are creating the todo with. We have many colors in the form, should we test all of them?&lt;/p&gt;

&lt;p&gt;If we have not enabled the "Mark as important" option, should we check that we are tracking all the colors in Google Analytics properly again?&lt;/p&gt;

&lt;p&gt;&lt;code&gt;TodoApp&lt;/code&gt; component tests &lt;strong&gt;shouldn't care about how many colors are, instead they should focus on completing the expected tasks once any color has been selected&lt;/strong&gt;. Not testing all the colors feels just bad, but all the clicking and typing we have to do for every test look very repetitive too.&lt;/p&gt;

&lt;p&gt;The ability of React Testing Library to test nested components working together is amazing, but &lt;strong&gt;it tends to move the checks to the top of the React tree&lt;/strong&gt;. When the children have many settings, we end up with really big test files in the top components, and those tests are usually made by repetitive tasks with small permutations of the checks we have to do. The more options we have in our child components, the bigger is the rate of growth for test cases in the parent component... it's like an explosion of tests.&lt;/p&gt;

&lt;h2&gt;
  
  
  Splitting test files using instances
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;CreateForm&lt;/code&gt; component has no complex logic, simply let the user type a name, select if it's important and pick a color. It doesn't know what to do with that information but, for example, it's responsible for how many colors are available to pick.&lt;/p&gt;

&lt;p&gt;We can listen to the user's selection thanks to the &lt;code&gt;onAdd&lt;/code&gt; prop, so a test file for &lt;code&gt;CreateForm&lt;/code&gt; looks like the perfect place for testing all the colors that are available:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="nf"&gt;it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;should select the color red when clicking on the red input&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;onAddMock&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;jest&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fn&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;queryByTestId&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;render&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;CreateForm&lt;/span&gt; &lt;span class="nx"&gt;onAdd&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;onAddMock&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;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nx"&gt;fireEvent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;click&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nf"&gt;queryByTestId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;color_red&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="nx"&gt;fireEvent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;click&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nf"&gt;queryByTestId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;addButton&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="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nx"&gt;onAddMock&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;mock&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;calls&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;color&lt;/span&gt; &lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toBe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;red&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="c1"&gt;// ...the same for the rest of the colors&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;That's a simple and well-scoped test. Once we had tested all the colors individually for the &lt;code&gt;CreateForm&lt;/code&gt;, we don't need to test them again for &lt;code&gt;TodoApp&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;We can trust that the rendered &lt;code&gt;CreateForm&lt;/code&gt; instance will provide no-matter-what color and check the logic without all the clicking, but be sure that the components are integrated properly:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;findInstance&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react-dom-instance&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nf"&gt;it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;should create an important item&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="o"&gt;=&amp;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;queryByTestId&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;render&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;TodoApp&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;createForm&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;findInstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nf"&gt;queryByTestId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;createForm&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="c1"&gt;// call the `onAdd` handler passed by `TodoApp` directly&lt;/span&gt;
  &lt;span class="nx"&gt;createForm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;onAdd&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;text&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Buy some bread&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;whatever&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;isImportant&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="c1"&gt;// We had mocked some actions beforehand&lt;/span&gt;
  &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nx"&gt;createItem&lt;/span&gt; &lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toHaveBeenCalledWith&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;id1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Buy some bread&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;whatever&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nx"&gt;addToImportant&lt;/span&gt; &lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toHaveBeenCalledWith&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;id1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nx"&gt;trackGAEvent&lt;/span&gt; &lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;not&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toHaveBeenCalled&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;In the &lt;code&gt;TodoApp&lt;/code&gt; tests, we went from &lt;em&gt;"I don't care how it works internally, just click things and see"&lt;/em&gt; to &lt;em&gt;"I don't care what the user clicks, I expect to receive this"&lt;/em&gt;. &lt;strong&gt;We are still not using the internals of the components, but we making the most of knowing their surface&lt;/strong&gt;, their API. &lt;/p&gt;

&lt;p&gt;It's not that we are not testing what user clicks, it's that we not need to repeat ourselves testing it in places that shouldn't depend on the exact user interaction. This approach has some advantages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Tests cases are better scoped, besides their source code.&lt;/li&gt;
&lt;li&gt;Changes in the &lt;code&gt;CreateForm&lt;/code&gt; won't be breaking &lt;code&gt;TodoApp&lt;/code&gt; tests, or, at least, we won't require multiple changes in it.&lt;/li&gt;
&lt;li&gt;No more big testing files for parent components with exponential growth of test cases when adding UI options.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Not that bad huh?&lt;/p&gt;

&lt;h2&gt;
  
  
  When to use instances in our React tests
&lt;/h2&gt;

&lt;p&gt;Splitting big test files is a nice example of how instances are handy in the way we test React components. There are other cases, like testing component's imperative methods, where they can be of help too.&lt;/p&gt;

&lt;p&gt;But keep in mind that the fact that we can use instances in our tests, doesn't mean that we should do it everywhere. This not a matter of taste, like in &lt;em&gt;"I prefer user interaction"&lt;/em&gt; or &lt;em&gt;"I prefer instances"&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;It's about finding the sweet spot where we can do deep tests of a group of components together by emulating user interaction, while we can abstract that complexity from other places, where the user interaction is not the main thing to test.&lt;/p&gt;

&lt;p&gt;I know that saying "finding the sweet spot" doesn't help on when to use instances, but it's not that hard. We all know when a test file is getting out of control. At that moment, identify an independent unit of functionality and extract its tests to its own file. With a little bit of practice, we'll quickly learn to foresee when it's a good idea to split :)&lt;/p&gt;




&lt;p&gt;This is my first article in DEV.to and I enjoyed writing it! If you liked it, follow me, give me love and unicorns, and sure I'll write much more!&lt;/p&gt;

&lt;p&gt;My twitter is &lt;a href="https://twitter.com/arqex" rel="noopener noreferrer"&gt;@arqex&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>testing</category>
      <category>react</category>
      <category>functional</category>
    </item>
  </channel>
</rss>
