<?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: Priyanka Malakolikar</title>
    <description>The latest articles on DEV Community by Priyanka Malakolikar (@priyankamalakolikar).</description>
    <link>https://dev.to/priyankamalakolikar</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%2F1222422%2F9c46b848-af09-4ced-9049-752d823ed652.png</url>
      <title>DEV Community: Priyanka Malakolikar</title>
      <link>https://dev.to/priyankamalakolikar</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/priyankamalakolikar"/>
    <language>en</language>
    <item>
      <title>Understanding Jest 'moduleNameMapper' for Effortless Testing</title>
      <dc:creator>Priyanka Malakolikar</dc:creator>
      <pubDate>Tue, 26 Dec 2023 18:04:57 +0000</pubDate>
      <link>https://dev.to/priyankamalakolikar/understanding-jest-modulenamemapper-for-effortless-testing-3961</link>
      <guid>https://dev.to/priyankamalakolikar/understanding-jest-modulenamemapper-for-effortless-testing-3961</guid>
      <description>&lt;p&gt;&lt;strong&gt;Introduction&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In the realm of JavaScript testing, Jest has emerged as a powerhouse, providing developers with a robust and intuitive framework to ensure their code behaves as expected. One of the key features that makes Jest so versatile is its '&lt;strong&gt;moduleNameMapper&lt;/strong&gt;' configuration option. This often-overlooked gem allows developers to map module paths in a way that can significantly streamline testing workflows. In this blog post, we'll delve into the intricacies of '&lt;strong&gt;moduleNameMapper&lt;/strong&gt;' and explore how it can be harnessed to make your testing experience even more powerful.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Overview of &lt;code&gt;moduleNameMapper&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;At its core, '&lt;strong&gt;moduleNameMapper&lt;/strong&gt;' is a Jest configuration option designed to map module paths. This means that, as a developer, you have the ability to redefine how Jest resolves module imports during testing. This flexibility proves invaluable in various scenarios, particularly when it comes to mocking dependencies or establishing module aliases.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Basic Usage&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To get started with '&lt;strong&gt;moduleNameMapper&lt;/strong&gt;', consider a scenario where your project includes a utility module, '&lt;strong&gt;utils/logger.js&lt;/strong&gt;'. Without '&lt;strong&gt;moduleNameMapper&lt;/strong&gt;', Jest would seek this module in its original path. However, with the addition of the following snippet to your Jest configuration, you can redirect Jest to look for '&lt;strong&gt;logger.js&lt;/strong&gt;' in a dedicated '&lt;strong&gt;_ &lt;em&gt;mocks&lt;/em&gt; _&lt;/strong&gt;'directory:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// jest.config.js
