<?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: Binit Chandra Jha</title>
    <description>The latest articles on DEV Community by Binit Chandra Jha (@binitgurzu).</description>
    <link>https://dev.to/binitgurzu</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%2F781229%2F9b17c77c-b33a-4d6f-8521-1f9c11e01fce.png</url>
      <title>DEV Community: Binit Chandra Jha</title>
      <link>https://dev.to/binitgurzu</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/binitgurzu"/>
    <language>en</language>
    <item>
      <title>Getting started with Unit testing in React Native</title>
      <dc:creator>Binit Chandra Jha</dc:creator>
      <pubDate>Sat, 01 Jan 2022 12:23:26 +0000</pubDate>
      <link>https://dev.to/binitgurzu/getting-started-with-unit-testing-in-react-native-3c7l</link>
      <guid>https://dev.to/binitgurzu/getting-started-with-unit-testing-in-react-native-3c7l</guid>
      <description>&lt;h2&gt;
  
  
  What is Unit testing?
&lt;/h2&gt;

&lt;p&gt;Unit testing is the practice of testing small, isolated pieces of code, like individual functions or classes.&lt;/p&gt;

&lt;p&gt;The great thing about unit testing is that they are quick to write and run. Therefore, as you work, you get fast feedback about whether your tests are passing or not.&lt;/p&gt;

&lt;h2&gt;
  
  
  Which Is the Best Framework for unit testing?
&lt;/h2&gt;

&lt;p&gt;The most popular JavaScript frameworks are &lt;a href="https://jasmine.github.io/"&gt;Jasmine&lt;/a&gt;, &lt;a href="https://enzymejs.github.io/enzyme/"&gt;Enzyme&lt;/a&gt;, &lt;a href="https://jestjs.io/"&gt;Jest&lt;/a&gt;, and &lt;a href="https://mochajs.org/"&gt;Mocha&lt;/a&gt;. But we will be using &lt;a href="https://jestjs.io/"&gt;Jest&lt;/a&gt; for unit-testing.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why Jest&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://jestjs.io/"&gt;Jest&lt;/a&gt; is a delightful JavaScript Testing Framework with a focus on simplicity. Facebook developed the Jest unit testing framework which is the best option for most React Native projects.&lt;/p&gt;

&lt;p&gt;React Native &lt;a href="https://reactnative.dev/docs/testing-overview"&gt;docs&lt;/a&gt; also recommends Jest for testing React Native applications.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Installation&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Jest setup has been included by default when running &lt;a href="https://reactnative.dev/blog/2016/11/08/introducing-button-yarn-and-a-public-roadmap#speed-up-react-native-init-using-yarn"&gt;react-native init&lt;/a&gt; so we don't need to do any extra steps for installation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Implementation
&lt;/h2&gt;

&lt;p&gt;Now, we will first create a &lt;code&gt;helper.js&lt;/code&gt; file inside the &lt;code&gt;src&lt;/code&gt; folder which will contain &lt;code&gt;addTodo&lt;/code&gt;, &lt;code&gt;findTodoItem&lt;/code&gt;, &lt;code&gt;updateTodo&lt;/code&gt;, and &lt;code&gt;removedTodo&lt;/code&gt; functions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;helper.js&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1.)&lt;/strong&gt; &lt;strong&gt;Test case for add Todo&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export const addTodo = (list, item) =&amp;gt; [item, ...list];

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

&lt;/div&gt;



&lt;p&gt;After that, we will create a &lt;code&gt;helper.test.js&lt;/code&gt; file for ensuring that these functions are working properly or not.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import {expect, it} from '@jest/globals';
import {v4 as uuidv4} from 'uuid';

import {addTodo} from './helper';

