<?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>Forem: Achilles Moraites</title>
    <description>The latest articles on Forem by Achilles Moraites (@achimoraites).</description>
    <link>https://forem.com/achimoraites</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%2F255338%2Fd1f1b8ad-0d65-4070-99c7-86406cba9643.png</url>
      <title>Forem: Achilles Moraites</title>
      <link>https://forem.com/achimoraites</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/achimoraites"/>
    <language>en</language>
    <item>
      <title>Github actions tweaks</title>
      <dc:creator>Achilles Moraites</dc:creator>
      <pubDate>Thu, 10 Jun 2021 19:54:41 +0000</pubDate>
      <link>https://forem.com/achimoraites/github-actions-tweaks-23pa</link>
      <guid>https://forem.com/achimoraites/github-actions-tweaks-23pa</guid>
      <description>&lt;p&gt;Here there are some cool tricks you can do to improve your Github Actions.&lt;/p&gt;

&lt;h2&gt;
  
  
  Caching your packages
&lt;/h2&gt;

&lt;p&gt;How many times you have re-installed all of your packages in your actions even when no packages were changed?&lt;br&gt;
This is not only time consuming but it can actually cost you more money as &lt;strong&gt;Github actions&lt;/strong&gt; are charged based on the time they actually run;&lt;br&gt;
you can &lt;a href="https://github.com/pricing" rel="noopener noreferrer"&gt;have a look here&lt;/a&gt; in case you are interested 😉&lt;/p&gt;
&lt;h3&gt;
  
  
  How
&lt;/h3&gt;

&lt;p&gt;To cache our modules we will use the &lt;a href="https://github.com/actions/cache" rel="noopener noreferrer"&gt;&lt;code&gt;actions/cache@v2&lt;/code&gt;&lt;/a&gt; action&lt;/p&gt;

&lt;p&gt;Here is an example of caching npm dependencies for Linux/MacOS&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Cache node_modules 📦&lt;/span&gt;
  &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;actions/cache@v2&lt;/span&gt;
  &lt;span class="na"&gt;with&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;~/.npm&lt;/span&gt;
    &lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}&lt;/span&gt;
    &lt;span class="na"&gt;restore-keys&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;|&lt;/span&gt;
      &lt;span class="s"&gt;${{ runner.os }}-node-&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;a href="https://github.com/achimoraites/achimoraites.github.io/blob/master/.github/workflows/deploy-gh.yaml#L24-L31" rel="noopener noreferrer"&gt;Here you can see it in a Github Action used for deploying my svelte-kit powered blog to gh-pages&lt;/a&gt;&lt;/strong&gt; 🚀&lt;/p&gt;

&lt;h2&gt;
  
  
  Create strings based on env variables
&lt;/h2&gt;

&lt;p&gt;There are times where you need to be able to create a string based on env variables.&lt;br&gt;
The most common case is when you want to create a path using one or more env variables;&lt;/p&gt;
&lt;h3&gt;
  
  
  How
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;paths-printer 🦄&lt;/span&gt;

&lt;span class="na"&gt;env&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;BASE_URL&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;https://example.com/"&lt;/span&gt;
&lt;span class="na"&gt;jobs&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;print-stuff&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;runs-on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;ubuntu-latest&lt;/span&gt;
    &lt;span class="na"&gt;steps&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Create paths from env variables&lt;/span&gt;
        &lt;span class="c1"&gt;# this outputs "IMAGES_PATH: https://example.com/assets/img/"&lt;/span&gt;
        &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;  &lt;span class="s"&gt;echo  "IMAGES_PATH&lt;/span&gt;&lt;span class="err"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${env.IMAGES_PATH}"&lt;/span&gt;
        &lt;span class="na"&gt;env&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
          &lt;span class="na"&gt;IMAGES_PATH&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;${{&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;env.BASE_URL&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;}}assets/img/"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h2&gt;
  
  
  Use a custom npm config
&lt;/h2&gt;

&lt;p&gt;This is very handly when you are working with private packages 📦&lt;br&gt;
The idea is to store your PAT TOKEN for accessing your packages in your repository secrets and then use it on a custom npm config file.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;📝Note that we can just use the default &lt;code&gt;.npmrc&lt;/code&gt; but it is a good idea to keep the npm config for your github actions separated.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3&gt;
  
  
  How
&lt;/h3&gt;

&lt;p&gt;In order for it to work we need to:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;an npm config file 🦄

&lt;ul&gt;
&lt;li&gt;includes the configuration for our private github packages&lt;/li&gt;
&lt;li&gt;we will take the PAT TOKEN from an env variable&lt;/li&gt;
&lt;li&gt;we can name it anything that we want&lt;/li&gt;
&lt;li&gt;place it in the project root folder&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;set our action to use our npm config file ✨&lt;/li&gt;
&lt;li&gt;create a PAT TOKEN&lt;/li&gt;
&lt;li&gt;place the PAT TOKEN in our repository secrets&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Lets have a look at a case where we are using private github packages&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Create the npm config file&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Create a file named &lt;code&gt;.ci.npmrc&lt;/code&gt; in the root folder of your project and place the following contents:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@your-scope:registry=https://npm.pkg.github.com
//npm.pkg.github.com/:_authToken=${NODE_AUTH_TOKEN}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;💡 Remember to replace "@your-scope" with your scope&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Set the action to use our custom npm config file&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Install dependencies ✨&lt;/span&gt;
  &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;npm ci&lt;/span&gt;
  &lt;span class="na"&gt;env&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;NPM_CONFIG_USERCONFIG&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;.ci.npmrc&lt;/span&gt;
    &lt;span class="na"&gt;NODE_AUTH_TOKEN&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${{ secrets.PAT_TOKEN }}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Create your PAT TOKEN
Follow the &lt;a href="https://docs.github.com/en/github/authenticating-to-github/keeping-your-account-and-data-secure/creating-a-personal-access-token" rel="noopener noreferrer"&gt;instructions here&lt;/a&gt;
When you are on the step 7 permissions and scopes:&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;make sure to select the "write:packages" option!&lt;/li&gt;
&lt;li&gt;the "read:packages" is enabled as well
&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F080dgwlsui9wnrke1zec.jpg" alt="Github token scopes" width="800" height="376"&gt;
&lt;/li&gt;
&lt;li&gt;Continue to the next steps and keep your PAT TOKEN ready!&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Place the PAT TOKEN to your repo secrets&lt;/strong&gt;
Go to your repository on github:

&lt;ol&gt;
&lt;li&gt;Click on "⚙️settings" tab&lt;/li&gt;
&lt;li&gt;Select "Secrets"&lt;/li&gt;
&lt;li&gt;Click on "New repository secret"&lt;/li&gt;
&lt;li&gt;Set the "name" to be "&lt;strong&gt;PAT_TOKEN&lt;/strong&gt;"&lt;/li&gt;
&lt;li&gt;Paste your token in the "value"&lt;/li&gt;
&lt;li&gt;Select "Add secret"&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;

