{"id":98854,"date":"2019-10-22T10:00:23","date_gmt":"2019-10-22T07:00:23","guid":{"rendered":"http:\/\/www.javacodegeeks.com\/?p=98854"},"modified":"2019-10-18T15:51:17","modified_gmt":"2019-10-18T12:51:17","slug":"reactjs-unit-testing-example","status":"publish","type":"post","link":"https:\/\/www.javacodegeeks.com\/reactjs-unit-testing-example.html","title":{"rendered":"ReactJS Unit Testing Example"},"content":{"rendered":"<p>In this article, we take a look at unit testing ReactJS code. Writing Unit tests for code is the norm these days making testing of code less of an after thought. We take a look at some Unit Testing frameworks for ReactJS and how to configure them. There is a healthy ecosystem of Testing tools &amp; Frameworks around ReactJS and we can pick and choose as per our preferences. In this article, though, I will describe using the Jest Testing Framework along with Enzyme Helper library. <\/p>\n<p>Enzyme helper library is an abstraction built on top of the more verbose and lower level API of React Test Utils.<\/p>\n<p>We build a primitive calculator application in this example and write tests to validate our code. For this example we use the following set of tools and frameworks. You can switch to your preferences for some of the optional tools listed below:<\/p>\n<ul class=\"wp-block-list\">\n<li><a aria-label=\"create-react-app\u2028 (opens in a new tab)\" href=\"https:\/\/www.npmjs.com\/package\/create-react-app\" target=\"_blank\" rel=\"noreferrer noopener\">create-react-app<\/a><\/li>\n<li><a aria-label=\"Jest (opens in a new tab)\" href=\"https:\/\/www.npmjs.com\/package\/jest\" target=\"_blank\" rel=\"noreferrer noopener\">Jest<\/a><\/li>\n<li><a aria-label=\"Bootstrap (opens in a new tab)\" href=\"https:\/\/getbootstrap.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">Bootstrap<\/a><\/li>\n<li><a aria-label=\"JQuery (opens in a new tab)\" href=\"https:\/\/jquery.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">JQuery<\/a><\/li>\n<li><a aria-label=\"Visual Studio Code IDE\u2028 (opens in a new tab)\" href=\"https:\/\/code.visualstudio.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">Visual Studio Code ID<\/a><\/li>\n<\/ul>\n<h2 class=\"wp-block-heading\">1. Initial Application Structure<\/h2>\n<p>We create our initial application structure using the create-react-app tool. To get started execute the below command to create the skeleton of our ReactJS Application.<\/p>\n<pre class=\"brush: bash;\">&gt;npx create-react-app unit-testing .\n<\/pre>\n<p>This command creates our application unit-testing and its structure should resemble the screenshot below<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" width=\"321\" height=\"580\" src=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2019\/10\/ProjectStructureUnitTesting.jpg\" alt=\"ReactJS Unit Testing - Project Structure\" class=\"wp-image-99287\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2019\/10\/ProjectStructureUnitTesting.jpg 321w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2019\/10\/ProjectStructureUnitTesting-166x300.jpg 166w\" sizes=\"(max-width: 321px) 100vw, 321px\" \/><figcaption>Project Structure<\/figcaption><\/figure>\n<\/div>\n<h2 class=\"wp-block-heading\">2. Writing our First Test<\/h2>\n<p>With create-react-app one advantage is that our application comes preconfigured with Jest testing framework. Yes, we do not need to perform any additional configuration. We can start writing our tests from the get go. So, let us get started writing our first test. But before we do that we need some functionality to test. I have coded a rudimentary calculator app for the purposes of this test. It has the below two components, we do not need to go into the implementation details of these for now. Instead focus on testing these components.<\/p>\n<p><span style=\"text-decoration: underline\"><em>Calculator.js<\/em><\/span><\/p>\n<pre class=\"brush: js;\">import React, { useState } from 'react';\nimport Keypad from \".\/Keypad\";\n\nfunction Calculator() {\n    const [result, setResult] = useState(0);\n    const [operator, setOperator] = useState(\"\");\n    const [operand2, setOperand2] = useState(0);\n    const [operand1, setOperand1] = useState(0);\n    const handleChange = ({ target }) =&gt; {\n        setResult(target.value);\n    }\n    const handleEqualsClick = ({ target }) =&gt; {\n        if (operand1 &amp;&amp; operand2 &amp;&amp; operator) {\n            switch (operator) {\n                case \"\/\": setResult(operand1 \/ operand2);\n                    setOperand1(operand1 \/ operand2);\n                    break;\n                case \"X\": setResult(operand1 * operand2);\n                    setOperand1(operand1 * operand2);\n                    break;\n                case \"+\": setResult(operand1 + operand2);\n                    setOperand1(operand1 + operand2);\n                    break;\n                case \"-\": setResult(operand1 - operand2);\n                    setOperand1(operand1 - operand2);\n                    break;\n                default:\n                    break;\n            }\n            setOperand2(0);\n            setOperator(\"\");\n        }\n    }\n    const handleClearClick = ({ target }) =&gt; {\n        setResult(0);\n        setOperator(\"\");\n        setOperand2(0);\n        setOperand1(0);\n    }\n    const handleKeypadClick = ({ target }) =&gt; {\n        if (!operator) {\n            setOperand1(+(operand1 + \"\" + target.value));\n            setResult(+(operand1 + \"\" + target.value));\n        } else {\n            setOperand2(+(operand2 + \"\" + target.value));\n            setResult(+(operand2 + \"\" + target.value));\n        }\n    }\n    const handleOperatorClick = ({ target }) =&gt; {\n        if (operator) {\n            handleEqualsClick({ target });\n        }\n        setOperator(target.value);\n        setOperand2(0);\n    }\n    return &lt;div className=\"container-fluid\"&gt;\n        &lt;div className=\"row\"&gt;\n            &lt;input type=\"text\" readOnly onChange={handleChange} value={result} \/&gt;\n        &lt;\/div&gt;\n        &lt;div className=\"row\"&gt;\n            &lt;Keypad onEqualsClick={handleEqualsClick}\n                onClearClick={handleClearClick}\n                onKeyClick={handleKeypadClick}\n                onOperatorClick={handleOperatorClick} \/&gt;\n        &lt;\/div&gt;\n    &lt;\/div&gt;;\n}\nexport default Calculator;\n<\/pre>\n<p><span style=\"text-decoration: underline\"><em>Keypad.js<\/em><\/span><div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<pre class=\"brush: js;\">import React from 'react';\n\nfunction Keypad(props) {\n\n    return &lt;div className=\"container\"&gt;\n        &lt;div className=\"row\"&gt;\n            &lt;button className=\"btn btn-info m-1\" value=\"1\" onClick={props.onKeyClick}&gt;1&lt;\/button&gt;\n            &lt;button className=\"btn btn-info m-1\" value=\"2\" onClick={props.onKeyClick}&gt;2&lt;\/button&gt;\n            &lt;button className=\"btn btn-info m-1\" value=\"3\" onClick={props.onKeyClick}&gt;3&lt;\/button&gt;\n            &lt;button className=\"btn btn-info m-1\" value=\"\/\" onClick={props.onOperatorClick}&gt;\/&lt;\/button&gt;\n        &lt;\/div&gt;\n        &lt;div className=\"row\"&gt;\n            &lt;button className=\"btn btn-info m-1\" value=\"4\" onClick={props.onKeyClick}&gt;4&lt;\/button&gt;\n            &lt;button className=\"btn btn-info m-1\" value=\"5\" onClick={props.onKeyClick}&gt;5&lt;\/button&gt;\n            &lt;button className=\"btn btn-info m-1\" value=\"6\" onClick={props.onKeyClick}&gt;6&lt;\/button&gt;\n            &lt;button className=\"btn btn-info m-1\" value=\"X\" onClick={props.onOperatorClick}&gt;X&lt;\/button&gt;\n        &lt;\/div&gt;\n        &lt;div className=\"row\"&gt;\n            &lt;button className=\"btn btn-info m-1\" value=\"7\" onClick={props.onKeyClick}&gt;7&lt;\/button&gt;\n            &lt;button className=\"btn btn-info m-1\" value=\"8\" onClick={props.onKeyClick}&gt;8&lt;\/button&gt;\n            &lt;button className=\"btn btn-info m-1\" value=\"9\" onClick={props.onKeyClick}&gt;9&lt;\/button&gt;\n            &lt;button className=\"btn btn-info m-1\" value=\"-\" onClick={props.onOperatorClick}&gt;-&lt;\/button&gt;\n        &lt;\/div&gt;\n        &lt;div className=\"row\"&gt;\n            &lt;button className=\"btn btn-info m-1\" value=\"Clear\" onClick={props.onClearClick}&gt;C&lt;\/button&gt;\n            &lt;button className=\"btn btn-info m-1\" value=\"0\" onClick={props.onKeyClick}&gt;0&lt;\/button&gt;\n            &lt;button className=\"btn btn-info m-1\" value=\"=\" onClick={props.onEqualsClick}&gt;=&lt;\/button&gt;\n            &lt;button className=\"btn btn-info m-1\" value=\"+\" onClick={props.onOperatorClick}&gt;+&lt;\/button&gt;\n        &lt;\/div&gt;\n    &lt;\/div&gt;;\n}\nexport default Keypad;\n<\/pre>\n<p>We write tests in files with the same name as the component we are testing but ending in test.js. These are then picked up by Jest when we test and the tests written inside are executed. Let us create two files to test each of our two components above. Our first test is to check if the above components render without crashing. The test for this looks like below:<\/p>\n<p><span style=\"text-decoration: underline\"><em>Keypad.test.js<\/em><\/span><\/p>\n<pre class=\"brush: js;\">import React from 'react';\nimport ReactDOM from 'react-dom';\nimport Keypad from '.\/Keypad';\n\nit('renders without crashing', () =&gt; {\n    const div = document.createElement('div');\n    ReactDOM.render(&lt;Keypad \/&gt;, div);\n    ReactDOM.unmountComponentAtNode(div);\n});\n<\/pre>\n<p><span style=\"text-decoration: underline\"><em>Calculator.test.js<\/em><\/span><\/p>\n<pre class=\"brush: js;\">import React from 'react';\nimport ReactDOM from 'react-dom';\nimport Calculator from '.\/Calculator';\n\nit('renders without crashing', () =&gt; {\n    const div = document.createElement('div');\n    ReactDOM.render(&lt;Calculator \/&gt;, div);\n    ReactDOM.unmountComponentAtNode(div);\n});\n<\/pre>\n<p>Each test begins with call to it function with the first parameter describing the expectations as in &#8220;it renders without crashing&#8230;&#8221;. This convention is followed with each test we write for all components. The second parameter is a callback with our test forming the body of the callback. In the above tests we render our components to the DOM using ReactDOM.<\/p>\n<p>Now to run these tests we run the following command from the terminal at the root of the project.<\/p>\n<pre class=\"brush: bash;\">&gt;npm test\n<\/pre>\n<p>This runs the test and the results are displayed like below:<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" width=\"558\" height=\"460\" src=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2019\/10\/FirstRunOfUnitTests.jpg\" alt=\"ReactJS Unit Testing - Project Output\" class=\"wp-image-99372\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2019\/10\/FirstRunOfUnitTests.jpg 558w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2019\/10\/FirstRunOfUnitTests-300x247.jpg 300w\" sizes=\"(max-width: 558px) 100vw, 558px\" \/><figcaption>Project Output<\/figcaption><\/figure>\n<\/div>\n<p>We explore more advanced scenarios in the following sections<\/p>\n<h2 class=\"wp-block-heading\">3. Snapshot Testing<\/h2>\n<p>The concept of snapshot testing works simply by comparing a reference snapshot of the UI with the current UI generated by the test. In case there is an anomaly, it could point to an issue leading to inadvertent change in the UI of the component. But it could also be due to intentional and deliberate change in the layout. In latter we need to update the stored snapshots. We take a look at both cases below as we test our Container Calculator Component. Point to note is that the snapshots are not images rather tree of nodes in our rendered Component serialized to a file as JSON.<\/p>\n<p>We will write a test to test the UI of our calculator. For reference this is what our UI looks like right now.<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" width=\"439\" height=\"463\" src=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2019\/10\/UILayoutCalculator.jpg\" alt=\"ReactJS Unit Testing - UI Layout\" class=\"wp-image-99376\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2019\/10\/UILayoutCalculator.jpg 439w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2019\/10\/UILayoutCalculator-284x300.jpg 284w\" sizes=\"(max-width: 439px) 100vw, 439px\" \/><figcaption>UI Layout<\/figcaption><\/figure>\n<\/div>\n<p>We write a test to validate that new changes do not make unintentional changes to the layout of our Calculator. We add the following code to our Calculator.test.js file.<\/p>\n<pre class=\"brush: js;\">it('renders correctly', () =&gt; {\n    const Calc = renderer.create(&lt;Calculator \/&gt;).toJSON();\n    expect(Calc).toMatchSnapshot();\n});\n<\/pre>\n<p>Let us run this test using the npm test command. This the first time we have run a snapshot test and this causes a snapshot to be generated and save to a folder __snapshots___ as  a file named Calculator.test.js.snap. Now when we run the test again we should see the below output as all the tests pass.<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" width=\"446\" height=\"316\" src=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2019\/10\/FirstSnapshotTest.jpg\" alt=\"\" class=\"wp-image-99379\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2019\/10\/FirstSnapshotTest.jpg 446w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2019\/10\/FirstSnapshotTest-300x213.jpg 300w\" sizes=\"(max-width: 446px) 100vw, 446px\" \/><figcaption>Snapshot Test Passes<\/figcaption><\/figure>\n<\/div>\n<p>To test out the snapshot test let us make a change to the layout to simulate an inadvertent change or bug. And then run the tests again to see the output below:<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" width=\"891\" height=\"351\" src=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2019\/10\/SnapshotTestFailed.jpg\" alt=\"\" class=\"wp-image-99380\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2019\/10\/SnapshotTestFailed.jpg 891w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2019\/10\/SnapshotTestFailed-300x118.jpg 300w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2019\/10\/SnapshotTestFailed-768x303.jpg 768w\" sizes=\"(max-width: 891px) 100vw, 891px\" \/><figcaption>Snapshot Test Fails<\/figcaption><\/figure>\n<\/div>\n<p>Such tests are useful to ensure that inadvertent or side effects leading to broken UI are caught early on. Next we take a look at how we can use Jest to make asserts and mock artifacts.<\/p>\n<h2 class=\"wp-block-heading\">4. Mocking<\/h2>\n<p>One of the essential parts of unit testing is to isolate or insulate the unit being tested. So that test results are not affected by external events or the environment. Like availability of server side APIs or external resources or other child or parent components. To enable such isolation all testing frameworks provide capabilities to mock artifacts. So that our tests focus on testing one unit at a time. <\/p>\n<p>For example here when we test the keypad component which is a child of Calculator component and renders buttons. We would like to isolate the Keypad component from both so that we can test the behavior of Keypad component alone. As can be seen from code we pass in event callbacks from Calculator to Keypad and we forward the callbacks to the button&#8217;s onclick handler based on its value in Keypad. Let us mock these callbacks and simulate button clicks to and then assert that the Calculator passed callbacks are invoked with proper arguments. Our test file would now look like below:<\/p>\n<p><span style=\"text-decoration: underline\"><em>Keypad.test.js<\/em><\/span><\/p>\n<pre class=\"brush: js;\">import React from 'react';\nimport ReactDOM from 'react-dom';\nimport Keypad from '.\/Keypad';\nimport Enzyme, { shallow } from 'enzyme';\nimport Adapter from 'enzyme-adapter-react-16';\n\nEnzyme.configure({ adapter: new Adapter() });\n\n\/\/ Mocked Callbacks\nconst handleKeypadClick = jest.fn(({ target }) =&gt; target.value);\nconst handleOperatorClick = jest.fn(({ target }) =&gt; target.value);\nconst handleEqualsClick = jest.fn(({ target }) =&gt; target.value);\nconst handleClearClick = jest.fn(({ target }) =&gt; target.value);\n\nit('renders without crashing', () =&gt; {\n    const div = document.createElement('div');\n    ReactDOM.render(&lt;Keypad \/&gt;, div);\n    ReactDOM.unmountComponentAtNode(div);\n});\n\nit('should call handleKeypadClick', () =&gt; {\n    const keypad = shallow(&lt;Keypad onEqualsClick={handleEqualsClick}\n        onClearClick={handleClearClick}\n        onKeyClick={handleKeypadClick}\n        onOperatorClick={handleOperatorClick} \/&gt;);\n\n    keypad.find({ value: '2' }).simulate('click', { target: { value: 2 } });\n\n    expect(handleKeypadClick).toBeCalledWith({ target: { value: 2 } });\n\n});\nit('should call handleEqualsClick', () =&gt; {\n    const keypad = shallow(&lt;Keypad onEqualsClick={handleEqualsClick}\n        onClearClick={handleClearClick}\n        onKeyClick={handleKeypadClick}\n        onOperatorClick={handleOperatorClick} \/&gt;);\n\n    keypad.find({ value: '=' }).simulate('click', { target: { value: '=' } });\n    expect(handleEqualsClick).toBeCalledWith({ target: { value: '=' } })\n});\n\nit('should call handleOperatorClick', () =&gt; {\n    const keypad = shallow(&lt;Keypad onEqualsClick={handleEqualsClick}\n        onClearClick={handleClearClick}\n        onKeyClick={handleKeypadClick}\n        onOperatorClick={handleOperatorClick} \/&gt;);\n\n    keypad.find({ value: '+' }).simulate('click', { target: { value: '+' } });\n    expect(handleOperatorClick).toBeCalledWith({ target: { value: '+' } })\n});\n\nit('should call handleClearClick', () =&gt; {\n    const keypad = shallow(&lt;Keypad onEqualsClick={handleEqualsClick}\n        onClearClick={handleClearClick}\n        onKeyClick={handleKeypadClick}\n        onOperatorClick={handleOperatorClick} \/&gt;);\n\n    keypad.find({ value: 'Clear' }).simulate('click', { target: { value: 'Clear' } });\n    expect(handleClearClick).toBeCalledWith({ target: { value: 'Clear' } })\n});\n<\/pre>\n<h2 class=\"wp-block-heading\">5. Download the Source Code<\/h2>\n<div class=\"download\"><strong>Download<\/strong><br \/>\nYou can download the full source code of this example here: <a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2019\/10\/JCG-ReactJS-Unit-Testing-Example.zip\"><strong>ReactJS Unit Testing Example<\/strong><\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we take a look at unit testing ReactJS code. Writing Unit tests for code is the norm these days making testing of code less of an after thought. We take a look at some Unit Testing frameworks for ReactJS and how to configure them. There is a healthy ecosystem of Testing tools &hellip;<\/p>\n","protected":false},"author":82952,"featured_media":92051,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1903],"tags":[1919],"class_list":["post-98854","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-react-js","tag-reactjs"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>ReactJS Unit Testing Example - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"Interested to learn more about ReactJS? Then check out our detailed example on ReactJS Unit Testing!Writing Unit tests for code is the norm these days.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.javacodegeeks.com\/reactjs-unit-testing-example.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"ReactJS Unit Testing Example - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"Interested to learn more about ReactJS? Then check out our detailed example on ReactJS Unit Testing!Writing Unit tests for code is the norm these days.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.javacodegeeks.com\/reactjs-unit-testing-example.html\" \/>\n<meta property=\"og:site_name\" content=\"Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2019-10-22T07:00:23+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2019\/05\/react-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Siddharth Seth\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Siddharth Seth\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/reactjs-unit-testing-example.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/reactjs-unit-testing-example.html\"},\"author\":{\"name\":\"Siddharth Seth\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/09c54717e2cc7956aa8a2df61330f18c\"},\"headline\":\"ReactJS Unit Testing Example\",\"datePublished\":\"2019-10-22T07:00:23+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/reactjs-unit-testing-example.html\"},\"wordCount\":979,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/reactjs-unit-testing-example.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2019\\\/05\\\/react-logo.jpg\",\"keywords\":[\"Reactjs\"],\"articleSection\":[\"React.js\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/reactjs-unit-testing-example.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/reactjs-unit-testing-example.html\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/reactjs-unit-testing-example.html\",\"name\":\"ReactJS Unit Testing Example - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/reactjs-unit-testing-example.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/reactjs-unit-testing-example.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2019\\\/05\\\/react-logo.jpg\",\"datePublished\":\"2019-10-22T07:00:23+00:00\",\"description\":\"Interested to learn more about ReactJS? Then check out our detailed example on ReactJS Unit Testing!Writing Unit tests for code is the norm these days.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/reactjs-unit-testing-example.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/reactjs-unit-testing-example.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/reactjs-unit-testing-example.html#primaryimage\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2019\\\/05\\\/react-logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2019\\\/05\\\/react-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/reactjs-unit-testing-example.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Web Development\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/web-development\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"JavaScript\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/web-development\\\/javascript\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"React.js\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/web-development\\\/javascript\\\/react-js\"},{\"@type\":\"ListItem\",\"position\":5,\"name\":\"ReactJS Unit Testing Example\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Developers Resource Center\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.javacodegeeks.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/javacodegeeks\",\"https:\\\/\\\/x.com\\\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/09c54717e2cc7956aa8a2df61330f18c\",\"name\":\"Siddharth Seth\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/86a5133a5e9d79f7997e2649b1afe58e895c0d88df47b3359103ec4c1a2077d6?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/86a5133a5e9d79f7997e2649b1afe58e895c0d88df47b3359103ec4c1a2077d6?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/86a5133a5e9d79f7997e2649b1afe58e895c0d88df47b3359103ec4c1a2077d6?s=96&d=mm&r=g\",\"caption\":\"Siddharth Seth\"},\"description\":\"Siddharth is a Software Development Professional with a Master degree in Computer Applications from IGNOU. He has over 14 years of experience. And currently focused on Software Architecture, Cloud Computing, JavaScript Frameworks for Client and Server, Business Intelligence.\",\"sameAs\":[\"https:\\\/\\\/www.linkedin.com\\\/in\\\/siddharth-seth-240180\\\/\"],\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/author\\\/siddharth_seth1980\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"ReactJS Unit Testing Example - Java Code Geeks","description":"Interested to learn more about ReactJS? Then check out our detailed example on ReactJS Unit Testing!Writing Unit tests for code is the norm these days.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.javacodegeeks.com\/reactjs-unit-testing-example.html","og_locale":"en_US","og_type":"article","og_title":"ReactJS Unit Testing Example - Java Code Geeks","og_description":"Interested to learn more about ReactJS? Then check out our detailed example on ReactJS Unit Testing!Writing Unit tests for code is the norm these days.","og_url":"https:\/\/www.javacodegeeks.com\/reactjs-unit-testing-example.html","og_site_name":"Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2019-10-22T07:00:23+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2019\/05\/react-logo.jpg","type":"image\/jpeg"}],"author":"Siddharth Seth","twitter_card":"summary_large_image","twitter_creator":"@javacodegeeks","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Siddharth Seth","Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.javacodegeeks.com\/reactjs-unit-testing-example.html#article","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/reactjs-unit-testing-example.html"},"author":{"name":"Siddharth Seth","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/09c54717e2cc7956aa8a2df61330f18c"},"headline":"ReactJS Unit Testing Example","datePublished":"2019-10-22T07:00:23+00:00","mainEntityOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/reactjs-unit-testing-example.html"},"wordCount":979,"commentCount":0,"publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/reactjs-unit-testing-example.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2019\/05\/react-logo.jpg","keywords":["Reactjs"],"articleSection":["React.js"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.javacodegeeks.com\/reactjs-unit-testing-example.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.javacodegeeks.com\/reactjs-unit-testing-example.html","url":"https:\/\/www.javacodegeeks.com\/reactjs-unit-testing-example.html","name":"ReactJS Unit Testing Example - Java Code Geeks","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/reactjs-unit-testing-example.html#primaryimage"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/reactjs-unit-testing-example.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2019\/05\/react-logo.jpg","datePublished":"2019-10-22T07:00:23+00:00","description":"Interested to learn more about ReactJS? Then check out our detailed example on ReactJS Unit Testing!Writing Unit tests for code is the norm these days.","breadcrumb":{"@id":"https:\/\/www.javacodegeeks.com\/reactjs-unit-testing-example.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.javacodegeeks.com\/reactjs-unit-testing-example.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/reactjs-unit-testing-example.html#primaryimage","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2019\/05\/react-logo.jpg","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2019\/05\/react-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.javacodegeeks.com\/reactjs-unit-testing-example.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Web Development","item":"https:\/\/www.javacodegeeks.com\/category\/web-development"},{"@type":"ListItem","position":3,"name":"JavaScript","item":"https:\/\/www.javacodegeeks.com\/category\/web-development\/javascript"},{"@type":"ListItem","position":4,"name":"React.js","item":"https:\/\/www.javacodegeeks.com\/category\/web-development\/javascript\/react-js"},{"@type":"ListItem","position":5,"name":"ReactJS Unit Testing Example"}]},{"@type":"WebSite","@id":"https:\/\/www.javacodegeeks.com\/#website","url":"https:\/\/www.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Developers Resource Center","publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/09c54717e2cc7956aa8a2df61330f18c","name":"Siddharth Seth","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/86a5133a5e9d79f7997e2649b1afe58e895c0d88df47b3359103ec4c1a2077d6?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/86a5133a5e9d79f7997e2649b1afe58e895c0d88df47b3359103ec4c1a2077d6?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/86a5133a5e9d79f7997e2649b1afe58e895c0d88df47b3359103ec4c1a2077d6?s=96&d=mm&r=g","caption":"Siddharth Seth"},"description":"Siddharth is a Software Development Professional with a Master degree in Computer Applications from IGNOU. He has over 14 years of experience. And currently focused on Software Architecture, Cloud Computing, JavaScript Frameworks for Client and Server, Business Intelligence.","sameAs":["https:\/\/www.linkedin.com\/in\/siddharth-seth-240180\/"],"url":"https:\/\/www.javacodegeeks.com\/author\/siddharth_seth1980"}]}},"_links":{"self":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/98854","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/users\/82952"}],"replies":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=98854"}],"version-history":[{"count":0,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/98854\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media\/92051"}],"wp:attachment":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=98854"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=98854"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=98854"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}