describe('todo', () =&amp;gt; {
  const id1 = uuidv4();
  const id2 = uuidv4();
  const id3 = uuidv4();
  it('should add todo to the list', () =&amp;gt; {
    const startTodos = [
      {id: id1, task: 'task-1'},
      {id: id2, task: 'task-2'},
    ];

    const newTodo = {id: id3, task: 'task-3'};
    const expected = [
      {id: id3, task: 'task-3'},
      {id: id1, task: 'task-1'},
      {id: id2, task: 'task-2'},
    ];

    const result = addTodo(startTodos, newTodo);
    expect(result).toEqual(expected);
  });
  });

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

&lt;/div&gt;



&lt;p&gt;Let's understand the code written inside the &lt;code&gt;helper.test.js&lt;/code&gt; file step-by-step.&lt;/p&gt;

&lt;p&gt;i.)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    import {expect, it} from '@jest/globals';
    import {v4 as uuidv4} from 'uuid';

    import {addTodo} from './helper';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;expect&lt;/code&gt; and &lt;code&gt;it&lt;/code&gt; are jest methods.&lt;br&gt;
  &lt;code&gt;uuid&lt;/code&gt; package is used for generating a unique ID.&lt;br&gt;
  &lt;code&gt;addTodo&lt;/code&gt; is a function which we want to test whether it returns the correct value or not.&lt;/p&gt;

&lt;p&gt;ii.)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    describe('todo', () =&amp;gt; {}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;describe(name, fn)&lt;/code&gt; creates a block that &lt;code&gt;groups&lt;/code&gt; together with several related tests. so we create a group named todo which includes todo-related tests like &lt;code&gt;addTodo&lt;/code&gt;, &lt;code&gt;delete Todo&lt;/code&gt;, etc.&lt;/p&gt;

&lt;p&gt;iii.)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;     const id1 = uuidv4();
     const id2 = uuidv4();
     const id3 = uuidv4();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we create three different unique IDs using the uuid package.&lt;/p&gt;

&lt;p&gt;iv.)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    it('should add todo to the list', () =&amp;gt; {
    const startTodos = [
      {id: id1, task: 'task-1'},
      {id: id2, task: 'task-2'},
    ];

    const newTodo = {id: id3, task: 'task-3'};
    const expected = [
      {id: id3, task: 'task-3'},
      {id: id1, task: 'task-1'},
      {id: id2, task: 'task-2'},
    ];

    const result = addTodo(startTodos, newTodo);
    expect(result).toEqual(expected);
  });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;it&lt;/code&gt; is for testing.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;startTodos&lt;/code&gt; variable defines the initial todo-list which has 2 items.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;newTodo&lt;/code&gt; variable defines a new todo value which we want to add inside the todo-list.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;expected&lt;/code&gt; variable defines that after adding a new todo value to the existing todo list, the new todo-list should be equal to our &lt;code&gt;expected&lt;/code&gt; list.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;result&lt;/code&gt; variable stores the value that is returned by &lt;code&gt;addTodo&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;expect(result).toEqual(expected);&lt;/code&gt; If our expected value is equal to the &lt;code&gt;result&lt;/code&gt; then our test should be passed and if our expected value is not equal to the result then our test should be failed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2.&lt;/strong&gt; &lt;strong&gt;Test case for find an item from todo-list&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export const findTodoItem = (list, id) =&amp;gt;
  list.find(listItem =&amp;gt; listItem.id === id);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;let's write the test case for above function.&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;it('find item by id from todos list', () =&amp;gt; {
    const startTodos = [
      {id: id1, task: 'task-1'},
      {id: id2, task: 'task-2'},
      {id: id3, task: 'task-3'},
    ];

    const expectedTodoItem = {id: id2, task: 'task-2'};

    const result = findTodoItem(startTodos, id2);

    expect(result).toEqual(expectedTodoItem);
  });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Let's understand the above test case step-by-step&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;startTodos&lt;/code&gt; variable defined that the initial todo list.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;expectedTodoItem&lt;/code&gt; variable defined that our expected todo-item.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;result&lt;/code&gt; variable store the value that &lt;code&gt;findTodoItem&lt;/code&gt; function returns. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;expect(result).toEqual(expectedTodoItem)&lt;/code&gt; If our expected value is equal to result then our test should be passed and if our expected value shouldn't equal to result then our test should be failed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3.&lt;/strong&gt; &lt;strong&gt;Test case for update an particular item from todo-list&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export const updateTodo = (list, updatedListItem) =&amp;gt; {
  const updatedList = list.map(listItem =&amp;gt;
    listItem.id === updatedListItem.id ? updatedListItem : listItem,
  );

  return updatedList;
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;let's write the test case for above function.&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;it('update todo should update todo by id', () =&amp;gt; {
    const startTodos = [
      {id: id1, task: 'task-1'},
      {id: id2, task: 'task-2'},
      {id: id3, task: 'task-3'},
    ];

    const updatedTodo = {id: id2, task: 'task-2-updated-successfully'};

    const expectedTodos = [
      {id: id1, task: 'task-1'},
      {id: id2, task: 'task-2-updated-successfully'},
      {id: id3, task: 'task-3'},
    ];

    const result = updateTodo(startTodos, updatedTodo);
    expect(result).toEqual(expectedTodos);
  });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;let's understand the above test case step-by-step&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;startTodos&lt;/code&gt; variable store the initial todo list.&lt;/p&gt;

