<?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: Mohamed ikram</title>
    <description>The latest articles on DEV Community by Mohamed ikram (@ikmofem7).</description>
    <link>https://dev.to/ikmofem7</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%2F2768665%2F1f149974-f4ff-4718-b81f-31610094f18c.png</url>
      <title>DEV Community: Mohamed ikram</title>
      <link>https://dev.to/ikmofem7</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ikmofem7"/>
    <language>en</language>
    <item>
      <title>Unit/Component Testing React Apps: From 'Why Bother?' to 'Can't Live Without It'</title>
      <dc:creator>Mohamed ikram</dc:creator>
      <pubDate>Wed, 19 Feb 2025 05:31:24 +0000</pubDate>
      <link>https://dev.to/ikmofem7/unitcomponent-testing-react-apps-from-why-bother-to-cant-live-without-it-5415</link>
      <guid>https://dev.to/ikmofem7/unitcomponent-testing-react-apps-from-why-bother-to-cant-live-without-it-5415</guid>
      <description>&lt;h1&gt;
  
  
  🚨 Anatomy of a 3 AM Production Crisis: A Testing Tale
&lt;/h1&gt;

&lt;h2&gt;
  
  
  🎭 The Scene:
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;// The innocent-looking code that ruined everyone&lt;span class="s1"&gt;'s night
const getDomainUrl = (config: BackendConfig) =&amp;gt; {
  return config.domain; // What could go wrong? Everything.
};
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  🔍 Let's Debug Together: The Case of the Phantom Redirect
&lt;/h1&gt;

