Testing pipes
As we learned in Chapter 4, Enriching Applications Using Pipes and Directives, a pipe is a TypeScript class that implements the PipeTransform interface. It exposes a transform method, which is usually synchronous, which means it is straightforward to test.
Consider the list.pipe.ts file containing a pipe that converts a comma-separated string into a list:
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'list'
})
export class ListPipe implements PipeTransform {
transform(value: string): string[] {
return value.split(',');
}
}
Writing a test is simple. The only thing that we need to do is to instantiate an instance of the ListPipe class and verify the outcome of the transform method with some mock data:
it('should return an array', () => {
const pipe = new ListPipe();
expect(pipe.transform('A,B,C')).toEqual(['A', 'B', 'C']);
});
...