<?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: TulusIbrahim</title>
    <description>The latest articles on DEV Community by TulusIbrahim (@tulusibrahim).</description>
    <link>https://dev.to/tulusibrahim</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%2F638187%2Fafff4d04-57f9-4df8-b124-9a669e4a86a7.png</url>
      <title>DEV Community: TulusIbrahim</title>
      <link>https://dev.to/tulusibrahim</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tulusibrahim"/>
    <language>en</language>
    <item>
      <title>How to Test React Component Using Jest</title>
      <dc:creator>TulusIbrahim</dc:creator>
      <pubDate>Sun, 09 Jul 2023 13:14:28 +0000</pubDate>
      <link>https://dev.to/tulusibrahim/how-to-test-react-component-using-jest-78f</link>
      <guid>https://dev.to/tulusibrahim/how-to-test-react-component-using-jest-78f</guid>
      <description>&lt;p&gt;In this tutorial, we will learn how to do unit testing to a react component using Jest. Jest is a testing framework that works with react, node, vue, and other tools with focus on simplicity. Unit test aim to make sure that one component is behave as expected despite many variations of props passed to it. The expectation here could be come from the design system.&lt;/p&gt;

&lt;p&gt;Some of the benefit of unit testing are:&lt;br&gt;
Make sure the code, design, implementation is behave as expected.&lt;br&gt;
Give the developer confident when about to do some refactoring, knowing that they have safety net about the component code.&lt;br&gt;
For the record I will use nextjs in this tutorial. Follow the installation guide on nextjs docs about jest here. After the step is done, now we are good to go.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Ok let’s get to the code&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Let’s start by creating a test file with .test.ts(.test.js if you use js) extension. Where to place the file is about your preference, but there are convention to place it within the &lt;strong&gt;tests&lt;/strong&gt; folder in the root folder or next to the component itself. So for example if you have button/index.tsx, then the test file would be in the button/index.test.ts, right next to the component file.&lt;/p&gt;

&lt;p&gt;Ok let’s start importing some stuff to begin with&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--SqOwfUWD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1bfavlvmdyehs4ebu5sj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--SqOwfUWD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1bfavlvmdyehs4ebu5sj.png" alt="Image description" width="800" height="226"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We import import ‘@testing-library/jest-dom’; so we can get access to test the DOM state, and some essential utilities from @testing-library/react to test the component.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--HhyiRaUU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t1bw82end6w4zway9v1x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--HhyiRaUU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t1bw82end6w4zway9v1x.png" alt="Image description" width="800" height="244"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is what a single test block look like. First give the test a clear name so we can undertand it better. You can render any component within the render method, in this case we want to render the button with a text within it. Render method return various function to assert the DOM state, but there we are destructuring the getByText function. Within the getByText function, you can use a string to find a text or a regex expression as well.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--RJypgXS---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/je8jhps2u5py6mbkb7n4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--RJypgXS---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/je8jhps2u5py6mbkb7n4.png" alt="Image description" width="800" height="355"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Another example is when we want to test the interaction with the component, we want to check if the onClick function actually called when we tapped on the button, we could create a mock function by writing let fn = jest.fn(); then pass it as a function to onClick props. Render the component, after it rendered, we fire an event by calling fireEvent.click. To target which DOM to click, we need to find it by findByText. Then we can expect the mock function fn is have been called.&lt;/p&gt;

&lt;p&gt;If we run yarn test in the terminal, we can see the result as below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bHFy2EYR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zf52a63tx40g9bbe54rs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bHFy2EYR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zf52a63tx40g9bbe54rs.png" alt="Image description" width="358" height="208"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Use Custom Hook to Separate Logic &amp; UI in React</title>
      <dc:creator>TulusIbrahim</dc:creator>
      <pubDate>Sat, 11 Jun 2022 02:52:59 +0000</pubDate>
      <link>https://dev.to/tulusibrahim/use-custom-hook-to-separate-logic-ui-in-react-b4</link>
      <guid>https://dev.to/tulusibrahim/use-custom-hook-to-separate-logic-ui-in-react-b4</guid>
      <description>&lt;p&gt;In this article, I will show you how to separate the concern between logic and UI when building react app. Let’s get started.&lt;/p&gt;

&lt;p&gt;Have a look at below code example.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ONFgMTse--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6u92d8uqw107elgaoj86.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ONFgMTse--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6u92d8uqw107elgaoj86.png" alt="Image description" width="700" height="682"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It’s a simple react counter app that works just perfectly fine. At this stage, your component will not have any problem by having two method inside it. But imagine as your component grow, the method inside it will increase, and your component will harder to debug, and so many lines in one file. This is where custom hook come in handy to separate between your logic and your actual UI.&lt;/p&gt;

&lt;p&gt;Now let’s create another file called useCounter, since our main component called counter, or whatever you like, but the name of the function is highly recommend to have use word in front of it as it state in react docs.&lt;/p&gt;

