<?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: Chidera Ani</title>
    <description>The latest articles on DEV Community by Chidera Ani (@anichidera).</description>
    <link>https://dev.to/anichidera</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%2F457982%2F946cfb0e-4df2-4b24-b012-1079c6fd5ca2.jpg</url>
      <title>DEV Community: Chidera Ani</title>
      <link>https://dev.to/anichidera</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/anichidera"/>
    <language>en</language>
    <item>
      <title>How to Write a Simple Test for Your Next.js App</title>
      <dc:creator>Chidera Ani</dc:creator>
      <pubDate>Sun, 12 Jun 2022 16:55:01 +0000</pubDate>
      <link>https://dev.to/anichidera/how-to-write-a-simple-test-for-your-nextjs-app-2k1j</link>
      <guid>https://dev.to/anichidera/how-to-write-a-simple-test-for-your-nextjs-app-2k1j</guid>
      <description>&lt;h3&gt;
  
  
  A guide to writing a simple test for a Next.js app.
&lt;/h3&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;First things first&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;It’s important for software developers to write tests for their software, especially in production to correctly determine whether or not it works effectively and as intended. We don’t want to assume it works only for it to fail later.&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%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2AGHgUPqdadA8z6EEVE319Hg.jpeg" 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%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2AGHgUPqdadA8z6EEVE319Hg.jpeg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Well, it might “work” but you still need to write tests :)&lt;/p&gt;

&lt;p&gt;In this tutorial, I’ll be taking you through writing a simple set of tests for a form in your &lt;a href="https://nextjs.org/" rel="noopener noreferrer"&gt;Next.js&lt;/a&gt; application using the &lt;a href="https://jestjs.io/" rel="noopener noreferrer"&gt;Jest&lt;/a&gt; and &lt;a href="https://testing-library.com/docs/react-testing-library/intro" rel="noopener noreferrer"&gt;React Testing Library&lt;/a&gt;. Let’s take a brief look at these tools mentioned above and set our project up.&lt;/p&gt;

&lt;h2&gt;
  
  
  Next.js
&lt;/h2&gt;

&lt;p&gt;Next.js is an open-source JavaScript framework built by &lt;a href="https://vercel.com/breezeio" rel="noopener noreferrer"&gt;Vercel&lt;/a&gt; which provides React-based web applications functionalities. It enables functionalities such as server-side rendering, serverless functions, static applications, etc.&lt;/p&gt;

&lt;p&gt;We’ll set up our project by creating a new Next.js app.&lt;/p&gt;

&lt;p&gt;Open your terminal and navigate to where you’d keep repos and type the command below.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ npx create next-app@latest
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;This will take you through some installation prompts after which it’ll create a basic Next.js app in our folder. If you’d prefer a TypeScript setup add a TypeScript flag as displayed below:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx create-next-app@latest --typescript
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Now that we’ve set up our Next.js app let’s add the testing tools to our app.&lt;/p&gt;
&lt;h2&gt;
  
  
  Jest
&lt;/h2&gt;

&lt;p&gt;Jest is a Javascript testing framework built by Christoph Nakazawa and currently being maintained by Facebook. One of Jest’s main advantages is simplicity. It’s relatively easy to set up especially for first-time users.&lt;/p&gt;

&lt;p&gt;Let’s install our Jest dependencies using npm:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ npm install -D jest babel-jest
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;This will install Jest and also Babel Jest which ensure Jest works properly with Next.js.&lt;/p&gt;

