<?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 Hugo Peyron on Medium]]></title>
        <description><![CDATA[Stories by Hugo Peyron on Medium]]></description>
        <link>https://medium.com/@itboy06?source=rss-523226ef8cf2------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*5DQvzlIb-oLuNFrB.jpg</url>
            <title>Stories by Hugo Peyron on Medium</title>
            <link>https://medium.com/@itboy06?source=rss-523226ef8cf2------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 07:11:38 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@itboy06/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[A gentle introduction to testing-library for testing React components and more.]]></title>
            <link>https://medium.com/swlh/a-gentle-introduction-to-testing-library-for-testing-react-components-and-more-692e46104d6?source=rss-523226ef8cf2------2</link>
            <guid isPermaLink="false">https://medium.com/p/692e46104d6</guid>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[test]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[react-testing-library]]></category>
            <dc:creator><![CDATA[Hugo Peyron]]></dc:creator>
            <pubDate>Sun, 03 May 2020 14:40:16 GMT</pubDate>
            <atom:updated>2020-05-04T17:07:10.963Z</atom:updated>
            <content:encoded><![CDATA[<h3>A gentle introduction to react-testing-library for testing React components and more.</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*k9IisF0AfbYJi7lHQmOiLg.png" /></figure><p>The test phase is an essential step in the realisation of an IT project. Indeed, more and more jobs in the IT domain will require a knowledge of one or more framework/library/testing tools. Regarding the development of React applications, developers generally rely on the <strong>Jest / Enzyme</strong> combo.</p><p>For the ones who are not yet familiar with these terms, I will briefly describe these two technologies. Jest is a test runner, that is to say, a javascript framework to write, structure and execute tests. It also allows mocking, meaning the replacement of objects. This feature is very useful when we want to check that the functions we pass to our components are called with the right arguments. Finally, Jest also gives you the possibility to do code coverage. Behind this computer term lies simply the fact of measuring the proportion of your code that is tested. Regarding Enzyme, it is a tool developed by Airbnb specifically to interact with your React components in order to test them.</p><p>Personally, I find that Enzyme is a rather complex tool to master. Indeed, I had to spend many hours reading their documentation and related articles to understand how it works. For instance, to test a component, you have several ways to do it. Indeed, to perform this task, Enzyme provides you with 3 functions: shallow, render or mount. Between these three functions, the first one is the one we use most often. Its objective is relatively simple. To test the component, the function will render it without rendering the children React components. The goal behind all this is to respect the principle of “unit” tests, i.e. to test one component at a time. However, this approach leads to so-called <strong>false positives</strong> and<strong> false negatives</strong>. Below are <a href="https://kentcdodds.com/blog/why-i-never-use-shallow-rendering">two quotes</a> from <em>Kent C. Dodds</em> defining these two terms respectively.</p><blockquote><strong><em>With shallow rendering, I can break my application and my tests say everything’s still working.</em></strong></blockquote><blockquote><strong><em>With shallow rendering, I can refactor my component’s implementation and my tests break.</em></strong></blockquote><p>By reading Mr. Dodds’ article, I discovered that there was an alternative to Enzyme called <strong>React Testing Library</strong>. On their website, the developers of the library define it as <em>“a very light-weight solution for testing without all the implementation details”</em>. By <em>“implementation details”</em>, they refer to anything that the users of an application do not see, use or know about.</p><p>After using this library on a few projects, I have to say that I’m rather smitten with it. That’s why, in this article, I propose you to have a small glimpse of it through one of my applications. As you may have seen on the cover, I will use the library to test my Todolist application.</p><p><em>Note: You can access the application code by clicking on this </em><a href="https://github.com/ITBoy06/TodoList"><em>link</em></a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*u8Pt9dgx3-SjKaGScEeUVQ.png" /><figcaption>TodoList Wireframes</figcaption></figure><p>Only 4 of the 5 visible components will be tested. Indeed, the MenuBar is only a decorative component and in my opinion does not need to be tested. Now, I propose that we look at the tests for each of the components in order of complexity.</p><h3>TodoWrapper</h3><p>If you are wondering about the usefulness of this component, I use it mainly as a container to place the TodoForm and TodoList components in the center of the screen.</p><p>I also use it to conditionally display the last component. Indeed, TodoWrapper queries my React context (TodoContext) to get a list of the registered todo. If the context returns an empty array then it does not display the TodoList component and vice-versa. Let’s see how we can check that the component performs this task.</p><pre><em>import </em>React <em>from </em>&quot;react&quot;;<br><em>import </em>{ <em>render</em>, screen, <em>cleanup </em>} <em>from </em>&quot;@testing-library/react&quot;;<br><em>import </em>TodoContext <em>from </em>&quot;../../../contexts/TodoContext&quot;;<br><em>import TodoWrapper from </em>&quot;./Wrapper&quot;;<br><br><em>const </em>todosList = [{ id: &quot;1&quot;, text: &quot;todo 1&quot;, checked: <em>false </em>}];<br><br><em>const </em>renderTodoProvider = (component, todos = todosList) =&gt; {<br>  <em>const </em>value = {<br>    todos,<br>  };<br><br>  <em>render</em>(<br>    &lt;TodoContext.Provider <em>value</em>={value}&gt;{component}&lt;/TodoContext.Provider&gt;<br>  );<br>};<br><br>afterEach(<em>async </em>() =&gt; {<br>  <em>await cleanup</em>();<br>});<br><br>describe(&quot;&lt;TodoWrapper /&gt;&quot;, () =&gt; {<br>  it(&quot;should render correctly&quot;, () =&gt; {<br>    <em>const </em>{ asFragment } = <em>render</em>(&lt;TodoWrapper /&gt;);<br><br>    expect(asFragment()).<em>toMatchSnapshot</em>();<br>  });<br><br>  it(&quot;should display the ItemList as there are todo items&quot;, () =&gt; {<br>    renderTodoProvider(&lt;TodoWrapper /&gt;);<br><br>    expect(screen.<em>getByTestId</em>(&quot;todo-list&quot;)).<em>toBeInTheDocument</em>();<br>  });<br><br>  it(&quot;should not display the ItemList as there are no todo items&quot;, () =&gt; {<br>    renderTodoProvider(&lt;TodoWrapper /&gt;, []);<br><br>    expect(screen.<em>queryByTestId</em>(&quot;todo-list&quot;)).not.toBeInTheDocument();<br>  });<br>});</pre><p>In this piece of code, I will explain the purpose of the following functions: <strong>render</strong>, <strong>cleanup</strong> as well as the <strong>toBeInTheDocument</strong> assertion and the <strong>screen</strong> object. Therefore I assume you have some knowledge about assertions with Jest and the use of context consumer and context provider. If not, I invite you to read the following articles: <a href="https://medium.com/javascript-in-plain-english/writing-your-first-unit-test-with-jest-b4953dabb931"><em>Jest’s article</em></a><em>, </em><a href="https://medium.com/@chathuranga94/introduction-to-react-context-api-90f5e4d7a7a9"><em>Context’s article</em></a><em> </em>before coming back to this one.</p><p><em>Note: The renderTodoProvider function is a simple helper function allowing me to avoid writing the provider and the consumer of my context in each of the tests requiring the use of the latter.</em></p><p>Previously, I provided a definition of the <strong>react-testing-library</strong>. However, this definition was incomplete. Indeed, it is worth noting that this library belongs to the <strong>DOM Testing Library</strong> family. Therefore, there is a library for React but also Angular or Vue.js. However, the principle of these libraries remains the same. In the documentation of this library family, we can read that: ”<em>The </em><em>DOM Testing Library is a very light-weight solution for testing DOM nodes (whether simulated with </em><em>JSDOM as provided by default with Jest or in the browser). The main utilities it provides involve querying the DOM for nodes in a way that&#39;s similar to how the user finds elements on the page. In this way, the library helps ensure your tests give you confidence in your UI code”.</em><strong><em> </em>This is how we test our React components by mounting them in a DOM.</strong></p><p>The mounting phase of the components is ensured in particular through the <strong>render</strong> function. As for the <strong>screen</strong> object, it allows us to <strong>access certain elements of the DOM through various queries</strong>. Its use is almost similar to that of the document.querySelector specific to vanilla JS. In my test file, you can see that I use the <strong>getByTestId query</strong> with the argument “todo-list” as a parameter. To make a similarity with Vanilla JS, it’s as if I was trying to intercept the HTML element with the id attribute set to “todo-list”. However in our case, we do not intercept the HTML element with an id attribute set to “todo-list” but the one with the <strong>data-testid attribute</strong> set to “todo-list”. Finally, we use the <strong>cleanup</strong> function to <strong>remove the component we have mounted in the DOM. </strong>For the test part, I check if the TodoList component is displayed or not in the DOM via the <strong>toBeInTheDocument</strong> assertion.</p><p><em>Note: The assertion named above is not imported by default. To use it, I recommend that you first create a file in your src folder named </em><strong><em>setupTests.js</em></strong><em>. Then add the following line of code.</em></p><pre><em>import </em>&quot;@testing-library/jest-dom/extend-expect&quot;;</pre><p>To have access to this “@testing-library”module, don’t forget to install the test library using one of the following commands.</p><pre>npm install --save-dev @testing-library/react</pre><pre>yarn add --dev @testing-library/react</pre><h3>TodoList</h3><p>As you can imagine, the test we will perform on this component consists in checking that my todo items are well displayed.</p><pre><em>import </em>React <em>from </em>&quot;react&quot;;<br><em>import </em>{ <em>render</em>, screen, <em>cleanup </em>} <em>from </em>&quot;@testing-library/react&quot;;<br><em>import TodoList from </em>&quot;./List&quot;;<br><br><em>const </em>todos = [<br>  { id: &quot;1&quot;, text: &quot;todo 1&quot;, checked: <em>false </em>},<br>  { id: &quot;2&quot;, text: &quot;todo 2&quot;, checked: <em>true </em>},<br>];<br><br>afterEach(<em>async </em>() =&gt; {<br>  <em>await cleanup</em>();<br>});<br><br>describe(&quot;&lt;TodoList /&gt;&quot;, () =&gt; {<br>  it(&quot;should render correctly&quot;, () =&gt; {<br>    <em>const </em>{ asFragment } = <em>render</em>(&lt;TodoList <em>todos</em>={todos} /&gt;);<br><br>    expect(asFragment()).<em>toMatchSnapshot</em>();<br>  });<br><br>  it(&quot;should render the correct number of todo items&quot;, () =&gt; {<br>    <em>render</em>(&lt;TodoList <em>todos</em>={todos} /&gt;);<br><br>    expect(screen.<em>getAllByRole</em>(&quot;listitem&quot;)).<em>toHaveLength</em>(todos.length);<br>  });<br>});</pre><p>In the code above, you can note that I use a new query to retrieve some elements of the DOM: <strong>getAllByRole</strong>. This last one returns an array of all the nodes whose role matches the one passed as argument of the query. <strong>Be aware that the role I mention corresponds to the ARIA role of each HTML element.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Wn_iJMV5hqhLAzdzQC_LwA.png" /><figcaption>Mapping HTML elements to their respective ARIA role / Source:<a href="https://github.com/A11yance/aria-query#elements-to-roles">https://github.com/A11yance/aria-query#elements-to-roles</a></figcaption></figure><p><em>Note: You may have noticed that in all my test files, I run a test for which the component must be rendered correctly. In this test, I simply check that the UI of my component has not changed </em>unexpectedly<em>. To achieve this at each test, the </em><strong><em>asFragment</em></strong><em> function is called to take a snapshot of my component at a given time and then the assertion compares this snapshot to the one taken during the previous test.</em></p><h3>TodoForm</h3><p>For this component, we want to check that when submitting the form, the <strong>addTodo</strong> function provided by my <strong>TodoContext</strong> is correctly called. This means checking that the function is called with the right arguments, i.e. with the value displayed by the input. If you want to improve the logic of the component, you could create a test verifying that the form is not submitted if the value of the input is an empty string and implement this feature.</p><pre><em>import </em>React <em>from </em>&quot;react&quot;;<br><em>import </em>{ <em>render</em>, screen, fireEvent, <em>cleanup </em>} <em>from </em>&quot;@testing-library/react&quot;;<br><em>import </em>TodoContext <em>from </em>&quot;../../../contexts/TodoContext&quot;;<br><em>import TodoForm from </em>&quot;./Form&quot;;<br><br><em>const </em>addTodo = jest.<em>fn</em>().mockImplementation((text) =&gt; {});<br><br><em>const </em>renderTodoProvider = (component) =&gt; {<br>  <em>const </em>value = {<br>    addTodo,<br>  };<br><br>  <em>render</em>(<br>    &lt;TodoContext.Provider <em>value</em>={value}&gt;{component}&lt;/TodoContext.Provider&gt;<br>  );<br>};<br><br>beforeEach(<em>async </em>() =&gt; {<br>  <em>await cleanup</em>();<br>});<br><br>describe(&quot;&lt;TodoForm /&gt;&quot;, () =&gt; {<br>  it(&quot;should render correctly&quot;, () =&gt; {<br>    <em>const </em>{ asFragment } = <em>render</em>(&lt;TodoForm /&gt;);<br><br>    expect(asFragment()).<em>toMatchSnapshot</em>();<br>  });<br><br>  it(&quot;should call addTodo after submitting the form&quot;, () =&gt; {<br>    renderTodoProvider(&lt;TodoForm /&gt;);<br><br>    fireEvent.<em>change</em>(screen.<em>getByRole</em>(&quot;textbox&quot;), {<br>      target: { value: &quot;Work on GAN&quot; },<br>    });<br><br>    fireEvent.<em>submit</em>(screen.<em>getByRole</em>(&quot;textbox&quot;));<br><br>    expect(addTodo).<em>toHaveBeenCalled</em>();<br>    expect(addTodo).<em>toHaveBeenCalledWith</em>(&quot;Work on GAN&quot;);<br>  });<br>});</pre><p>The test of this component is the opportunity to tackle a new function provided by the test library: <strong>fireEvent</strong>. This function allows to interact with the nodes of our DOM. Thus with it, it is possible to fill a form by modifying the value of the inputs but also to submit it or to click on buttons.</p><p>To check that the addTodo function is called with the right arguments, I mocked a function. As a reminder, this is a feature offered by Jest. For information, the mocked function is not the one my TodoContext uses because it was already tested separately. As a matter of fact, I will show in a next section that it is also possible to test contexts with this library.</p><h3>TodoItem</h3><p>Let’s now discuss the tests concerning the TodoItem component, but first of all, let’s remind ourselves how this component was designed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bjAnZk9ZCMS1e7aB7j6k_Q.png" /></figure><p>My readers will note that this component must first display the task to be done. Then it must allow the user to mark the task as having been completed via a checkbox. The user can also delete the task by clicking on the dustbin icon. Finally, this component also gives the ability to modify the task to be accomplished. To do so, a form appears as soon as the edit button is clicked. Once the form is submitted, the default view reappears and displays the modified todo.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*3fNa2D_5jCFNTuQTYcMrFg.gif" /></figure><p>To verify that all these functionalities are operational, we need to check the following points:</p><p><strong>. Check that the toggleTodo function provided by our TodoContext is called when you click on the checkbox to modify the checked property of the todo.</strong></p><p><strong>. Ensure that the deleteTodo function provided by our TodoContext is called when clicking on the checkbox and that it takes the todo id as parameter.</strong></p><p><strong>. We must also make sure that the todo edit form is displayed when the edit button is clicked.</strong></p><p><strong>. When submitting the edit form, we need to verify that the editTodo function again provided by the TodoContext is called, taking as parameter the id of the todo to be modified and the new task to be performed. Finally, we will check that the default view is re-displayed soon after the todo is updated.</strong></p><p>Hereunder, you will find my code proposal to verify that the features work properly.</p><pre><em>import </em>React <em>from </em>&quot;react&quot;;<br><em>import </em>{ <em>render</em>, screen, fireEvent, <em>cleanup </em>} <em>from </em>&quot;@testing-library/react&quot;;<br><em>import </em>TodoContext <em>from </em>&quot;../../../contexts/TodoContext&quot;;<br><em>import TodoItem from </em>&quot;./Item&quot;;<br><br><em>const </em>deleteTodo = jest.<em>fn</em>().mockImplementation((id) =&gt; {});<br><em>const </em>editTodo = jest.<em>fn</em>().mockImplementation((id, text) =&gt; {});<br><em>const </em>toggleTodo = jest.<em>fn</em>().mockImplementation(() =&gt; {});<br><br><em>const </em>renderTodoProvider = (component) =&gt; {<br>  <em>const </em>value = {<br>    deleteTodo,<br>    editTodo,<br>    toggleTodo,<br>  };<br><br>  <em>render</em>(<br>    &lt;TodoContext.Provider <em>value</em>={value}&gt;{component}&lt;/TodoContext.Provider&gt;<br>  );<br>};<br><br>afterEach(<em>async </em>() =&gt; {<br>  <em>await cleanup</em>();<br>});<br><br>describe(&quot;&lt;TodoItem /&gt;&quot;, () =&gt; {</pre><pre>  <em>const </em>todo = { id: &quot;1&quot;, text: &quot;testing TodoItem&quot;, checked: <em>true </em>};</pre><pre><br>  it(&quot;should render correctly&quot;, () =&gt; {<br>    <em>const </em>{ asFragment } = <em>render</em>(&lt;TodoItem <em>todo</em>={todo} /&gt;);<br><br>    expect(asFragment()).<em>toMatchSnapshot</em>();<br><br>    <em>// We verify that the content of the todoItem is correctly displayed<br>    </em>expect(screen.<em>getByText</em>(&quot;testing TodoItem&quot;)).<em>toBeInTheDocument</em>();<br>    expect(screen.<em>getByRole</em>(&quot;checkbox&quot;)).<em>toBeChecked</em>();<br>  });</pre><pre>  it(&quot;should call toggleTodo after clicking on the checkbox&quot;, () =&gt; {<br>    renderTodoProvider(&lt;TodoItem <em>todo</em>={todo} /&gt;);<br><br>    fireEvent.<em>click</em>(screen.<em>getByRole</em>(&quot;checkbox&quot;));<br><br>    expect(toggleTodo).<em>toHaveBeenCalled</em>();<br>  });</pre><pre>  it(&quot;should call deleteTodo with the correct id after clicking on the trash icon button&quot;, () =&gt; {<br>    renderTodoProvider(&lt;TodoItem <em>todo</em>={todo} /&gt;);<br><br>    fireEvent.<em>click</em>(screen.<em>getByTestId</em>(&quot;delete-button&quot;));<br><br>    expect(deleteTodo).<em>toHaveBeenCalled</em>();<br>    expect(deleteTodo).<em>toHaveBeenCalledWith</em>(&quot;1&quot;);<br>  });</pre><pre>  it(&quot;should display the edit form after clicking on the edit icon button&quot;, () =&gt; {<br>    renderTodoProvider(&lt;TodoItem <em>todo</em>={todo} /&gt;);<br><br>    fireEvent.<em>click</em>(screen.<em>getByTestId</em>(&quot;edit-button&quot;));<br><br>    expect(screen.<em>getByRole</em>(&quot;textbox&quot;)).<em>toBeInTheDocument</em>();<br>  });</pre><pre>  it(&quot;should call editTodo with correct id and value after submitting the form&quot;, () =&gt; {<br>    renderTodoProvider(&lt;TodoItem <em>todo</em>={todo} /&gt;);<br><br>    fireEvent.<em>click</em>(screen.<em>getByTestId</em>(&quot;edit-button&quot;));<br><br>    fireEvent.<em>change</em>(screen.<em>getByRole</em>(&quot;textbox&quot;), {<br>      target: { value: &quot;Watch Mister Robot&quot; },<br>    });<br><br>    fireEvent.<em>submit</em>(screen.<em>getByRole</em>(&quot;textbox&quot;));<br><br>    expect(editTodo).<em>toHaveBeenCalled</em>();<br>    expect(editTodo).<em>toHaveBeenCalledWith</em>(&quot;1&quot;, &quot;Watch Mister Robot&quot;);<br><br>    <em>// Check that the edit form is not displayed after submitting<br>    </em>expect(screen.<em>queryByRole</em>(&quot;textbox&quot;)).not.toBeInTheDocument();<br>  });<br>});</pre><p>After reading my code, you will realise that I used a new query: <strong>screen.queryByRole</strong>. You are probably wondering what the difference is between <strong>getByRole</strong> and <strong>queryByRole</strong>. In practice, these two functions lead to the same result, i.e. they will select the first element whose ARIA role is equivalent to the one passed as parameter of the query. However, if they don’t find the element they are looking for, this is where the two functions behave differently. Indeed, <strong>if the query getByRole doesn’t find the searched element, this function will throw an error while the second query will simply return null.</strong> This is the reason why I used queryByRole because I wanted to check that the targeted element was no longer present in the DOM.</p><h3>TodoContext</h3><p>In one of the previous sections, I stressed that it was possible to test each of the functions of the TodoContext in a unit way. The following lines of code will show you the approach I chose to do so.</p><pre><em>import </em>React <em>from </em>&quot;react&quot;;<br><em>import </em>{ <em>render</em>, screen, fireEvent, <em>cleanup </em>} <em>from </em>&quot;@testing-library/react&quot;;<br><em>import </em>TodoContext, { TodoStore } <em>from </em>&quot;./TodoContext&quot;;<br><br>afterEach(<em>async </em>() =&gt; {<br>  <em>await cleanup</em>();<br>});<br><br><em>const </em>renderTodoStore = (consumerFunc, todos = undefined) =&gt; {<br>  <em>render</em>(<br>    &lt;TodoStore <em>todos</em>={todos}&gt;<br>      &lt;TodoContext.Consumer&gt;{consumerFunc}&lt;/TodoContext.Consumer&gt;<br>    &lt;/TodoStore&gt;<br>  );<br>};<br><br>describe(&quot;TodoContext&quot;, () =&gt; {</pre><pre>  it(&quot;should show zero todos&quot;, () =&gt; {<br>    renderTodoStore(({ todos }) =&gt; {<br>      <em>return </em>todos.map((todo) =&gt; &lt;span <em>key</em>={todo.id}&gt;{todo.text}&lt;/span&gt;);<br>    });<br><br>    expect(screen.<em>queryAllByRole</em>(&quot;span&quot;)).<em>toHaveLength</em>(0);<br>  });</pre><pre>  it(&quot;should display a todo after adding one&quot;, () =&gt; {<br>    renderTodoStore(({ todos, addTodo }) =&gt; (<br>      &lt;div&gt;<br>        {todos.map((todo) =&gt; (<br>          &lt;span <em>key</em>={todo.id}&gt;{todo.text}&lt;/span&gt;<br>        ))}<br>        &lt;button <em>onClick</em>={() =&gt; addTodo(&quot;test adding a todo&quot;)}&gt;<br>          Add a <em>todo<br>        </em>&lt;/button&gt;<br>      &lt;/div&gt;<br>    ));<br><br>    fireEvent.<em>click</em>(screen.<em>getByRole</em>(&quot;button&quot;));<br><br>    expect(screen.<em>getByText</em>(&quot;test adding a todo&quot;)).<em>toBeInTheDocument</em>();<br>  });</pre><pre>  it(&quot;should remove a todo from the document after deleting it&quot;, () =&gt; {<br>    renderTodoStore(<br>      ({ todos, deleteTodo }) =&gt; (<br>        &lt;div&gt;<br>          {todos.map((todo) =&gt; (<br>            &lt;span <em>key</em>={todo.id}&gt;{todo.text}&lt;/span&gt;<br>          ))}<br>          &lt;button <em>onClick</em>={() =&gt; deleteTodo(&quot;1&quot;)}&gt;Delete TodoItem&lt;/button&gt;<br>        &lt;/div&gt;<br>      ),<br>      [{ id: &quot;1&quot;, text: &quot;test deleting todo&quot;, checked: <em>false </em>}]<br>    );<br><br>    <em>// First we verify that our TodoItem is present in the dom<br>    </em>expect(screen.<em>getByText</em>(&quot;test deleting todo&quot;)).<em>toBeInTheDocument</em>();<br><br>    fireEvent.<em>click</em>(screen.<em>getByText</em>(&quot;Delete TodoItem&quot;));<br><br>    <em>// The TodoItem should have been removed<br>    </em>expect(screen.<em>queryByText</em>(&quot;test deleting todo&quot;)).not.toBeInTheDocument();<br>  });</pre><pre>  it(&quot;should edit the content of the todo item&quot;, () =&gt; {<br>    renderTodoStore(<br>      ({ todos, editTodo }) =&gt; (<br>        &lt;div&gt;<br>          {todos.map((todo) =&gt; (<br>            &lt;span <em>key</em>={todo.id}&gt;{todo.text}&lt;/span&gt;<br>          ))}<br>          &lt;button <em>onClick</em>={() =&gt; editTodo(&quot;1&quot;, &quot;Watch Mister Robot&quot;)}&gt;<br>            Edit TodoItem<br>          &lt;/button&gt;<br>        &lt;/div&gt;<br>      ),<br>      [{ id: &quot;1&quot;, text: &quot;test editing todo&quot;, checked: <em>false </em>}]<br>    );<br><br>    <em>// First we verify that our TodoItem is present in the dom<br>    </em>expect(screen.<em>getByText</em>(&quot;test editing todo&quot;)).<em>toBeInTheDocument</em>();<br><br>    fireEvent.<em>click</em>(screen.<em>getByText</em>(&quot;Edit TodoItem&quot;));<br><br>    <em>// The TodoItem should have been edited<br>    </em>expect(screen.<em>queryByText</em>(&quot;Watch Mister Robot&quot;)).<em>toBeInTheDocument</em>();<br>  });</pre><pre>  it(&quot;should mark the todo item as being checked&quot;, () =&gt; {<br>    renderTodoStore(<br>      ({ todos, toggleTodo }) =&gt; {<br>        <em>return </em>todos.map((todo) =&gt; (<br>          &lt;input<br>            <em>key</em>={todo.id}<br>            <em>type</em>=&quot;checkbox&quot;<br>            <em>onChange</em>={() =&gt; toggleTodo(todo.id)}<br>            <em>checked</em>={todo.checked}<br>          /&gt;<br>        ));<br>      },<br>      [{ id: &quot;1&quot;, text: &quot;test editing todo&quot;, checked: <em>false </em>}]<br>    );<br><br>    <em>// First we verify that our TodoItem is present in the DOM<br>    </em>expect(screen.<em>getByRole</em>(&quot;checkbox&quot;)).not.toBeChecked();<br><br>    fireEvent.<em>click</em>(screen.<em>getByRole</em>(&quot;checkbox&quot;));<br><br>    <em>// The TodoItem should have been marked as being checked<br>    </em>expect(screen.<em>getByRole</em>(&quot;checkbox&quot;)).<em>toBeChecked</em>();<br>  });<br>});</pre><p>This last test file helps me to show you another query you can use to target DOM elements: <strong>getByText</strong>. <strong>This query will retrieve the first element whose textContent matches the one passed as parameter of the query.</strong></p><h3>Bonus</h3><p>If at any time in your tests you want to check the content of the DOM used for the test, you can do so by calling the <strong>debug</strong> function of the <strong>screen</strong> object.</p><p>It is on these last lines that my article ends. I hope you enjoyed it and that it will make you want to use this test library in your next projects. As the title indicated, this was only a brief introduction. So I invite you to read its <a href="https://testing-library.com/docs/react-testing-library/intro">documentation</a> to further your knowledge.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FBoHCeLmEKytt7oFxyR%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FBoHCeLmEKytt7oFxyR%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2FBoHCeLmEKytt7oFxyR%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="348" frameborder="0" scrolling="no"><a href="https://medium.com/media/9439cb96cf2a6120cf2884cba54c7aaa/href">https://medium.com/media/9439cb96cf2a6120cf2884cba54c7aaa/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=692e46104d6" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/a-gentle-introduction-to-testing-library-for-testing-react-components-and-more-692e46104d6">A gentle introduction to testing-library for testing React components and more.</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>