&lt;p&gt;In this file, we move all our method and state inside our main component to here. It would be look like code below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--O982l16F--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lpion234n1svjrxmdxff.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--O982l16F--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lpion234n1svjrxmdxff.png" alt="Image description" width="700" height="606"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We have move all our method to here, and notice that in the end, we return all the value and method that our main component will need. Now move back to our main component and import these from our custom hook like below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FCgEh48Z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9exa497b4veve1imd1b3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FCgEh48Z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9exa497b4veve1imd1b3.png" alt="Image description" width="700" height="582"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now our code will look much cleaner and readable because it’s only concern about the UI, and only import all the method and value from the custom hook. This also make the method inside the hook is reuseable for other component that need it. Thank you for reading, see ya!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Tricks to Debug JavaScript Faster</title>
      <dc:creator>TulusIbrahim</dc:creator>
      <pubDate>Fri, 27 May 2022 03:38:02 +0000</pubDate>
      <link>https://dev.to/tulusibrahim/tricks-to-debug-javascript-faster-23h6</link>
      <guid>https://dev.to/tulusibrahim/tricks-to-debug-javascript-faster-23h6</guid>
      <description>&lt;p&gt;If you’re a developer, you might find yourself tired after writing the same &lt;em&gt;console.log()&lt;/em&gt; syntax to check your current variable value. Here are some ways to log your values faster.&lt;/p&gt;

&lt;h2&gt;
  
  
  Create Log Function
&lt;/h2&gt;

