<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Bearded JavaScripter</title>
    <description>The latest articles on DEV Community by Bearded JavaScripter (@qarunqb).</description>
    <link>https://dev.to/qarunqb</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%2F309151%2Feb15bde3-67eb-4033-b69a-bb20cb3f267d.jpg</url>
      <title>DEV Community: Bearded JavaScripter</title>
      <link>https://dev.to/qarunqb</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/qarunqb"/>
    <language>en</language>
    <item>
      <title>Set up your next Angular 9 Project for CI/CD on Bitbucket Pipelines</title>
      <dc:creator>Bearded JavaScripter</dc:creator>
      <pubDate>Tue, 27 Oct 2020 06:34:50 +0000</pubDate>
      <link>https://dev.to/qarunqb/set-up-your-next-angular-9-project-for-ci-cd-on-bitbucket-pipelines-2i9</link>
      <guid>https://dev.to/qarunqb/set-up-your-next-angular-9-project-for-ci-cd-on-bitbucket-pipelines-2i9</guid>
      <description>&lt;p&gt;Continuous Integration and Continuous Deployment are practices of a good Software Engineering Team and is usually a sign of healthy codebases. It also encourages us to approach Software Development from a TDD standpoint.&lt;/p&gt;

&lt;p&gt;Pipelines are a feature of Bitbucket that allow us to run CI/CD when committing/merging into designated branches on the repository. This automates much of what developers were doing before the days of CI/CD.&lt;/p&gt;

&lt;p&gt;To learn more about Pipelines, check out this link &lt;a href="https://support.atlassian.com/bitbucket-cloud/docs/get-started-with-bitbucket-pipelines/"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In this article, I’ll show how to get started on running Pipelines on your next Angular Project. &lt;strong&gt;This article assumes that you’ll be using the usual Karma/Jasmine test suite that comes with Angular.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The usual method for a CI/CD Workflow is as follows:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Run unit tests&lt;/li&gt;
&lt;li&gt;Run integration tests&lt;/li&gt;
&lt;li&gt;Build for Production&lt;/li&gt;
&lt;li&gt;Deploy&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Here’s the &lt;a href="https://bitbucket.org/qqbissoondial/bitbucket-pipelines-demo/src/master/"&gt;link to the repo&lt;/a&gt; and the &lt;a href="https://bitbucket.org/qqbissoondial/bitbucket-pipelines-demo/commits/35ee1a947b8c90ac3776772b78cccb4cdf27bc14"&gt;commit&lt;/a&gt; where I made the changes described in this article. Also, you may have to install karma-spec-reporter if it isn’t already installed. Run &lt;code&gt;npm install karma-spec-reporter — save-dev&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Some Background
&lt;/h2&gt;

&lt;p&gt;Pipelines in Bitbucket are triggered by a bitbucket-pipelines.yml file. I’ll get into the configuration of that file in a bit, but for now, it’s important to note that &lt;strong&gt;Pipelines run in a Docker environment&lt;/strong&gt;. While you don’t need to know about Docker, it helps a bunch in understanding what’s happening. The configuration file is also &lt;strong&gt;similar&lt;/strong&gt; to a Dockerfile.&lt;/p&gt;

&lt;p&gt;The goal is to run our test using a Headless Browser (preferable Chrome Headless) inside the docker environment. That means:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;All our usual testing commands have to be available inside the container&lt;/li&gt;
&lt;li&gt;Node has to be in container&lt;/li&gt;
&lt;li&gt;Chrome has to be in the container&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let’s start by addressing the first issue.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting the Stage
&lt;/h2&gt;

&lt;p&gt;There’s a few things that we need to change in our Karma configuration file as well as our package.json file. You should update your karma.conf.js file to look like the following:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Notice how we added “ChromiumNoSandbox” to our browsers list. In reality, this can be whatever name we want (as long as it’s not the name of another browser). We would have also specified the configuration for ChromiumNoSandbox as using ChromeHeadless browser for testing as well as some flags.&lt;/p&gt;

&lt;p&gt;Now we’ll need to edit the scripts portion of our package.json file. This is how it should look:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;We’ll run test:local when we want to test locally and test:prod in our bitbucket-pipelines.yml file. Notice how we also changed the npm build command to “ng build — prod”.&lt;/p&gt;

&lt;p&gt;Using the “— browsers Chrome” flag on our test:local npm script causes the Chrome UI to be launched. This is fine for testing on our machine locally. However, we won’t be able to do this in our production Pipelines since &lt;em&gt;you won’t have access to a GUI&lt;/em&gt; (remember, docker container). Your commands are run in a CLI automatically. This is why we need the Headless Browser for production.&lt;/p&gt;

&lt;p&gt;Additionally, by default, karma will always listen for changes so that it can re-run tests. In Bitbucket Pipelines, we don’t want this since we need to move on to additional steps in the pipelines and &lt;strong&gt;your build minutes are limited&lt;/strong&gt;. Listening to changes indefinitely is disabled by using the “ — watch=false” flag on our production test script.&lt;/p&gt;

&lt;h2&gt;
  
  
  Configuring Bitbucket Pipelines
&lt;/h2&gt;

&lt;p&gt;As I mentioned earlier, there needs to be a bitbucket-pipelines.yml file at the root of our Angular project so that Bitbucket knows to enable Pipelines.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;I’ll translate what the above file is saying:&lt;/p&gt;

&lt;h4&gt;
  
  
  Initialization
&lt;/h4&gt;

&lt;p&gt;Under branches, ‘{master}’ is telling us that Bitbucket will trigger Pipelines when a Commit or Pull Request is made into the master branch. If you want this effect on other branches, then replace master with that branch’s name.&lt;/p&gt;

&lt;h4&gt;
  
  
  Step
&lt;/h4&gt;

&lt;p&gt;‘step’ tells Bitbucket that this is the first step in the Pipeline. Pipelines can have multiple steps and can even run in parallel to speed up the overall process. Check out more on steps &lt;a href="https://bitbucket.org/blog/multiple-steps-in-bitbucket-pipelines"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Name
&lt;/h4&gt;

&lt;p&gt;‘name’ is the name of the current step (pretty self-explanatory).&lt;/p&gt;

&lt;h4&gt;
  
  
  Image
&lt;/h4&gt;

&lt;p&gt;Strap in, this part merits some explanation. If you’re unfamiliar with Docker and containerization in general, then &lt;a href="https://medium.com/zero-equals-false/docker-introduction-what-you-need-to-know-to-start-creating-containers-8ffaf064930a"&gt;this article&lt;/a&gt;  provides an amazing insight. But I’ll still try to simplify as much as possible :)&lt;/p&gt;

&lt;p&gt;A container is simply an execution environment. This environment needs a base image, which is just some installed software and some commands that are available at command line. So for example, a container that has node image as its base has node, npm and npx installed and available at the command line.&lt;/p&gt;

&lt;p&gt;Bitbucket Pipelines run inside a Docker container and you are allowed to specify whatever image you want (as long as it’s available publicly such as on &lt;a href="https://hub.docker.com/"&gt;DockerHub&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;In the context of the bitbucket-pipelines.yml file, we defined a rastasheep/alpine-node-chromium:12-alpine image. That’s a lot to digest from just the name alone. What it simply means is:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;The image was made by &lt;a href="https://github.com/rastasheep"&gt;rastasheep&lt;/a&gt; (a huge shoutout and thank you to him for this Docker image)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Alpine Linux is the base of this image&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;This image has node 12 and Chromium installed.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This is good news as this gives us Chrome Headless inside our container! It also allows us to run those npm test scripts that we set up earlier since node 12 is installed as well.&lt;/p&gt;

&lt;h4&gt;
  
  
  Caches
&lt;/h4&gt;

&lt;p&gt;‘cache’ tells Bitbucket to save certain resources instead of having to re-download them all the time. This ultimately saves time on Pipeline execution.&lt;/p&gt;

&lt;p&gt;The ‘cache’ field works with multiple languages. In this case, we’re telling Bitbucket to cache our node_modules folder after the pipelines is complete.&lt;/p&gt;

&lt;h4&gt;
  
  
  Script
&lt;/h4&gt;

&lt;p&gt;‘script’ defines a series of instructions to be run once the pipeline has been set up. Pipeline set up simply involves pulling the specified image and copying the files made on the latest commit that triggered the pipelines into the docker container.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;We’ll run “npm install” as the first command to create a node_modules folder inside our docker container.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Then we’ll run “npm run test:prod to allow Karma to run our Jasmine tests in the Headless Browser.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Finally we’ll run “npm run build” to trigger the Angular production build.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  Artifacts (optional)
&lt;/h4&gt;

&lt;p&gt;‘artifacts’ allow us to store our artifacts in the pipelines docker container to carry forward into subsequent steps. This is useful for multi-step pipelines or it might precede a series of parallel steps that require the build artifacts (such as integration tests and deployment).&lt;/p&gt;

&lt;p&gt;&lt;em&gt;One final point to note is that any error thrown will stop the pipeline and cause it to fail. Make sure that the bitbucket-pipelines.yml file is configured correctly and all your tests are passing!&lt;/em&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Enable Pipelines
&lt;/h4&gt;

&lt;p&gt;Now that your changes have been made and your bitbucket-pipelines.yml is at the root of your repository. You now have to manually enable pipelines.&lt;/p&gt;

&lt;p&gt;This can be done by clicking “Pipelines” on the left side menu, scrolling to the bottom where you’ll see your .yml file. After a few seconds of Bitbucket checking the .yml file for syntax errors, you should see the “Enable” button turn green. This will trigger a pipeline build and enable pipelines on your repository.&lt;/p&gt;

&lt;h2&gt;
  
  
  Deployment
&lt;/h2&gt;

&lt;p&gt;At this point, our tests are passing and the production build works. Now all that’s left is to deploy our application. Deployment can happen in a number of ways:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;You can use the scp command to deploy to a remote web server (not really recommended)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You can push the build artifacts to another repository.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The second option requires a little more setup which I won’t go into now. But it does involve using an image that also has git installed and setting the password to the account of the repository that houses the build artifacts.&lt;/p&gt;

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

&lt;p&gt;And that’s it! You’ve successfully set up a CI/CD environment for Angular using Bitbucket Pipelines! I’ve only included the basic default tests that come with generated components and services to keep the demo short and simple.&lt;/p&gt;

&lt;p&gt;Let me know what you think of this article and I’ll try to improve it as much as I can :)&lt;/p&gt;