&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7bbz3gttu5ulziao31si.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7bbz3gttu5ulziao31si.jpg" alt="Github add secret" width="800" height="391"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Summary 👨🏻‍💻
&lt;/h3&gt;

&lt;p&gt;We have explored ways to improve the performance of our github actions using caching.&lt;/p&gt;

&lt;p&gt;Then we explored how we can create strings dynamically from env variables and finnaly we had a look in configuring our npm config for using private github packages in a secure and scalable way 😉.&lt;/p&gt;

&lt;p&gt;Happy coding 😄 !!!&lt;/p&gt;

&lt;p&gt;(&lt;em&gt;The original post was published &lt;a href="https://achimoraites.github.io/blog/github-actions-tips-and-tricks/" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/em&gt;)&lt;/p&gt;

</description>
      <category>github</category>
      <category>productivity</category>
      <category>devops</category>
      <category>npm</category>
    </item>
    <item>
      <title>Optimizing moment-timezone using webpack</title>
      <dc:creator>Achilles Moraites</dc:creator>
      <pubDate>Sun, 20 Sep 2020 09:21:36 +0000</pubDate>
      <link>https://forem.com/achimoraites/optimizing-moment-timezone-using-webpack-13m6</link>
      <guid>https://forem.com/achimoraites/optimizing-moment-timezone-using-webpack-13m6</guid>
      <description>&lt;p&gt;&lt;a href="https://momentjs.com/timezone/" rel="noopener noreferrer"&gt;moment-timezone&lt;/a&gt; is an awesome tool to have when working with different time-zones.&lt;/p&gt;

&lt;p&gt;While the tool is great it also has lots of data that we might never use, bloating the size of our applications.&lt;/p&gt;

&lt;p&gt;We can do better!&lt;/p&gt;

&lt;h2&gt;
  
  
  But first, some facts
&lt;/h2&gt;

&lt;p&gt;To optimize an application we need to know some things about it.&lt;/p&gt;

&lt;p&gt;Imagine that we have an application that is providing a search functionality for scientific articles.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Each article has information about it's creation and last modification dates.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The oldest article is from 1990.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Our application is being used by universities in Europe and Australia only.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  So we need the following timezone data
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;time-zones from Europe and Australia only&lt;/li&gt;
&lt;li&gt;year range from 1990 - 2020 (present year)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Optimizing based on our needs
&lt;/h2&gt;

&lt;p&gt;Now that we know what we need, we can start the optimization.&lt;/p&gt;

&lt;p&gt;We will use a webpack plugin called &lt;a href="https://github.com/gilmoreorless/moment-timezone-data-webpack-plugin" rel="noopener noreferrer"&gt;moment-timezone-data-webpack-plugin&lt;/a&gt; to keep only the data we need from our application.&lt;/p&gt;

&lt;h4&gt;
  
  
  Install the plugin
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;npm i -D moment-timezone-data-webpack-plugin&lt;/code&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Adding the plugin to webpack
&lt;/h4&gt;

&lt;p&gt;For simplicity I will only show the plugin related configuration in the webpack file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;MomentTimezoneDataPlugin&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;moment-timezone-data-webpack-plugin&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;plugins&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;MomentTimezoneDataPlugin&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
        &lt;span class="na"&gt;matchZones&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sr"&gt;/^Europe/&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/^Australia/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
        &lt;span class="na"&gt;startYear&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1990&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;endYear&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2020&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;}),&lt;/span&gt;
  &lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that's it :)&lt;/p&gt;

&lt;p&gt;Now our application will have only the timezone data that we actually need!&lt;/p&gt;

&lt;p&gt;Happy coding! :)&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>webpack</category>
    </item>
    <item>
      <title>Design Patterns - Basic Iterator</title>
      <dc:creator>Achilles Moraites</dc:creator>
      <pubDate>Fri, 22 May 2020 17:54:32 +0000</pubDate>
      <link>https://forem.com/achimoraites/design-patterns-basic-iterator-104c</link>
      <guid>https://forem.com/achimoraites/design-patterns-basic-iterator-104c</guid>
      <description>&lt;p&gt;Among the most common things in a collection of items is the ability to iterate through the collection.&lt;/p&gt;

&lt;p&gt;In Java for example there are data structures that are allowing us to create collections for primitive data or our custom classes.&lt;/p&gt;

&lt;p&gt;Here are some : &lt;code&gt;ArrayList&amp;lt;T&amp;gt;&lt;/code&gt;, &lt;code&gt;LinkedList&amp;lt;T&amp;gt;&lt;/code&gt; , &lt;code&gt;HashSet&amp;lt;T&amp;gt;&lt;/code&gt; and others... &lt;/p&gt;

&lt;p&gt;The above have something in common , all are implementing the Interfaces : &lt;strong&gt;Collection&lt;/strong&gt; and &lt;strong&gt;Iterable&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This can give us the abillity to make our own classes iterable :)&lt;/p&gt;

&lt;p&gt;In this article we will explore how to do it.&lt;/p&gt;

&lt;h2&gt;
  
  
  How it works
&lt;/h2&gt;

&lt;p&gt;Below there is a simplified UML diagram illustrating the ArrayList :&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fib8qz3kjprrp5pzsh25f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fib8qz3kjprrp5pzsh25f.png" alt="Alt Text" width="400" height="273"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above relationships are allowing us to use this magic:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nl"&gt;s:&lt;/span&gt; &lt;span class="n"&gt;stringList&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
     &lt;span class="c1"&gt;// do something with s&lt;/span&gt;
      &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;print&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above is a convenient way to write a foreach loop in Java, but the language it self translates it to:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;Iterator&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;si&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;stringList&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;iterator&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

&lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;si&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;hasNext&lt;/span&gt;&lt;span class="o"&gt;()){&lt;/span&gt;
   &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;si&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getNext&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
   &lt;span class="c1"&gt;// do something with s&lt;/span&gt;
    &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;print&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Now armed with this knowledge we can go beyond the default behaviors of the build in collections and can customize it for our own needs!&lt;/p&gt;

&lt;p&gt;Lets play with a project!&lt;/p&gt;

&lt;h2&gt;
  
  
  Project : CarPartsStore
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://github.com/achimoraites/DesignPatterns/tree/master/IteratorPatternExamples/apps" rel="noopener noreferrer"&gt;get the source code here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here we will illustrate the customized behavior by exploring a Store that sells car parts, in this code-base we have the Warehouse class that contains a List of CarPart .&lt;/p&gt;

&lt;p&gt;What we do here: We will use the basic Iterator Pattern to iterate through our Warehouse class for CarPart .&lt;/p&gt;

&lt;p&gt;We will make something like the following to work:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;CarPart&lt;/span&gt; &lt;span class="nl"&gt;part:&lt;/span&gt; &lt;span class="nc"&gt;Warehouse&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
  &lt;span class="c1"&gt;// do interesting stuff ...&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;by default the above is not working as our warehouse has a field of ArrayList , but having the new awesome Iterator powers we can can do it !&lt;/p&gt;

&lt;p&gt;in our Warehouse class we have:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Warehouse&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;Iterable&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;CarPart&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;CarPart&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;carPartList&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;location&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In our Warehouse class we add the implements Iterable :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Warehouse&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;Iterable&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;CarPart&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;notice that we used the &lt;strong&gt;CarPart&lt;/strong&gt; type , as we want to iterate through a collection of CarPart.&lt;/p&gt;

&lt;p&gt;also we will implement the following in our Warehouse class :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Override&lt;/span&gt; 
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;Iterator&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;CarPart&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;iterator&lt;/span&gt;&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="n"&gt;carPartList&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;iterator&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now when we try to iterate through our warehouse we iterate through the carPartList !&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Now this magic is available :)&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;CarPart&lt;/span&gt; &lt;span class="nl"&gt;part:&lt;/span&gt; &lt;span class="nc"&gt;Warehouse&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
  &lt;span class="c1"&gt;// do interesting stuff ...&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Congratulations ! now you have learned how to take iteration to your hands!&lt;/p&gt;