&lt;p&gt;You can create this function on top of your JavaScript file, or create it in separate file if you want to use it in multiple files. This function pretty simple, it look 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;function log(...params){
    console.log(...params)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice that we use spread operator both in the function parameter and &lt;code&gt;console.log&lt;/code&gt; statement because we want to iterate all the item passed to the parameter, and print it to the &lt;em&gt;console.log&lt;/em&gt; statement.&lt;/p&gt;

&lt;p&gt;Somewhere below in your JavaScript file you can use this function like regular &lt;em&gt;console.log&lt;/em&gt; statement.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let yourVariable = 'Print me!';
log(yourVariable)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or something like,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;log('this is the data: ', yourVariable)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Create a snippet
&lt;/h2&gt;

&lt;p&gt;If you’re using vscode as code editor, you can click a gear setting in left bottom, and choose user snippets. You can choose existing snippet or create a new one. You can write the snippet like below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"log": {
   "prefix": "getlog",
   "body": "console.log()"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The log &lt;em&gt;key&lt;/em&gt; is just the name of the snippet, but the &lt;em&gt;prefix&lt;/em&gt; value is the word that you will type in the editor to get the body value. So if I type &lt;em&gt;getlog&lt;/em&gt; in my vscode, a console.log suggestion will appear as I type.&lt;/p&gt;

&lt;p&gt;That’s it! Thank you for reading, see ya!🍻&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Using React Hooks to Get Input Value</title>
      <dc:creator>TulusIbrahim</dc:creator>
      <pubDate>Thu, 05 Aug 2021 02:01:34 +0000</pubDate>
      <link>https://dev.to/tulusibrahim/using-react-hooks-to-get-input-value-4f25</link>
      <guid>https://dev.to/tulusibrahim/using-react-hooks-to-get-input-value-4f25</guid>
      <description>&lt;p&gt;Hi! Today we will learn how to use react hooks in a simple way in order to get strong basic fundamental in react hooks.&lt;/p&gt;

&lt;p&gt;The first thing we need to set up is of course react JS environment which you can refer to their &lt;a href="https://reactjs.org/"&gt;documentation&lt;/a&gt;. If all is ready, then we are good to go!&lt;/p&gt;

&lt;h2&gt;
  
  
  First Step
&lt;/h2&gt;

&lt;p&gt;Lets open up project file then open App.js file, there you can start by importing react and hooks useState so we can use it later. Here is how it looks.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState } from 'react'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next is creating function called App, here we using functional component so it is aligned because we are going to use react hooks.&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(){

}
export default App;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Don’t forget to import it in the end of our code so it does not produce error.&lt;/p&gt;

&lt;h2&gt;
  
  
  Second Step
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function App(){
const [name, setName] = useState('')
const [password, setPassword] = useState('')
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There we define name, setName. Name is the variable that we can use to display the value it has. Meanwhile setName is the setter method that we can use to change the value of name. useState is to define the initial value of the Name, it can be empty string, empty array, boolean, etc. To get better explanation of react hooks, you can refer to their &lt;a href="https://reactjs.org/docs/hooks-intro.html"&gt;docs&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Third Step
&lt;/h2&gt;

&lt;p&gt;We need to provide return inside our App component so it will display something to the screen.&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() {
  const [name, setName] = useState('');
  const [password, setPassword] = useState('');

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;input placeholder="username" onChange={e =&amp;gt; setName(e.target.value)} /&amp;gt;
      &amp;lt;input
        placeholder="password"
        onChange={e =&amp;gt; setPassword(e.target.value)}
      /&amp;gt;
      &amp;lt;button onClick={() =&amp;gt; handleInput()}&amp;gt;Input&amp;lt;/button&amp;gt;
      {name ? &amp;lt;p&amp;gt;Welcome {name}!&amp;lt;/p&amp;gt; : ''}
    &amp;lt;/div&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Looks terrible? yes. Calm down, I’ll explain it.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;So the first thing is we create a div tag, which will wraps up all other element. There I added some style to the div tag.&lt;/li&gt;
&lt;li&gt;Next is we define two input tag, a button with some placeholder in it&lt;/li&gt;
&lt;li&gt;Then we define onChange props inside input tag. Inside onChange props we define anonymous function that has e parameter, so then we can get access to the value inside input tag. (The anonymous function is using arrow function, you can check it through W3School if you’re not familiar yet with it.)&lt;/li&gt;
&lt;li&gt;Then inside anonymous function, we call the setter method that I already explain before, and we also pass the e parameter inside setter method, so the value of name, password is change every time the value inside input tag is changing.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const handleInput =  () =&amp;gt;{
    console.log(name, password)
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;There I also add a button with handleInput method just to console.log it to make sure everything is works.&lt;/li&gt;
&lt;li&gt;Beneath the button, I added ternary operator to show some text if the name variable is filled with something.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here is the looks of the whole code that we write so far.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState } from 'react';

function App() {
  const [name, setName] = useState('');
  const [password, setPassword] = useState('');

  const handleInput =  () =&amp;gt;{
    console.log(name, password)
  }

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;input placeholder="username" onChange={e =&amp;gt; setName(e.target.value)} /&amp;gt;
      &amp;lt;input
        placeholder="password"
        onChange={e =&amp;gt; setPassword(e.target.value)}
      /&amp;gt;
      &amp;lt;button onClick={() =&amp;gt; handleInput()}&amp;gt;Input&amp;lt;/button&amp;gt;
      {name ? &amp;lt;p&amp;gt;Welcome {name}!&amp;lt;/p&amp;gt; : ''}
    &amp;lt;/div&amp;gt;
  );
}
export default App
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Wrap’s up! Those three simple step is enough to use hooks in simple way just to get started and get solid basic understanding about react hooks. Thank you for reading up to this point, see ya!✨&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Building Backend Website With Supabase</title>
      <dc:creator>TulusIbrahim</dc:creator>
      <pubDate>Mon, 02 Aug 2021 15:10:25 +0000</pubDate>
      <link>https://dev.to/tulusibrahim/building-backend-website-with-supabase-49aa</link>
      <guid>https://dev.to/tulusibrahim/building-backend-website-with-supabase-49aa</guid>
      <description>&lt;p&gt;If you’re a front end web developer, you definitely doesn’t have any difficulties while deploying web app to hosting services like github pages, vercel, etc.. Especially if it is only static website, portfolio, or landing page.&lt;/p&gt;

&lt;p&gt;But when it comes to uploading the back end part, I sometimes get confused how to do it. I know I could upload the database to heroku using postgres, but another problem comes in when mostly we developing the database using mysql in local environment, and many other things related to backend that I don’t really familiar with.&lt;/p&gt;

&lt;p&gt;Lately I have a personal project that need a user authentication, a database to store some data. I know I could use firebase to make it happen, but I think it’s too much for a personal project. Not long after that, I see someone from twitter that recommended someone else to use &lt;em&gt;supabase&lt;/em&gt; just to store some simple data. I got curious, and finally I found solution for my personal project.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://supabase.io/"&gt;&lt;em&gt;Supabase&lt;/em&gt;&lt;/a&gt; is a backend as a platform (BaaS) service, where you can directly create your database there, having user authentication for your website, etc.. They offer three main product such as database, storage, authentication. I personally only use database and authentication.&lt;/p&gt;

&lt;p&gt;So basically we just call API that supabase generated automatically for us to do any operation we want, and we can keep easily deploying website in github pages, vercel, or other web hosting.&lt;/p&gt;

&lt;p&gt;In my opinion, their interface is good, especially in table editor section, I could get started quickly with it to arrange database design like I want. They also provide feature like foreign key relation in our tables, which is good for me.&lt;/p&gt;

&lt;p&gt;They also provide auto generated docs for us on how to fetch data from our tables and other operation like edit, update, delete data. It’s really simple to read all rows from your table, you can just like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let { data: blog, error } = await supabase
.from('blogs')
.select('*')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;em&gt;DISCLAIMER: I made the project in react&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I need the foreign key relation to link between post and comments, and also maintaining the ‘comment tables’. So when particular post is deleted, the comment attached with that post is deleted too.&lt;/p&gt;

&lt;p&gt;Another thing I’ve done with supabase is authentication. They provide many method like &lt;code&gt;signIn&lt;/code&gt;, &lt;code&gt;signUp&lt;/code&gt;, &lt;code&gt;session&lt;/code&gt; , etc.. It’s really convenient for us from front end to do it. For example, when someone wants to login, we can just write code this simple&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const { user, session, error } = await supabase.auth.signIn({
email: 'emailFromUser',
password: 'passwordFromUser',
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
I finally able to create backend without having to upload my own backend code and all the configuration that I need to do. I hope this article help you, thank you for reading this post.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>react</category>
      <category>javascript</category>
      <category>supabase</category>
    </item>
  </channel>
</rss>