</description>
      <category>angular</category>
      <category>bitbucket</category>
    </item>
    <item>
      <title>TDD in Angular - Further HTTP Testing</title>
      <dc:creator>Bearded JavaScripter</dc:creator>
      <pubDate>Tue, 06 Oct 2020 03:04:34 +0000</pubDate>
      <link>https://dev.to/qarunqb/tdd-in-angular-further-http-testing-3mf2</link>
      <guid>https://dev.to/qarunqb/tdd-in-angular-further-http-testing-3mf2</guid>
      <description>&lt;p&gt;In our last article, we would have tested basic HTTP requests and dictated how the response would look. However, many unexpected things can happen when sending a request. We need to handle these situations so that our application doesn't crash and the User Experience remains flawless.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Code for this article can be found &lt;a href="https://github.com/Qarun-Qadir-Bissoondial/ng-jasmine-testing-demo/tree/testing-http" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding the HttpErrorResponse
&lt;/h2&gt;

&lt;p&gt;Before we test situations where our requests can fail, we need to understand the &lt;a href="https://angular.io/api/common/http/HttpErrorResponse" rel="noopener noreferrer"&gt;HttpErrorResponse&lt;/a&gt;. This is a class that Angular wraps around any network errors thrown by the browser before it reaches into our application. &lt;/p&gt;

&lt;p&gt;Failing HTTP requests are caught by the &lt;code&gt;error&lt;/code&gt; callback of the &lt;code&gt;subscribe&lt;/code&gt; function and the error parameter is of type &lt;code&gt;HttpErrorResponse&lt;/code&gt;. This is useful for UI where we can grab the error message and display it to the user. It is also useful for testing where we expected our request to fail and have a certain status code. &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;


&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/some/failing/request&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
   &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&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="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="nx"&gt;data&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="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;HttpErrorResponse&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="cm"&gt;/*Access error object here*/&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;All responses carrying 400 and 500 status codes are immediately treated as an error and will cause the subscription to fail.&lt;/p&gt;
&lt;h2&gt;
  
  
  Handling Failed requests
&lt;/h2&gt;

&lt;p&gt;There are a number of ways to handle failed requests and the choice may depend on the application domain and business rules. Generally speaking, we can:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Tell the user something went wrong&lt;/li&gt;
&lt;li&gt;Retry the request in the background x number of times&lt;/li&gt;
&lt;li&gt;Redirect to another page&lt;/li&gt;
&lt;li&gt;Return a default value &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It's always a good idea to let the user know what happened so they won't be left waiting and confused. This can take the form of a simple pop up message on the screen. It is easy to replace this code in the future if error handling methods change. &lt;/p&gt;

&lt;p&gt;Generally, when subscribing to observables fail, code inside the &lt;code&gt;next&lt;/code&gt; and the &lt;code&gt;complete&lt;/code&gt; callbacks never run. Therefore, whenever we expect our requests to fail, we need to run our assertions inside the &lt;code&gt;error&lt;/code&gt; callback. This is useful for testing if certain error messages are being displayed for different types of errors.&lt;/p&gt;

&lt;p&gt;Ideally, we want to simulate a failing request and test that our application recovers. In other words, even though the request may have failed, our application code won't throw an error and freeze up. Let's get started.&lt;/p&gt;
&lt;h2&gt;
  
  
  Writing our tests
&lt;/h2&gt;

&lt;p&gt;We'll use the same To-Do list service from our &lt;a href="https://dev.to/qarunqb/tdd-in-angular-basics-of-http-testing-5a52"&gt;previous article&lt;/a&gt;. &lt;br&gt;
Let's test our &lt;code&gt;getAllTodos&lt;/code&gt; function but if the server fails, we return an empty array.&lt;/p&gt;

&lt;p&gt;Remember that our service looks like this:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;I made a separate test suite for this function since I wanted to test more than what I described above. &lt;/p&gt;

&lt;p&gt;Since our function is supposed to recover from the error and continue normally, our assertions are in the &lt;code&gt;next&lt;/code&gt; function of the &lt;code&gt;subscribe&lt;/code&gt;. We would expect that the response data is defined, it is an array and it has a length of 0. &lt;/p&gt;

&lt;p&gt;We can simulate different statuses, status texts, headers and more by passing in a second parameter into &lt;code&gt;testRequest.flush&lt;/code&gt;. In this case, a status of 500 was simulated which means an internal error has occurred in the server.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;When we run our test, it fails since we haven't modified our code to take care of this situation.&lt;/p&gt;

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

&lt;p&gt;Notice how the &lt;code&gt;error&lt;/code&gt; callback is triggered and &lt;code&gt;fail&lt;/code&gt; function that Jasmine provides is executed. However, our tests will pass if we modify our code to the following: &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;getAllTodos&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&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;url&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nf"&gt;catchError&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;HttpErrorResponse&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="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;status&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;500&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="k"&gt;of&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;span class="p"&gt;}&lt;/span&gt;


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

&lt;/div&gt;
&lt;p&gt;The above code says to execute the HTTP request but if an error occurs and the response status is 500, then return an Observable containing an empty array. We return an Observable as opposed to the raw value because this is what &lt;code&gt;catchError&lt;/code&gt; expects.&lt;/p&gt;
&lt;h2&gt;
  
  
  Testing unauthorized requests
&lt;/h2&gt;

&lt;p&gt;Usually, when dealing with authorization, we include an access token in our request headers so that the server knows who we are. Absence of this token means that the server should reject the request and return a 401 response. &lt;/p&gt;

&lt;p&gt;Let's say that we needed to be authorized to update a to-do item.&lt;br&gt;
We can test that a certain error message is displayed if the request is unauthorized.&lt;/p&gt;

&lt;p&gt;Our test would look something like this:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;and the corresponding code to make the test pass will be: &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;updateTodo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;updatedItem&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Todo&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;put&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&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="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;/&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;updatedItem&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="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;updatedItem&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nf"&gt;catchError&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;HttpErrorResponse&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="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;status&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;401&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;displayError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;statusText&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;of&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&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;span class="p"&gt;}&lt;/span&gt;



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

&lt;/div&gt;
&lt;h2&gt;
  
  
  Testing retries
&lt;/h2&gt;

&lt;p&gt;There are times where the user, through no fault of their own, may have an unstable connection. Though we may display an error message on the screen when a request fails, we should first retry the request in hopes that the response comes through.&lt;/p&gt;

&lt;p&gt;Let's say we want to retry getting a single to-do item 3 more times after it fails the first time. If it fails, after 3 retries, then it should throw an error.&lt;/p&gt;

&lt;p&gt;Our test: &lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;And the corresponding code:&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;getSingleTodo&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="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;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&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="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;/&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="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="nf"&gt;retry&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
      &lt;span class="nf"&gt;catchError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;throwError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Failed to fetch item with id &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="s2"&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;p&gt;In our test, we would have simulated a 404 error but our function actually catches all errors and then retries the request. Additionally, notice that the for loop in our test runs 4 times. This is for the original request and then the following 3 retries.&lt;/p&gt;

&lt;p&gt;We also expected this function to throw an error. Therefore, our assertion was in the &lt;code&gt;error&lt;/code&gt; callback of the Observable. &lt;/p&gt;

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

&lt;p&gt;In this article, we gained a deeper understanding of the HttpErrorResponse and how it appears in Observables. We also tested Http Requests further by manipulating the response data and status code. &lt;/p&gt;

&lt;p&gt;This forms merely the foundation of testing more complex Http Requests that chain main RxJs operators together. Hope you begin writing your requests with more confidence and for a better User Experience. Thanks for reading 😄&lt;/p&gt;

</description>
      <category>angular</category>
      <category>typescript</category>
      <category>javascript</category>
      <category>tdd</category>
    </item>
    <item>
      <title>TDD in Angular - Basics of HTTP Testing</title>
      <dc:creator>Bearded JavaScripter</dc:creator>
      <pubDate>Thu, 01 Oct 2020 00:14:19 +0000</pubDate>
      <link>https://dev.to/qarunqb/tdd-in-angular-basics-of-http-testing-5a52</link>
      <guid>https://dev.to/qarunqb/tdd-in-angular-basics-of-http-testing-5a52</guid>
      <description>&lt;p&gt;It won't be much of a Web Application if it doesn't make HTTP requests, does it? However, it's easy to forget that these need to be tested as well! &lt;/p&gt;

&lt;p&gt;In this article, I'll cover how to unit test HTTP requests in your Angular Application so you can have the confidence that they'll always work as expected.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Code for the article can be found &lt;a href="https://github.com/Qarun-Qadir-Bissoondial/ng-jasmine-testing-demo/tree/testing-http"&gt;here&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting up our code
&lt;/h2&gt;

&lt;p&gt;Let's say that our HTTP calls live in a service that handles to-do items. We can do our basic CRUD operations: getting our to-do items or a single one, creating new items, updating or deleting items.&lt;/p&gt;

&lt;p&gt;I'll be using &lt;a href="https://jsonplaceholder.typicode.com/"&gt;JSONPlaceHolder&lt;/a&gt; for this since it's the quickest way to get started. The most barebones CRUD service should look like this: &lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;After importing &lt;code&gt;HttpClientTestingModule&lt;/code&gt;, the auto-generated spec file should look like this:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;At this point your only test ("it should be created") should be passing.&lt;/p&gt;

&lt;h2&gt;
  
  
  HTTP Testing Method
&lt;/h2&gt;

&lt;p&gt;Remember that during testing, &lt;em&gt;we're not actually making any requests&lt;/em&gt;. We're only concerned with if the request was sent and if a response is handled properly when it returns. Anything that happens in between that is out of the scope of unit testing. &lt;/p&gt;