&lt;p&gt;Feel free to experiment with the code!!!&lt;/p&gt;

&lt;p&gt;Happy coding!!!&lt;/p&gt;

&lt;p&gt;the original article was posted &lt;a href="https://achimoraites.blogspot.com/2017/11/design-patterns-basic-iterator.html" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>oop</category>
      <category>productivity</category>
      <category>codequality</category>
    </item>
    <item>
      <title>Types of Modules in Angular</title>
      <dc:creator>Achilles Moraites</dc:creator>
      <pubDate>Sun, 10 May 2020 13:23:12 +0000</pubDate>
      <link>https://forem.com/achimoraites/types-of-modules-in-angular-5cpb</link>
      <guid>https://forem.com/achimoraites/types-of-modules-in-angular-5cpb</guid>
      <description>&lt;p&gt;Crafting high quality Angular apps requires to have knowledge of how to use the different types of modules to ensure readability, performance and scalability.&lt;/p&gt;

&lt;p&gt;By setting up your project to use each module type properly you will have better control over your project to add more advanced features such as Lazy Loading and a more organized structure to work with. &lt;/p&gt;

&lt;p&gt;We have 3 types of modules:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Feature modules&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Core Module&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Shared Module&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Feature Modules
&lt;/h2&gt;

&lt;p&gt;those are the modules that encapsulate a specific feature at a logic level, for example you have a dashboard page that allows the users to see their projects.&lt;/p&gt;

&lt;p&gt;The dashboard module will have everything that is needed to allow a user to see their projects:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;components&lt;/li&gt;
&lt;li&gt;services&lt;/li&gt;
&lt;li&gt;pipes&lt;/li&gt;
&lt;li&gt;directives&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In general feature specific functionality is included in the module.&lt;/p&gt;

&lt;p&gt;If we need to use some common functionality in our Feature Modules we import the Shared Module in the Modules that needs it.&lt;/p&gt;

&lt;p&gt;We will talk more about shared functionality later.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// feature module example&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;NgModule&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@angular/core&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;SharedModule&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;../shared/SharedModule&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;DashboardComponent&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./dashboard/dashboard.component&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;ProjectComponent&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./project/project.component&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;NgModule&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;imports&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="nx"&gt;NgModule&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;SharedModule&lt;/span&gt;
  &lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="na"&gt;declarations&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="nx"&gt;DashboardComponent&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;ProjectComponent&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;DashboardModule&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Core Module
&lt;/h2&gt;

&lt;p&gt;Here we include functionality that will be used only ONCE!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Core module is used ONLY in the root (app) Module!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Common &lt;strong&gt;services are placed in the Core Module&lt;/strong&gt; to ensure we have only a single instance of the services to avoid unexpected behaviors.&lt;/p&gt;

&lt;p&gt;In this type of module we also place components that are used only ONCE for example the NavBar and the Footer components.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// core module example&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;NgModule&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Optional&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;SkipSelf&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@angular/core&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;ApiService&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./services/api.service&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;NgModule&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;providers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="nx"&gt;ApiService&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;CoreModule&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="c1"&gt;// do not allow to be used more than ONCE!!!&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(@&lt;/span&gt;&lt;span class="nd"&gt;Optional&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;SkipSelf&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="nx"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;CoreModule&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Import CoreModule in the root module only!!!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
      &lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Shared Module
&lt;/h2&gt;

&lt;p&gt;This is the most missunderstooded kind of Module!&lt;/p&gt;

&lt;p&gt;The purpose of the SharedModule is to make available commonly used:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;components&lt;/li&gt;
&lt;li&gt;directives&lt;/li&gt;
&lt;li&gt;pipes&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We use the SharedModule in the feature modules to make common functionality available.&lt;/p&gt;

&lt;p&gt;We also make sure to have only one Shared Module.&lt;/p&gt;

&lt;p&gt;We avoid placing services here!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// shared module example&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;CommonModule&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@angular/common&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;NgModule&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@angular/core&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;CustomerComponent&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./components/customer/customer.component&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;PercentagePipe&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./pipes/percentage/percentange.pipe&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;CustomerStyleDirective&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./directives/customer-style/customer-style.directive&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;NgModule&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;imports&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="nx"&gt;CommonModule&lt;/span&gt; &lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="na"&gt;exports&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="nx"&gt;CommonModule&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;CustomerComponent&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;PercentagePipe&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;CustomerStyleDirective&lt;/span&gt; 
  &lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="na"&gt;declarations&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="nx"&gt;CustomerComponent&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;CustomerStyleDirective&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;PercentagePipe&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SharedModule&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This was a brief introduction to the 3 most common Modules types used in Angular Applications.&lt;/p&gt;

&lt;p&gt;Happy coding :) &lt;/p&gt;

</description>
      <category>angular</category>
      <category>typescript</category>
      <category>codequality</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Advanced Angular Testing using Jasmine</title>
      <dc:creator>Achilles Moraites</dc:creator>
      <pubDate>Sat, 21 Mar 2020 09:18:07 +0000</pubDate>
      <link>https://forem.com/achimoraites/advanced-angular-testing-using-jasmine-3ooo</link>
      <guid>https://forem.com/achimoraites/advanced-angular-testing-using-jasmine-3ooo</guid>
      <description>&lt;p&gt;When testing our apps there are times when we need to control things that are beyond our control, like the window object.&lt;br&gt;
One common scenario is when we need to test our code against browser specific APIs.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you are looking how to upgrade your testing skills this is the article you have been looking for!&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2&gt;
  
  
  Show me the code
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// history.component.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Component&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;OnInit&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@angular/core&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Component&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;selector&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;app-history&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;templateUrl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./history.component.html&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;styleUrls&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./history.component.css&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;HistoryComponent&lt;/span&gt; &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nx"&gt;OnInit&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;ngOnInit&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;goBack&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;history&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;length 1&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;history&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;back&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;saveFile&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;blob&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Blob&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;text/html&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="c1"&gt;// IE&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;navigator&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;navigator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;msSaveOrOpenBlob&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;navigator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;msSaveOrOpenBlob&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;blob&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;file.txt&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;custom handling&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  Now lets test the &lt;code&gt;goBack()&lt;/code&gt; method
&lt;/h3&gt;

