<?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: rasmusWU</title>
    <description>The latest articles on DEV Community by rasmusWU (@rasmuswu).</description>
    <link>https://dev.to/rasmuswu</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%2F580750%2F8a1f0670-20dd-497e-8109-aeed09ba5c8b.png</url>
      <title>DEV Community: rasmusWU</title>
      <link>https://dev.to/rasmuswu</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rasmuswu"/>
    <language>en</language>
    <item>
      <title>React and Testing (a slightly comprehensive guide, maybe)</title>
      <dc:creator>rasmusWU</dc:creator>
      <pubDate>Fri, 19 Feb 2021 17:16:54 +0000</pubDate>
      <link>https://dev.to/rasmuswu/react-and-testing-a-slightly-comprehensive-guide-maybe-4abl</link>
      <guid>https://dev.to/rasmuswu/react-and-testing-a-slightly-comprehensive-guide-maybe-4abl</guid>
      <description>&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;We as coders, old or new, should always strive for our code to be as close to perfection as possible, and if not that, at least make it functional. This is, of course, easier said than done, as coding, depending on the functionality, can take an enormous amount of effort, so using the tools at our disposal becomes essential.&lt;/p&gt;

&lt;p&gt;Testing is one of, and probably the most important, of those tools, and luckily for us, testing is easy and fast in React.&lt;/p&gt;

&lt;h1&gt;
  
  
  React
&lt;/h1&gt;

&lt;p&gt;React is a framework made for efficient navigation, without full page-loads. This means our sites will be faster on all devices. This is especially beneficial for slower devices, such as mobile networks, or weak home-signals.&lt;/p&gt;

&lt;p&gt;React is also good for us developers, as it lets us split our apps/sites into components, that can be used, reused, and manipulated specifically to suit our needs.&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating a React app
&lt;/h3&gt;

&lt;p&gt;Creating a React app is very simple. Once you've installed Nodejs onto your computer, and opened Visual Studio Code, before even having made any files, you should open your terminal, and type this command (period included. Otherwise it won't work):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx install create-react-app .
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can then, optionally, remove all the non-essential parts of the React app, and only keep the absolute minimum. If you choose to do this, feel free to erase these files:&lt;br&gt;
&lt;code&gt;favicon.ico&lt;/code&gt;&lt;br&gt;
&lt;code&gt;logo192.png&lt;/code&gt;&lt;br&gt;
&lt;code&gt;logo512.png&lt;/code&gt;&lt;br&gt;
&lt;code&gt;manifest.json&lt;/code&gt;&lt;br&gt;
&lt;code&gt;robots.txt&lt;/code&gt;&lt;br&gt;
&lt;code&gt;App.test.js&lt;/code&gt;&lt;br&gt;
&lt;code&gt;logo.svg&lt;/code&gt;&lt;br&gt;
&lt;code&gt;reportWebVitals.js&lt;/code&gt;&lt;br&gt;
&lt;code&gt;setupTests.js&lt;/code&gt;&lt;br&gt;
Also make sure to remove all references to these files, as React is very fragile, and will crash if it has trouble finding a file. The files that reference the removed files are:&lt;br&gt;
&lt;code&gt;App.js&lt;/code&gt;&lt;br&gt;
&lt;code&gt;index.js&lt;/code&gt;&lt;br&gt;
And with that, you have a configured a React app. Type the command below into the terminal to test your React app!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm start
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Making Components
&lt;/h3&gt;

&lt;p&gt;Making components is simple as well. For this showcase, I'll call my files 'comp1'.&lt;br&gt;
Make a new folder titled 'components' in your src folder. This is optional, but highly recommended. Afterwards, make these two files in the components folder:&lt;br&gt;
&lt;code&gt;Comp1.js&lt;/code&gt;&lt;br&gt;
&lt;code&gt;Comp.css&lt;/code&gt;&lt;br&gt;
It is also recommended that you change the file associations in VSCode settings to match the React format, by typing "*.js" under item, and "javascriptreact" under value.&lt;/p&gt;