&lt;p&gt;Next up we’ll create a .babelrc file and add the configuration displayed below. This will help with configuring Babel Jest we already installed.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{  
 "presets": ["next/babel"] 
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;This ensures Jest works in our app as expected.&lt;/p&gt;

&lt;p&gt;While Jest enables us easily test javascript apps and code it cannot directly test our Next.js app because it does not have the functionality to render React-based components. We’ll therefore need a tool that can work with Jest to render our Next.js app and then run tests on it.&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%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2ACACgabuoMFE_DUrXGAdcjQ.jpeg" 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%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2ACACgabuoMFE_DUrXGAdcjQ.jpeg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That’s where &lt;strong&gt;React Testing Library&lt;/strong&gt; comes in.&lt;/p&gt;
&lt;h2&gt;
  
  
  React Testing Library
&lt;/h2&gt;

&lt;p&gt;React Testing Library is an open-source tool that helps test your React.js app by rendering it and exposing the DOM to be queried. This helps you test your React.js app in its intention to be used rather than just the implementation details.&lt;/p&gt;

&lt;p&gt;Let’s install the dependencies to our app.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ npm install -D @testing-library/jest-dom @testing-library/react
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;This will install the React Testing Library and a @testing-library/jest-dom package that’ll work with Jest in testing our app.&lt;/p&gt;

&lt;p&gt;Before we start writing our tests let’s make some changes to the package.json file in our project directory.&lt;/p&gt;

&lt;p&gt;The first change is in the scripts field which tells npm how to run tests on our app.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;“test”: “jest — watch”
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;This tells npm to run jest in watch mode (monitor our changes and run tests accordingly) when we run the npm test a command. Our scripts field should now look as displayed below.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;“scripts”: {
 “dev”: “next dev”,
 “build”: “next build”,
 “start”: “next start”,
 “test”: “jest — watch”
},
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Secondly, we’ll add a new field to our package.json called jest .&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"jest": {
 "testEnvironment": "jsdom"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;This tells Node to use jsdom as our test environment. The default Node testEnvironment won’t enable us to test using a browser environment.&lt;/p&gt;

&lt;p&gt;Having set up these tools we can now proceed to code and write our tests.&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%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2A8g1HtXc2IlMAD48S4TZh4w.jpeg" 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%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2A8g1HtXc2IlMAD48S4TZh4w.jpeg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We’ll start by creating a basic sign-up form and then we’ll write tests for it.&lt;/p&gt;
&lt;h2&gt;
  
  
  Sign up form
&lt;/h2&gt;

&lt;p&gt;We’ll navigate to our index.js file, delete all its content, and import useState.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useState } from 'react';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Next up, we create a RegisterPage component and in it, we’ll create a basic form which we’ll be testing.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
In this RegisterPage component we declare an isLoading state value and set it to false. This value will indicate whether our being submitted (isLoading) or not.&lt;/p&gt;

&lt;p&gt;We then proceed to create a registerUser function which we’ll use to simulate a form submission, it’ll prevent the default form submission, set isLoading to true and set it back to false after 5 seconds or 5000 milliseconds.&lt;/p&gt;

&lt;p&gt;Next up we create formInputs, an array of objects, containing form inputs which we’ll render in our return block.&lt;/p&gt;

&lt;p&gt;Moving on, in our component, we’ll create a form, map through our formInputs array, and add a button that calls registerUser when it’s clicked. We can now export our components.&lt;/p&gt;

&lt;h2&gt;
  
  
  Styles
&lt;/h2&gt;

&lt;p&gt;Let’s add some basic styling to our styles/globals.css. If you do not have the file create one and import it to your _app.js file.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
We’ll now save these files and run our Next app using npm run dev . When we open our browsers to &lt;a href="http://localhost:3000" rel="noopener noreferrer"&gt;http://localhost:3000&lt;/a&gt; we should see our app now up and running.

&lt;p&gt;Now it’s time to write tests for the form in our app.&lt;/p&gt;

&lt;h2&gt;
  
  
  Testing the app
&lt;/h2&gt;

&lt;p&gt;Let’s start by creating a tests folder, in it, we’ll create a subfolder called pages . This is where we’ll be keeping test files for our pages (Create your first test file and name it index.test.js).&lt;/p&gt;

&lt;p&gt;First, we’ll make some imports to our test file.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import '@testing-library/jest-dom';

import { render, screen, fireEvent } from '@testing-library/react';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;We import @testing-library/jest-dom which was earlier installed, we also import render, screen and fireEvent from @testing-library/react more on their usage in this tutorial.&lt;/p&gt;

&lt;p&gt;Next up we import the Index file we’ll be testing.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import Index from '../../pages/index';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Before we write our tests let us create an array formInputValues that will contain mock data we’ll use in testing our form.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
Now, the tests.&lt;/p&gt;

&lt;p&gt;We’ll start by describing what our tests are for. We’ll start by creating a describe block of code. Describe is a Jest method used to group related testing blocks together. It takes two arguments: a string for describing the test suite and a callback function for wrapping the test or tests you’ll be writing.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;describe(‘Simple working form’, () =&amp;gt; {

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

&lt;/div&gt;
&lt;p&gt;Next up we’ll be writing our test cases in it blocks. it is a Jest method where the actual test functionalities are written. Just like a describe block, it takes 2 arguments: It takes two arguments: a string for describing the test suite and a callback function for wrapping the test functionality. An alternative method to it is test. Let’s start by writing one that tests if all our form inputs were correctly rendered. We’ll do this in our describe block.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
In our it block we’ll pass our Index component to a render method. render is a @testing-library/react method that simulates the render of the React component passed as an argument.&lt;/p&gt;

&lt;p&gt;We then proceed to loop through our formInputValues using forEach . For each value, we call screen.getByLabelText on value.label . screen is a @testing-library/react object that exposes methods used to query our component previously rendered, one of which is getByLabelText . getByLabelText is used to retrieve one element with the label passed as an argument.&lt;/p&gt;

&lt;p&gt;We pass the value returned from screen.getByLabelText as an argument to expect . expect is a Jest method that allows us access to matchers that help us test for certain conditions. An example of a matcher which we use is toBeInTheDocument , calling it on our expect function checks if our argument passed to expect is present in the component we rendered, i.e in the document.&lt;/p&gt;

&lt;p&gt;In essence, we expect elements with labels in our formInputValuesarray to exist in our component.&lt;/p&gt;

&lt;p&gt;Let’s add two more tests to complete our test cases. One that’ll check if our button is present in the document and another that’ll check if our button loads after it’s clicked.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
In our 2nd it block, we render Index, retrieve our button by calling the getByRole method from the screen object and initialize submitButton with the value. getByRole takes several arguments but for this tutorial, we only pass the name of the role we are querying for and an object containing the name of the button (The button’s text). We use two matchers to test for our button. toBeInTheDocument and not.toBeDisabled check if our button is present and not disabled.

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: Using not before any matcher tests for the reverse of the matcher.&lt;/p&gt;

&lt;p&gt;In our 3rd it block, we render Index and retrieve our submitButton . We loop through our inputFormValues array, get the respective inputs, and use fireEvent.change to simulate filling each input with values from our array.&lt;/p&gt;

&lt;p&gt;fireEvent is an object from @testing-library/react with methods used to simulate real dom events. We used change to change the form values and then we use click to simulate a click on our button.&lt;/p&gt;

&lt;p&gt;At last, we check if the value of our button has now been changed to Loading… after the click. We’re able to do that with another query method, findByRole . It is similar to getByRole but it returns a promise that’s resolved after a while.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: If you expect that your fireEvent changes won’t reflect immediately use findBy, rather than getBy.&lt;/p&gt;

&lt;p&gt;Our index.test.js should now look like this:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
run npm test a to see your test results, you should see something like this


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;PASS  tests/pages/index.test.js (14.833 s)&lt;br&gt;
  Simple working form&lt;br&gt;
    √ Should render all form inputs (208 ms)&lt;br&gt;
    √ Should render submit button (458 ms)&lt;br&gt;
    √ Should submit when inputs are filled and submit button clicked (303 ms)

&lt;p&gt;Test Suites: 1 passed, 1 total&lt;br&gt;
Tests:       3 passed, 3 total&lt;br&gt;
Snapshots:   0 total&lt;br&gt;
Time:        30.426 s&lt;br&gt;
Ran all test suites matching /a/i.&lt;br&gt;
&lt;/p&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  Conclusion&lt;br&gt;
&lt;/h2&gt;

&lt;p&gt;Congratulations, we’ve successfully tested our Next.js app. Feel free to include more test cases/increase the scope of the tests. The full project is available &lt;a href="https://github.com/chani24/Testing-React-Form" rel="noopener noreferrer"&gt;here&lt;/a&gt; on my GitHub.&lt;/p&gt;

&lt;p&gt;For more details on the tools used in this tutorial check out the &lt;a href="https://nextjs.org/docs/getting-started" rel="noopener noreferrer"&gt;Next.js&lt;/a&gt;, &lt;a href="https://jestjs.io/docs/getting-started" rel="noopener noreferrer"&gt;Jest&lt;/a&gt;, and &lt;a href="https://testing-library.com/docs/react-testing-library/intro" rel="noopener noreferrer"&gt;React Testing Library&lt;/a&gt; docs.&lt;/p&gt;

&lt;p&gt;I’ll appreciate feedback on this tutorial :), Goodluck coding!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>react</category>
      <category>beginners</category>
    </item>
    <item>
      <title>User re-authentication in your React app.</title>
      <dc:creator>Chidera Ani</dc:creator>
      <pubDate>Fri, 10 Jun 2022 08:18:04 +0000</pubDate>
      <link>https://dev.to/anichidera/user-re-authentication-in-your-web-app-16bj</link>
      <guid>https://dev.to/anichidera/user-re-authentication-in-your-web-app-16bj</guid>
      <description>&lt;p&gt;An approach to authenticating your users after a period of idle 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%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2A77mZX1a8XD-0anpgbYdCdQ.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%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2A77mZX1a8XD-0anpgbYdCdQ.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Intro…
&lt;/h2&gt;

&lt;p&gt;One of the ways to beef up your app’s security is to re-authenticate users when necessary. In this guide, we’re going to implement a re-authentication feature on a frontend application using react-idle-timer library to detect when a user has been idle after some time. For this guide, the authentication setup will just be localStorage .&lt;/p&gt;

&lt;p&gt;**React-idle-timer **is a javascript library used to detect and monitor user activity on your web application. We’ll be using it for this application, run yarn add react-idle-timer to install on your repo.&lt;/p&gt;

&lt;h2&gt;
  
  
  The App.
&lt;/h2&gt;

&lt;p&gt;Our Nextjs app will have 2 pages namely login.js and index.js . login.js will contain the login view and logic while index.js will contain our “dashboard” screen and the re-auth logic.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
In login.js we’ll create a simple login form and a login function that saves a random token to localStorage and redirects to our index.js page.

&lt;p&gt;&lt;em&gt;For this guide, only the form button is truly functional but ensure your form inputs are also functional in your app.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Next up is our index.js page where the re-auth feature is implemented.&lt;/p&gt;

&lt;p&gt;We import useIdleTimer hook from react-idle-timer and call it passing in some properties.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const { isIdle } = useIdleTimer({

onIdle,

timeout: 15000

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

&lt;/div&gt;
&lt;p&gt;We’re passing 2 properties;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;onIdle: a function that is called when our user is idle after some time.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;timeout: a period (in milliseconds) of inactivity after which our user is declared idle (onIdle is called). We made timeout 15 seconds for this guide, however in a real app timeout can be 15–30 minutes&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;useIdleTimer &lt;em&gt;accepts other properties like onActive, crossTab, startManually, etc. but for the purpose and simplicity of this guide, we’ll stick to just 2 properties.&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
Finally, In our index.js file we create a modal that becomes visible when a user has been idle and the user enters their password to re-authenticate themselves. If they close the modal or reload the page logout function is called.&lt;/p&gt;

&lt;p&gt;We’ve gone through a simple way to implement re-authentication in your web apps. You should check out the &lt;a href="https://idletimer.dev/" rel="noopener noreferrer"&gt;react-idle-timer documentation&lt;/a&gt; for more info and use cases.&lt;/p&gt;

&lt;p&gt;Happy coding…..&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>react</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>The difference between get, find, query (React Testing Library).</title>
      <dc:creator>Chidera Ani</dc:creator>
      <pubDate>Sat, 01 Jan 2022 14:48:53 +0000</pubDate>
      <link>https://dev.to/anichidera/the-difference-between-get-find-query-react-testing-library-472k</link>
      <guid>https://dev.to/anichidera/the-difference-between-get-find-query-react-testing-library-472k</guid>
      <description>&lt;p&gt;Choosing a suitable query type while working with React Testing Library.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Msd6Y1Uv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/500/1%2ABg_nX5M7EWctOBUJFT1BqQ.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Msd6Y1Uv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/500/1%2ABg_nX5M7EWctOBUJFT1BqQ.jpeg" alt="" width="500" height="200"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Intro
&lt;/h3&gt;

&lt;p&gt;React Testing Library (RTL) gives developers methods to find elements on the component it rendered for testing, these methods are called queries. There are 3 main types of RTL query types namely get, find and query.&lt;/p&gt;

&lt;p&gt;In this guide we’ll be looking at the differences and tips to note while working with any of these query types.&lt;/p&gt;

&lt;h3&gt;
  
  
  get
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;getBy : returns the matching node, however will throw an error if &lt;strong&gt;multiple&lt;/strong&gt; matches or &lt;strong&gt;no&lt;/strong&gt; matches are found.&lt;/li&gt;
&lt;li&gt;getAllBy : returns an array of matching nodes if &lt;strong&gt;at least one&lt;/strong&gt; match is found and throws an error if &lt;strong&gt;no&lt;/strong&gt; match is found.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Tip&lt;/strong&gt;  : Use these methods if you expect the element / elements to be present upon query.&lt;/p&gt;

&lt;h3&gt;
  
  
  query
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;queryBy : returns the matching node if one match is found and null if no match is found, however will throw an error if &lt;strong&gt;multiple&lt;/strong&gt; matches are found.&lt;/li&gt;
&lt;li&gt;queryAllBy : returns an array of matching nodes if &lt;strong&gt;at least one&lt;/strong&gt; match is found and an empty array if &lt;strong&gt;no&lt;/strong&gt; match is found.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Tip&lt;/strong&gt;  : Use these methods if you are looking to confirm presence of an element / elements.&lt;/p&gt;

&lt;h3&gt;
  
  
  find
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;findBy : returns a promise that returns the matching node, however will throw an error if &lt;strong&gt;multiple&lt;/strong&gt; matches or &lt;strong&gt;no&lt;/strong&gt; matches are found.&lt;/li&gt;
&lt;li&gt;findAllBy : returns a promise that returns an array of matching nodes if &lt;strong&gt;at least one&lt;/strong&gt; match is found and throws an error if &lt;strong&gt;no&lt;/strong&gt; match is found.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Tip&lt;/strong&gt;  : Use these methods if the element / elements being queried might display asynchronously (for example, if your element is expected to only display after an event is fired consider using find as it retries the query after some time).&lt;/p&gt;

&lt;p&gt;For more information on the queries check out the &lt;a href="https://testing-library.com/docs/queries/about/#types-of-queries"&gt;React Testing Library&lt;/a&gt; docs. I’ll appreciate feedback :).&lt;/p&gt;

&lt;p&gt;Good luck coding!&lt;/p&gt;

</description>
      <category>react</category>
      <category>reacttestinglibrary</category>
      <category>javascript</category>
      <category>testing</category>
    </item>
    <item>
      <title>Managing auth state in your Angular Firebase app</title>
      <dc:creator>Chidera Ani</dc:creator>
      <pubDate>Wed, 15 Jul 2020 16:32:58 +0000</pubDate>
      <link>https://dev.to/anichidera/managing-auth-state-in-your-angular-firebase-app-dp4</link>
      <guid>https://dev.to/anichidera/managing-auth-state-in-your-angular-firebase-app-dp4</guid>
      <description>&lt;h3&gt;
  
  
  Managing auth state in your Angular Firebase app.
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--41mZQ4yV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/626/1%2ANnQ1RFFyigISAmsqw5S3XA.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--41mZQ4yV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/626/1%2ANnQ1RFFyigISAmsqw5S3XA.jpeg" alt="" width="626" height="354"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Firebase authentication usually comes in handy when building any type of application. It helps manage your app’s users, auth state, as well as track user sessions.&lt;/p&gt;

&lt;p&gt;In this article we’ll go through tracking authentication state in your angular application and emitting changes to other components using Firebase’s onAuthStateChanged Observable and an RXJS Subject.&lt;/p&gt;

&lt;p&gt;let’s dive in……..&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;In the &lt;a href="https://gist.github.com/chani24/1dca2d5d2f315fa0500c1cab0dff289a#file-signin-component-ts"&gt;signin.component.ts&lt;/a&gt; , we get login data from the form in our template, which we will then pass to our auth service for authentication.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;In our &lt;a href="https://gist.github.com/chani24/4b9d75f79d14ec239d91975683573f2a#file-auth-service-ts"&gt;auth.service.ts&lt;/a&gt;, we import BehaviourSubject and Observable from RXJS. BehavourSubject is a special type of Observable that allows values to be multicast to many Observers. We also import AngularFireAuth from Angular Fire and auth from Firebase/app.&lt;/p&gt;

&lt;p&gt;The following steps explain the process.&lt;/p&gt;

&lt;p&gt;Step 1: Initialize a property, currentUser.&lt;/p&gt;

&lt;p&gt;Step 2 : Initialize a new BehaviourSubject called authStatusSub and pass currentUser as it’s first value.&lt;/p&gt;

&lt;p&gt;Step 3 : Initialize a property, currentAuthStatus which returns authStatusSub as an Obvservable that can be observed across components.&lt;/p&gt;

&lt;p&gt;Step 4 : Create a method called authStatusListener and used onAuthStateChanged to check auth state. If a user is Logged in, we call authStatusSub.next and pass in the current user credentials however if no user is logged in, we pass null into authStatusSub.next.&lt;/p&gt;

&lt;p&gt;authStatusSub.next passes the current value to all Observers. See &lt;a href="https://rxjs-dev.firebaseapp.com/guide/subject"&gt;https://rxjs-dev.firebaseapp.com/guide/subject&lt;/a&gt; for more explanation.&lt;/p&gt;

&lt;p&gt;Let’s proceed to listen to it’s emissions in another component.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;In &lt;a href="https://gist.github.com/chani24/d9f87683c7d1f7e16c077f1c359c7a42#file-header-component-ts"&gt;header.component.ts&lt;/a&gt;, we subscribe to currentAuthStatus from our auth service and pass it’s value into an earlier initialized isAuthenticated property.&lt;/p&gt;

&lt;p&gt;In &lt;a href="https://gist.github.com/chani24/d9f87683c7d1f7e16c077f1c359c7a42#file-header-component-html"&gt;header.component.html&lt;/a&gt;, we use conditionals to check if isAuthenticated is truthy or falsy, and then proceed to display elements in accordance.&lt;/p&gt;

&lt;p&gt;We’ve been able to successfully track auth state and emit new changes in our auth status across different components. We’ve also been able to subscribe and get emitted values and use in our application logic.&lt;/p&gt;

&lt;p&gt;Goodluck coding….&lt;/p&gt;

</description>
      <category>angular</category>
      <category>firebase</category>
      <category>javascript</category>
      <category>rxjs</category>
    </item>
  </channel>
</rss>