&lt;p&gt;As you know already the &lt;code&gt;window.history&lt;/code&gt; is read only.&lt;br&gt;
We need to test two cases:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;history.length == 1&lt;/li&gt;
&lt;li&gt;history.length &amp;gt; 1&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;in our history.component.spec.ts&lt;br&gt;
we use the &lt;code&gt;spyOnProperty&lt;/code&gt; to mock the &lt;code&gt;window.history.length&lt;/code&gt; to be able to test our both cases:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;  &lt;span class="nf"&gt;it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;should execute "goBack" as expected when history === 1&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// spy on console.log()&lt;/span&gt;
    &lt;span class="nf"&gt;spyOn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;log&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="c1"&gt;// here we mock the history length to be 1&lt;/span&gt;
    &lt;span class="nf"&gt;spyOnProperty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;history&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;length&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;get&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;and&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;returnValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;component&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;goBack&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toHaveBeenCalledWith&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;length 1&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="nf"&gt;it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;should execute "goBack" as expected when history &amp;gt; 1&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// spy on window.history.back()&lt;/span&gt;
    &lt;span class="nf"&gt;spyOn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;history&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;back&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="c1"&gt;// here we mock the history length to be 2&lt;/span&gt;
    &lt;span class="nf"&gt;spyOnProperty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;history&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;length&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;get&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;and&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;returnValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;component&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;goBack&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;history&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;back&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toHaveBeenCalled&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;that was easy :)&lt;/p&gt;

&lt;p&gt;Now lets tackle a more interesting case, what about testing a browser specific api?&lt;/p&gt;

&lt;h3&gt;
  
  
  Testing browser specific APIs
&lt;/h3&gt;

&lt;p&gt;Now in the &lt;code&gt;saveFile()&lt;/code&gt; method we are using a browser specific API, here things are getting more interesting.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;window.navigator.msSaveOrOpenBlob&lt;/code&gt; is available only on IE,&lt;br&gt;
on other supported browsers we have a different implementation.&lt;/p&gt;

&lt;p&gt;Let's dive to our test code!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt; &lt;span class="nf"&gt;it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;should execute "saveFile" as expected on IE&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// create a mock navigator&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;mockNavigator&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;jasmine&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createSpyObj&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;msSaveOrOpenBlob&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
    &lt;span class="c1"&gt;// here we use the mockNavigator to simulate IE&lt;/span&gt;
    &lt;span class="nf"&gt;spyOnProperty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;navigator&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;get&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;and&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;returnValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;mockNavigator&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;component&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;saveFile&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="c1"&gt;// verify that method has been called :)&lt;/span&gt;
    &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;mockNavigator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;msSaveOrOpenBlob&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toHaveBeenCalled&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="nf"&gt;it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;should execute "saveFile" as expected on browsers other than IE&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// spy on console.log()&lt;/span&gt;
    &lt;span class="nf"&gt;spyOn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;log&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="c1"&gt;// create a mock navigator&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;mockNavigator&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;jasmine&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createSpyObj&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
    &lt;span class="c1"&gt;// here we use the mockNavigator to simulate behavior&lt;/span&gt;
    &lt;span class="nf"&gt;spyOnProperty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;navigator&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;get&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;and&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;returnValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;mockNavigator&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;component&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;saveFile&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="c1"&gt;// verify that method has been called :)&lt;/span&gt;
    &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toHaveBeenCalledWith&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;custom handling&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Here we mocked the &lt;code&gt;window.navigator&lt;/code&gt; to be able to simulate the behavior on both cases!&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;Today we learned about mocking the window object to be able to do tests against browser specific APIs.&lt;br&gt;
By using this technique you will be able to mock anything you need to test your code.&lt;/p&gt;

&lt;p&gt;I hope you enjoyed it,&lt;br&gt;
Happy coding :)&lt;/p&gt;

</description>
      <category>angular</category>
      <category>javascript</category>
      <category>typescript</category>
      <category>testing</category>
    </item>
    <item>
      <title>Component Inheritance</title>
      <dc:creator>Achilles Moraites</dc:creator>
      <pubDate>Sun, 08 Mar 2020 19:49:17 +0000</pubDate>
      <link>https://forem.com/achimoraites/component-inheritance-5n7</link>
      <guid>https://forem.com/achimoraites/component-inheritance-5n7</guid>
      <description>&lt;h2&gt;
  
  
  About
&lt;/h2&gt;

&lt;p&gt;Component inheritance can be useful when you &lt;br&gt;
have duplicated/shared code between your components.&lt;/p&gt;

&lt;p&gt;Think about having a situation where you have 3 components&lt;br&gt;
that share a &lt;code&gt;theme @Input()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This could be the case where you want to have your components to adapt based on a provided theme.&lt;/p&gt;
&lt;h2&gt;
  
  
  Why
&lt;/h2&gt;

&lt;p&gt;When you have 2-3 components with shared functionality &lt;br&gt;
you might say "well why to bother with inheritance?".&lt;/p&gt;

&lt;p&gt;And in the most common cases that's okay, you don't need to.&lt;/p&gt;

&lt;p&gt;But by using inheritance you do more than just to avoid to type the shared functionality between your components.&lt;/p&gt;

&lt;p&gt;The benefit of inheritance is when we have shared functionality in many components!&lt;/p&gt;
&lt;h2&gt;
  
  
  How
&lt;/h2&gt;

&lt;p&gt;Using component inheritance is simple, you need:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A Base component&lt;/li&gt;
&lt;li&gt;The Component/s to extend the Base component&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  A simple example
&lt;/h2&gt;

&lt;p&gt;Here we will use only one component to extend the Base component but the same process applies to no matter how many components we are going to use.&lt;/p&gt;
&lt;h3&gt;
  
  
  Base component
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// base.component.ts&lt;/span&gt;
&lt;span class="c1"&gt;//....&lt;/span&gt;
&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Component&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
&lt;span class="na"&gt;selector&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;my-base&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="na"&gt;template&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt; &lt;span class="c1"&gt;// notice that the template is empty!&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BaseComponent&lt;/span&gt; &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nx"&gt;OnInit&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Input&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="nx"&gt;theme&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

    &lt;span class="nf"&gt;ngOnInit&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  ThemedInput component
&lt;/h3&gt;

&lt;p&gt;By extending the Base component the ThemedInput has the theme input!&lt;/p&gt;

