<?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: Ademola Onasoga</title>
    <description>The latest articles on DEV Community by Ademola Onasoga (@hellodemola).</description>
    <link>https://dev.to/hellodemola</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%2F646423%2Ff8f29095-3d3f-47c5-ada7-2e70fd913b15.jpg</url>
      <title>DEV Community: Ademola Onasoga</title>
      <link>https://dev.to/hellodemola</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/hellodemola"/>
    <language>en</language>
    <item>
      <title>Are you writing test for the first time? This might help.</title>
      <dc:creator>Ademola Onasoga</dc:creator>
      <pubDate>Sat, 06 Jul 2024 16:04:13 +0000</pubDate>
      <link>https://dev.to/hellodemola/are-you-testing-in-react-for-the-first-time-this-might-help-2jec</link>
      <guid>https://dev.to/hellodemola/are-you-testing-in-react-for-the-first-time-this-might-help-2jec</guid>
      <description>&lt;p&gt;It can be a daunting task to write tests when you do not even know what to test for or why you are testing. In this article, I will attempt to answer these three questions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Why am I writing tests?&lt;/li&gt;
&lt;li&gt;What am I testing for?&lt;/li&gt;
&lt;li&gt;How do I write tests in ReactJs?&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Why am I writing tests?
&lt;/h3&gt;

&lt;p&gt;I still remember the confusion I felt when testing became mandatory at one of my previous engagements. The first question I asked, in protest, was "Why am I testing on the front end?!"&lt;/p&gt;

&lt;p&gt;Good question. &lt;/p&gt;

&lt;p&gt;Can't I open the browser and test manually? Why do I need to write tests? And what am I even testing for? &lt;/p&gt;

&lt;p&gt;These tests are written to catch issues during development. Their purpose is to help write more maintainable code. For instance, if you are working on a project with other developers, it is wise to write tests to monitor and check how changes in code affect other functions dependent on the changed functions. This also applies when you are writing the code, to ensure that you catch other use cases you have not prepared for while working on your code.&lt;/p&gt;

&lt;h3&gt;
  
  
  What am I testing for?
&lt;/h3&gt;

&lt;p&gt;So let's write a function that takes two parameters and based on those two parameters returns a statement.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg0yp116b17w9bpipntrx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg0yp116b17w9bpipntrx.png" alt="Image description" width="800" height="273"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is a simple function that returns a statement about the name and age passed into the function. What could be wrong with this code?&lt;br&gt;
Well, what happens if, for some strange reason, name and age are not passed into this function, what would happen?&lt;/p&gt;

&lt;p&gt;It will return &lt;code&gt;my name is undefined and I am undefined years old&lt;/code&gt;. I know someone would notice the function is written in typescript, (Javascript with type). That should detect the error at build time, but what happens if the endpoint does not return the parameters you will use for this function? It will print something that was not intended to be printed. With tests, unintentional results can be caught in the 'test coverage'. Once caught, you can adjust your functions to address these issues. In our case, we can handle this issue in our function:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flx5jdpckh4eogg999nvu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flx5jdpckh4eogg999nvu.png" alt="Image description" width="800" height="298"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The type of test written for single functions is called a unit test.  You are testing the unit of test.&lt;/p&gt;

&lt;p&gt;Other form of written tests, among others, are regression test and integration test; to test how different part of the application works together. &lt;/p&gt;

&lt;h3&gt;
  
  
  How do I write tests in ReactJs?
&lt;/h3&gt;

&lt;p&gt;The first thing to be decided is the environment that provides the DOM API. This is where the test will be run. For that, we have options in Javascript like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://jestjs.io/" rel="noopener noreferrer"&gt;Jest&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://mochajs.org/" rel="noopener noreferrer"&gt;Mocha + JSDOM&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Jest, for example, can run tests for our sample code.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F123vjwy1f3amsgme335n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F123vjwy1f3amsgme335n.png" alt="Image description" width="800" height="218"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Weird looking? You can consult the Jest documentation but let me break this down a little.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;it('NAME_OF_TEST', () =&amp;gt; {})&lt;/code&gt; can also be &lt;code&gt;test('NAME_OF_TEST', () =&amp;gt; {})&lt;/code&gt; which means you are testing the function inside this test.&lt;/p&gt;

