Book Image

Mastering Angular Components - Second Edition

By : Gion Kunz
Book Image

Mastering Angular Components - Second Edition

By: Gion Kunz

Overview of this book

Mastering Angular Components will help you learn how to invent, build, and manage shared and reusable components for your web projects. Angular components are an integral part of any Angular app and are responsible for performing specific tasks in controlling the user interface. Complete with detailed explanations of essential concepts and practical examples, the book begins by helping you build basic layout components, along with developing a fully functional task-management application using Angular. You’ll then learn how to create layout components and build clean data and state architecture for your application. The book will even help you understand component-based routing and create components that render Scalable Vector Graphics (SVG). Toward the concluding chapters, you’ll be able to visualize data using the third-party library Chartist and create a plugin architecture using Angular components. By the end of this book, you will have mastered the component-based architecture in Angular and have the skills you need to build modern and clean user interfaces.
Table of Contents (12 chapters)

Spying on component output

In testing, a common practice is to spy on function calls during the execution of tests, and then evaluate these calls, checking whether all functions were called correctly.

Jasmine provides us with some nice helpers, in order to use spy function calls. We can use the spyOn function of Jasmine to replace the original function with a spy function. The spy function will record any calls, and we can evaluate how many times they were called, and with what parameters.

Let's look at a simple example of how to use the spyOn function:

class Calculator { 
  multiply(a, b) { 
    return a * b; 
  } 
   
  pythagorean(a, b) { 
    return Math.sqrt(this.multiply(a, a) + this.multiply(b, b)); 
  } 
} 

We will test a simple calculator class that has two methods. The multiply method simply multiplies two numbers and returns the result. The pythagorean method calculates...