&lt;p&gt;Anything added / updated in the Base component will be reflected here too!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// themed-input.component.ts&lt;/span&gt;
&lt;span class="c1"&gt;//....&lt;/span&gt;
&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Component&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
&lt;span class="na"&gt;selector&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;my-themed-input&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="na"&gt;templateUrl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./themed-input-component.html&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;span class="na"&gt;styleUrls&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./themed-input-component.scss&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ThemedInputComponent&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;BaseComponent&lt;/span&gt; &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nx"&gt;OnInit&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nf"&gt;ngOnInit&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Changing/Updating shared functionality
&lt;/h3&gt;

&lt;p&gt;Having a base component with the shared functionality you can now &lt;strong&gt;change/update the shared functionality in one place&lt;/strong&gt; instead of doing it in every component that exists or will be created in the future!&lt;/p&gt;

&lt;p&gt;A good scenario would be when you decide to use a service that is common to all the components to share state.&lt;/p&gt;

&lt;p&gt;Imagine that one day you need to switch to NgRx:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Case 1 - You are using inheritance&lt;/strong&gt;: all you need to do is to update the base component to start using it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Case 2 - You are NOT using inheritance&lt;/strong&gt;: oh poor soul... you will need to go to all of the components and you will need to update the code for each one of them!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I believe the first Case is faster, simpler and less error prone ;)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Adding new shared functionality becomes simple!&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Testing shared code
&lt;/h3&gt;

&lt;p&gt;Testing the shared functionality is simpler, all we need to do is to test the base class.&lt;/p&gt;

&lt;p&gt;This way we avoid having to write tests that are actually testing the same shared code across components.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;Using the right tool for the job is important, here we explored using component inheritance to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;simplify the usage of shared component code&lt;/li&gt;
&lt;li&gt;allow for easier future change / update&lt;/li&gt;
&lt;li&gt;optimizing the testing of our shared code&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Inheritance is powerful yet keep in mind that to use it only when it makes sense.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Most of the times composition does the trick!&lt;/p&gt;

&lt;p&gt;Happy coding!!!&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>angular</category>
      <category>oop</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Testable Code 101</title>
      <dc:creator>Achilles Moraites</dc:creator>
      <pubDate>Tue, 04 Feb 2020 21:16:30 +0000</pubDate>
      <link>https://forem.com/achimoraites/testable-code-101-cph</link>
      <guid>https://forem.com/achimoraites/testable-code-101-cph</guid>
      <description>&lt;h2&gt;
  
  
  What
&lt;/h2&gt;

&lt;p&gt;Testable code is code that is easy to test.&lt;br&gt;
Having testable code improves our codebase making it easier to add features, maintain or even scale it.&lt;/p&gt;
&lt;h2&gt;
  
  
  Why?
&lt;/h2&gt;

&lt;p&gt;Testable code is an investment that will benefit your team and project in various ways, it can even make your grumpy project manager happy. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Testable &lt;strong&gt;code is more organized&lt;/strong&gt; and will put us in the process of splitting our code's functionality to small, manageable and reusable components leading to &lt;strong&gt;more robust designs&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Refactoring your code is easier&lt;/strong&gt; and you can verify on the spot if something is wrong thus saving you precious time and effort.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Writing your tests is faster and simpler&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Testable code has the following properties&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Controllability&lt;/strong&gt; : Is the extend of our ability to execute the code under test with the states and parameters we care about.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Observability&lt;/strong&gt;: The behaviors of our code that can be verified. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Isolateability&lt;/strong&gt;: Failures can be isolated from each other.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Automatability&lt;/strong&gt;: How able we are to automate our tests.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  How
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Examples of code being improved&lt;/strong&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Controllability:
&lt;/h3&gt;

&lt;p&gt;Our Function checks is it is Monday: if it is Monday returns true otherwise false.&lt;/p&gt;

&lt;p&gt;This code is not testable : we have no control over the date object.&lt;/p&gt;

&lt;p&gt;The CONTROLABILITY attribute is missing!&lt;/p&gt;

&lt;p&gt;This function will return true once a week and is not reliable for testing!!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;isMonday&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;&lt;span class="nx"&gt;boolean&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;date&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getDay&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we have refactored our code to be more controllable : we now pass a date object.&lt;/p&gt;

&lt;p&gt;We can create reliable test cases for this function because we have control over the date object used in the function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;isMonday&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;date&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;&lt;span class="nx"&gt;boolean&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getDay&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Observabillity:
&lt;/h3&gt;

&lt;p&gt;The setName method is not observable, so we can't verify that the name is assigned.&lt;/p&gt;

&lt;p&gt;We have made the name field public and this is a BAD IDEA to be able to test our method&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Student&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

     &lt;span class="nf"&gt;setName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
         &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
     &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;So in our test suite we have something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt; &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;student&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Student&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;setName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Patrick&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
 &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;student&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;to&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;equal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Patrick&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;we use the name field. There has to be a better way!&lt;/p&gt;

&lt;p&gt;We refactored our code and now we can verify that the name is assigned while our name field remains private!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Student&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

     &lt;span class="nf"&gt;setName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;&lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
         &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
         &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
     &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we can test the method like that:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt; &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;student&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Student&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
 &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;student&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Patrick&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nx"&gt;to&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;equal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Patrick&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Isoletabillity:
&lt;/h3&gt;

&lt;p&gt;Isoletabillity can be achieved by splitting large methods to smaller components enabling us to better trace where a fault is located.&lt;/p&gt;

&lt;p&gt;If you have a method of 80+ lines of code there are various things that are going on in there. &lt;br&gt;
So it's not easy to find where the fault is located.&lt;/p&gt;

&lt;p&gt;If we split the functionality inside the method using helper methods we can better test and understand where things gone bad.&lt;/p&gt;

&lt;p&gt;Note here our enrollStudent method: if something fails in there can be a fault in setting the school functionallity , setName or Both. This method has no isoletabillity!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Student&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

     &lt;span class="nf"&gt;setName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;&lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
         &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
         &lt;span class="c1"&gt;// 20 lines of functionallity&lt;/span&gt;
         &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
     &lt;span class="p"&gt;}&lt;/span&gt;




     &lt;span class="nf"&gt;enrollStudent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;schoolID&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
         &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="c1"&gt;// set the school functionallity&lt;/span&gt;
        &lt;span class="c1"&gt;// lots of things here&lt;/span&gt;
        &lt;span class="c1"&gt;// like 30 lines of code&lt;/span&gt;
     &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Refactoring our code now our method is using helper methods to achieve the same functionality but now we can better test the method and it's helpers to be able to effectively trace the fault.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Student&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

     &lt;span class="nf"&gt;setName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;&lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
         &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
         &lt;span class="c1"&gt;// 20 lines of functionallity&lt;/span&gt;
         &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
     &lt;span class="p"&gt;}&lt;/span&gt;


     &lt;span class="nf"&gt;setSchool&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
         &lt;span class="c1"&gt;// lots of things here&lt;/span&gt;
         &lt;span class="c1"&gt;// 30 lines of code&lt;/span&gt;
     &lt;span class="p"&gt;}&lt;/span&gt;


     &lt;span class="nf"&gt;enrollStudent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;schoolID&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
         &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
         &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setSchool&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;schoolID&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
     &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Automatabillity