&lt;h3&gt;
  
  
  React testing Library
&lt;/h3&gt;

&lt;p&gt;But to test your react components and hooks, you will need a DOM Testing Library.&lt;/p&gt;

&lt;p&gt;A good DOM testing library for React is &lt;a href="https://testing-library.com/docs/react-testing-library/intro/" rel="noopener noreferrer"&gt;React testing library&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;With this, you can test your react components. You can test cases such as when a form is filled, when a button is pressed, or if this button is disabled while the form is loading.&lt;/p&gt;

&lt;p&gt;I hope I have clarified testing if you are doing it for the first, or second or You still do not get the concept of testing and testing on React Js.&lt;/p&gt;

&lt;p&gt;Thanks for reading.&lt;/p&gt;

</description>
      <category>jest</category>
      <category>testing</category>
      <category>react</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Handle form better in Nextjs with react-hook-form</title>
      <dc:creator>Ademola Onasoga</dc:creator>
      <pubDate>Sun, 05 Jun 2022 01:08:47 +0000</pubDate>
      <link>https://dev.to/hellodemola/handle-form-better-in-nextjs-with-react-hook-form-3o61</link>
      <guid>https://dev.to/hellodemola/handle-form-better-in-nextjs-with-react-hook-form-3o61</guid>
      <description>&lt;p&gt;&lt;em&gt;On a regular tuesday afternoon, you attempted to implement a registration form with 3 field inputs, and for each field you need to have at least 1 validation. The most common approach to handle this would be with an &lt;code&gt;Onchange&lt;/code&gt; function with a dash of &lt;code&gt;Regular Expression&lt;/code&gt;. But as the form gets more complex with new set of validations and additional fields, you thought to yourself that there must be a better way to handle this. Alas! You were right all along.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In this article, we look at how to handle simple form data and validation with &lt;strong&gt;React-hook-form&lt;/strong&gt;. Our case study is a registration form that collect users' information; first name, last name, email address, phone number and address. &lt;/p&gt;