&lt;p&gt;Once Comp1.js is made, import your css, with this line of code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import './Comp1.css';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, make a function with one parameter (I'll call it header), and export it. I'm calling it Comp1, but anything goes. You should also make a return statement, containing a HTML element, and give it a class (IMPORTANT: since it is javascript, and not HTML, you need to call 'className' and not 'class' like in regular HTML).&lt;br&gt;
You also wanna call the parameter by typing it within a couple of braces, as shown below, in my h1 element.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export default function Comp1({header}) {
    return (
        &amp;lt;section className="Comp1"&amp;gt;
            &amp;lt;h1&amp;gt;{header}&amp;lt;/h1&amp;gt;
        &amp;lt;/section&amp;gt;
    )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once that's done, go into your App.js, and import your component, like you would in normal javascript.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import Comp1 from './components/Comp1';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once you've imported the component, you can insert it anywhere in the HTML, as many times as you want. In my case, I deleted all the contents of the div in App.js, and replaced it with my component.&lt;br&gt;
The parameter for the header is also gonna get used now, as I can insert whatever text I want into its place, like the example below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function App() {
    return (
        &amp;lt;div className="App"&amp;gt;
            &amp;lt;Comp1 header="Hello"/&amp;gt;
            &amp;lt;Comp1 header="World"/&amp;gt;
        &amp;lt;/div&amp;gt;
    );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You've now made a component, and imported it. Now it's time for testing.&lt;/p&gt;

&lt;h1&gt;
  
  
  Testing
&lt;/h1&gt;

&lt;p&gt;Testing is, of course, very important. I don't need to explain that, do I?&lt;br&gt;
Anyways, the test I'll show off is about testing whether elements exist in the document, without actually checking on the site itself.&lt;/p&gt;

&lt;p&gt;This is not particularly useful for such a basic app, but it's immensely useful for bigger projects to quickly run through all the components that need to be tested, without rendering them all out on a website.&lt;/p&gt;
&lt;h3&gt;
  
  
  Making a test
&lt;/h3&gt;

&lt;p&gt;For this example, I'll make a test-file in the components folder like this:&lt;br&gt;
&lt;code&gt;Comp1.test.js&lt;/code&gt;&lt;br&gt;
In the test-file, import the component you wanna test like any other import, as well as { render } from the React testing-library like I have done.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import Comp1 from './Comp1';
import { render } from '@testing-library/react';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After you've done that, it's time to actually set up the test. It'll all take place within a single 'test' function, so start off by making that, and putting a string into the parenthesis. The string will describe what the test is about.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;test("If my component is functioning as intended")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After the message, we need to input what our test should actually test, so add a comma after the string, then make a function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;test("If my component is functioning as intended", function() {

})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Inside this function, make a variable like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var { getByText } = render(&amp;lt;Comp1 header="test" /&amp;gt;);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What it does, is it will render a Comp1 component, with the parameter set as "test". What you input into your parameter doesn't matter, as long as it's a string.&lt;/p&gt;

&lt;p&gt;Next line, make an 'expect' expression, and input the 'getByText' as a parameter within the 'expect' statement. After the getByText, put a parenthesis, with the string from the parameter above, enclosed by a couple of slashes, then followed by an 'i'. This is what it looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;expect(getByText(/test/i));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is what your function/document should look like at this point:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import Comp1 from './Comp1';
import { render } from '@testing-library/react';

test("If header is displaying properly", function() {
    var { getByText } = render(&amp;lt;Comp1 header="test" /&amp;gt;);
    expect(getByText(/test/i));
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will test whether the component exists on your page or not, and whether the text within the parameter matches what you've specified in your getByText function.&lt;br&gt;
To activate the test, just input the line below into the terminal, and it'll run all your tests. In our case, one.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm test
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If all is right, it should pass the test. If not, look back in the blog and try again. You might have made a typo or other mistake along the way.&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;And with that, you've made a React app, a component with a parameter, and tested said component. Now it's just time to expand and get even more out of those tools. Get coding!&lt;/p&gt;

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