&lt;/h3&gt;

&lt;p&gt;The goal is to be able to automate testing our functionality, to strive for code that can be tested and verified programmatically.&lt;/p&gt;

&lt;p&gt;Assume we are developing a weather application , instead of relying on the actual api and having delays , network errors and other things out of our control, we can build our own mock api to simulate the functionality and to be able to better automate the testing process.&lt;/p&gt;

&lt;h2&gt;
  
  
  Recap
&lt;/h2&gt;

&lt;p&gt;Writing our code to be testable makes it well organized, modular and loosely coupled.&lt;/p&gt;

&lt;p&gt;This naturally leads to software that is clean and easier to maintain.&lt;/p&gt;

&lt;p&gt;This article has been published on &lt;a href="https://www.linkedin.com/pulse/testable-code-101-achilles-moraites/" rel="noopener noreferrer"&gt;linkedin&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Photo by &lt;a href="https://www.pexels.com/@pixabay?utm_content=attributionCopyText&amp;amp;utm_medium=referral&amp;amp;utm_source=pexels" rel="noopener noreferrer"&gt;Pixabay&lt;/a&gt; from &lt;a href="https://www.pexels.com/photo/black-and-white-blank-challenge-connect-262488/?utm_content=attributionCopyText&amp;amp;utm_medium=referral&amp;amp;utm_source=pexels" rel="noopener noreferrer"&gt;Pexels&lt;/a&gt;&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>testing</category>
      <category>productivity</category>
      <category>tdd</category>
    </item>
    <item>
      <title>Typescript Tips and Tricks</title>
      <dc:creator>Achilles Moraites</dc:creator>
      <pubDate>Wed, 29 Jan 2020 08:50:46 +0000</pubDate>
      <link>https://forem.com/achimoraites/typescript-tips-and-tricks-4fnh</link>
      <guid>https://forem.com/achimoraites/typescript-tips-and-tricks-4fnh</guid>
      <description>&lt;p&gt;Here are two Typescript tricks to make your life as a software developer easier.&lt;/p&gt;

&lt;h2&gt;
  
  
  Make an interface that has any number of attributes of a specific type
&lt;/h2&gt;

&lt;p&gt;Ever wondering if you can find a middle ground between the &lt;code&gt;any&lt;/code&gt; type and of a specific type/interface ?&lt;/p&gt;

&lt;p&gt;The answer is yes! &lt;br&gt;
You can make an interface that contains any number of attributes of a specific type while having the ability to have any attribute name:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;Person&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;FlexInterface&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt; &lt;span class="nx"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// now you can use it like&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Group&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;FlexInterface&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;alice&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Alice Byers&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;24&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="na"&gt;brian&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Brian Sanders&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;16&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// still this will fail!&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Group2&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;FlexInterface&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;alice&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Alice Byers&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;24&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="na"&gt;brian&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Brian Sanders&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;16&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
 &lt;span class="c1"&gt;// Type 'string' is not assignable to type 'Person'&lt;/span&gt;
  &lt;span class="na"&gt;matt&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Mathew&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Taking advantage of this technique you can create flexible types without using type &lt;code&gt;any&lt;/code&gt; !&lt;/p&gt;

&lt;h2&gt;
  
  
  Create a type that has a partial match of another type
&lt;/h2&gt;

&lt;p&gt;This effectively means that we can match any number of properties of a given type:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;
&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;Person&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;PartialPerson&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Partial&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Person&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;updatePerson&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;personToUpdate&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;attributes&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;PartialPerson&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="nx"&gt;personToUpdate&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;attributes&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;person&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;John Doe&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;


&lt;span class="c1"&gt;// this works as expected&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;attrs1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;PartialPerson&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;38&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// this will fail&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;attrs2&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;PartialPerson&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;hobbies&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;chess&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;swimming&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;coding&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;updatePerson&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;person&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;attrs1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;I you have some tips and tricks of your own feel free to share them in the comments below!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Happy coding :)  &lt;/p&gt;

&lt;h3&gt;
  
  
  Credits
&lt;/h3&gt;

&lt;p&gt;Photo by &lt;a href="https://www.pexels.com/@yogendras31?utm_content=attributionCopyText&amp;amp;utm_medium=referral&amp;amp;utm_source=pexels" rel="noopener noreferrer"&gt;Yogendra Singh&lt;/a&gt;  from &lt;a href="https://www.pexels.com/photo/man-raising-both-his-arms-1701208/?utm_content=attributionCopyText&amp;amp;utm_medium=referral&amp;amp;utm_source=pexels" rel="noopener noreferrer"&gt;Pexels&lt;/a&gt;&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>oop</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Simplify your Angular Component testing</title>
      <dc:creator>Achilles Moraites</dc:creator>
      <pubDate>Fri, 17 Jan 2020 12:01:53 +0000</pubDate>
      <link>https://forem.com/achimoraites/simplify-your-angular-component-testing-1l3f</link>
      <guid>https://forem.com/achimoraites/simplify-your-angular-component-testing-1l3f</guid>
      <description>&lt;p&gt;Components are everywhere in our apps and testing them is part of our daily software development process.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;But here are some good news: Testing should not be hard!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;There are 3 types of tests for our Components:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Isolated (fastest)&lt;/li&gt;
&lt;li&gt;Shallow (fast)&lt;/li&gt;
&lt;li&gt;Deep (not so fast)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;In this tutorial we will explore Isolated tests&lt;/strong&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Isolated Tests
&lt;/h1&gt;

&lt;p&gt;Those are the less utilized tests in Angular testing yet the most fast and simple to use.&lt;/p&gt;

&lt;p&gt;In those kinds of tests we treat the Component as a simple class.&lt;br&gt;
Testing classes is easy, we can call their methods and verify the results.&lt;/p&gt;

&lt;p&gt;Note that we just test the class functionality and NOT anything in the DOM.&lt;/p&gt;
&lt;h3&gt;
  
  
  Case 1 (simple) the component has no dependencies
&lt;/h3&gt;

&lt;p&gt;Here is our component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// explorer.component.ts

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-explorer',
  templateUrl: './explorer.component.html',
  styleUrls: ['./explorer.component.scss']
})
export class ExplorerComponent implements OnInit {

  explorerName: string;
  location: string;
  constructor() { }

  ngOnInit() {
  }