&lt;p&gt;Therefore, let's say that we're unit testing a GET request, we only care that the request is sent and, if the request is successful, some data comes back.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Keep in mind that Angular HttpClient uses Observables rather than promises so we have to adjust our tests accordingly!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Angular provides an entire module to help us test HTTP Requests called the &lt;code&gt;HttpClientTestingModule&lt;/code&gt; that allows us to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Mock a request&lt;/li&gt;
&lt;li&gt;Create a fake request with any status code&lt;/li&gt;
&lt;li&gt;Pass along a response with fake data&lt;/li&gt;
&lt;li&gt;Cancel a request&lt;/li&gt;
&lt;li&gt;And much much more!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This means we can test our requests from every angle possible and have our application handle as much cases as possible. I'll cover these in future articles. Let's get set up!&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting up HTTP Mocks
&lt;/h2&gt;

&lt;p&gt;We need to establish a Mock network that allows us to control when requests are sent, what data is returned and whether or not the request was successful. This comes in the form of &lt;code&gt;HttpTestingController&lt;/code&gt; as seen below:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Notice that in the &lt;code&gt;afterEach&lt;/code&gt; block, we called &lt;code&gt;httpMock.verify()&lt;/code&gt;. This ensures that there are no pending requests in our mock network before moving on to other tests.&lt;/p&gt;

&lt;p&gt;Now we can really start testing our code!&lt;/p&gt;

&lt;h2&gt;
  
  
  Testing Requests
&lt;/h2&gt;

&lt;p&gt;We want to test &lt;code&gt;getSingleTodo&lt;/code&gt;. Based on the function name, we can expect that the HTTP response will contain a todo object. Let's represent our expectations as tests.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;In the above code, we: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;ran our &lt;code&gt;getSingleTodo&lt;/code&gt; function and expected the result to be defined&lt;/li&gt;
&lt;li&gt;used the controller to expect a request to have the URL &lt;code&gt;https://jsonplaceholder.typicode.com/todos/1&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;expected the request to be a GET request&lt;/li&gt;
&lt;li&gt;used the controller to manually send the request with the fake todo data.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Under normal circumstances, the &lt;code&gt;getSingleTodo&lt;/code&gt; function would have made an actual request but our &lt;code&gt;HttpTestingController&lt;/code&gt; intercepted the request and returned the fake data using &lt;code&gt;testRequest.flush&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We can now use this information to test POST requests. These contain a request body and can return data along with the status code.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;This test ensures a POST request is sent to the correct URL and the created data is returned.&lt;/p&gt;

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

&lt;p&gt;In this article, we learned: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the theory behind testing HTTP requests by using a mock network&lt;/li&gt;
&lt;li&gt;setting up the mock network using &lt;code&gt;HttpTestingController&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;running fake requests and testing their URLs, methods and return values &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The next article will show how to control fake requests so we can test our application in the event of unauthorized requests, server crashes and basic error handling.&lt;/p&gt;

&lt;p&gt;Hope you enjoyed reading! 😄&lt;/p&gt;

</description>
      <category>angular</category>
      <category>javascript</category>
      <category>tdd</category>
    </item>
    <item>
      <title>Angular Modules - Basics</title>
      <dc:creator>Bearded JavaScripter</dc:creator>
      <pubDate>Sun, 13 Sep 2020 05:52:45 +0000</pubDate>
      <link>https://dev.to/qarunqb/angular-basics-intro-to-modules-11mj</link>
      <guid>https://dev.to/qarunqb/angular-basics-intro-to-modules-11mj</guid>
      <description>&lt;p&gt;Angular is a popular framework for building rich UIs and high-performance Web Applications. It's also very opinionated in its code structure, meaning that the framework prefers things to be done "the angular way". Part of this means including Angular Modules in your application/library. But what are modules exactly? 🤔🤔🤔&lt;/p&gt;

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

&lt;p&gt;An Angular Module is simply a way to group individual pieces of logic and components together under one umbrella. Let's take a look at the most common module that all Angular applications have: AppModule.&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="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;BrowserModule&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/platform-browser&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;AppComponent&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;./app.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;BrowserModule&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;AppComponent&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="na"&gt;bootstrap&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;    &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="nx"&gt;AppComponent&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="nx"&gt;AppModule&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;There is an @NgModule decorator that tells Angular to prepare this class to be a Module.&lt;br&gt;
The decorator accepts an object that customizes the module.&lt;/p&gt;




&lt;p&gt;The &lt;code&gt;bootstrap&lt;/code&gt; option is specific to &lt;code&gt;AppModule&lt;/code&gt; since it specifies the entry point through which Angular loads the rest of the application. &lt;/p&gt;




&lt;p&gt;The &lt;code&gt;declarations&lt;/code&gt; array shows what this module contains. &lt;em&gt;Components, pipes and directives&lt;/em&gt; are listed here and can be used within this module and can interact with each other. For example, if our AppModule was:&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="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;BrowserModule&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;AppComponent&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;TestOneComponent&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;TestTwoComponent&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="na"&gt;bootstrap&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;    &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="nx"&gt;AppComponent&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="nx"&gt;AppModule&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;Then inside our &lt;code&gt;app.component.html&lt;/code&gt;, we could have something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;app-test-one&amp;gt;&lt;/span&gt; &lt;span class="nt"&gt;&amp;lt;/app-test-one&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;app-test-two&amp;gt;&lt;/span&gt; &lt;span class="nt"&gt;&amp;lt;/app-test-two&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is because these 2 components were declared in &lt;code&gt;AppModule&lt;/code&gt; and therefore, were made available. Generally speaking, whatever we put in the &lt;code&gt;declarations&lt;/code&gt; array of &lt;code&gt;AppModule&lt;/code&gt; becomes globally available to use.&lt;/p&gt;




&lt;p&gt;The &lt;code&gt;imports&lt;/code&gt; and &lt;code&gt;exports&lt;/code&gt; array tells us what other modules are connected to this one. It allows us to compose modules together like lego blocks. For example:&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="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;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;FirstComponent&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;SecondComponent&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="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;FirstComponent&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="nx"&gt;FirstModule&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="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="nx"&gt;FirstModule&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="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="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="nx"&gt;SecondModule&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;Usually these modules would be in different files but they are next to each other for the sake of example. We can see that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;FirstModule&lt;/code&gt; exports a component called &lt;code&gt;FirstComponent&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;SecondModule&lt;/code&gt; imports &lt;code&gt;FirstModule&lt;/code&gt;. This means that &lt;code&gt;FirstComponent&lt;/code&gt; is now available to use inside of &lt;code&gt;SecondModule&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We also see that &lt;code&gt;FirstModule&lt;/code&gt; has another component called &lt;code&gt;SecondComponent&lt;/code&gt; but it isn't exported. This means it can never be made available to other modules that import &lt;code&gt;FirstModule&lt;/code&gt;.&lt;/p&gt;




&lt;p&gt;The &lt;code&gt;providers&lt;/code&gt; array allows us to inject default or replacement values, classes and more wherever it appears in code associated with our module. It's much clearer with an example. &lt;/p&gt;

&lt;p&gt;Angular Material Components have many defaults but we can change those defaults using the &lt;code&gt;providers&lt;/code&gt; array of the module where our Material Components are imported.&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="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;BrowserModule&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="nx"&gt;BrowserAnimationsModule&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;MatExpansionModule&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;AppComponent&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="p"&gt;{&lt;/span&gt; 
      &lt;span class="na"&gt;provide&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;MAT_EXPANSION_PANEL_DEFAULT_OPTIONS&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;useValue&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;collapsedHeight&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;64px&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;expandedHeight&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;80px&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="na"&gt;bootstrap&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;    &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="nx"&gt;AppComponent&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="nx"&gt;AppModule&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;In the above example, we tell Angular Material to ignore the defaults set for &lt;code&gt;&amp;lt;mat-expansion-panel&amp;gt;&lt;/code&gt; and apply the configuration that we provided. Remember that this will affect all expansion panels used in this module.  &lt;/p&gt;

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

&lt;p&gt;We covered what an Angular Module is and the basic parts that make up Angular Modules. Hopefully this article was helpful and that you understand modules just a little more. &lt;/p&gt;

&lt;p&gt;There are lots of other topics to cover in Angular Modules and I aim to tackle them all. So stay tuned and thanks for reading! ❤️&lt;/p&gt;

</description>
      <category>angular</category>
      <category>javascript</category>
      <category>typescript</category>
    </item>
    <item>
      <title>Angular Material Handbook: Setup and Basics</title>
      <dc:creator>Bearded JavaScripter</dc:creator>
      <pubDate>Sat, 30 May 2020 00:30:13 +0000</pubDate>
      <link>https://dev.to/qarunqb/angular-material-handbook-setup-and-basics-3ie0</link>
      <guid>https://dev.to/qarunqb/angular-material-handbook-setup-and-basics-3ie0</guid>
      <description>&lt;p&gt;If you're an Angular developer, chances are that you've heard of &lt;a href="https://material.angular.io/" rel="noopener noreferrer"&gt;Angular Material&lt;/a&gt;. If not, then that's okay, I'm here to guide you. &lt;/p&gt;

&lt;p&gt;Angular Material is a component library that follows the Google &lt;a href="https://material.io/design/guidelines-overview" rel="noopener noreferrer"&gt;Material Design Spec&lt;/a&gt;. Buttons, tabs, form fields and loading spinners are just a few of the many components in this library and it's super easy to develop applications quickly. &lt;/p&gt;

&lt;p&gt;My goal with this series is to walk through each and every single component and show you how to use it, take advantage of its API and customize them for your own applications. We will need to run through a few foundation concepts before hitting the components but I'll try my best to keep it short and sweet. Let's get started!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note that at the time of writing, I'm currently using Angular 9 with its corresponding Angular Material Version. I'll try my best to update these articles as Angular updates.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Hint: All the code for this article can be found &lt;a href="https://github.com/Qarun-Qadir-Bissoondial/angular-material-tutorial/tree/1-setup-and-basic-concepts" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/em&gt; &lt;/p&gt;

&lt;h2&gt;
  
  
  Installation and Setup
&lt;/h2&gt;

&lt;p&gt;Create a new project by running &lt;code&gt;ng new angular-material-tutorial&lt;/code&gt; and &lt;code&gt;cd&lt;/code&gt; into the root directory of the project. Once inside, we need to run &lt;code&gt;ng add @angular/material&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;This will install the necessary npm modules and walks us through some default configuration options. Let's walk through them.&lt;/p&gt;