&lt;p&gt;We will validate each fields with at least one validation. For example, a mobile number field should not accept an email value or any letters for that matter. And this use case also assumes that names are not in numbers ( I know '2 pac' has a number, but let's limit the scope to only letters for name inputs). And if the fields are compulsory, it should not accept an empty value, the field must be filled.&lt;/p&gt;

&lt;h2&gt;
  
  
  Handling form using OnChange
&lt;/h2&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%2Fuploads%2Farticles%2F0mjt3quvvcexkfncfdlx.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%2Fuploads%2Farticles%2F0mjt3quvvcexkfncfdlx.png" alt="OnChange function form management"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As shown above, for every new fields introduced, there would be a need to keep adding a new state to handle it. So if we have, 14 fields in the form...you guessed it, 14 states. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I know some of you are thinking that this componenet can still be further broken down into container and stateless form, you are right. However, we are using it this way so that it can be easy to follow&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Let's add validation with regEx
&lt;/h3&gt;

&lt;p&gt;Validate that no fields cannot be empty&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%2Fuploads%2Farticles%2F3sjxv8c0i63h3nvcr2vk.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%2Fuploads%2Farticles%2F3sjxv8c0i63h3nvcr2vk.png" alt="validation for empty fields"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The form will be invalid if any of the fields are empty.&lt;/p&gt;

&lt;p&gt;Now let's go a step futher by preventing the form from fulfilling if the value entered for first name or last name is a number or even has space in between the words. For that we will use a regular expression, also known as regEx. RegEx are patterns used to match character combination in strings. [&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions" rel="noopener noreferrer"&gt;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions&lt;/a&gt;]&lt;/p&gt;

&lt;p&gt;We will define a regular expression for that validation:&lt;br&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%2Fuploads%2Farticles%2Ff417k8iiunum0l9w9ek0.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%2Fuploads%2Farticles%2Ff417k8iiunum0l9w9ek0.png" alt="RegEx validation"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We have added two validations for names, and we might need more; like the names should be at least three characters. And we have four other fields to validate deeper. This is looking like a lot of work. And what if, the form is longer and we are actually calling an API to submit the form, do we want the user to fill all the form before she realises all the checks. That would be frustration for the user.  &lt;/p&gt;

&lt;h2&gt;
  
  
  React-hook-form to the rescue
&lt;/h2&gt;

&lt;p&gt;Add React-hook-form in the follow steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;code&gt;npm install react-hook-form&lt;/code&gt; or &lt;code&gt;yarn add react-hook-form&lt;/code&gt; if you use yarn.&lt;/li&gt;
&lt;li&gt;Add &lt;code&gt;import { useForm } from 'react-hook-form'&lt;/code&gt; to your file&lt;/li&gt;
&lt;li&gt;Add &lt;code&gt;const { handleSubmit, register } = useForm({mode: 'onChange')}&lt;/code&gt; to your component.
&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%2Fuploads%2Farticles%2Fibx37jkcs3nzzc7lomrb.png" alt="use form"&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The handleSubmit works to submit the form, and the register works like the onChange. The mode, 'OnChange' means you want to form to be reactive in real time.&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%2Fuploads%2Farticles%2Fclemlkg9wzlv60f7dyx9.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%2Fuploads%2Farticles%2Fclemlkg9wzlv60f7dyx9.png" alt="use form react-hook-form"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Just like that, you trim off all the useState and extra onChange collection away. &lt;/p&gt;

&lt;p&gt;Can we handle some layer of validations using this package, of course.&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%2Fuploads%2Farticles%2Fn4f5531w9bk0ik2aarz9.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%2Fuploads%2Farticles%2Fn4f5531w9bk0ik2aarz9.png" alt="useForm"&gt;&lt;/a&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%2Fuploads%2Farticles%2F7oiv5zjatgz67ljrv8zi.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%2Fuploads%2Farticles%2F7oiv5zjatgz67ljrv8zi.png" alt="Full form"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Just pass your RegEx inside the pattern object in the register, the user is prevented from submitting her form if the fields are empty or the wrong patterns are entered. &lt;/p&gt;

&lt;p&gt;Of couse, there are other form of validations to add to the form fileds and the form itself, eg preventing the form from been clickable.&lt;/p&gt;

&lt;p&gt;For that we will add a new object in the &lt;code&gt;useForm&lt;/code&gt; function, formState.&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%2Fuploads%2Farticles%2Ft2lcq7mcspthblcpswm4.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%2Fuploads%2Farticles%2Ft2lcq7mcspthblcpswm4.png" alt="FormState"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And we will add a disabled option to the button to disable the button unless all the fields are valid&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%2Fuploads%2Farticles%2F0xiyihhvo8x1e1zkple5.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%2Fuploads%2Farticles%2F0xiyihhvo8x1e1zkple5.png" alt="Disable button"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Finally&lt;/strong&gt; , we want to give the user a feedback in realtime of why the button is still disabled even when all fields are entered, it might be that the patterns entered are wrong. eg. '2 pac'. For that, we will need to add another item in the formState, &lt;code&gt;errors&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%2Fuploads%2Farticles%2Fjvt0t9f3y1evko8z7r5e.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%2Fuploads%2Farticles%2Fjvt0t9f3y1evko8z7r5e.png" alt="Adding errors"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then add the feedback error message in the body of the form&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%2Fuploads%2Farticles%2Fe8o70lxs6b935c51kppu.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%2Fuploads%2Farticles%2Fe8o70lxs6b935c51kppu.png" alt="Error message"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Full form:&lt;/p&gt;

&lt;p&gt;These is just few reason to consider using &lt;code&gt;React-hook-form&lt;/code&gt; in your fo&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%2Fuploads%2Farticles%2F1bvx4b73iumlr957121k.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%2Fuploads%2Farticles%2F1bvx4b73iumlr957121k.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
 rm management toolkit. And I will follow these articles up with other use cases.&lt;br&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%2Fuploads%2Farticles%2Fo129kjowuqc6dvlms8te.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%2Fuploads%2Farticles%2Fo129kjowuqc6dvlms8te.png" alt="React hook form"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>nextjs</category>
      <category>react</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
