<?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 Yashika Garg on Medium]]></title>
        <description><![CDATA[Stories by Yashika Garg on Medium]]></description>
        <link>https://medium.com/@yashikagarg13?source=rss-3573a92bb7ef------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*9TT-snRoFTIZzW2W.</url>
            <title>Stories by Yashika Garg on Medium</title>
            <link>https://medium.com/@yashikagarg13?source=rss-3573a92bb7ef------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 20 May 2026 13:30:30 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@yashikagarg13/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[Typography Series (Part — 2) — Major challenges]]></title>
            <link>https://medium.com/@yashikagarg13/typography-series-part-2-major-challenges-f7566dea463c?source=rss-3573a92bb7ef------2</link>
            <guid isPermaLink="false">https://medium.com/p/f7566dea463c</guid>
            <category><![CDATA[css]]></category>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[typography]]></category>
            <category><![CDATA[frontend-development]]></category>
            <dc:creator><![CDATA[Yashika Garg]]></dc:creator>
            <pubDate>Fri, 28 Sep 2018 17:05:16 GMT</pubDate>
            <atom:updated>2018-09-28T17:05:16.628Z</atom:updated>
            <content:encoded><![CDATA[<p>Hola folks! This blog series about Typography has two parts</p><ul><li>Importance and basic terminology of typography that every frontend developer should know. Check it out <a href="https://medium.com/@yashikagarg13/typography-series-part-1-terminology-every-frontend-developer-should-know-ccb0a05c7d9b">here</a>.</li><li>Major challenges faced while getting typography right.</li></ul><p>If you haven’t already been through basic typography terminology, check it out <a href="https://medium.com/@yashikagarg13/typography-series-part-1-terminology-every-frontend-developer-should-know-ccb0a05c7d9b">here</a>. In this blog post, we will be discussing the major challenges we face in order to get the typography right on our websites. So, let’s get started.</p><h4>Major Challenges</h4><p>We have been through some terms in Typography world and we are now aware of some fundamentals. Now let&#39;s try to solve some problems we face when we try to set typography of pages of our application.<br> <br> Let’s see a page with bad typography.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/0*fE2F6og31RUFaXci.png" /><figcaption>Bad Typography Example</figcaption></figure><p>We apply our knowledge step by step to improve the page’s typography and make it look like this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/0*XPNLumcrkHLqvkQO.png" /><figcaption>Good Typography Example</figcaption></figure><p><strong>Different font sizes for different screen sizes. </strong>When we move from large desktops to medium screens or tablets or mobile screen which in themselves have a large range of screen sizes, we want our content to adjust and stay readable. To achieve this we can make use of media queries in our CSS files. Adjust font size for different headings and paragraphs to make them look pretty on different screen sizes. But there are hundreds of screen sizes and it’s entirely impossible to make adjustments for each of those.</p><p>CSS world has frameworks like bootstrap and gaining knowledge from them, we can try to find the solution to our problem — <strong>Breakpoints</strong>! Here are some commonly use breakpoints:</p><pre>xs - 480px - small screen phones<br>sm - 768px - Tablets and phones<br>md - 992px - Desktop<br>lg - 1200px - Wide Desktop</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/180/0*h1F6qn49XcHDjJrg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/179/0*U0ObahNlpJEql6VO.png" /></figure><p>As clearly visible, the second image looks much better for the small screen as compared to the first one. This is the magic of media queries.</p><p><strong>Attaining Vertical Rhythm.</strong> Firstly, I would like to put some light on what vertical rhythm is. It is a phenomenon of keeping consistent vertical spaces between different elements on page. Vertical rhythm help us establish hierarchy on page and convey the relation how different content is connected to each other. It gives a very professional look to our page. To achieve vertical rhythm, we need a base line-height value and compute all our vertical spaces, i.e., margins, paddings, line-heights, as a multiple of that base line height. To figure out our base line-height, we need to find the value of a vertical space which gets repeated most on our page. For instance, what is the most common margin or line-height or padding we are using in our page. This value would be our base line-height. We will be repeating this value on our page to bring consistency and this repetition has to vary. We definitely don’t want each of our vertical space to be same. We need to be variable in order to establish hierarchy and relation between different elements of page. For achieving this variation we will use multiples of our base line height. We can use either LESS or SASS preprocessor. I am demonstrating using LESS.</p><pre>@base-line-height: 24px;</pre><pre>p {<br>line-height: @base-line-height;<br>margin-bottom: @base-line-height;<br>}</pre><pre>h2 {<br>line-height: @base-line-height * 2;<br>margin-bottom: @base-line-height * 2;<br>}</pre><pre>h2 {<br>line-height: @base-line-height * 1.5;<br>margin-bottom: @base-line-height * 1.5;<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/0*3wlxvqRMfG0LtL9q.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/0*5NfaofOlQjNKwdzo.png" /></figure><p>With help of some CSS, I add these horizontal lines on our page to see how our content is vertically setup. In the first snapshot, you will notice that its pretty bad and there is no hierarchy at all. But as soon as we apply our varying repetition of baseline height, we noticed everything looks pretty good (Snapshot 2).</p><p><strong>Update typography throughout the site.</strong> Sometimes you decide for your website a typography in terms of font-size for headings and paragraphs, vertical spaces and so on, and you grow your website on that. But at a later stage, you found that something is missing and it’s not fitting the way you presumed. Now you want to change it all through! Wait a minute! Change typography throughout the site, that’s a large amount of work and tedious. Changing each and every page, each and every element throughout the site. But its important you see. You need your work to match your expectation.</p><p>Lets try to think a solution for this. What if we set a base font size for our application and manipulate all other font-sizes and vertical spaces based on that. Now if we think of changing typography to lets say a bigger font-size or a smaller one, adjust vertical spaces, then all we need to do is to change our base font size and tweak our manipulations around it to handle this scenario.</p><pre>@base-font-size: 15px;<br>@base-line-height: 1.6; // Unit less value<br>@computed-line-height: ceil(@base-font-size * @base-line-height);</pre><pre>p {<br>font-size: @base-font-size;<br>line-height: @computed-line-height;<br>margin-bottom: @computed-line-height;<br>}</pre><pre>h2 {<br>font-size: ceil(@base-font-size * 2.15);     // 32px<br>line-height: @computed-line-height * 2;      // 48px<br>margin-bottom: @computed-line-height * 2;    // 48px<br>}</pre><pre>h4 {<br>font-size: ceil(@base-font-size * 1.6);      // 24px<br>line-height: @computed-line-height * 1.5;    // 36px<br>margin-bottom: @computed-line-height * 1;    // 24px<br>}</pre><p>Now I want to make things look bigger and all I need to tweak is following:</p><pre><strong>@base-font-size: 20px;<br>@base-line-height: 1.2;</strong> // Unit less value<br>@computed-line-height: ceil(@base-font-size * @base-line-height);</pre><pre>p {<br>font-size: @base-font-size;<br>line-height: @computed-line-height;<br>margin-bottom: @computed-line-height;<br>}</pre><pre>h2 {<br><strong>font-size: ceil(@base-font-size * 1.8);      // 36px</strong><br>line-height: @computed-line-height * 2;      // 48px<br>margin-bottom: @computed-line-height * 2;    // 48px<br>}</pre><pre>h4 {<br><strong>font-size: ceil(@base-font-size * 1.4);      // 28px</strong><br>line-height: @computed-line-height * 1.5;    // 36px<br>margin-bottom: @computed-line-height * 1;    // 24px<br>}</pre><p><strong>Responsive designs</strong> I know you have stick too long and I swear this is the last challenge. We have read above that we need different font-sizes for different screens. Along with that we need our pages to be responsive, to be fluid as we move from different ranges of devices. We need our vertical spaces and paddings to fit in every scenario.</p><p>Thinking about this, we have everything we need to beat this up. We will use media queries, we will make computations on base-font and we will use our scalable unit.</p><p>Remember, we talked about them just a few minutes before — <strong>Em</strong> and <strong>Rem.</strong></p><p>Lets use rem and we know it is relative base font size of root element. If we adjust the font size for our root element for different screens using media queries and make all are manipulations in terms of rem, we are all set :). Let see</p><pre>html { font-size: 16px; }</pre><pre>@media (max-width: 480px) {<br> html { font-size: 16px; }<br>}</pre><pre>@media (min-width: 480px) and (max-width: 1200px) {<br> html { font-size: 18px; }<br>}</pre><pre>@media (min-width: 1200px) {<br> html { font-size: 20px }<br>}</pre><pre>@line-height: 1.5rem;</pre><pre>.container {<br> max-width: 32rem;              // 75 chars per line<br> margin: 0 auto;<br> padding: @line-height * 2;</pre><pre>@media (max-width: 480px) {   //smaller screen<br>   padding: @line-height * 0.8<br> }<br>}</pre><pre>h2 {<br> font-size: 2.25rem;<br> line-height: @line-height * 2;<br> margin: 0 0 @line-height * 2 0;<br>}</pre><p>In above example, we set different font size for html tag under different break points and all our computations are rem based. So now the line-heights, margins will differ for different screens based on the base font size our root element html will have. Thus, we achieved responsive layouts.</p><h4>Typography Tips</h4><ul><li><strong>Consider size: </strong>Currently standard min size is 16px, Very thin weights might not display well in small sizes or vice versa.</li><li><strong>Consider contrast: </strong>Contrast between text and background should be strong.</li><li><strong>Consider space: </strong>Give your type some space to breathe.</li><li><strong>Consider Hierarchy: </strong>Use font size, font weight, line heights to establish a hierarchy and use it throughout your application.</li></ul><h4>Design Tips</h4><ul><li>Use professional graphical tools to analyse the designs.</li><li>Match your work with given designs as much as possible.</li><li>Consider bigger picture. Your part must be a fit in bigger picture and shouldn’t look standalone.</li><li>Typography is important and we have talked a lot about it throughout.</li><li>Consult your designer if you think something is wrong in design before making the change.</li><li>Once you are done, take feedback from Designer.</li></ul><p>Here I am ending this blog post. Hope this helps and if you like it, please 👏. Thank you so much 😄</p><p>Keep dreaming! Keep Learning! Keep Sharing. Have a great day ahead.</p><h4>References</h4><ul><li><a href="https://medium.com/@annagrahamhunter/nailing-studs-a80706fc8363#.hn5ed0b3y">Good typography example</a></li><li><a href="https://fonts.google.com/">Google Fonts</a></li><li><a href="https://github.com/yashikagarg13/typography-basics">Github code used in blog</a></li><li><a href="http://www.thrive-creative.co.uk/web-typography/">http://www.thrive-creative.co.uk/web-typography/</a></li><li><a href="https://blog.prototypr.io/typography-for-front-end-developers-46949338220e#.9436twg9a">https://blog.prototypr.io/typography-for-front-end-developers-46949338220e#.9436twg9a</a></li><li><a href="https://www.toptal.com/front-end/front-end-design-principles">https://www.toptal.com/front-end/front-end-design-principles</a></li><li><a href="http://www.degordian.com/education/blog/best-practice-for-responsive-typography/">http://www.degordian.com/education/blog/best-practice-for-responsive-typography/</a></li><li><a href="https://gist.github.com/scabbiaza/0cf0138544b6f5a6226e">https://gist.github.com/scabbiaza/0cf0138544b6f5a6226e</a></li><li><a href="https://www.youtube.com/watch?v=IbfsvI6dh4U">https://www.youtube.com/watch?v=IbfsvI6dh4U</a></li><li><a href="https://zellwk.com/blog/why-vertical-rhythms/">https://zellwk.com/blog/why-vertical-rhythms/</a></li><li><a href="http://graphicdesign.stackexchange.com/questions/199/point-vs-pixel-what-is-the-difference">http://graphicdesign.stackexchange.com/questions/199/point-vs-pixel-what-is-the-difference</a></li><li><a href="http://www.proximasoftware.com/fontexpert/terms/">http://www.proximasoftware.com/fontexpert/terms/</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f7566dea463c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Typography Series (Part — 1) — Terminology every frontend developer should know]]></title>
            <link>https://medium.com/@yashikagarg13/typography-series-part-1-terminology-every-frontend-developer-should-know-ccb0a05c7d9b?source=rss-3573a92bb7ef------2</link>
            <guid isPermaLink="false">https://medium.com/p/ccb0a05c7d9b</guid>
            <category><![CDATA[typography]]></category>
            <category><![CDATA[frontend-development]]></category>
            <category><![CDATA[css]]></category>
            <category><![CDATA[front-end-development]]></category>
            <dc:creator><![CDATA[Yashika Garg]]></dc:creator>
            <pubDate>Fri, 28 Sep 2018 17:03:54 GMT</pubDate>
            <atom:updated>2018-09-28T17:21:21.767Z</atom:updated>
            <content:encoded><![CDATA[<p>Hola folks! This blog series about Typography has two parts</p><ul><li>Importance and basic terminology of typography that every frontend developer should know.</li><li>Major challenges faced while getting typography right. Check it out <a href="https://medium.com/@yashikagarg13/typography-series-part-2-major-challenges-f7566dea463c">here</a>.</li></ul><p>In this blog post, we will be discussing basic typography terminology that every frontend developer should know. So, let’s get started.</p><p>In simple words, Typography is an art and technique of arranging type. When we talk about the presentation of content on a page, like its font size, font family, weight, vertical spaces, the hierarchy of content and so on, we are referring to its typography.</p><h4>Importance</h4><p>If the typography is right, half of your job is done. It’s typography that helps in conveying the feeling, your content is meant for. A right typography connects with the reader, keeps him energised and focused on content, thus the reader ends up reading the content posted.</p><p>Above all, a right typography is the main factor of making your applications/websites look beautiful. If you follow typography rules, each page of your website will look intact and will grab users attention.</p><p>So in short, three major things:</p><ul><li>Conveys a feeling</li><li>Keeps user reading</li><li>Helps building beautiful applications/websites</li></ul><h4>Font and Typeface</h4><p>A typeface is a set of one or more fonts. Each font in a typeface has a specific weight, style, condensation, width, slant, italicisations, ornamentation and designer, foundry and so on.</p><p>Let me explain with an example.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/320/0*1JCnBW_2U7k08DzA.png" /><figcaption>Typeface</figcaption></figure><p>Here Robot is a typeface and <em>Roboto Condensed</em>, <em>Roboto Slab</em> and <em>Roboto</em> <em>Mono</em> are fonts belonging to this typeface.</p><p>If you notice, in <em>Roboto Condensed</em> distance between characters is smaller, it is condensed. For<em> Robot Slab</em> there is a small line attached at the end of the stroke, whereas for <em>Robot Mono</em> each character takes the same space.</p><h4>Categories of Typeface</h4><p>There are different categories of typeface and there are all sorts of myths around when to use what. We will now study five basic categories and it will eventually help us understand which typeface to use when and the reason behind it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/320/0*FU4I7Bfk_Kvc-Usq.png" /></figure><ul><li><strong>Serif: </strong>A small line attached at the end of the stroke of each character know as serif. Each serif typeface will have a distinct style for this mark that makes the family identifiable. It gives a very classic, elegant and confident look to the content.</li><li><strong>Sans Serif: </strong>Sans in French means “without”. So Sans Serif is without serif. It gives a very simple and precise look. As print has higher resolution serif helps distinguish characters and thus enhances readability. For contents in print media, serif font suits best, whereas, sans-serif is best for the web. This is so because screens do not have an as good resolution as print and serif can blur text. For readability of content on the web, sans-serif suits well.</li><li><strong>Display: </strong>Display typeface has a decorative style and hence used for decorative and illustrative use of content and majorly in print media, like, advertisements, banners, flexes and so on.</li><li><strong>Handwriting:</strong> As the name suggests it would be more humanistic depicting a human handwriting. It is usually difficult to read and therefore used in either images or headlines, given the text used is short. It is also called Script or Calligraphy typeface.</li><li><strong>Monospace:</strong> In Monospace typeface, each character takes equal space. It is mainly used in instructions, code blocks or typewriter text.</li></ul><p>To read more about these typefaces and to see more examples, visit <a href="https://fonts.google.com/">Google fonts</a>.</p><h4>What makes up a character</h4><p>A brief introduction to some important typography terminology:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*bZaLvWuTQ7YSJuKT." /></figure><ul><li><strong>Glyph:</strong> A single character in a typeface.</li><li><strong>Baseline:</strong> Invisible line on which all characters sit.</li><li><strong>x-height: </strong>Height of a lower case ‘x’ character.</li><li><strong>Cap Height:</strong> Distance between baseline and top of the uppercase character.</li><li><strong>Ascenders:</strong> Part of lowercase character extend above x-height.</li><li><strong>Ascender height: </strong>Height of ascender.</li><li><strong>Descender: </strong>Part of lowercase character extends below x-height.</li><li><strong>Descender height: </strong>Height of descender.</li></ul><h4>Size Units</h4><ul><li><strong>Pixel: </strong>A dot on your display is called a pixel. So a grid of 10px height and 10px width will have 100 pixels.</li><li><strong>Point: </strong>A point is a physical unit of length.</li></ul><pre>1 point = 1/12 pica<br>1 pica = 1/6 inch</pre><p>So for 72 pixels per inch display, <strong>1 pixel = 1 point</strong></p><ul><li><strong>Em</strong> and <strong>Rem: </strong>These are scalable units and thus helps in creating mobile friendly screens. Em is a base font size of an application.</li></ul><pre>div { font-size: 15px; }<br>1em = 15px<br>2em = 30px<br>0.5em = 7.5px</pre><p>It is relative font-size of current element being styled or if the current element does not specify any particular font-size then it inherits it from parent elements.</p><p>REM, on the other hand, is relative to font-size of root document.</p><pre>html { font-size: 15px; }<br>1rem = 15px;</pre><h4>Characters per line</h4><p>Having the right amount of characters on each line is key to the readability of your text. What if we violate this range:</p><ul><li><strong>Too Wide:</strong> At the beginning of every new line the reader is focused, but this focus gradually wears off over the duration of the line. It may also make it difficult to figure out correct line if its large block of text</li><li><strong>Too Narrow:</strong> The eyes will have to travel back and forth too often, which also breaks readers rhythm</li></ul><p>In order to energise your readers and keep them engaged in reading, it is suggested to follow the range of <strong>60–75 characters per line</strong> including spaces. <br> In web design, we can achieve this by having fixed width containers and using scalable units like em/rem for measuring the width of these containers. So, if we base font size is 15px, then we can have the container of width 40–50 em.</p><p>Here, I wind up the discussion about different terms we use when we talk about typography. In next blog post, we will talk about the major challenges we as frontend developers face in order to get the typography of our websites right. Stay tuned!</p><p>Hope this helps and if you like it, please 👏. Thank you so much 😄</p><p>Keep dreaming! Keep Learning! Keep Sharing. Have a great day ahead.</p><h4>References</h4><ul><li><a href="https://medium.com/@annagrahamhunter/nailing-studs-a80706fc8363#.hn5ed0b3y">Good typography example</a></li><li><a href="https://fonts.google.com/">Google Fonts</a></li><li><a href="https://github.com/yashikagarg13/typography-basics">Github code used in blog</a></li><li><a href="http://www.thrive-creative.co.uk/web-typography/">http://www.thrive-creative.co.uk/web-typography/</a></li><li><a href="https://blog.prototypr.io/typography-for-front-end-developers-46949338220e#.9436twg9a">https://blog.prototypr.io/typography-for-front-end-developers-46949338220e#.9436twg9a</a></li><li><a href="https://www.toptal.com/front-end/front-end-design-principles">https://www.toptal.com/front-end/front-end-design-principles</a></li><li><a href="http://www.degordian.com/education/blog/best-practice-for-responsive-typography/">http://www.degordian.com/education/blog/best-practice-for-responsive-typography/</a></li><li><a href="https://gist.github.com/scabbiaza/0cf0138544b6f5a6226e">https://gist.github.com/scabbiaza/0cf0138544b6f5a6226e</a></li><li><a href="https://www.youtube.com/watch?v=IbfsvI6dh4U">https://www.youtube.com/watch?v=IbfsvI6dh4U</a></li><li><a href="https://zellwk.com/blog/why-vertical-rhythms/">https://zellwk.com/blog/why-vertical-rhythms/</a></li><li><a href="http://graphicdesign.stackexchange.com/questions/199/point-vs-pixel-what-is-the-difference">http://graphicdesign.stackexchange.com/questions/199/point-vs-pixel-what-is-the-difference</a></li><li><a href="http://www.proximasoftware.com/fontexpert/terms/">http://www.proximasoftware.com/fontexpert/terms/</a></li></ul><p><em>Originally published at </em><a href="http://kyg-tech-stack.blogspot.in/2016/12/typography-basics-every-frontend.html"><em>kyg-tech-stack.blogspot.com</em></a><em>.</em></p><p><em>Originally published at </em><a href="http://kyg-tech-stack.blogspot.in/2016/12/typography-basics-every-frontend.html"><em>kyg-tech-stack.blogspot.com</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ccb0a05c7d9b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[React unit testing series (Part 1)]]></title>
            <link>https://medium.com/@yashikagarg13/react-unit-testing-series-part-1-5aed1eecb90c?source=rss-3573a92bb7ef------2</link>
            <guid isPermaLink="false">https://medium.com/p/5aed1eecb90c</guid>
            <category><![CDATA[jest]]></category>
            <category><![CDATA[unit-testing]]></category>
            <category><![CDATA[enzyme]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Yashika Garg]]></dc:creator>
            <pubDate>Sat, 02 Jun 2018 13:13:29 GMT</pubDate>
            <atom:updated>2018-06-02T13:13:29.900Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/560/1*qEAkQgnm0bNcvcuE2V4T-w.png" /></figure><p>This blog is going to have two parts:</p><ul><li>Basics about unit testing, testing frameworks and DOM interaction libraries</li><li>Bursting myths about Jest testing framework. (Yet to come)</li></ul><p>As your web application grows with increasing business requirements, in order to maintain sanity of your front-end code, unit testing is a must. The way we have multiple frameworks in our front-end world, we also multiple testing frameworks and technology. Somebody who is starting with front-end development can easily get confused around all the stuff available. This blog is a small attempt to introduce unit testing in a simpler way. So let’s get started.</p><h3><strong>Testing Techniques</strong></h3><p>There are basically two ways using which we can cover the critical functionality of our front-end code.</p><h4><strong>Unit Testing</strong></h4><p>In unit testing, we basically tests, independent units of behaviour in order to ensure that the unit performs the set of specific tasks it is meant to perform. Unit tests are written from programmer’s aspect. Consider the following analogy: Imagine a building construction site and a building inspector takes a visit. What he will be most concerned about is internal systems of house, like, framing foundation, electrical, plumbing, etc. He focusses on ensuring that each part of house is working correctly and safely.</p><h4><strong>Integration / Functional Testing</strong></h4><p>In functional testing, tests are written to test an independent piece of functionality, to make sure the system is doing what users are expecting it to. Functional tests are written from user’s perspective. Let’s see the same building analogy, we talked about above. Imagine the home owner visits the construction site. His perspective will be entirely different from building inspectors’s perspective. He assumes that internal systems, behave correctly and focusses on what will it be like to live in that house, how the rooms are looking, will it meet family needs and so on.</p><p>I hope it gives a clear idea about testing techniques. The scope of this blog is unit testing. So moving forward, lets talk in detail about unit as well as features and benefits of unit testing.</p><h3><strong>Unit Testing</strong></h3><p>Lets go in depth and try to understand what actually a unit is.</p><h4><strong>Unit</strong></h4><blockquote>A unit test is an automated piece of code that invokes the unit of work being tested, and then checks some assumptions about a single end result of that unit. A unit test is almost always written using a unit testing framework. It can be written easily and runs quickly. It’s trustworthy, readable, and maintainable. It’s consistent in its results as long as production code hasn’t changed. — Roy Osherove’s The Art of Unit Testing book.</blockquote><p>Irrespective of whether we are writing our application in object oriented programming or in functional programming, a single method in class or module can be considered as a testable unit. A unit is assumed to return a value for a given set of inputs, handles failures or even call another function/method/procedure from with in. Lets consider this example:</p><pre>// Person class<br>class Person {<br>    sayHello (name) {<br>        console.log(&quot;Hello&quot; + name + &quot;!&quot;);<br>    }<br>}<br><br>// sayHello test cases<br>// - it should print &quot;Hello JS!” if sayHello is called with “JS” =&gt; PASSED<br>// - it should print “Hello World!” if sayHello is called with no parameters =&gt; FAILED</pre><p>In above example, we can easily consider “sayHello” method as a unit and tests its behaviour individually. Considering we are on same page, lets move further in understanding what are the things we need to consider where we are writing unit tests.</p><h4><strong>Unit Test</strong></h4><p>In order to test different aspects of a unit, we will be required to write various unit tests where each of them focusses on testing one aspect of unit.</p><ul><li>A unit test should be atomic, i.e., it should be independent of other tests.</li><li>Running one test shouldn’t affect the assertions in another.</li><li>Each unit test can be executed in any order and can be executed repeatedly or concurrently.</li></ul><p>This behaviour isn’t hard to achieve if me make sure either our unit tests is not using any shared variable or if they are using it we must be able to reset the value of shared variable before or after the execution of each test.</p><p>Also, a complex unit may have multiple dependencies which it is using in order to complete the task it is mean to do. Consider a react component rendering a form with an input value and a submit button. On submitting the form, an API call is made to save the value of input in database.</p><pre>class Form extends React.Component {<br>    constructor(props) {<br>        super(props);<br>        this.state = {<br>            input: &quot;&quot;,<br>        };<br><br>        this.onChangeHandler = this.onChangeHandler.bind(this);<br>        this.onSubmitHandler = this.onSubmitHandler.bind(this);<br>    }<br>    <br>    onChangeHandler(event) {<br>        this.setState({<br>            input: event.target.value<br>        });<br>    }<br><br>    onSubmitHandler(event) {<br>        Axios.post(&quot;some_url&quot;, {data: this.state.input})<br>        .then(response =&gt; {<br>            AlertsHelper.show({<br>                type: &quot;success&quot;,<br>                message: response.message,<br>            });<br>        })<br>        .catch(error =&gt; {<br>            AlertsHelper.show({<br>                type: &quot;error&quot;,<br>                message: error.message,<br>            });<br>        });<br>    }<br>    render () {<br>        return (<br>            &lt;form onSubmit={this.onSubmitHandler}&gt;<br>                &lt;input type=“text” <br>                       onChange={this.onChangeHandler} /&gt;<br>                &lt;button type=“submit”&gt;Submit&lt;/button&gt;<br>            &lt;/form&gt;<br>        );<br>    }<br>}</pre><p>Now, this submit handler is a unit and its task is to make an API call to save data, alert success message on success and error message on failure. This handler is making use of few dependencies like “Axios” library to make an API call, “AlertsHelper” library to show alert messages. But when we are going to write unit test for this particular unit, we are not supposed to test the behaviour of “Axios” and “AlertsHelper” as these are dependencies used by unit under consideration. It is not scope of this unit test to test whether the POST request made it through or whether “AlertsHelper” were able to show alert message or not. Thus we can mock these dependencies to perform as required and can test different aspects of the unit under consideration.</p><p>We can mock Axios.post to be successful in order to test success case or to be a failure so as to test failure case. We are not concern about what happens in “AlertHelpers.show” but all we care about is that the “onSubmitHandler” should call it with different data based on success and failure case. I hope this gives an idea about how to differentiate dependencies from actual unit and make unit test independent of functionality of those dependencies. Moving ahead, lets talk about unit testing frameworks.</p><h4><strong>Unit Testing Framework</strong></h4><p>Testing framework is an execution environment for tests. When we test the behaviour of our application using a source code call unit test, in order to make application behave same way as it would have done in a browser, it is required to provide an environment to the application for the execution of tests. This environment is established using a testing framework, which is responsible for the mechanism to drive application under tests.</p><p>A testing framework defines the format for expressing expectations, executes tests and generates reports of test results. When we write a unit test for a unit, we make some assertions or expectations on the output of the unit given different conditions. Different frameworks support different assertion libraries as well as reporting tools. Testing framework should be application independent, easy to expand, maintain and perpetuate.</p><p>Let’s explore some of the unit testing frameworks. I am going to talk about majorly two of them — Mocha and Jest.</p><h4><a href="https://mochajs.org/"><strong>Mocha</strong></a></h4><p>It is the most famous and used testing framework in JS world and has a very active community support. As they says, it is <em>“simple, flexible and fun”</em>. It can run tests for both browser as well node applications. It gives user various options in terms of choosing assertion library, mocking library, spies and so on. This gives the user power to create a package based on one’s needs and customise it on the go. When writing tests for React component, you will also need to setup virtual DOM using JSDOM library. <a href="http://blog.ricardofilipe.com/post/react-enzyme-tdd-tutorial">Here</a> is a great article that helps you set up mocha for a React application.</p><h4><a href="https://facebook.github.io/jest/">Jest</a></h4><p>It’s the recommended way of testing React applications developed by <a href="http://www.facebook.com">Facebook</a>, painless javascript testing framework. It is based on famous <a href="https://jasmine.github.io/">Jasmine</a> testing framework which has its own setup for assertions, spies, stubs and mocks. Jest doesn’t give options to select assertion or mock library of your choice but it comes with everything pre-setup. This has its own pros and cons. If you want flexibility and choice in your setup, definitely Jest is not the one but if you need quick setup to get started with writing your tests, Jest is definitely a good option. Moreover, it sets up fake DOM for you too, thus no need for JSDOM library. You can setup Jest using <a href="https://facebook.github.io/jest/docs/en/getting-started.html">Getting Started</a> guide from Facebook.</p><p>The major change that came with Jest and the one I super liked was it offered auto mocking of all the dependencies of your unit. If you remember we talked few minutes before about unit’s unit test should test unit’s behaviour independent of its dependencies. Now if everything is mocked all ready, we don’t expect dependencies to run their logic when we are testing the unit and thus we achieve correct behaviour testing of the unit.</p><p>Lets understand it with an example:</p><pre>import {add} from &quot;helpers&quot;<br><br>const calculator = (a, b, op) =&gt; {<br>  if (op == &quot;+&quot;) <br>    return add (a, b);<br>}<br><br>/*****************************************************************<br>When we unit test calculator, we are concerned with whether<br>calculator calls add helper or not if `op` is `+` and not with<br>whether calculator returns sum of `a` and `b` if op is `+`<br><br>When we test this with Jest, `add` from helpers is automatically<br>mocked to return undefined and we shouldn&#39;t be concerned about it.<br>And as it is auto mocked, we can easliy spy on it as below<br>*******************************************************************/<br><br>describe(&quot;Calculator&quot;, () =&gt; {<br>  it(&quot;should call &#39;add&#39; helper, if op is &#39;+&#39;&quot;, () =&gt; {<br>    // Given<br>    let a = 10;<br>    let b = 2;<br>    let op = &quot;+&quot;;<br>    <br>    // When<br>    const result = calculator(a, b, op);<br>    <br>    // Then<br>    expect(add).toBeCalledWith(a, b);<br>  })<br>});</pre><h3>DOM Interaction Libraries</h3><p>Moving ahead, lets talk a bit about DOM interaction Libraries. As the name suggests, DOM interaction libraries helps to access components with given class name, tag name or type. They make it really easy to test React components by simplifying manipulation, traversal or access of elements from React Component’s output.</p><p>Facebook provides <a href="https://reactjs.org/docs/test-utils.html">ReactTestUtils</a><em> </em>library for DOM interactions. Another great library is <a href="http://airbnb.io/enzyme/">Enzyme</a> by Airbnb, which basically use jQuery API for DOM manipulation and traversal. These libraries are independent of test runners, which means we can use any combination — <em>Jest + Enzyme / Jest + TestUtils / Mocha + Enzyme / Mocha + TestUtils</em>. Both libraries support Shallow rendering and Full DOM rendering.</p><p>Following is a simple React Component and its test using Jest and Enzyme:</p><pre>export const Form = ({name, age, onSubmit, onUpdate}) =&gt; {<br>  return (<br>    &lt;form onSubmit={onSubmit}&gt;<br>      &lt;div className=&quot;form-group&quot;&gt;<br>        &lt;label className=&quot;control-label&quot;&gt;Name&lt;/label&gt;<br>        &lt;div className=&quot;controls&quot;&gt;<br>          &lt;input type=&quot;text&quot; <br>                 className=&quot;input-md form-control&quot; <br>                 id=&quot;name&quot; <br>                 name=&quot;name&quot;<br>                 value={name} <br>                 onChange={onUpdate.bind(null, &quot;name&quot;)}/&gt;<br>        &lt;/div&gt;<br>      &lt;/div&gt;<br>      &lt;div className=&quot;form-group&quot;&gt;<br>        &lt;label className=&quot;control-label&quot;&gt;Age&lt;/label&gt;<br>        &lt;div className=&quot;controls&quot;&gt;<br>          &lt;input type=&quot;number&quot; <br>                 className=&quot;input-md form-control&quot; <br>                 id=&quot;age&quot; <br>                 name=&quot;age&quot;<br>                 onChange={onUpdate.bind(null, &quot;age&quot;)}/&gt;<br>        &lt;/div&gt;<br>      &lt;/div&gt;<br>      &lt;div className=&quot;controls clearfix&quot;&gt;<br>        &lt;button type=&quot;submit&quot; <br>                className=&quot;btn btn-primary btn-md btn-block&quot;&gt;<br>          Submit<br>        &lt;/button&gt;<br>      &lt;/div&gt;<br>    &lt;/form&gt;<br>  );<br>}</pre><pre>// Form.test.js<br>describe(&#39;PageForm&#39;, () =&gt; {<br>  let form;<br>  const props = {<br>    name: &#39;Xyz&#39;,<br>    age: 20,<br>    onSubmit: jest.fn(),<br>    onUpdate: jest.fn()<br>  }<br><br>  beforeEach(() =&gt; {<br>    form = shallow(&lt;Form {...props} /&gt;);<br>  });<br><br>  it(&#39;should exists&#39;, () =&gt; {<br>    // Given<br><br>    // When<br><br>    // Then<br>    expect(form.length).toEqual(1);<br>  });<br><br>  it(&#39;should render name input&#39;, () =&gt; {<br>    // Given<br><br>    // When<br>    let nameInput = form.find(&#39;input[id=&quot;name&quot;]&#39;);<br><br>    // Then<br>    expect(nameInput.length).toEqual(1);<br>  });<br><br>  it(&#39;should render age input&#39;, () =&gt; {<br>    // Given<br><br>    // When<br>    let ageInput = form.find(&#39;input[id=&quot;age&quot;]&#39;);<br><br>    // Then<br>    expect(ageInput.length).toEqual(1);<br>  });<br><br>  it(&#39;should render submit button&#39;, () =&gt; {<br>    // Given<br><br>    // When<br>    let submitBtn = form.find(&#39;button[type=&quot;submit&quot;]&#39;);<br><br>    // Then<br>    expect(submitBtn.length).toEqual(1);<br>  });<br><br>  it(&#39;should call &quot;onUpdate&quot; on change of input&#39;, () =&gt; {<br>    // Given<br><br>    // When<br>    let nameInput = form.find(&#39;input[id=&quot;name&quot;]&#39;);<br>    nameInput.simulate(&#39;change&#39;);<br><br>    // Then<br>    expect(props.onUpdate).toHaveBeenCalledWith(&#39;name&#39;);<br>  });<br><br>  it(&#39;should call &quot;onSubmit&quot; on form submit&#39;, () =&gt; {<br>    // Given<br><br>    // When<br>    let submitBtn = form.find(&#39;form&#39;);<br>    submitBtn.simulate(&#39;submit&#39;);<br><br>    // Then<br>    expect(props.onSubmit).toHaveBeenCalled();<br>  });<br>});</pre><h3>Shallow Rendering</h3><p>Shallow rendering is a concept of rendering component only “one-level” deep which means you do not have access to child component’s behaviour. You make assertions on what render method returns. Child components are not initiated or rendered. Also, it doesn’t need a DOM which makes test quicker to load and run.</p><p>Here is a simple Page Component which renders Form and Output components as it’s children.</p><pre>import React from &quot;react&quot;;</pre><pre>import { Form } from &#39;./Form&#39;;<br>import { Output } from &#39;./Output&#39;;</pre><pre>export class Page extends React.Component {<br>  constructor(props) {<br>    super(props);</pre><pre>  this.state = {<br>      name: &#39;&#39;,<br>      age: undefined,<br>      output: &#39;&#39;,<br>    };<br>  }</pre><pre>  onSubmit(event) {<br>    event.preventDefault();<br>    const {name, age} = this.state;<br>    const output = `My name is ${name}. I am ${age} years old.`;<br>    this.setState({output});<br>  }</pre><pre>  onUpdate(key, event) {<br>    const state = this.state;<br>    state[key] = event.target.value;<br>    this.setState(state);<br>  }</pre><pre>  render() {<br>    const {name, age, output} = this.state;</pre><pre>    return (<br>      &lt;div&gt;<br>        &lt;Form name={name}<br>              age={age}<br>              onSubmit={this.onSubmit.bind(this)}<br>              onUpdate={this.onUpdate.bind(this)} /&gt;<br>        &lt;Output output={output} /&gt;<br>      &lt;/div&gt;<br>    );<br>  }<br>}</pre><p>When we are writing unit tests for above Page component, we are only considered to test whether this component is rendering Form and Output components or not and the concept of shallow rendering allows us to test this real quick. Below is code for Page.test.js where I am using <a href="http://airbnb.io/enzyme/docs/api/shallow.html">Shallow Rendering API</a> of enzyme.</p><pre>import { shallow } from &#39;enzyme&#39;;</pre><pre>import { Page } from &#39;../../src/components/Page&#39;;<br>import { Form } from &#39;../../src/components/Form&#39;;<br>import { Output } from &#39;../../src/components/Output&#39;;</pre><pre>describe(&#39;Page&#39;, () =&gt; {<br>  let page;</pre><pre>  beforeEach(() =&gt; {<br>    page = shallow(&lt;Page /&gt;);<br>  });</pre><pre>  it(&#39;should exists&#39;, () =&gt; {<br>    // Given</pre><pre>    // When</pre><pre>    // Then<br>    expect(page.length).toEqual(1);<br>  });</pre><pre>  it(&#39;should render Form component&#39;, () =&gt; {<br>    // Given</pre><pre>    // When<br>    let formCmp = page.find(Form);</pre><pre>    // Then<br>    expect(formCmp.length).toEqual(1);<br>  });</pre><pre>  it(&#39;should render Output component&#39;, () =&gt; {<br>    // Given</pre><pre>    // When<br>    let outputCmp = page.find(Output);</pre><pre>    // Then<br>    expect(outputCmp.length).toEqual(1);<br>  });</pre><p>This is where I am ending this post and soon I will be writing Part2 of the series where I will be bursting different myths about Jest unit testing framework.</p><p>Hope this helps and if you like it please 👏. Thank you so much 😄</p><p>Keep dreaming! Keep Learning! Keep Sharing. Have a great day ahead.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5aed1eecb90c" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>