Unit Tests

Simple Class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import {User} from './user';
let user: User = {
id: 'dtrump',
firstName: 'Jean',
lastName: 'Bambois',
university: 'Univesity of Toronto',
title: 'Assistant professor of English Literature',
avatar:'assets/img/a5.jpg',
lang: 'en'
};


describe('User', () => {
describe('user constructor', () => {
it('should create user with correct fields', () => {
expect(user.id).toBe('dtrump');
expect(user.firstName).toBe('Jean');
expect(user.lastName).toBe('Bambois');
expect(user.university).toBe('Univesity of Toronto');
expect(user.title).toBe('Assistant professor of English Literature');
expect(user.avatar).toBe('assets/img/a5.jpg');
expect(user.lang).toBe('en');
});
});
});

Simple Component

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import {TestBed, inject} from '@angular/core/testing';
import {EvaluationsListComponent} from './evaluations-list.component';
import {Evaluation} from '../../evaluation/model/evaluation';
describe('EvaluationsListComponent', () => {
beforeEach(() => TestBed.configureTestingModule({
providers: [
EvaluationsListComponent
]
}));

it('should be initiale correctly', inject([EvaluationsListComponent], (component: EvaluationsListComponent) => {
expect(component.evaluations.length).toBe(0);
expect(component.selectedEvaluation).toBeUndefined();
}));

describe('hasEvaluations method', () => {
it('should return false by default', inject([EvaluationsListComponent], (component: EvaluationsListComponent) => {
expect(component.hasEvaluations()).toBeFalsy();
}));

it('should return true when evaluations is not empty', inject([EvaluationsListComponent], (component: EvaluationsListComponent) => {
component.evaluations = [new Evaluation({id: 'ev1'})];
expect(component.hasEvaluations()).toBeTruthy();
}));
});

describe('isSelected method', () => {
it('should return false id evaluation is not selected', inject([EvaluationsListComponent], (component: EvaluationsListComponent) => {
expect(component.isSelected(new Evaluation({id: 'ev1'}))).toBeFalsy();
}));

it('should return true when evaluations is not empty', inject([EvaluationsListComponent], (component: EvaluationsListComponent) => {
let evaluation = new Evaluation({id: 'ev1'});
component.selectedEvaluation = evaluation;
expect(component.isSelected(evaluation)).toBeTruthy();
}));
});
});