  getLocation(location) {
    this.location = `Your location is ${location}`;
  }

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

&lt;/div&gt;



&lt;p&gt;Here we perform tests:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// explorer.component.spec.ts

import { async, ComponentFixture, TestBed } from '@angular/core/testing';

import { ExplorerComponent } from './explorer.component';



describe('Isolated ExplorerComponent Tests', () =&amp;gt; {
  let component: ExplorerComponent;
  beforeEach(() =&amp;gt; {
    component = new  ExplorerComponent();
 });

  it('should set the correct location', () =&amp;gt; {
   component.getLocation('Mars');
   expect(component.location).toContain('Mars');
 });

});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can test it just like any other class!&lt;/p&gt;

&lt;h3&gt;
  
  
  Case 2 : the component has dependencies
&lt;/h3&gt;

&lt;p&gt;here is our service:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// explorer.service.ts

import { Injectable } from '@angular/core';
import { of } from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class ExplorerService {

  constructor() { }

  getExplorer() {
    return of('Josh the Lost');
  }

  getLocation() {
    return 'The dark side of the moon';
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;our component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// explorer.component.ts

import { Component, OnInit } from '@angular/core';
import { ExplorerService } from './services/explorer.service';

@Component({
  selector: 'app-explorer',
  templateUrl: './explorer.component.html',
  styleUrls: ['./explorer.component.scss']
})
export class ExplorerComponent implements OnInit {

  explorerName: string;
  location: string;
  constructor(
    private explorerService: ExplorerService
  ) { }

  ngOnInit() {
    this.explorerService.getExplorer().subscribe(data =&amp;gt; {
      this.explorerName = data;
    });
  }

  getLocation(location) {
    this.location = `Your location is ${location}`;
  }

}

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

&lt;/div&gt;



&lt;p&gt;our tests:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// explorer.component.spec.ts

import { async, ComponentFixture, TestBed } from '@angular/core/testing';

import { ExplorerComponent } from './explorer.component';
import { of } from 'rxjs';



describe('Isolated ExplorerComponent Tests', () =&amp;gt; {
  let component: ExplorerComponent;
  // create a spyObject to simplify our mock service creation
  const mockExplorerService = jasmine.createSpyObj(['getExplorer']);
  // set the getExplorer method to return an Observable just like the real one
  mockExplorerService.getExplorer.and.returnValue(of('Dave the brave'));

  beforeEach(() =&amp;gt; {
    component = new  ExplorerComponent(mockExplorerService);
 });

  it('should set the correct location', () =&amp;gt; {
   component.getLocation('Mars');
   expect(component.location).toContain('Mars');
 });

  it('should set the correct explorer', () =&amp;gt; {
  // trigger
  component.ngOnInit();

  expect(component.explorerName).toEqual('Dave the brave');
  // verify that getExplorer from the service has been called
  expect(mockExplorerService.getExplorer).toHaveBeenCalled();
});

});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Using Isolated tests we can test functionality with a simple way that is performant and easy to understand.&lt;/p&gt;

&lt;p&gt;Note that this approach is great for testing just the component (class) code and is not meant to replace TestBed in any way. &lt;br&gt;
We can just combine different kinds of tests to compliment our testing efforts :)&lt;/p&gt;

&lt;p&gt;Happy testing !!!&lt;/p&gt;

&lt;p&gt;Photo by Rodolfo Clix from Pexels&lt;/p&gt;

</description>
      <category>angular</category>
      <category>testing</category>
      <category>javascript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>A developers guide to Run Puppeteer on Elastic Beanstalk (no ubuntu linux)</title>
      <dc:creator>Achilles Moraites</dc:creator>
      <pubDate>Fri, 03 Jan 2020 07:41:55 +0000</pubDate>
      <link>https://forem.com/achimoraites/a-developers-guide-to-run-puppeteer-on-elastic-beanstalk-no-ubuntu-linux-4pl5</link>
      <guid>https://forem.com/achimoraites/a-developers-guide-to-run-puppeteer-on-elastic-beanstalk-no-ubuntu-linux-4pl5</guid>
      <description>&lt;p&gt;The problem with running puppeteer on elastic beanstalk is that chrome is not included by default.&lt;/p&gt;

&lt;p&gt;This has been published on &lt;a href="https://medium.com/@achillesmoraites/a-developers-guide-to-run-puppeteer-on-elastic-beanstalk-linux-no-ubuntu-linux-d137fa2641ae" rel="noopener noreferrer"&gt;Medium&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Make sure you have
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Installed EB CLI&lt;/strong&gt; &lt;a href="https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/eb-cli3-install.html" rel="noopener noreferrer"&gt;you can find it here&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;a href="https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/eb-cli3-configuration.html" rel="noopener noreferrer"&gt;Configured the EB CLI&lt;/a&gt;&lt;/strong&gt; make sure you setup ssh for your instances !&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Install chrome on elastic beanstalk
&lt;/h2&gt;

&lt;p&gt;Connect with ssh to elastic beanstalk&lt;br&gt;
To connect with ssh the easiest way is to use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;eb ssh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;after you have connected, run the following command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;curl https://intoli.com/install-google-chrome.sh | bash
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the command above fetches and runs the installation script on elastic beanstalk.&lt;/p&gt;

&lt;h2&gt;
  
  
  Lets Use it
&lt;/h2&gt;

&lt;p&gt;After we have chrome installed we need to setup puppeteer to use it instead of the default chromium that will not work no matter what.&lt;/p&gt;

&lt;p&gt;here you can see a simple snippet of how we can use puppeteer to take a screenshot of a page in &lt;a href="http://www.example.com" rel="noopener noreferrer"&gt;www.example.com&lt;/a&gt; , note the executablePath is pointing to the directory where our installed chrome lives.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;'use strict';                                               
const puppeteer = require('puppeteer');

(async() =&amp;gt; {

const browser = await puppeteer.launch({executablePath: '/usr/bin/google-chrome-stable',
headless: true, args: ['--no-sandbox', '--disable-setuid-sandbox']});

const page = await browser.newPage();

await page.goto('https://www.example.com');                                                               

await page.screenshot({ path: 'screenshot.png', fullPage: true });

browser.close();

})();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Congrats you can now use Puppeteer on elastic beanstalk !&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.pexels.com/@magda-ehlers-pexels" rel="noopener noreferrer"&gt;cover photo by Magda Ethels&lt;/a&gt;&lt;/p&gt;

</description>
      <category>aws</category>
      <category>linux</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Design Patterns — Composite</title>
      <dc:creator>Achilles Moraites</dc:creator>
      <pubDate>Thu, 19 Dec 2019 08:19:01 +0000</pubDate>
      <link>https://forem.com/achimoraites/design-patterns-composite-4k49</link>
      <guid>https://forem.com/achimoraites/design-patterns-composite-4k49</guid>
      <description>&lt;p&gt;&lt;a href="https://medium.com/@achillesmoraites/design-patterns-composite-a77e570e9105" rel="noopener noreferrer"&gt;This has been posted on Medium&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The composite design pattern is about a hierarchy where nodes with children have different behavior than childless nodes.&lt;/p&gt;

&lt;p&gt;The pattern consists of three elements:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Component&lt;/strong&gt; : a super-class typically an interface&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Composite&lt;/strong&gt; : a node with children implements Component&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Leaf&lt;/strong&gt; : a childless node implements Component&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmkmdqwrqvs8mgq8b5h16.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmkmdqwrqvs8mgq8b5h16.png" alt="Alt Text" width="800" height="466"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A typical example is a file system where there are folders and files:&lt;/p&gt;

&lt;p&gt;Folders can contain files , other folders or can be empty. On the other hand files don’t contain folders or other files!&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Remember , the most important aspect of a design pattern is it’s intent! Once you understand it, you can use the pattern effectively in your projects!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Project : FileSystem
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://github.com/Cyb3rN4u7/DesignPatterns/tree/master/CompositePattern/FileSystem/src" rel="noopener noreferrer"&gt;get the source code here&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Here you will explore a simple file system , where you can see a simple implementation of the pattern.&lt;/p&gt;

&lt;p&gt;Here you can see a UML of our File System :&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fshdpfgbw4uvl3cf04u8z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fshdpfgbw4uvl3cf04u8z.png" alt="Alt Text" width="800" height="468"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Take a moment to explore the code and:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;note what component of the pattern each class represents&lt;/li&gt;
&lt;li&gt;observe the output of the code and see the difference between a file and a folder&lt;/li&gt;
&lt;li&gt;in what scenarios do you think this pattern would be useful?&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Project : Extending our FileSystem
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://github.com/Cyb3rN4u7/DesignPatterns/tree/master/CompositePattern/FileSystem/src" rel="noopener noreferrer"&gt;get the source code here&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Time to get your hands dirty:&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Now that you understand how the composite pattern works , lets extend our FileSystem by adding new file types!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Take the code and implement a new File type: Img&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;General Guidelines:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; The Img should implement FileComponent&lt;/li&gt;
&lt;li&gt; Img must have a data field, you can use anything you like&lt;/li&gt;
&lt;li&gt; You can use the display method of the file to customize it for the new file&lt;/li&gt;
&lt;li&gt; Create an Img object and add it to a folder!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By the end of this project you have applied the pattern in an existing code-base and gained practical experience applying it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://github.com/Cyb3rN4u7/DesignPatterns/tree/master/CompositePattern/FileSystem%20-%20Solution/src" rel="noopener noreferrer"&gt;See the solution&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Happy coding !!!&lt;/em&gt;&lt;/p&gt;

</description>
      <category>oop</category>
      <category>productivity</category>
      <category>codequality</category>
      <category>java</category>
    </item>
    <item>
      <title>Lazy loading modules in Angular</title>
      <dc:creator>Achilles Moraites</dc:creator>
      <pubDate>Fri, 13 Dec 2019 13:12:56 +0000</pubDate>
      <link>https://forem.com/achimoraites/lazy-loading-modules-in-angular-3ln9</link>
      <guid>https://forem.com/achimoraites/lazy-loading-modules-in-angular-3ln9</guid>
      <description>&lt;p&gt;This has been posted on &lt;a href="https://medium.com/@achillesmoraites/lazy-loading-modules-in-angular-ea7362ff179" rel="noopener noreferrer"&gt;medium&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As we develop our applications you might have noticed that the initial load speed gets slower as the application is being developed, especially on mobile devices and slow networks.&lt;/p&gt;

&lt;p&gt;Imagine that you are developing an e-commerce web app that allows people to buy and sell products.&lt;/p&gt;

&lt;p&gt;So you have pages for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; Searching products and people&lt;/li&gt;
&lt;li&gt; Store page&lt;/li&gt;
&lt;li&gt; Product page&lt;/li&gt;
&lt;li&gt; User profile&lt;/li&gt;
&lt;li&gt; Login/Registration&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As a good developer you had organized the code in modules, but as the application grows the problem is only getting worse …&lt;/p&gt;

&lt;h2&gt;
  
  
  But first lets find out why.
&lt;/h2&gt;

&lt;p&gt;What is happening?&lt;/p&gt;

&lt;p&gt;Any modules you have used above are loaded on the application start even when they are not needed, this translates to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Slow initial page load/speed: The perceived performance of the application is LOW.&lt;/li&gt;
&lt;li&gt;That happens because the users have to wait for the browser to download all the modules, even those that are not needed at the given time!&lt;/li&gt;
&lt;li&gt;Excessive network data usage: Mobile internet in many countries is expensive and slow. That means that visiting your application costs MONEY and TIME for each user!&lt;/li&gt;
&lt;li&gt;Poor mobile performance: If you combine the above you have an application that is not mobile friendly.&lt;/li&gt;
&lt;li&gt;Does not matter if you do RWD (Responsive Web Design) as the excessive volume that has to be downloaded and run on the application start is a sure way to make it unresponsive for a couple of seconds.&lt;/li&gt;
&lt;li&gt;This becomes even worse on low end mobile devices…&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Summarizing: loading almost everything on the application start is a terrible idea.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Why to lazy-load modules
&lt;/h2&gt;

&lt;p&gt;To avoid all the bad things mentioned above we will need to lazy-load any module that has no need to be loaded on application start.&lt;br&gt;
If the user of your application on the app start is on the search page there is no need to load any module that is no being used in the search page, as the user might never need to use them:&lt;/p&gt;

&lt;p&gt;For example what happens if a user just searches for a product and goes to the product page?&lt;/p&gt;

&lt;p&gt;The user will just need the modules for the search and product display, all the other modules have no need to be loaded at all.&lt;br&gt;
How to lazy-load a module&lt;/p&gt;

&lt;p&gt;Assuming we want to lazy-load the UserProfileModule&lt;/p&gt;

&lt;p&gt;The module:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@NgModule({
    imports: [CommonModule,                        
         RouterModule.forChild([                   
         {path: '', component: UserProfileComponent}    
     ])],
    declarations: [UserProfileComponent]
})

export class UserProfileModule{}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The example above is a simple module with a single component that we will lazy-load.&lt;/p&gt;

&lt;p&gt;Inside our app-routing.module.ts&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const routes: Routes = [{  path: '', component: SearchComponent  },
  { path: 'products/:productId', component: ProductDetailComponent},
  { path: 'profile',   loadChildren:'./userprofile.module#UserProfileModule'}
];

 @NgModule({
   imports: [RouterModule.forRoot(routes)],
   exports: [RouterModule]
})
export class AppRoutingModule { }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice that we use loadChildren instead of component and we provide the path to the module and we add to it at the end a hash # followed with the module class name: in our example we append #UserProfileModule&lt;br&gt;
How it works&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The UserProfileModule is moved to a separate file when we build the application.&lt;/li&gt;
&lt;li&gt;When the user navigates to the profile the UserProfileModule is being lazy-loaded: The module is downloaded and then used by the application.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If the user never visits the profile this module will never be loaded!&lt;br&gt;
That’s it you have just lazy-loaded a module in Angular!&lt;br&gt;
Summary&lt;/p&gt;

&lt;p&gt;Module lazy-loading gives you more control on what modules will be loaded, allowing you to reduce the app bundle and the initial load times.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Module lazy-loading gives you awesome superpowers!&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>angular</category>
      <category>typescript</category>
      <category>webdev</category>
      <category>codequality</category>
    </item>
  </channel>
</rss>
