Show List
Pipe Testing
Angular Pipes are used to transform the data. Below is the custom pipe we created in the earlier chapter.
cuberoot.pipe.ts
import { Pipe, PipeTransform } from '@angular/core';@Pipe({name: 'cuberoot'})export class CuberootPipe implements PipeTransform {transform(value: number): number {return Math.cbrt(value);}}
Pipes have transform function. To test the pipe, we have to get the instance of class object, call the transform function with a test data and validate the result.
cuberoot.pipe.spec.ts
import { CuberootPipe } from './cuberoot.pipe';describe('CuberootPipe Test Suite', () => {let pipe: CuberootPipe;beforeEach(() => {pipe = new CuberootPipe();});it ('Return correct cuberoot of a number', ()=> {expect(pipe.transform(27)).toBe(3);})});
Here we are getting the instance in the beforeEach function so that it will be called every time the test is called. We can run the "ng test" command to ensure the test is successful.
Source Code:
https://github.com/it-code-lab/angular-application
Leave a Comment