&lt;h3&gt;
  
  
  🚨 &lt;strong&gt;The Crime Scene&lt;/strong&gt;
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Dashboard users land at &lt;code&gt;undefined/rest-path&lt;/code&gt; after a "harmless" config change&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Suspect:&lt;/strong&gt; A sneaky backend configuration tweak
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Weapon:&lt;/strong&gt; Missing validation for domain paths
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Motive:&lt;/strong&gt; "It worked in dev!" 🤦
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Unit testing
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;describe&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Domain Configuration'&lt;/span&gt;, &lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  it&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'should reject missing domain configuration'&lt;/span&gt;, &lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    const emptyConfig &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{}&lt;/span&gt; as BackendConfig&lt;span class="p"&gt;;&lt;/span&gt;
    expect&lt;span class="o"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; getDomainUrl&lt;span class="o"&gt;(&lt;/span&gt;emptyConfig&lt;span class="o"&gt;))&lt;/span&gt;.toThrow&lt;span class="o"&gt;()&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;})&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;})&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Code
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;const getDomainUrl &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;config: BackendConfig&lt;span class="o"&gt;)&lt;/span&gt;: string &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(!&lt;/span&gt;config.domain&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    throw new DomainConfigError&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Domain URL is required'&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(!&lt;/span&gt;isValidDomainUrl&lt;span class="o"&gt;(&lt;/span&gt;config.domain&lt;span class="o"&gt;))&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    throw new DomainConfigError&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Invalid domain URL format'&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;config.domain&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Component Testing
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;describe&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'RedirectHandler'&lt;/span&gt;, &lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  it&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'should handle domain configuration gracefully'&lt;/span&gt;, &lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    const mockConfig &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    const &lt;span class="o"&gt;{&lt;/span&gt; getByTestId &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; render&lt;span class="o"&gt;(&lt;/span&gt;&amp;lt;RedirectHandler &lt;span class="nv"&gt;config&lt;/span&gt;&lt;span class="o"&gt;={&lt;/span&gt;mockConfig&lt;span class="o"&gt;}&lt;/span&gt; /&amp;gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    expect&lt;span class="o"&gt;(&lt;/span&gt;getByTestId&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'config-error'&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;.toBeInTheDocument&lt;span class="o"&gt;()&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    expect&lt;span class="o"&gt;(&lt;/span&gt;captureError&lt;span class="o"&gt;)&lt;/span&gt;.toHaveBeenCalledWith&lt;span class="o"&gt;(&lt;/span&gt;expect.any&lt;span class="o"&gt;(&lt;/span&gt;DomainConfigError&lt;span class="o"&gt;))&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;})&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;})&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Code
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;const RedirectHandler: React.FC&amp;lt;&lt;span class="o"&gt;{&lt;/span&gt; config: BackendConfig &lt;span class="o"&gt;}&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;({&lt;/span&gt; config &lt;span class="o"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  const &lt;span class="o"&gt;[&lt;/span&gt;error, setError] &lt;span class="o"&gt;=&lt;/span&gt; useState&amp;lt;string&amp;gt;&lt;span class="o"&gt;()&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  const navigate &lt;span class="o"&gt;=&lt;/span&gt; useNavigate&lt;span class="o"&gt;()&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  useEffect&lt;span class="o"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    try &lt;span class="o"&gt;{&lt;/span&gt;
      const domainUrl &lt;span class="o"&gt;=&lt;/span&gt; getDomainUrl&lt;span class="o"&gt;(&lt;/span&gt;config&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      // Safely handle navigation
      navigate&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="sb"&gt;`&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;domainUrl&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;/rest-path&lt;span class="sb"&gt;`&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt; catch &lt;span class="o"&gt;(&lt;/span&gt;e&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;e instanceof DomainConfigError&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        setError&lt;span class="o"&gt;(&lt;/span&gt;e.message&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        // Log to monitoring system
        captureError&lt;span class="o"&gt;(&lt;/span&gt;e&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;, &lt;span class="o"&gt;[&lt;/span&gt;config, navigate]&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;error&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;
      &amp;lt;ErrorBoundary&amp;gt;
        &amp;lt;ConfigurationError 
          &lt;span class="nv"&gt;message&lt;/span&gt;&lt;span class="o"&gt;={&lt;/span&gt;error&lt;span class="o"&gt;}&lt;/span&gt;
          &lt;span class="nv"&gt;retry&lt;/span&gt;&lt;span class="o"&gt;={()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; window.location.reload&lt;span class="o"&gt;()}&lt;/span&gt;
        /&amp;gt;
      &amp;lt;/ErrorBoundary&amp;gt;
    &lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &amp;lt;LoadingSpinner /&amp;gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  🛠️ &lt;strong&gt;The Evolution of Robust Domain Configuration&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  🎯 &lt;strong&gt;What Changed?&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Prevention Layers:&lt;/strong&gt;  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Custom error types (&lt;code&gt;InvalidDomainException&lt;/code&gt;)
&lt;/li&gt;
&lt;li&gt;Domain validation &lt;strong&gt;before&lt;/strong&gt; redirect logic
&lt;/li&gt;
&lt;li&gt;Error boundaries to catch misconfigurations
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Test-First Approach:&lt;/strong&gt;  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Wrote failing test for missing config &lt;em&gt;first&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;Added domain validation logic
&lt;/li&gt;
&lt;li&gt;Simulated edge cases (malformed URLs, empty configs)
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Better Error UX:&lt;/strong&gt;  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;User-friendly message: "Oops! We hit a snag – try again?"
&lt;/li&gt;
&lt;li&gt;Retry button with auto-logging for engineers
&lt;/li&gt;
&lt;li&gt;Integrated error tracking (Sentry/Datadog)
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  🚀 &lt;strong&gt;The Results&lt;/strong&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;No more undefined redirects &lt;/li&gt;
&lt;li&gt;Clear error tracking&lt;/li&gt;
&lt;li&gt;Happy users (and developers who can sleep at night!)&lt;/li&gt;
&lt;/ul&gt;




&lt;blockquote&gt;
&lt;h1&gt;
  
  
  🎯 The Tale of Two Developers: TDD vs. "We'll Test Later"
&lt;/h1&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  🐇 Bugs Bunny (Traditional):
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;3 AM:&lt;/strong&gt; Debugging while production burns
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Result:&lt;/strong&gt; Chasing midnight bugs, endless firefighting
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  ⭐ Iron Man (TDD):
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;"Test first, code with confidence."&lt;/em&gt;  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Result:&lt;/strong&gt; Deploys and sleeps soundly
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Secret Weapon:&lt;/strong&gt; Tests act as code armor
&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🔄 The Real Difference:
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Same deadline, drastically different outcomes.&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
While Bugs Bunny chases production bugs at midnight, Iron Man’s approach means:&lt;/p&gt;

&lt;h3&gt;
  
  
  🛡️ Prevention &amp;gt; Cure
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Catches bugs before they reach production
&lt;/li&gt;
&lt;li&gt;Each test is a shield against future issues
&lt;/li&gt;
&lt;li&gt;No more &lt;strong&gt;3 AM emergency fixes&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  🎯 Design That Makes Sense
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Tests force you to &lt;strong&gt;think before coding&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Code becomes &lt;strong&gt;naturally modular&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Refactoring becomes a breeze
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  📚 Code That Tells a Story
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Tests &lt;strong&gt;document how things should work&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;New team members get &lt;strong&gt;up to speed faster&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Requirements are &lt;strong&gt;crystal clear&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>testdev</category>
      <category>react</category>
      <category>typescript</category>
    </item>
  </channel>
</rss>
