Angular pipe unit testing is an example of testing a class independently of the Angular test environment. In this example, we test our friend's date pipe class and create the object in the test class:
const pipe = new FriendsDatePipe();
On this object, we will write the following two test cases:
- First, test the green field scenario, where we pass a valid date in milliseconds and test the transformed human-readable date format
- Second, test the edge-case scenario where we pass an invalid date as
-1
, and we expect a string return value as"Invalid Date"
import {FriendsDatePipe} from './friendsdate.pipe'; describe('friendsdatepipe', () => { const pipe = new FriendsDatePipe(); it('Transform dateInMillis to MM/DD/YY', () => { expect(pipe.transform('1506854340801')).toBe('10/01/17'); }); it('Transform invalid date', () => { expect(pipe.transform('-1')).toBe('Invalid Date'); }); });