&lt;h3&gt;
  
  
  Themes
&lt;/h3&gt;

&lt;p&gt;The first question we need to answer is what theme we would like to pick. &lt;/p&gt;

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

? Choose a prebuilt theme name, or "custom" for a custom theme: (Use arrow keys)
❯ Indigo/Pink        [ Preview: https://material.angular.io?theme=indigo-pink ] 
  Deep Purple/Amber  [ Preview: https://material.angular.io?theme=deeppurple-amber ] 
  Pink/Blue Grey     [ Preview: https://material.angular.io?theme=pink-bluegrey ] 
  Purple/Green       [ Preview: https://material.angular.io?theme=purple-green ] 
  Custom 


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

&lt;/div&gt;

&lt;p&gt;Pick any that you like, I'll choose Indigo/Pink as my default. We'll look at custom themes in a later article, I promise! But for now, I'll link the previews below. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://material.angular.io?theme=indigo-pink" rel="noopener noreferrer"&gt;Indigo/Pink&lt;/a&gt;&lt;br&gt;
&lt;a href="https://material.angular.io?theme=deeppurple-amber" rel="noopener noreferrer"&gt;Deep Purple/Amber&lt;/a&gt;&lt;br&gt;
&lt;a href="https://material.angular.io?theme=pink-bluegrey" rel="noopener noreferrer"&gt;Pink/Blue Grey&lt;/a&gt;&lt;br&gt;
&lt;a href="https://material.angular.io?theme=purple-green" rel="noopener noreferrer"&gt;Purple/Green&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It's important to pick a theme that matches the feel of your application or a theme that matches the brand of your company. But more importantly, we need to keep accessibility in mind. Angular Material Themes should all conform to the &lt;a href="https://www.w3.org/WAI/standards-guidelines/wcag/" rel="noopener noreferrer"&gt;WCAG Standards&lt;/a&gt;. These rules are laid out so that people who are differently-abled can still access our applications.&lt;/p&gt;

&lt;p&gt;One example is maintaining an acceptable level of contrast between background and foreground to ensure visibility of content. This is also part of WCAG Compliance and should be adhered to. Failure to adhere to these rules can actually result in a lawsuit in some parts of the world!&lt;/p&gt;

&lt;h3&gt;
  
  
  Angular Material Typography
&lt;/h3&gt;

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

? Choose a prebuilt theme name, or "custom" for a custom theme: Indigo/Pink        [ Preview: https://material.angular.io?theme=indigo-pink ]
? Set up global Angular Material typography styles? (y/N) y


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

&lt;/div&gt;

&lt;p&gt;Unless you have your own Typography System, you'll want to select yes for this setting. This gives us access to a lot of classes responsible for spacing, shadows, animations, etc. By default, Angular Material does not apply any global CSS but these will be applied within components and to child elements of a parent bearing the &lt;code&gt;.mat-typography&lt;/code&gt; class. You can find out more &lt;a href="https://material.angular.io/guide/typography" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Browser Animations
&lt;/h3&gt;

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

 Choose a prebuilt theme name, or "custom" for a custom theme: Indigo/Pink        [ Preview: https://material.angular.io?theme=indigo-pink ]
? Set up global Angular Material typography styles? Yes
? Set up browser animations for Angular Material? (Y/n) y


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

&lt;/div&gt;

&lt;p&gt;You'll want to select yes for this one as well as most of Angular Material UX interactions rely on animations. It follows the &lt;a href="https://material.io/design/motion/understanding-motion.html#principles" rel="noopener noreferrer"&gt;Material Design Guidelines on Motion&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Finishing Installation
&lt;/h3&gt;

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

UPDATE src/app/app.module.ts (502 bytes)
UPDATE angular.json (3972 bytes)
UPDATE src/index.html (526 bytes)
UPDATE src/styles.scss (181 bytes)


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

&lt;/div&gt;

&lt;p&gt;At this point, the installation is complete and the above files have been altered. Here's a brief summary:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Material Design Icons and the Roboto Font (at font weights 300, 400 and 500) has been added to your &lt;code&gt;index.html&lt;/code&gt;. You can add more of these weights if needed. Weights are in multiples of 100 and ranges 100-900. &lt;code&gt;.mat-typography&lt;/code&gt; has also been applied to your body.&lt;/li&gt;
&lt;li&gt;Default styles have been added to your &lt;code&gt;styles.scss&lt;/code&gt; file&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;.css&lt;/code&gt; theme that we selected earlier has been inserted into &lt;code&gt;angular.json&lt;/code&gt; under the &lt;code&gt;styles&lt;/code&gt; section of our application &lt;code&gt;build&lt;/code&gt; and &lt;code&gt;test configuration&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;package.json&lt;/code&gt; and &lt;code&gt;package-lock.json&lt;/code&gt; were updated with the installed packages.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now we can run &lt;code&gt;ng serve&lt;/code&gt; and open our browser on &lt;code&gt;localhost:4200&lt;/code&gt; to see our Angular Application. If you've reached this far without any errors, then you've sucessfully added Angular Material to your project 😄&lt;/p&gt;

&lt;h2&gt;
  
  
  Templating Basics
&lt;/h2&gt;

&lt;p&gt;Angular gives us access to the properties of an HTML Element by allowing us to create a Template Reference. This reference can now be accessed directly by our HTML just like a component variable or inside the component itself using the &lt;code&gt;@ViewChild&lt;/code&gt; decorator.&lt;/p&gt;

&lt;p&gt;We won't need to use &lt;code&gt;@ViewChild&lt;/code&gt; unless necessary but I wanted to illustrate how to use Template Referencing to our advantage. It's also extremely vital knowledge to have when working with Angular Material.&lt;/p&gt;

&lt;p&gt;Let's say we have a counter component. This component starts at 0 and shows the number by default. We have the option to show, hide and increment the number. But we want parent components to control these actions. I used inline styles and template as the component was small.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&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="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-counter&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="s2"&gt;`
  &amp;lt;h2&amp;gt;My Counter - {{showNumber ? "shown" : "hidden"}}&amp;lt;/h2&amp;gt;
  &amp;lt;p *ngIf = 'showNumber'&amp;gt;{{num}}&amp;lt;/p&amp;gt;`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;styles&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;:host { text-align: center; }&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;CounterComponent&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

  &lt;span class="nl"&gt;num&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;showNumber&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&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;increment&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="nx"&gt;num&lt;/span&gt;&lt;span class="o"&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;show&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="nx"&gt;showNumber&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nf"&gt;hide&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="nx"&gt;showNumber&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; 
  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nf"&gt;toggle&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="nx"&gt;showNumber&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&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;showNumber&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 let's take a look at our &lt;code&gt;app.component.ts&lt;/code&gt;. I also used inline styles and template here since there isn't much code.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&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="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-root&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="s2"&gt;`
    &amp;lt;h1&amp;gt;Angular-Material-Tutorial&amp;lt;/h1&amp;gt;

    &amp;lt;app-counter #counter&amp;gt;&amp;lt;/app-counter&amp;gt;
    &amp;lt;button (click) = 'counter.increment()'&amp;gt;Add number&amp;lt;/button&amp;gt;
    &amp;lt;button (click) = 'counter.show()'&amp;gt;Show Counter&amp;lt;/button&amp;gt;
    &amp;lt;button (click) = 'counter.hide()'&amp;gt;Hide Counter&amp;lt;/button&amp;gt;
  `&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;styles&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="s2"&gt;`h1 {
      text-align: center;
      padding: 20px;
    }`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;`button {
      border: 0;
      outline: 0;
      padding: 10px;
      border-radius: 8px;
      display: block;
      margin: 10px auto;
  }`&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;AppComponent&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;


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

&lt;/div&gt;

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

&lt;p&gt;Notice how our app component has absolutely no code inside the class. Not even a constructor! Yet, if we click on the Add Number button, it will increment the count, and our Show and Hide Counter buttons will also work as expected. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;We managed to access the properties and methods of the &lt;code&gt;CounterComponent&lt;/code&gt; through the &lt;code&gt;AppComponent&lt;/code&gt; because of Template Referencing&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;When we say &lt;code&gt;&amp;lt;app-counter #counter&amp;gt;&amp;lt;/app-counter&amp;gt;&lt;/code&gt;, we are accessing this particular instance of the &lt;code&gt;CounterComponent&lt;/code&gt; class. And since the properties and methods boil down to plain JavaScript Objects, we can access them as we do with regular objects (&lt;code&gt;counter.increment()&lt;/code&gt; for example). Any property or instance marked as private cannot be accessed through Template Referencing. &lt;/p&gt;

&lt;p&gt;This simple, but powerful, feature of Angular allows us to accomplish useful tasks without needed to bloat our TypeScript Class. Additionally, this is the very technique that we will use to access many of the properties of the Angular Material Components in order to take full advantage of them.&lt;/p&gt;

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

&lt;p&gt;In this article, we gained an understanding of what Angular Material is, added Angular Material to our project and learned about Template Referencing and how it relates to Angular Material. &lt;/p&gt;

&lt;p&gt;In the next article, we'll get started on simple components from the &lt;a href="https://material.angular.io/components/categories/buttons" rel="noopener noreferrer"&gt;Buttons and Indicators Section&lt;/a&gt;. Thanks a lot for reading! 😄&lt;/p&gt;

</description>
      <category>angular</category>
      <category>javascript</category>
      <category>typescript</category>
    </item>
    <item>
      <title>TDD in Angular - Dependency Injection and Mocking</title>
      <dc:creator>Bearded JavaScripter</dc:creator>
      <pubDate>Thu, 28 May 2020 01:36:22 +0000</pubDate>
      <link>https://dev.to/qarunqb/tdd-in-angular-dependency-injection-and-mocking-4jnh</link>
      <guid>https://dev.to/qarunqb/tdd-in-angular-dependency-injection-and-mocking-4jnh</guid>
      <description>&lt;p&gt;In our last article &lt;a href="https://dev.to/qarunqb/tdd-in-angular-understanding-an-angular-unit-test-jja"&gt;here&lt;/a&gt;, we went through the basic structure of an Angular Unit Test and went on to test services. In this article, I want to show how to connect your service to a component and how to properly test this from a Test-Driven Development perspective.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Code for this article can be found &lt;a href="https://github.com/Qarun-Qadir-Bissoondial/ng-jasmine-testing-demo/tree/dependency-injection-and-mocking" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Let's get started!&lt;/p&gt;

&lt;h2&gt;
  
  
  Creation of an inventory component
&lt;/h2&gt;

&lt;p&gt;Let's say we want to create a component that takes our &lt;code&gt;inventoryCount&lt;/code&gt; from the Inventory Service and displays it, as well as increase and decrease the count. This means that &lt;code&gt;InventoryService&lt;/code&gt; is a dependency of  &lt;code&gt;InventoryComponent&lt;/code&gt;. In Angular, we inject dependencies through the constructor.&lt;/p&gt;

&lt;p&gt;Therefore, we'll need to inject our &lt;code&gt;InventoryService&lt;/code&gt; through the constructor of our &lt;code&gt;InventoryComponent&lt;/code&gt; to have access to the methods.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;em&gt;I know there are better ways to update a count in a service and bind it to a component (such as using an Observable). This is just to illustrate a concept.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Whenever we bring in dependencies into components, we should always make sure that those services are tested first so that they behave as expected. Our &lt;code&gt;InventoryService&lt;/code&gt; was tested in the previous article so it's safe for us to use it now.&lt;/p&gt;

&lt;p&gt;The logic for this component is beyond simple but there's still a key concept of testing that it covers. We don't need to re-test the service code in this component, but &lt;em&gt;we do need to make sure that it is called when needed&lt;/em&gt;. &lt;/p&gt;

&lt;p&gt;Let's focus on the component test and run through what the auto-generate code means. Remember we can focus on a test suite by using &lt;code&gt;fdescribe&lt;/code&gt; (focused describe) and focus on a single test using &lt;code&gt;fit&lt;/code&gt; (focused it).&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;We see that an instance of the component is created and a &lt;code&gt;fixture&lt;/code&gt; is set up to house the component instance. This also gives us access to component life cycle methods and a DOM that we can use during our unit tests. You can read more about fixtures &lt;a href="https://angular.io/api/core/testing/ComponentFixture" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;TestBed.createComponent(InventoryComponent)&lt;/code&gt; instantiates the component, which means that the constructor code is immediately executed along with all the component life cycle hooks implemented by that component. &lt;code&gt;fixture.detectChanges()&lt;/code&gt; is responsible for any updates made to the component. It syncs any component variables bound to the DOM. On the first time that it is is run, it runs &lt;code&gt;ngOnChanges()&lt;/code&gt; and &lt;code&gt;ngOnInit()&lt;/code&gt; (Thanks &lt;a href="https://dev.to/layzee"&gt;@LayZeeDK&lt;/a&gt; for the correction! ❤️). You can read more about &lt;a href="https://angular.io/api/core/OnChanges" rel="noopener noreferrer"&gt;ngOnChanges&lt;/a&gt; and &lt;a href="https://angular.io/api/core/OnInit" rel="noopener noreferrer"&gt;ngOnInit&lt;/a&gt; on the docs.&lt;/p&gt;

&lt;p&gt;If the component has any dependencies, those are instantiated as well, meaning that their constructor functions are immediately executed. This breaks our concept of unit testing since multiple pieces of code are being brought into this one unit test suite. These dependencies need to be &lt;strong&gt;mocked&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Mocking Dependencies
&lt;/h2&gt;

&lt;p&gt;Typically when mocking a dependency, a dummy class is provided with many of the same methods as the original. These methods do not provide functionality, but they may just return predictable values that we can use for testing purposes. &lt;/p&gt;

&lt;p&gt;For example, you may want to mock network calls, return a known value and see if your components and services behave as they should. You may want to willingly return errors from mock services to see if your application handles errors gracefully. You can even mock Angular features such as the Router.&lt;/p&gt;

&lt;p&gt;All this is necessary to isolate the piece of code to be tested. Otherwise, when a test fails we won't know if a dependency or the code in question caused it, which leads to many wasted hours and a poorly designed codebase.&lt;/p&gt;

&lt;p&gt;Let's create a &lt;code&gt;MockInventoryService&lt;/code&gt; and supply that in place of our  &lt;code&gt;InventoryService&lt;/code&gt; in the component unit test. We know that the service is already tested, so if any tests fail, the bad code &lt;em&gt;has&lt;/em&gt; to be in our component.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Notice how our &lt;code&gt;incrementCount&lt;/code&gt; and &lt;code&gt;decrementCount&lt;/code&gt; are basically No-ops. Because the logic of this service is so simple, we just want to test if these functions are going to be called in our component. If the methods of the mock service are called in the unit test then it is safe to assume that the actual methods of the real service are called in the component during normal execution. &lt;/p&gt;

&lt;p&gt;We need to tell our component unit test to replace the injected &lt;code&gt;InventoryService&lt;/code&gt; with the &lt;code&gt;MockInventoryService&lt;/code&gt;. This is done in the &lt;code&gt;providers&lt;/code&gt; array in the module setup of the component test as follows: &lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Now, whenever &lt;code&gt;incrementCount&lt;/code&gt; is called in the component during the unit test, the method from the mock service will be invoked instead.&lt;/p&gt;

&lt;h2&gt;
  
  
  Writing our tests
&lt;/h2&gt;

&lt;p&gt;In order for us to tell when a method has been called on a service or not, we need to spy on that method. Jasmine can tell us when a function has been invoked, what the parameters were and what the return value was. &lt;br&gt;
This is useful for us to test our component. &lt;/p&gt;

&lt;p&gt;When &lt;code&gt;increment()&lt;/code&gt; is called in the component, we expect that &lt;code&gt;incrementCount()&lt;/code&gt; is called in the service. Similarly, when &lt;code&gt;decrement()&lt;/code&gt; is called in the component, we expect that &lt;code&gt;decrementCount()&lt;/code&gt; is called in the service. Let's set up our Jasmine spies and write our tests.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;We set up our spies at the very beginning of our test suite and instantiated them after we got a hold of the service from &lt;code&gt;TestBed.inject&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;expect(incrementSpy).toHaveBeenCalled()&lt;/code&gt; tests whether or not the function being spied upon was called during the test.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fhvxog3zdtzqh91c1erog.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fhvxog3zdtzqh91c1erog.png" alt="Jasmine Spies Tests Passing"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;In this article, we covered the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How to inject dependencies into components&lt;/li&gt;
&lt;li&gt;The auto-generated unit test of a component&lt;/li&gt;
&lt;li&gt;Producing a mock service&lt;/li&gt;
&lt;li&gt;Providing the mock service to the component&lt;/li&gt;
&lt;li&gt;Spying on functions inside that service.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Hopefully this article was useful to you. There are lots more to learn about mocking and test strategies in Angular and I aim to cover them all.&lt;br&gt;
Thanks a lot for reading! &lt;/p&gt;

</description>
      <category>angular</category>
      <category>javascript</category>
      <category>tdd</category>
    </item>
    <item>
      <title>My Back-end Adventures - Setting up a local database (Postgres)</title>
      <dc:creator>Bearded JavaScripter</dc:creator>
      <pubDate>Sun, 10 May 2020 22:11:08 +0000</pubDate>
      <link>https://dev.to/qarunqb/my-back-end-adventures-setting-up-a-local-database-postgres-508i</link>
      <guid>https://dev.to/qarunqb/my-back-end-adventures-setting-up-a-local-database-postgres-508i</guid>
      <description>&lt;p&gt;One of the things I really wanted to get started with was setting up a local database for my Express servers. While using an in-memory data object inside of Express is fine, I wanted something a little closer to a real project. &lt;/p&gt;

&lt;p&gt;So I decided to have a local instance of Postgres running. What better way than to host your database inside of a docker container? It eliminates all the complexity of installing Postgres on your system itself and it's easy to get up and running.&lt;/p&gt;

&lt;p&gt;In this article, I'll run through how to set up a local postgres database inside a docker container and connect your Express back end to it using Sequelize. Let's go! &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;TL;DR: All the code can be found &lt;a href="https://github.com/Qarun-Qadir-Bissoondial/express-postgres-docker"&gt;here&lt;/a&gt;.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting up Docker
&lt;/h2&gt;

&lt;p&gt;First thing you'll wanna do is to &lt;a href="https://docs.docker.com/get-docker/"&gt;install docker&lt;/a&gt; on your system as well as &lt;a href="https://docs.docker.com/compose/install/"&gt;docker-compose&lt;/a&gt;. Docker is a runtime that allows us to run software inside of containers regardless of operating system. That means no frustrating installs and no OS-specific instructions. It works for all! 😄&lt;/p&gt;

&lt;p&gt;Once Docker and docker-compose is on your system and ready to go, you'll need to create a &lt;code&gt;docker-compose.yml&lt;/code&gt; file. This is a config file for docker-compose that can spin up multiple containers and lets them communicate. Copy and paste the following code into the &lt;code&gt;docker-compose.yml&lt;/code&gt;:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The config file does a couple things for us:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Creates 2 services (containers) for us called &lt;code&gt;postgres&lt;/code&gt; and &lt;code&gt;pgadmin&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Sets the default username and password for us to access externally&lt;/li&gt;
&lt;li&gt;Maps the corresponding ports. &lt;code&gt;5432:5432&lt;/code&gt; on the postgres container means that port 5432 on the host machine is mapped to 5432 inside the docker container. The same logic applies for &lt;code&gt;5050:80&lt;/code&gt; for pgadmin&lt;/li&gt;
&lt;li&gt;Creates volumes for our containers. Even if our container is shut down, our data will persist. &lt;code&gt;postgres:/data/postgres&lt;/code&gt; means the postgres folder in our local machine is mapped to the &lt;code&gt;/data/postgres&lt;/code&gt; folder inside the docker container&lt;/li&gt;
&lt;li&gt;Creates a common network called &lt;code&gt;postgres&lt;/code&gt; so that our 2 containers can communicate with each other.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Running the Docker containers and connecting PgAdmin to Postgres
&lt;/h2&gt;

&lt;p&gt;Run &lt;code&gt;docker-compose up&lt;/code&gt; in your terminal. Your computer will download the specified versions of the containers before activating them.&lt;/p&gt;

&lt;p&gt;Your console output should look something like the following. I'm running an ubuntu system but it should pretty much be the same thing&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Once this is up and running, go to &lt;code&gt;localhost:5050&lt;/code&gt; in your browser and you should be greeting with the PgAdmin login screen. This is the administrative interface for Postgres. While this is not necessary to have, it's still pretty cool.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--53Is2TnL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ewl59tazkt2sd9wtj0z2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--53Is2TnL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ewl59tazkt2sd9wtj0z2.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Log using the &lt;code&gt;PGADMIN_DEFAULT_EMAIL&lt;/code&gt; and the &lt;code&gt;PGADMIN_DEFAULT_PASSWORD&lt;/code&gt; from the &lt;code&gt;docker-compose.yml&lt;/code&gt; file. In my example, these are &lt;code&gt;pgadmin4@pgadmin.com&lt;/code&gt; and &lt;code&gt;admin&lt;/code&gt; respectively.&lt;/p&gt;

&lt;p&gt;Let's just recap what we did so far. We created 2 docker containers, one for Postgres and one for PgAdmin. We then launched those 2 containers and connected to PgAdmin through &lt;code&gt;localhost:5050&lt;/code&gt; in the browser. Now we're going to connect PgAdmin to the Postgres container.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GgAotL6t--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/q77w2c5eqsq981kgfmv2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GgAotL6t--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/q77w2c5eqsq981kgfmv2.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Create a new server by navigating to the right and right-clicking Servers &amp;gt; Create &amp;gt; Server.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Lznijg_y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/aafla0flu0o9l8h022sw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Lznijg_y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/aafla0flu0o9l8h022sw.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Give your server a name. I'll call mine &lt;code&gt;express-postgres-docker&lt;/code&gt;. Then, navigate to the Connection tab. Fill out the details so it looks like the following: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--uR47H3d3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/dhk14axnmwk8m5l9660d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--uR47H3d3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/dhk14axnmwk8m5l9660d.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's run through what's happening:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Host name/address is &lt;code&gt;postgres&lt;/code&gt;. This field is asking us for the name or IP address of the machine that the Postgres database is located. Since we're using Docker, the container is considered a machine of its own with a name and address. The name is &lt;code&gt;postgres&lt;/code&gt; as specified by our &lt;code&gt;docker-compose.yml&lt;/code&gt; file. If we were connecting to a locally installed version of Postgres without Docker, it would have been &lt;code&gt;localhost&lt;/code&gt; since the database would be located on your local machine.&lt;/li&gt;
&lt;li&gt;Port is &lt;code&gt;5432&lt;/code&gt;. This is the standard Postgres port. Since both Docker container are using the same network, they can see each other's port withouth having to use different IP addresses&lt;/li&gt;
&lt;li&gt;Maintenance Database is &lt;code&gt;postgres&lt;/code&gt;. This is standard and should not be altered.&lt;/li&gt;
&lt;li&gt;Username is &lt;code&gt;postgres&lt;/code&gt;. This was specified as &lt;code&gt;POSTGRES_USER&lt;/code&gt; in the &lt;code&gt;docker-compose.yml&lt;/code&gt; file. If you changed it, then you need to specify that change in this field.&lt;/li&gt;
&lt;li&gt;Password is &lt;code&gt;password&lt;/code&gt;. This was specified as &lt;code&gt;POSTGRES_PASSWORD&lt;/code&gt; in the &lt;code&gt;docker-compose.yml&lt;/code&gt; file. If you changed it, then you need to specify that change in this field.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Once you hit save, PgAdmin should be connected to your Postgres container. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ZaSDJpX1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/560rzm8qn45filgizjs7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ZaSDJpX1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/560rzm8qn45filgizjs7.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting up your Express server
&lt;/h2&gt;

&lt;p&gt;We'll need to install a few packages to get started. Our dependencies are going to be:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;express&lt;/li&gt;
&lt;li&gt;pg&lt;/li&gt;
&lt;li&gt;pg-hstore&lt;/li&gt;
&lt;li&gt;sequelize&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Once installed, you'll need to create an &lt;code&gt;app.js&lt;/code&gt; that looks like the following: &lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Run &lt;code&gt;node app.js&lt;/code&gt; in your terminal and you should get the following output if your connection was successful:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Listening on port 5000
Executing (default): SELECT 1+1 AS result
Database connected!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;Congratulations! You just set up Postgres and PgAdmin in Docker containers and connected it to an Express Sequelize Project. Now you can use this to actually store data for your projects or use it as a platform to start learning more about Database and Database Caching. Happy Coding! 😄&lt;/p&gt;

</description>
      <category>node</category>
      <category>javascript</category>
      <category>sql</category>
    </item>
    <item>
      <title>My Back-end Adventures - Intro</title>
      <dc:creator>Bearded JavaScripter</dc:creator>
      <pubDate>Sat, 09 May 2020 19:05:31 +0000</pubDate>
      <link>https://dev.to/qarunqb/my-back-end-adventures-intro-3poo</link>
      <guid>https://dev.to/qarunqb/my-back-end-adventures-intro-3poo</guid>
      <description>&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;: This series is about my journey into the world of Back-end Development. Every milestone, small accomplishment, frustration and major achievement is going to be documented in this series. I'll include a few tutorial articles to run through set ups that I found challenging to understand so that others after me can have an easier time. Enjoy! 😄 &lt;/p&gt;




&lt;p&gt;Just until recently (at the time of publishing this article), I decided to dive into the world of Back end Web Development. I already had a lot of experience on the front end and a deep understanding of Angular. But I wanted to change my Web Development focus slightly as well as my career. &lt;/p&gt;

&lt;p&gt;There were a lot of choices laid out in front of me such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Python Flask &lt;/li&gt;
&lt;li&gt;Node.js/Express&lt;/li&gt;
&lt;li&gt;Golang&lt;/li&gt;
&lt;li&gt;PHP&lt;/li&gt;
&lt;li&gt;Elixir (This one is pretty cool!)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Eventually I settled on Node.js/Express since I was already familiar with JavaScript and already had done one or two really small Express projects on my own. Plus I really like JavaScript modern syntax.&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://www.100daysofcode.com/"&gt;#100DaysOfCode&lt;/a&gt; challenge seemed like a really good way to get me going. I've always had problems with motivation and keeping the discipline to stick with a habit so it was a good fit. &lt;/p&gt;

&lt;p&gt;I laid out a bunch of topics to cover and projects along the way so that I don't get stuck in tutorial hell. Here are some of those topics:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Beginner

&lt;ul&gt;
&lt;li&gt;Node.js Express basics&lt;/li&gt;
&lt;li&gt;Routing&lt;/li&gt;
&lt;li&gt;MiddleWare&lt;/li&gt;
&lt;li&gt;View/Templating Engines&lt;/li&gt;
&lt;li&gt;SQL ORM (Sequelize)&lt;/li&gt;
&lt;li&gt;NoSQL ORM (Mongoose)&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Intermediate

&lt;ul&gt;
&lt;li&gt;Security, Passwords and OAuth&lt;/li&gt;
&lt;li&gt;Caching&lt;/li&gt;
&lt;li&gt;State Management&lt;/li&gt;
&lt;li&gt;More Database management&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Advanced

&lt;ul&gt;
&lt;li&gt;Node.js Core Modules&lt;/li&gt;
&lt;li&gt;Web Sockets&lt;/li&gt;
&lt;li&gt;NginX&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are probably some more that I missed but I'll get to them along the way.  So far I've only covered some of the basics but I learned so much that I'm excited to keep going. Can't wait to see what else I learn! &lt;/p&gt;

</description>
      <category>node</category>
      <category>javascript</category>
      <category>sql</category>
    </item>
    <item>
      <title>TDD in Angular - Understanding an Angular Unit Test</title>
      <dc:creator>Bearded JavaScripter</dc:creator>
      <pubDate>Tue, 21 Apr 2020 22:23:39 +0000</pubDate>
      <link>https://dev.to/qarunqb/tdd-in-angular-understanding-an-angular-unit-test-jja</link>
      <guid>https://dev.to/qarunqb/tdd-in-angular-understanding-an-angular-unit-test-jja</guid>
      <description>&lt;p&gt;In my &lt;a href="https://dev.to/qarunqb/tdd-in-angular-the-basics-of-general-testing-2bfb"&gt;previous post&lt;/a&gt;, I talked about the Basics of General Testing. It was a short introduction into the different types of testing and how to approach testing in Jasmine. &lt;/p&gt;

&lt;p&gt;In this article, I want to take a look at the auto-generated unit test files of an Angular Application and explain what's happening. I'll unit test services for my examples since it's a good place to start understanding the fundamentals. Also, Angular Components have a bit more going on under the hood and that requires an article of its own.&lt;/p&gt;

&lt;p&gt;Many Angular developers get confused and even overwhelmed by what's happening in a &lt;code&gt;.spec.ts&lt;/code&gt; file. This article will change that.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Code for this article can be found &lt;a href="https://github.com/Qarun-Qadir-Bissoondial/ng-jasmine-testing-demo/tree/understanding-an-angular-unit-test" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Angular Services
&lt;/h3&gt;

&lt;p&gt;Services are by far the easiest to unit test. They're simply Injectable classes that may or may not have some state and contain a collection of functions. However, they shouldn't be taken lightly. &lt;strong&gt;Your services are where all your business logic should be.&lt;/strong&gt; Therefore, testing them as much as you can will prove useful. &lt;/p&gt;

&lt;p&gt;Let's take a look at an InventoryService and its auto-generated &lt;code&gt;.spec.ts&lt;/code&gt; file: &lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;There's a few points to note here.&lt;/p&gt;

&lt;p&gt;There's a &lt;code&gt;describe&lt;/code&gt; block that groups all our tests together for this service. A variable called &lt;code&gt;service&lt;/code&gt; is also initialized.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;beforeEach&lt;/code&gt; block contains code that is run before every single unit test in this spec file. It helps us have a clean slate before running each test so that previous tests don't interfere. This helps with the essence of unit testing (testing one thing in isolation without any external factors).&lt;br&gt;
&lt;em&gt;There are other blocks of code like this to help us maintain clean tests, namely &lt;code&gt;beforeAll&lt;/code&gt;, &lt;code&gt;afterEach&lt;/code&gt; and &lt;code&gt;afterAll&lt;/code&gt;. You can read more about them and more pretty cool testing tools in the &lt;a href="https://jasmine.github.io/api/3.3/global" rel="noopener noreferrer"&gt;Jasmine Global API&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Looking inside the &lt;code&gt;beforeEach&lt;/code&gt; block, we see 2 unfamiliar pieces of code. &lt;code&gt;TestBed.configureTestingModule&lt;/code&gt; creates a dummy module for us to work with. &lt;code&gt;TestBed.inject&lt;/code&gt; initializes our service and injects it into that dummy module. This holds true for components, pipes, guards, etc. This is the unit testing philosophy combined with Angular's architecture. &lt;/p&gt;

&lt;p&gt;An Angular application must have at least one module so a dummy module is created with only the piece of code being tested (in this case, the service) and nothing else. This way, nothing else from the outside can interfere with the tests. Pure Isolation. &lt;/p&gt;

&lt;p&gt;Each &lt;code&gt;it&lt;/code&gt; block of code is a unit test. &lt;code&gt;it&lt;/code&gt; is a function that accepts 2 arguments: A string describing the test and a function that must contain an &lt;code&gt;expect&lt;/code&gt; assertion function. This &lt;code&gt;expect&lt;/code&gt; function is what Jasmine runs to assert expected values against actual results.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;it('should be created'), () =&amp;gt; {...}&lt;/code&gt; is a unit test that is always created for any component, pipe, service, etc. It doesn't make sense looking at other unit tests if we can't initialize our code in the first place.&lt;/p&gt;
&lt;h3&gt;
  
  
  Building our Service
&lt;/h3&gt;

&lt;p&gt;Let's say that I wanted to add some code to track the number of items in my inventory and a way to increment and decrement the amount.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Remember! We're approaching this from a TDD standpoint. We can write empty placeholders and then Tests first!&lt;/strong&gt;&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;We write tests for what we want our code to do and then consider other cases. Tests for main functionality should look something like this:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;You'll notice I used &lt;code&gt;fdescribe&lt;/code&gt; as opposed to &lt;code&gt;describe&lt;/code&gt;. This means "Focused Describe" and Jasmine will only run this suite of tests instead of all tests in the Application. &lt;/p&gt;

&lt;p&gt;When we run our unit tests, we'll notice some failures (as expected). &lt;/p&gt;

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

&lt;p&gt;It's telling us that the "should increment the count" and "should decrement the count" tests are failing. This is expected since we didn't write any code in there as yet. Let's change that.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;And now our tests are passing: &lt;/p&gt;

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

&lt;p&gt;The more astute among you might have realized that our decrement function isn't finished. We shouldn't be able to decrement if the &lt;code&gt;inventoryCount&lt;/code&gt; is already 0.&lt;/p&gt;

&lt;p&gt;A reasonable test for this can be:&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 not decrement when count is 0&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="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;service&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;inventoryCount&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toBe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;service&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;decrementCount&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;service&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;inventoryCount&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toBe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&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;Re-running the tests gives us:&lt;/p&gt;

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

&lt;p&gt;Our function currently decrements whatever value is stored in the service. We want it to decrement only when the value isn't 0. This is also a simple way to make sure that &lt;code&gt;inventoryCount&lt;/code&gt; never falls below 0 (assuming you want your system like that).&lt;/p&gt;

&lt;p&gt;We can modify the function to be:&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;decrementCount&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;inventoryCount&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;inventoryCount&lt;/span&gt;&lt;span class="o"&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 our tests are passing again.&lt;/p&gt;

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

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;In this article, we covered the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The basic structure of an Angular Unit Test&lt;/li&gt;
&lt;li&gt;What happens in the &lt;code&gt;beforeEach&lt;/code&gt; block&lt;/li&gt;
&lt;li&gt;Why a dummy module is necessary for testing&lt;/li&gt;
&lt;li&gt;Building an Angular Service using TDD&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are a lot more of these articles to come where we'll dive deeper into services, mocking, component life cycles, etc. Stay tuned for more and thanks a bunch for reading! 😄&lt;/p&gt;

</description>
      <category>angular</category>
      <category>javascript</category>
      <category>testing</category>
      <category>jasmine</category>
    </item>
    <item>
      <title>TDD in Angular - The Basics of General Testing</title>
      <dc:creator>Bearded JavaScripter</dc:creator>
      <pubDate>Sun, 19 Apr 2020 00:32:02 +0000</pubDate>
      <link>https://dev.to/qarunqb/tdd-in-angular-the-basics-of-general-testing-2bfb</link>
      <guid>https://dev.to/qarunqb/tdd-in-angular-the-basics-of-general-testing-2bfb</guid>
      <description>&lt;p&gt;You've all hear the crowd harping about how great testing is. But has anyone ever told you what testing is? Or what it means? Or how to even approach testing?&lt;/p&gt;

&lt;p&gt;Don't worry, young padawan. I'll guide you through the basics of testing, the different types of testing and how to code in a Test-Driven Development style in Angular. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;Hint: You can find all the completed code for this article &lt;a href="https://stackblitz.com/edit/jasmine-testing-demo" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Why bother to test?
&lt;/h3&gt;

&lt;p&gt;Consider this: If you're a car manufacturer, are you going to sell cars when you don't know whether or not it works? If you're a good car dealer then of course you'll make sure that it works in all the expected conditions. Why is software any different?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Testing gives developers the confidence that our code will work 100% of the time as expected in the expected conditions.&lt;/strong&gt; If not, then at least our code can &lt;em&gt;fail gracefully&lt;/em&gt; (more on that later). &lt;a href="https://www.youtube.com/watch?v=BSaAMQVq01E" rel="noopener noreferrer"&gt;Here's a really nice (but slightly long) video on how TDD relates to good code and professionalism&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Types of Tests
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Regression Testing&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You've actually unconsciously done this type of testing in all the code that you've written! Regression testing formally refers to checking if changes to one part of the code has affected any other parts. &lt;/p&gt;

&lt;p&gt;We might unprofessionally know this as making a change, seeing something else break and following the breadcrumbs of broken code until everything works. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Unit Testing&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This type of testing will make up at least 65% of your test suite. It's focused on testing individual components. When I say "components" here, I don't mean Angular or React components, I'm just referring to single, small, individual pieces of logic. &lt;/p&gt;

&lt;p&gt;That doesn't mean we're going to test each and every function but we test those pieces of code that are most important (which are usually those focused around business logic).&lt;/p&gt;

&lt;p&gt;So for example, in an Inventory Management System, we'll want a test to ensure discounts are applied to certain items. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Integration Testing&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We know our individual components work individually, but we also need to make sure they don't break when we put them together. This is what Integration Tests are for.&lt;/p&gt;

&lt;p&gt;In our Inventory Management System, we'll want tests to make sure that a restocking order is places when inventory on a certain item falls below a certain amount. These tests may combine Inventory Counts and an Ordering System.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;End-to-End (e2e) Testing&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The applications we write usually have a start point (for example, a login) and an endpoint (for example, a purchase). Testing our apps from start to finish (or from &lt;strong&gt;end to end&lt;/strong&gt;) is critical as this is as close to real-world usage as automated testing can get.&lt;/p&gt;

&lt;p&gt;You'll want to take more customer-driven scenarios into these tests such as navigation within the app to ensure the user is still authenticated or if animations and error messages pop up after certain actions.&lt;/p&gt;

&lt;p&gt;There are probably more types of tests but these mentioned are the most common.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to prepare yourself for Test-Driven Development
&lt;/h3&gt;

&lt;p&gt;Test-Driven Development simply means to write our tests before we write our code. Since most of us haven't grown up with TDD in mind, it sounds pretty absurd. Why write the tests first when there's no code to start with?&lt;/p&gt;

&lt;p&gt;The reason is that it keeps us very focused on what the code is supposed to do and nothing more. In a way, we subconsciously do this when we write our code, but we don't put down our thoughts into tests.&lt;/p&gt;

&lt;p&gt;We usually start off with what the code is supposed to do in our heads, write the code in our IDE and then assume it works. Writing out tests gets those initial thoughts out of our heads and into a form that's more concrete.&lt;/p&gt;

&lt;p&gt;Let's do a simple example. We want to write a function that accepts an object, capitalizes the value in the "name" key and returns a new object.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;You can fork &lt;a href="https://stackblitz.com/edit/jasmine-testing" rel="noopener noreferrer"&gt;this StackBlitz repo&lt;/a&gt; and code along.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We'll write an empty function first and then write our tests.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;We know what we want our code to do, so let's write the corresponding test. If you're using Jasmine, the first unit test should look something like this. Remember, we have an empty function so the first test should fail.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;And the resulting failing test: &lt;/p&gt;

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

&lt;p&gt;We expect that the &lt;code&gt;actualResult&lt;/code&gt; should be the same as the &lt;code&gt;expectedResult&lt;/code&gt;. This is the basis of all tests. As long as our expectations match what is actually produced, then our tests will pass.&lt;/p&gt;

&lt;p&gt;Now we can modify the code so that the test passes. &lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


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

&lt;p&gt;We've just done TDD! We thought about what the code needed to do, wrote the test first and then wrote the code to make the test pass. &lt;/p&gt;

&lt;h3&gt;
  
  
  Handling more test cases
&lt;/h3&gt;

&lt;p&gt;Our code above works fine but it assumes that the object:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;is defined&lt;/li&gt;
&lt;li&gt;has a key called "name"&lt;/li&gt;
&lt;li&gt;has a defined value in the key called "name"&lt;/li&gt;
&lt;li&gt;has a string value in the key called "name"&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When writing functions, you may not know where your arguments may come from (perhaps from sources that you cannot easily control such as form data or from an HTTP request). You have to be prepared for a number of cases like those described above so that it is robust. &lt;strong&gt;The more assumptions you make, the more room for error that you leave in your code.&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;Let's throw in some more test cases and see what happens: &lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


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

&lt;p&gt;Our tests are failing again so we know the areas that we need to work on.&lt;br&gt;
I've decided to include a name key with an empty string if the name isn't available. I've also decided to throw an error if the name key in the object is not a string or if the object is falsy. Let's modify the code so that it works in those cases.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;And now all our tests are passing:&lt;/p&gt;

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

&lt;h3&gt;
  
  
  Final Words
&lt;/h3&gt;

&lt;p&gt;Test-Driven Development allows us to write simple, yet robust code. It teaches us to consider many cases upfront as opposed to just the way the code is supposed to work. This way, the code isn't prone to breaking at all, or at least as often. &lt;/p&gt;

&lt;p&gt;Tests also act as a good form of documentation. Running the tests on a codebase and seeing all the test cases gives us a pretty good indication of what the code is supposed to do. &lt;/p&gt;

&lt;p&gt;Unit test descriptions tell us what each piece of logic is supposed to do. Integration tests tell us how pieces are supposed to connect together. End-to-end tests tell us what to expect when using the entire system.&lt;/p&gt;

</description>
      <category>angular</category>
      <category>javascript</category>
      <category>testing</category>
      <category>jasmine</category>
    </item>
    <item>
      <title>Use Pipes in your next Angular Application! (Part 4)</title>
      <dc:creator>Bearded JavaScripter</dc:creator>
      <pubDate>Thu, 09 Apr 2020 14:03:50 +0000</pubDate>
      <link>https://dev.to/qarunqb/use-pipes-in-your-next-angular-application-part-4-50aj</link>
      <guid>https://dev.to/qarunqb/use-pipes-in-your-next-angular-application-part-4-50aj</guid>
      <description>&lt;p&gt;Pipes are a really simple way to transform how data is displayed to the user. Angular comes with lots of built-in pipes for common scenarios (JsonPipe, CurrencyPipe, DatePipe, etc). But what if you need something custom for your project?&lt;/p&gt;

&lt;p&gt;In this article, we'll be exploring how to create your own pipes. I'll run through a simple example that adds the correct order suffix to a number (eg. 1st, 12th, -123rd, etc). Let's dive in!&lt;/p&gt;

&lt;p&gt;(&lt;a href="https://stackblitz.com/edit/ng-pipes-demo"&gt;All code here&lt;/a&gt;)&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating your own Pipes
&lt;/h3&gt;

&lt;p&gt;The first thing you'll need to do is to come up with a suitable name for your pipe. It should reflect what the pipe does. In my case, I'll call the pipe OrderSuffix. Then, run the following command in your terminal.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ng generate pipe OrderSuffix&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This will add your pipe to your declarations array in &lt;code&gt;AppModule&lt;/code&gt;. The generated code should look like this:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The &lt;code&gt;transform&lt;/code&gt; function is responsible for taking the input from the HTML template and producing the output to be displayed. &lt;/p&gt;

&lt;p&gt;We want to be able to add an order suffix based on the number, which is mostly determined by the last digit (least significant digit). So 1 becomes 1st, 2 becomes 2nd, 4 becomes 4th and so forth. It's also helpful to note that we're taking in a number and returning a string.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;This pipe looks pretty good so far. We defined a suffix map for digits ending in 1, 2 and 3, converting them to 1st, 2nd and 3rd respectively. If the last digit is none of these 3, then we assume the suffix is "th" (4th, for example).&lt;/p&gt;

&lt;p&gt;We can demo our pipe inside a simple component as follows: &lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The component displays the array of numbers with their order suffixes. However, if we look at the very last number, there's a problem.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1st
2nd
3rd
14th
-128th
11st
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;We need to convert 11 to 11th, not to 11st. Our current pipe will also convert 12 to 12nd and 13 to 13rd, which aren't correct. Remember that the pipe has to work with larger numbers as well, so for example, 1259239411 should still give 1259239411th.&lt;/p&gt;

&lt;p&gt;Notice how this problem only occurs for 11, 12 and 13 or numbers that end with 11, 12 and 13. We should deal with those cases first before our general code runs.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;Now, our pipe looks for the last 2 digits being 11, 12 or 13 and returns the number with the suffix "th" for all of them. If not, then we run our previous code. Now our output looks a little more accurate.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1st
2nd
3rd
14th
-128th
11th
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Lessen your burden:
&lt;/h3&gt;

&lt;p&gt;If you're writing a custom parsing function just to change how data is displayed, then you're better off putting that parsing function inside a pipe.&lt;/p&gt;

&lt;p&gt;Without using pipes, inside your component would follow this process:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Define/Import your parsing function&lt;/li&gt;
&lt;li&gt;Get the data you want to transform&lt;/li&gt;
&lt;li&gt;Run the data through the function and store the result in a new variable&lt;/li&gt;
&lt;li&gt;Display that new variable&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Whereas with Pipes, we just: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Define the parsing function inside the pipe&lt;/li&gt;
&lt;li&gt;Use the pipe in the component HTML template.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;No extra imports, no extra variables, and the component is &lt;u&gt;&lt;a href="https://dev.to/mquanit/concept-of-smart-dumb-components-in-angular-2fom"&gt;as dumb as possible&lt;/a&gt;&lt;/u&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Now you know how to create your own pipes! &lt;/p&gt;

&lt;p&gt;This is also the end to my 4 part series on Pipes in Angular. There are 2 built-in pipes that I haven't mentioned (&lt;a href="https://angular.io/api/common/I18nPluralPipe"&gt;I18nPluralPipe&lt;/a&gt; and &lt;a href="https://angular.io/api/common/I18nSelectPipe"&gt;I18nSelectPipe&lt;/a&gt;). Now you know enough to explore these pipes on your own and create some of your own! &lt;/p&gt;

&lt;p&gt;Good luck in your Angular journey! 😄&lt;/p&gt;

</description>
      <category>angular</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Use Pipes in your next Angular Application! (Part 3)</title>
      <dc:creator>Bearded JavaScripter</dc:creator>
      <pubDate>Fri, 03 Apr 2020 13:33:38 +0000</pubDate>
      <link>https://dev.to/qarunqb/use-pipes-in-your-next-angular-application-part-3-30nd</link>
      <guid>https://dev.to/qarunqb/use-pipes-in-your-next-angular-application-part-3-30nd</guid>
      <description>&lt;p&gt;The main use of Pipes in Angular is to transform how data is displayed. Dates, currency and JSON objects are just some of the structures that Angular Pipes can work with. &lt;/p&gt;

&lt;p&gt;Check out my &lt;a href="https://dev.to/qarunqb/use-pipes-in-your-next-angular-application-part-1-39mk"&gt;first&lt;/a&gt; and &lt;a href="https://dev.to/qarunqb/use-pipes-in-your-next-angular-application-part-2-3nnm"&gt;second&lt;/a&gt; articles on Pipes where we cover some of the simpler pipes. &lt;/p&gt;

&lt;p&gt;In this article, we'll be moving on to 2 very useful pipes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://angular.io/api/common/DatePipe"&gt;DatePipe&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://angular.io/api/common/AsyncPipe"&gt;AsyncPipe&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;a href="https://angular.io/api/common/DatePipe"&gt;DatePipe&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;This is used for displaying JavaScript Date Objects in a readable format. Angular provides many ways for us to configure the DatePipe, from full or abbreviated day names, from the hours down to the seconds and, of course, varying locales as well (en-US locale is used by default).&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;And the corresponding output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Date without Pipe: Fri Apr 03 2020 08:43:22 GMT-0400 (Venezuela Time)
Date with Pipe: Apr 3, 2020

Showing Dates:
Short Date: 4/3/20
Medium Date: Apr 3, 2020
Long Date: April 3, 2020

Date Formatting with Parameters:
dd/mm/yyyy: 3 Apr 2020
yyyy/mm/dd: 2020 Apr 3

Formatting Times
Short time: 8:43 AM
Medium time: 8:43:22 AM
Long time: 8:43:22 AM GMT-4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;There's loads more examples on the &lt;a href="https://angular.io/api/common/DatePipe"&gt;DatePipe Documentation&lt;/a&gt;. Pre-defined formatting options as well as custom formats.&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;a href="https://angular.io/api/common/AsyncPipe"&gt;AsyncPipe&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Asynchronous Programming is at the very heart of JavaScript. With Angular being built on RxJS and using Observables for HTTP requests, more and more developers end up writing the same code just to extract their data from HTTP Async responses.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;With the AsyncPipe, it can dramatically reduce the above code to the following:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The HTML from the second example is important.&lt;/p&gt;

&lt;p&gt;Line 8 says: &lt;em&gt;"Subscribe to the &lt;code&gt;character&lt;/code&gt; observable and store the response data in &lt;code&gt;char&lt;/code&gt;. If &lt;code&gt;char&lt;/code&gt; is defined, then show the &lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt; tag. Else, show the &lt;code&gt;#loading&lt;/code&gt; template."&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This allows us to set a variable to store the response data straight from the HTML as opposed to declaring it in our Component TypeScript.&lt;/p&gt;

&lt;p&gt;Additionally, notice how in the second example, we didn't need to call subscribe. This is because &lt;em&gt;AsyncPipe automatically subscribes for you&lt;/em&gt;. Generally speaking, we don't need to unsubscribe from HTTP Observables since &lt;u&gt;&lt;a href="https://medium.com/@garfunkel61/unsubscribing-from-angular-httpclient-data-requests-observables-44fc5a4965d5"&gt;Angular auto-unsubscribes for us&lt;/a&gt;.&lt;/u&gt;&lt;/p&gt;

&lt;p&gt;But in general, &lt;em&gt;AsyncPipe automatically unsubscribes Observables on Component Destruction&lt;/em&gt;. That's a lot less for us for think about and a lot less for us to code. &lt;strong&gt;Always leverage the framework as much as possible.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Note that the same works for promises as well :)&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;This article was a short, but powerful, introduction to using DatePipe and AsyncPipe in Angular. DatePipe is used to display dates and can show various formats. AsyncPipe extracts the value from an asynchronous data structure and gives us access to it straight from the HTML.&lt;/p&gt;

&lt;p&gt;Thanks a bunch for reading! Stay tuned for the next Article where I talk about making your own pipes 😄&lt;/p&gt;

</description>
      <category>angular</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