&lt;p&gt;Update the existing todo-item and stored in the &lt;code&gt;updatedTodo&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;result&lt;/code&gt; variable store the value that &lt;code&gt;updateTodo&lt;/code&gt; function returns.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;expectedTodos&lt;/code&gt; variable defined that our new updated todo-list.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;expect(result).toEqual(expectedTodos)&lt;/code&gt; If our expected value is equal to result then our test should be passed and if our expected value shouldn't equal to result then our test should be failed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Test case for delete an particular item from todo-list&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; export const removedTodo = (list, id) =&amp;gt;
  list.filter(listItem =&amp;gt; listItem.id !== id);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;let's write the test case for above function.&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;it('removeTodo should remove todo from todos by id', () =&amp;gt; {
    const startTodos = [
      {id: id1, task: 'task-1'},
      {id: id2, task: 'task-2'},
      {id: id3, task: 'task-3'},
    ];

    const expected = [
      {id: id1, task: 'task-1'},
      {id: id3, task: 'task-3'},
    ];

    const result = removedTodo(startTodos, id2);
    expect(result).toEqual(expected);
  });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;let's understand the above test case step-by-step&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;startTodos&lt;/code&gt; variable store the initial todo list.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;expected&lt;/code&gt; variable store the expected todo list after deleting the particular todo item.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;result&lt;/code&gt; variable store the value that &lt;code&gt;removedTodo&lt;/code&gt; function returns.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;expect(result).toEqual(expected)&lt;/code&gt; If our expected value is equal to result then our test should be passed and if our expected value shouldn't equal to result then our test should be failed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Run &lt;code&gt;yarn run test&lt;/code&gt; command and you see all test case are passed.&lt;/p&gt;

&lt;p&gt;In this example we write the unit test case for Todo App which includes test case for AddTodo , UpdateTodo and DeleteTodo functions.&lt;/p&gt;

&lt;p&gt;If you want to write unit test then be ensure to write&lt;a href="https://www.geeksforgeeks.org/pure-functions-in-javascript/"&gt; Pure Functions&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Divide the logic of complex functions into a small piece of pure functions then it's very easy to write a unit test case.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;For writing test cases of a pure function you should follow these steps:-&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Define your initial value.&lt;/li&gt;
&lt;li&gt;Define your expected value after the function executes.&lt;/li&gt;
&lt;li&gt;Call the function with expected params and store the value returned by the function.&lt;/li&gt;
&lt;li&gt;Check if your expected value is equal to the result or not.&lt;/li&gt;
&lt;li&gt;If they are equal then the test will pass, otherwise the test will fail.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Finally, here is the full source code of this &lt;a href="https://github.com/binitgurzu/TodoApp"&gt;Todo app&lt;/a&gt;&lt;/p&gt;

</description>
      <category>reactnative</category>
      <category>testing</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
