Unit testing ngModel in Angular 4

Angular 4 is a good choice for building modern web applications. It comes bundled with Test utilities that makes it easy to write good quality test cases quite easily. Angular official testing guide does a good job explaining how to get started with testing Angular components. One thing that it does not cover is how to test components that use two way data binding using ngModel directive. Last week I had to write test case for a component that uses data binding so I end up learning a lot about testing such components. In this blog, I will share my learnings with you.

Continue reading “Unit testing ngModel in Angular 4”

Angular 4: Use of base-href and deploy-url build options

I use Angular 4 at work for building a single page application. Today, I faced the problem where I had to use different base paths for router and static assets. Angular makes use of the base href to tell router how to compose navigation URLs. If your application exists at the root, then you can use / as the href value as shown below.

<base href="/">

I wanted to use /users as my application base for router and /public as base for my assets.

If you build the project the Angular project for the production environment using the default values you will get the following index.html generated.

$ ng build -prod
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>SampleNgApp</title>
  <base href="/">

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
<link href="styles.xxxx.bundle.css" rel="stylesheet"/></head>
<body>
  <app-root>Loading...</app-root>
</body>
</html>

This generated page will not work as it router will use / as base for composing navigation URLs and static assets will be looked in the current directory.

To solve my problem, I used —-base-href and —-deploy-url options as shown below.

$ ng build -prod --base-href /users --deploy-url /public
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>SampleNgApp</title>
  <base href="/users">

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
<link href="/public/styles.xxxx.bundle.css" rel="stylesheet"/></head>
<body>
  <app-root>Loading...</app-root>
</body>
</html>

This way I solved my problem.

Teradata Covalent with Angular 4 Compilation Error

Today, I decided use Teradata Covalent components in my Angular 4 application. After adding the dependency, my project stopped compiling. I started getting following errors

ERROR in /Users/dev/app//node_modules/@covalent/core/common/animations/toggle/toggle.directive.d.ts (2,10): Module "/Users/dev/app/node_modules/@angular/animations/animations" has no exported member 'AnimationBuilder'.
ERROR in /Users/dev/app/node_modules/@covalent/core/common/animations/fade/fade.directive.d.ts (2,10): Module "/Users/dev/app/node_modules/@angular/animations/animations:" has no exported member 'AnimationBuilder'.

I tried googling this error but couldn’t find a fix. Then, i looked at the Covalent Github repository to find the Angular version  they are using. It turned out to be Angular version issue. I was using Angular 4.1.3 and Covalent needs Angular version 4.2.0 and above. After making the change, I was able to get my app working.