module.exports = {
  // other configurations...
  moduleNameMapper: {
    '^utils/logger$': '&amp;lt;rootDir&amp;gt;/__mocks__/logger.js',
  },
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This simple mapping instructs Jest to import the mock logger module instead of the original one during testing. This proves especially useful when dealing with modules that have side effects, such as logging, networking, or file I/O, allowing you to isolate and control these effects during tests.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Mocking Dependencies&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Consider a React component that relies on an external API module, '&lt;strong&gt;services/api.js&lt;/strong&gt;'. You can use '&lt;strong&gt;moduleNameMapper&lt;/strong&gt;' to redirect Jest to a mock version of the API module for testing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// jest.config.js
module.exports = {
  // other configurations...
  moduleNameMapper: {
    '^services/api$': '&amp;lt;rootDir&amp;gt;/__mocks__/api.js',
  },
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This enables you to simulate different API responses, error scenarios, or even offline conditions without making actual network requests during your tests.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Advanced Configurations&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;While the basic usage of '&lt;strong&gt;moduleNameMapper&lt;/strong&gt;' is powerful, Jest provides additional features for more advanced scenarios. You can leverage regular expressions and other options to create dynamic mappings based on patterns. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// jest.config.js
module.exports = {
  // other configurations...
  moduleNameMapper: {
    '^utils/(.*)$': '&amp;lt;rootDir&amp;gt;/src/utils/$1',
  },
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, any module starting with '&lt;strong&gt;utils/&lt;/strong&gt;' will be mapped to the corresponding module in the '&lt;strong&gt;src/utils/&lt;/strong&gt;' directory.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Best Practices&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;As you dive deeper into using '&lt;strong&gt;moduleNameMapper&lt;/strong&gt;', it's essential to follow best practices. Keep your mappings organized, use clear and concise patterns, and document the purpose of each mapping. This ensures that your test configurations remain maintainable as your project grows.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Troubleshooting&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Despite its power, '&lt;strong&gt;moduleNameMapper&lt;/strong&gt;' might pose challenges if misconfigured. Common issues include incorrect paths, conflicting mappings, or unexpected behaviour. When facing such issues, use Jest's debugging features and carefully review your configurations to identify and resolve the root cause.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Demystifying Jest's moduleNameMapper Pattern: ^@/(.*)$&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In the context of Jest's '&lt;strong&gt;moduleNameMapper&lt;/strong&gt;' configuration, the pattern &lt;strong&gt;^@/(.*)$&lt;/strong&gt; is a regular expression used for mapping module names to file paths. Let's break down the meaning of each symbol in this expression:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;'^' (caret)&lt;/strong&gt;: Asserts the start of the string. In regular expressions, &lt;strong&gt;'^'&lt;/strong&gt; indicates that the pattern must match at the beginning of the input.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;'@/'&lt;/strong&gt;: Matches the literal characters "@/" in the module name. This could be a custom alias or prefix used in your project.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;'(.*)'&lt;/strong&gt;: This is a capturing group that matches any sequence of characters (except for a newline). The &lt;strong&gt;'.'&lt;/strong&gt; matches any character, and &lt;strong&gt;'*'&lt;/strong&gt; means zero or more occurrences. The parentheses &lt;strong&gt;'()'&lt;/strong&gt; are used to capture the matched sequence for later use.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;4.&lt;strong&gt;'$'&lt;/strong&gt;: Asserts the end of the string. In regular expressions, &lt;strong&gt;'$'&lt;/strong&gt;indicates that the pattern must match at the end of the input.&lt;/p&gt;

&lt;p&gt;Putting it all together, &lt;strong&gt;'^@/(.*)$'&lt;/strong&gt;is a regular expression pattern that matches module names starting with "@/" in Jest's module resolution. The part of the module name after "@/" is captured using the &lt;strong&gt;'(.*)'&lt;/strong&gt; group, and this captured part is then used in the replacement path.&lt;/p&gt;

&lt;p&gt;For example, if you have an import statement like &lt;strong&gt;import someModule from '@/path/to/someModule';&lt;/strong&gt;, Jest will use this pattern to map the module name &lt;strong&gt;@/path/to/someModule&lt;/strong&gt; to the corresponding file path in your project.&lt;/p&gt;

&lt;p&gt;In the &lt;strong&gt;moduleNameMapper&lt;/strong&gt; configuration, you might use it 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;// jest.config.js

module.exports = {
  // ... other Jest configurations ...
  moduleNameMapper: {
    '^@/(.*)$': '&amp;lt;rootDir&amp;gt;/src/$1',
    // ... other mappings ...
  },
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, &lt;strong&gt;/src/$1&lt;/strong&gt; is the replacement path where &lt;strong&gt;$1&lt;/strong&gt; refers to the captured part of the module name, allowing Jest to map the module name to the correct file path.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In conclusion, the &lt;strong&gt;&lt;code&gt;moduleNameMapper&lt;/code&gt;&lt;/strong&gt; configuration in Jest is a powerful tool that allows developers to exert fine-grained control over module resolution during testing. Whether you're mocking dependencies to isolate side effects or establishing module aliases for cleaner imports, &lt;strong&gt;&lt;code&gt;moduleNameMapper&lt;/code&gt;&lt;/strong&gt;empowers you to create more robust and maintainable test suites.&lt;/p&gt;

&lt;p&gt;By grasping the basics, exploring advanced configurations, and following best practices, you unlock the full potential of &lt;strong&gt;&lt;code&gt;moduleNameMapper&lt;/code&gt;&lt;/strong&gt;. Integrate it into your projects, share insights, and witness how it elevates the quality and reliability of your JavaScript applications.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Customizing Increment &amp; Decrement Arrows on Text Field in React MUI</title>
      <dc:creator>Priyanka Malakolikar</dc:creator>
      <pubDate>Mon, 25 Dec 2023 18:21:26 +0000</pubDate>
      <link>https://dev.to/priyankamalakolikar/customizing-increment-decrement-arrows-on-text-field-in-react-mui-222e</link>
      <guid>https://dev.to/priyankamalakolikar/customizing-increment-decrement-arrows-on-text-field-in-react-mui-222e</guid>
      <description>&lt;p&gt;&lt;strong&gt;Introduction&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When working with forms in a React application, it's common to use the &lt;strong&gt;TextField&lt;/strong&gt; component with the &lt;strong&gt;type="number"&lt;/strong&gt; attribute for numeric inputs. However, the default appearance of the increment and decrement arrows might not always match the design requirements. In this blog post, we'll explore how to customize these arrows in a React application using Material-UI (MUI).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Prerequisites&lt;/strong&gt; :&lt;/p&gt;

&lt;p&gt;Before we get started, make sure you have the following installed:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Node.js and npm&lt;/li&gt;
&lt;li&gt;React and Material-UI in your project&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you haven't set up your project with React and MUI, you can do so by running the following commands:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npx create-react-app my-demo-app&lt;br&gt;
cd my-demo-app&lt;br&gt;
npm install @mui/material @emotion/react @emotion/styled&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Implementation Steps:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1&lt;/strong&gt;: &lt;strong&gt;Create a Custom Component &amp;amp; Implement the Customized TextField&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Create a component with .js extension &amp;amp; customize the TextField component  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2&lt;/strong&gt;: &lt;strong&gt;Handle Increment and Decrement&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Implement the &lt;strong&gt;handleIncrement&lt;/strong&gt; and &lt;strong&gt;handleDecrement&lt;/strong&gt; functions to update the value accordingly.&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";
import { Stack, styled } from "@mui/material";
import TextField from "@mui/material/TextField";
import IconButton from "@mui/material/IconButton";
import ExpandLessOutlinedIcon from "@mui/icons-material/ExpandLessOutlined";
import ExpandMoreOutlinedIcon from "@mui/icons-material/ExpandMoreOutlined";


const StyledTextField = styled(TextField)`
  input[type="number"]::-webkit-inner-spin-button,
  input[type="number"]::-webkit-outer-spin-button {
    display: none;
  }
`;

const StyledIconButton = styled(IconButton)({
  "&amp;amp; .MuiSvgIcon-root": {
    fontSize: "16px",
  },
  padding: "0",
  color: "#080707",
});

const TextFieldInputComponent = () =&amp;gt; {
  const [num, setNum] = useState(0);
  const handleIncrement = () =&amp;gt; {
    setNum(num + 1);
     // Implement your logic to increment the value
  };
  const handleDecrement = () =&amp;gt; {
    setNum(num - 1);
    // Implement your logic to decrement the value
  };
  return (
    &amp;lt;StyledTextField
      type="number"
      value={num}
      InputProps={{
        endAdornment: (
          &amp;lt;Stack&amp;gt;
            &amp;lt;StyledIconButton onClick={handleIncrement}&amp;gt;
              &amp;lt;ExpandLessOutlinedIcon /&amp;gt;
            &amp;lt;/StyledIconButton&amp;gt;
            &amp;lt;StyledIconButton onClick={handleDecrement}&amp;gt;
              &amp;lt;ExpandMoreOutlinedIcon /&amp;gt;
            &amp;lt;/StyledIconButton&amp;gt;
          &amp;lt;/Stack&amp;gt;
        ),
      }}
    /&amp;gt;
  );
};

export default TextFieldInputComponent;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output :&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--HsEshh6n--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/615ifhotrqdkdibcrlx5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--HsEshh6n--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/615ifhotrqdkdibcrlx5.png" alt="Customized TextField Input" width="614" height="182"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In this post, we learned how to customize increment and decrement arrows on a React Material-UI TextField of type number. By leveraging Emotion for styling, we achieved a seamless integration with the application's design.&lt;br&gt;
By following these steps, you can not only meet design requirements but also enhance the overall user experience in numeric input fields.Experiment with different styles and functionalities to create a user-friendly and aesthetically pleasing numeric input experience.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
