<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Adam Plesník on Medium]]></title>
        <description><![CDATA[Stories by Adam Plesník on Medium]]></description>
        <link>https://medium.com/@adamplesnik?source=rss-e7d31c3dc1c4------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*9UUcj2qs1EWZMwkCXElGYQ.jpeg</url>
            <title>Stories by Adam Plesník on Medium</title>
            <link>https://medium.com/@adamplesnik?source=rss-e7d31c3dc1c4------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 05:14:26 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@adamplesnik/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Test Your UI Components]]></title>
            <link>https://javascript.plainenglish.io/test-your-ui-components-8c75d9ea7576?source=rss-e7d31c3dc1c4------2</link>
            <guid isPermaLink="false">https://medium.com/p/8c75d9ea7576</guid>
            <category><![CDATA[tailwind-css]]></category>
            <category><![CDATA[components]]></category>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[component-testing]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Adam Plesník]]></dc:creator>
            <pubDate>Wed, 17 Apr 2024 20:16:29 GMT</pubDate>
            <atom:updated>2024-04-18T01:06:14.964Z</atom:updated>
            <content:encoded><![CDATA[<h4>Make sure the code looks fresh and works as new.</h4><p>Automatic testing of UI components may seem to have no sense at first sight. But it is crucial in continuously delivering the code of consistent quality and function.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lGlVRq3J-smBAMGYOHz6pA.png" /><figcaption>Run your tests. Period.</figcaption></figure><h3>The Toolkit</h3><p>There are lots of test libraries available. I built my example app with Vite and React, so <a href="https://vitest.dev/">Vitest</a> and <a href="https://www.npmjs.com/package/react-test-renderer">react-test-renderer</a> are the tools I chose for running tests.</p><p>Tools aren’t that much important, they should run well and serve the purpose. The important thing is the concept behind the testing itself.</p><h3>What to Test</h3><p>Finished components have some structure, function and styling. They are often used <em>dozens</em> of times round and around the code, so they become hard to keep an eye on. Tests come to play to ensure the components work and look like new.</p><h4>The Component</h4><p>Let’s test the &lt;Hr /&gt; component we created in one of <a href="https://medium.com/@adamplesnik/the-art-of-crafting-microcomponents-777922312a39">my previous posts</a>. Tests should cover all of its variants. There were two and I am going to add the third one to keep things more interesting.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_0YV-gM-dld5AS90urNKAw.png" /><figcaption>Three variants of the component.</figcaption></figure><p>The variants are the simple line, the line with the text in the beginning, and the line with the text at the end. There are three optional props textOrientation, text and className.</p><pre>import { addWithSpace } from &#39;@/utils/addWithSpace&#39;<br>import { HTMLAttributes } from &#39;react&#39;<br><br>const Hr = ({ text, textOrientation = &#39;start&#39;, className, ...rest }: HrProps) =&gt; {<br>  const flexOrderClassName = textOrientation === &#39;end&#39; ? &#39; order-last&#39; : &#39;&#39;<br>  <br>  return (<br>    &lt;div<br>      className={<br>        &#39;my-4 flex items-center gap-2 text-gray-700 dark:text-gray-300&#39; + addWithSpace(className)<br>      }<br>      {...rest}<br>    &gt;<br>      {text &amp;&amp; &lt;span className={&#39;text-xs font-medium&#39; + flexOrderClassName}&gt;{text}&lt;/span&gt;}<br>      &lt;hr className=&quot;h-px flex-1 border-none bg-current&quot; /&gt;<br>    &lt;/div&gt;<br>  )<br>}<br><br>export type HrProps = {<br>  text?: string<br>  textOrientation?: &#39;start&#39; | &#39;end&#39;<br>} &amp; HTMLAttributes&lt;HTMLDivElement&gt;<br><br>export default Hr</pre><h4>Test Cases</h4><p>We will have four test cases, three for the component alterations and one for adding a custom CSS class.</p><pre>&lt;!-- We are going to test these component variants --&gt;<br>&lt;Hr /&gt;<br>&lt;Hr text=&quot;End of Section 1&quot; /&gt;<br>&lt;Hr text=&quot;End of Section 2&quot; textOrientation=&quot;end&quot; /&gt;<br>&lt;Hr className=&quot;test-class&quot; /&gt;</pre><h3>The Test File and Snapshots</h3><p>There must be two files to run the component test. The Component.tsx is usually accompanied by a test file named like Component.test.tsx, or component.spec.ts. These are common patterns, but they of course depend on the framework.</p><p>The Hr.tsx component has its tests written in Hr.test.tsx. They are in the same folder, which is a personal preference. Some developers choose to have tests in a separate directory.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xykGqlZiTZ_f57oMw2CySg.png" /><figcaption>Component with it test file.</figcaption></figure><p>Let’s write a test for checking the component’s structure and style.</p><pre>import renderer from &#39;react-test-renderer&#39;<br>import { expect, test } from &#39;vitest&#39;<br>import Hr from &#39;./Hr&#39;<br>import { toJson } from &#39;./utils&#39;<br><br>test(&#39;Add class name to the component&#39;, () =&gt; {<br>  const component = renderer.create(&lt;Hr className=&quot;test-class&quot; /&gt;)<br>  let tree = toJson(component)<br>  expect(tree).toMatchSnapshot()<br>})<br><br>test(&#39;Render simple separator&#39;, () =&gt; {<br>  const component = renderer.create(&lt;Hr /&gt;)<br>  let tree = toJson(component)<br>  expect(tree).toMatchSnapshot()<br>})<br><br>test(&#39;Render separator with text in the beginning&#39;, () =&gt; {<br>  const component = renderer.create(&lt;Hr text=&quot;End of Section 1&quot; /&gt;)<br>  let tree = toJson(component)<br>  expect(tree).toMatchSnapshot()<br>})<br><br>test(&#39;Render separator with text at the end&#39;, () =&gt; {<br>  const component = renderer.create(&lt;Hr text=&quot;End of Section 2&quot; textOrientation=&quot;end&quot; /&gt;)<br>  let tree = toJson(component)<br>  expect(tree).toMatchSnapshot()<br>})</pre><p>To execute the test, you must define a CLI run script. They are usually in the package.json file. Vitest tests are executed by npx vitest, so the run script should look like in the example.</p><pre>&quot;scripts&quot;: {<br>  &quot;dev&quot;: &quot;vite&quot;,<br>  &quot;test&quot;: &quot;vitest&quot;<br>},<br>...</pre><p>The test generates the component code with the renderer.create(…) function and then compares it with the <em>snapshot</em>. The snapshot is saved during the first run of the test. Snapshots in our case are saved in a __snapshots__ folder. They must be commited with the code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DyRj8MdECSszoMa3lguPEw.png" /><figcaption>Don’t worry about the code structure and formatting, it is generated automatically.</figcaption></figure><p>It all happens under the hood. You see the result in a console. The test below fails at a <em>missing space</em> when appending the optional className. Such a small typo would be easy to overlook and also hard to debug manually.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tM7hY5gjJvTCH5h20UeB_g.png" /><figcaption>The Vitest console with a failed test.</figcaption></figure><h3>How to Implement Tests in Your Workflow</h3><p>Tests should run every time something important happens with your code. For example when you push to the repo, create a pull request, publish a release.</p><p>It is common to use GitHub actions to run tests. Check the <a href="https://docs.github.com/en/actions">documentation</a> to see all the options they provide.</p><pre>name: Build and test<br><br>on:<br>  push:<br>  pull_request:<br>  workflow_dispatch:<br><br>jobs:<br>  build:<br>    ...<br><br>    steps:<br>    ...<br>    - run: npm test</pre><p>Run your tests manually as well, especially when you commit to a public repository or create a merge request.</p><h3>Let’s Test!</h3><p>Do you test your code? What is your tool of choice? Let me know in the comments below!</p><p>Take a look at my <a href="https://github.com/adamplesnik/adamplesnik.com/tree/main/blog/2024/04/tests">GitHub</a> where you find all the source files for this article.</p><h3>In Plain English 🚀</h3><p><em>Thank you for being a part of the </em><a href="https://plainenglish.io/"><strong><em>In Plain English</em></strong></a><em> community! Before you go:</em></p><ul><li>Be sure to <strong>clap</strong> and <strong>follow</strong> the writer ️👏<strong>️️</strong></li><li>Follow us: <a href="https://twitter.com/inPlainEngHQ"><strong>X</strong></a><strong> | </strong><a href="https://www.linkedin.com/company/inplainenglish/"><strong>LinkedIn</strong></a><strong> | </strong><a href="https://www.youtube.com/channel/UCtipWUghju290NWcn8jhyAw"><strong>YouTube</strong></a><strong> | </strong><a href="https://discord.gg/in-plain-english-709094664682340443"><strong>Discord</strong></a><strong> | </strong><a href="https://newsletter.plainenglish.io/"><strong>Newsletter</strong></a></li><li>Visit our other platforms: <a href="https://stackademic.com/"><strong>Stackademic</strong></a><strong> | </strong><a href="https://cofeed.app/"><strong>CoFeed</strong></a><strong> | </strong><a href="https://venturemagazine.net/"><strong>Venture</strong></a><strong> | </strong><a href="https://blog.cubed.run/"><strong>Cubed</strong></a></li><li>More content at <a href="https://plainenglish.io/"><strong>PlainEnglish.io</strong></a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8c75d9ea7576" width="1" height="1" alt=""><hr><p><a href="https://javascript.plainenglish.io/test-your-ui-components-8c75d9ea7576">Test Your UI Components</a> was originally published in <a href="https://javascript.plainenglish.io/">JavaScript in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The Art of Crafting Microcomponents]]></title>
            <link>https://javascript.plainenglish.io/the-art-of-crafting-microcomponents-777922312a39?source=rss-e7d31c3dc1c4------2</link>
            <guid isPermaLink="false">https://medium.com/p/777922312a39</guid>
            <category><![CDATA[microcomponent]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[atomization]]></category>
            <category><![CDATA[tailwind-css]]></category>
            <category><![CDATA[components]]></category>
            <dc:creator><![CDATA[Adam Plesník]]></dc:creator>
            <pubDate>Mon, 15 Apr 2024 21:30:21 GMT</pubDate>
            <atom:updated>2024-04-17T01:09:42.450Z</atom:updated>
            <cc:license>https://creativecommons.org/licenses/by-sa/4.0/</cc:license>
            <content:encoded><![CDATA[<h3>The Art of Writing Microcomponents</h3><h4>Master the atomization by splitting your code to pieces.</h4><p>Microcomponent is the new black in the front-end dev universe. Frameworks like React or Vue.js make the process of converting pieces of code into components almost effortless.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UDpcaG1bTRkKJjGJ1g0FXQ.png" /><figcaption>You can have as many components as you like.</figcaption></figure><p>Of course it takes some time to write a component, but it is beneficial in the long run. Components make the code more readable, testable and easier to maintain in general.</p><p>We will build a <em>horizontal separator </em>as an example. I didn’t choose &lt;hr /&gt; by accident. It actually makes sense to create a component even for such a small HTML element.</p><h4>The Classic Element</h4><p>Let’s start with the traditional approach: HTML and CSS.</p><pre>&lt;hr&gt;</pre><pre>hr {<br>  background-color: #374151;<br>  border: none;<br>  height: 1px;<br>  margin: 1rem 0;<br><br>  @media (prefers-color-scheme: dark) {<br>    background-color: #d1d5db;<br>  }<br>}</pre><p>The &lt;hr&gt; in this example gets dark grey background, top and bottom spacing and its height is set to one pixel. It turns light grey on dark mode environments.</p><h4>The React Component</h4><p>Now let’s take a look at the React component with Tailwind CSS.</p><pre>&lt;Hr /&gt;</pre><pre>import { addWithSpace } from &#39;@/utils/addWithSpace&#39;<br>import { HTMLAttributes } from &#39;react&#39;<br><br>const Hr = ({ className, ...rest }: HTMLAttributes&lt;HTMLHRElement&gt;) =&gt; {<br>  return (<br>    &lt;hr<br>      className={&#39;my-4 h-px border-none bg-gray-700 dark:bg-gray-300&#39; + addWithSpace(className)}<br>      {...rest}<br>    /&gt;<br>  )<br>}<br><br>export default Hr</pre><p>This &lt;Hr /&gt; is styled to be exactly the same as the former one.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*A7-XfHAuRia8Szwqx9SkIw.png" /><figcaption>Horizontal line separator in light and dark mode.</figcaption></figure><p>There are multiple ways how to style the component. I chose Tailwind CSS because I love working with it, but the atomic component doesn’t need atomic CSS. You can totally style it with CSS-in-JS, scoped &lt;style&gt; section when using Vue, or style HTML attribute.</p><h3>Cons?</h3><p>I honestly don’t see <em>any</em> disadvantages in using the component-based approach, but some may be overwhelmed by its complexity at first sight.</p><p>The only <em>downside</em> of writing such a component may be that it may be intimidating to begin with. There is some overhead which we may not need in the beginning, but as the project grows, components like this help to keep things organised.</p><h3>One Source of Truth</h3><p>The component is unique, it has its own file, tag and props. You can always find all its references. Renaming or refactoring comes without fear of forgetting some instance or breaking the styling.</p><figure><img alt="VS Code interface with references of &lt;Hr /&gt; component." src="https://cdn-images-1.medium.com/max/1024/1*8G_0ou9PE88jJ5mTnYkiTQ.png" /><figcaption>VS Code interface with the references of the &lt;Hr /&gt; component.</figcaption></figure><p>Fellow developers will appreciate your effort as well. The component comes with firmly set collection of required, or optional props. Example below shows how TypeScript helps to avoid mistakes when using the component.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nCBYDJv8Dmw6Ul-wYjpOEA.png" /><figcaption>Well designed and typed component leaves no margin for errors.</figcaption></figure><p>You’d never know if you missed some crucial part of the code if you see the HTML tag. Component on the other hand always returns an error when there is a missing or mistyped prop.</p><pre>&lt;hr&gt; &lt;!-- &quot;Did I miss anything?&quot; --&gt;</pre><pre>return (<br>  &lt;Hr /&gt;<br>  /* &quot;No error, I am good to go!&quot; */<br>)</pre><p>And as a bonus, when you delete the component, all its related styling is magically ✨ removed, with no traces left scattered around CSS files.</p><h3>Add an Alternative Content</h3><p>Some components stay the same throughout the whole app, but majority of them change in relationship to the context they are used in. We may need to add an <em>icon</em>, or in our case, a <em>text</em> to the separator.</p><figure><img alt="The separator with optional text." src="https://cdn-images-1.medium.com/max/1024/1*RbG6PAh75bYMdlvDkEFApA.png" /><figcaption>The separator with optional text.</figcaption></figure><h4>Updated Classic Element</h4><p>Traditionally we’d have to alter each &lt;hr&gt; we have in the project. The only option we have is to perform <em>find–&amp;–replace</em>, add new class names, style it and hope we didn’t forget anything.</p><pre>&lt;div class=&quot;separator&quot;&gt;<br>  &lt;!-- Added flex wrapper, new styled span with text --&gt;<br>  &lt;span class=&quot;separator-text&quot;&gt;<br>    End of Section 1<br>  &lt;/span&gt;<br>  &lt;hr&gt;<br>&lt;/div&gt;</pre><pre>.separator {<br>  align-items: center;<br>  display: flex;<br>  gap: 0.5rem;<br>  <br>  .separator-text {<br>    ...<br>  }<br>  <br>  hr {<br>    flex: 1;<br>  }<br>}</pre><h4>Updated React Component</h4><p>The change like this is much safer to perform in the component. We add a new optional text prop, the horizontal ruler gets the currentColor background color, text color is set on the wrapper &lt;div&gt;.</p><pre>&lt;Hr text=&quot;End of Section 1&quot; /&gt;</pre><pre>import { addWithSpace } from &#39;@/utils/addWithSpace&#39;<br>import { HTMLAttributes } from &#39;react&#39;<br><br>const Hr = ({ text, className, ...rest }: HrProps) =&gt; {<br>  return (<br>    &lt;div<br>      className={<br>        &#39;my-4 flex items-center gap-2 text-gray-700 dark:text-gray-300&#39; + addWithSpace(className)<br>      }<br>      {...rest}<br>    &gt;<br>      {text &amp;&amp; &lt;span className=&quot;text-xs font-medium&quot;&gt;{text}&lt;/span&gt;}<br>      &lt;hr className=&quot;h-px flex-1 border-none bg-current&quot; /&gt;<br>    &lt;/div&gt;<br>  )<br>}<br><br>export type HrProps = {<br>  text?: string<br>} &amp; HTMLAttributes&lt;HTMLDivElement&gt;<br><br>export default Hr</pre><p>We made the component much more capable while still keeping its integrity and isolation.</p><h3>What is Next?</h3><p>The sky is the limit with the components. It is always possible to extend its functionality, or split it to more parts at some point. The example above may be even more granular, broken into two pieces, etc.</p><pre>&lt;!-- Simple separator without text --&gt;<br>&lt;Hr /&gt;<br><br>&lt;!-- Composed separator with text, which contains the &lt;Hr /&gt; inside --&gt;<br>&lt;HrWithText text=&quot;End of Section 1&quot; /&gt;</pre><h4>Testing</h4><p>Every component should be tested. Tests are your code’s guardians; they run with every build and make sure nothing has been broken during the development. I test this component in my next post.</p><h3>Conclusion</h3><p>Modern frameworks make writing and maintaining components very easy and straightforward. In my opinion, it makes sense to create a component for anything, even for such a small thing as a horizontal separator.</p><p>Do you agree? How do you tackle this issue? I’d love to hear, and discuss your opinions down in the comments.</p><p>You can find the code for this post on my <a href="https://github.com/adamplesnik/adamplesnik.com/blob/main/blog/2024/04/atomic-components/Hr.tsx">GitHub</a> as well.</p><p>Read more about the <a href="https://react.dev/learn/thinking-in-react#step-1-break-the-ui-into-a-component-hierarchy">React components</a>, <a href="https://tailwindcss.com/docs/reusing-styles#extracting-components-and-partials">Tailwind CSS</a>.</p><h3>In Plain English 🚀</h3><p><em>Thank you for being a part of the </em><a href="https://plainenglish.io/"><strong><em>In Plain English</em></strong></a><em> community! Before you go:</em></p><ul><li>Be sure to <strong>clap</strong> and <strong>follow</strong> the writer ️👏<strong>️️</strong></li><li>Follow us: <a href="https://twitter.com/inPlainEngHQ"><strong>X</strong></a><strong> | </strong><a href="https://www.linkedin.com/company/inplainenglish/"><strong>LinkedIn</strong></a><strong> | </strong><a href="https://www.youtube.com/channel/UCtipWUghju290NWcn8jhyAw"><strong>YouTube</strong></a><strong> | </strong><a href="https://discord.gg/in-plain-english-709094664682340443"><strong>Discord</strong></a><strong> | </strong><a href="https://newsletter.plainenglish.io/"><strong>Newsletter</strong></a></li><li>Visit our other platforms: <a href="https://stackademic.com/"><strong>Stackademic</strong></a><strong> | </strong><a href="https://cofeed.app/"><strong>CoFeed</strong></a><strong> | </strong><a href="https://venturemagazine.net/"><strong>Venture</strong></a><strong> | </strong><a href="https://blog.cubed.run/"><strong>Cubed</strong></a></li><li>More content at <a href="https://plainenglish.io/"><strong>PlainEnglish.io</strong></a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=777922312a39" width="1" height="1" alt=""><hr><p><a href="https://javascript.plainenglish.io/the-art-of-crafting-microcomponents-777922312a39">The Art of Crafting Microcomponents</a> was originally published in <a href="https://javascript.plainenglish.io/">JavaScript in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>