Skip to content

Commit 67ed8c4

Browse files
committed
Unit test code examples
1 parent c7c4321 commit 67ed8c4

File tree

5 files changed

+153
-1
lines changed

5 files changed

+153
-1
lines changed

frontend/src/app/project/auth/auth.service.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { AuthService } from './auth.service';
22
import { Subject } from 'rxjs';
3-
import { fakeAsync, tick } from '@angular/core/testing';
3+
import { fakeAsync } from '@angular/core/testing';
44

55
describe('AuthService', () => {
66
let service: AuthService;
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { AddIssueModalComponent } from '@trungk18/project/components/add-issue-modal/add-issue-modal.component';
2+
3+
describe('AddIssueModalComponent', () => {
4+
let component: AddIssueModalComponent;
5+
6+
const formBuilder: any = {
7+
group: jasmine.createSpy('group').and.returnValue({
8+
invalid: false,
9+
getRawValue: jasmine.createSpy('getRawValue')
10+
}),
11+
};
12+
const nzModalRef: any = {
13+
close: jasmine.createSpy('close').and.callThrough()
14+
};
15+
const projectService: any = {
16+
updateIssue: jasmine.createSpy('updateIssue')
17+
};
18+
const projectQuery: any = {};
19+
20+
beforeEach(() => {
21+
component = new AddIssueModalComponent(
22+
formBuilder,
23+
nzModalRef,
24+
projectService,
25+
projectQuery
26+
);
27+
});
28+
29+
it('should be able to initForm', () => {
30+
component.initForm();
31+
expect(formBuilder.group).toHaveBeenCalled();
32+
});
33+
it('should be able to submit Form', () => {
34+
component.initForm();
35+
component.submitForm();
36+
expect(projectService.updateIssue).toHaveBeenCalled();
37+
expect(formBuilder.group).toHaveBeenCalled();
38+
});
39+
});
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import { FormControl } from '@angular/forms';
2+
import { IssueTitleComponent } from './issue-title.component';
3+
import {SimpleChange} from '@angular/core';
4+
import {JComment} from '@trungk18/interface/comment';
5+
import {IssuePriority, IssueStatus, IssueType} from '@trungk18/interface/issue';
6+
7+
describe('IssueTitleComponent', () => {
8+
let component: IssueTitleComponent;
9+
10+
const projectService: any = {
11+
updateIssue: jasmine.createSpy('updateIssue').and.callThrough()
12+
};
13+
14+
beforeEach(() => {
15+
component = new IssueTitleComponent(
16+
projectService
17+
);
18+
component.titleControl = new FormControl('test');
19+
});
20+
21+
it('should be able to make onBlur action', () => {
22+
component.onBlur();
23+
expect(projectService.updateIssue).toHaveBeenCalled();
24+
});
25+
it('should be able to change title', () => {
26+
component.issue = {
27+
id: '',
28+
title: 'New title',
29+
type: IssueType.BUG,
30+
status: IssueStatus.BACKLOG,
31+
priority: IssuePriority.HIGH,
32+
listPosition: 0,
33+
description: '',
34+
estimate: 0,
35+
timeSpent: 0,
36+
timeRemaining: 0,
37+
createdAt: '',
38+
updatedAt: '',
39+
reporterId: '',
40+
userIds: [],
41+
comments: [],
42+
projectId: ''
43+
};
44+
component.ngOnChanges({
45+
issue: new SimpleChange(null, {title: 'New title'}, null)
46+
});
47+
expect(component.titleControl.value).toEqual('New title');
48+
});
49+
it('should not be able to change title', () => {
50+
component.issue = {
51+
id: '',
52+
title: 'New title 2',
53+
type: IssueType.BUG,
54+
status: IssueStatus.BACKLOG,
55+
priority: IssuePriority.HIGH,
56+
listPosition: 0,
57+
description: '',
58+
estimate: 0,
59+
timeSpent: 0,
60+
timeRemaining: 0,
61+
createdAt: '',
62+
updatedAt: '',
63+
reporterId: '',
64+
userIds: [],
65+
comments: [],
66+
projectId: ''
67+
};
68+
69+
const expected = {title: 'New title'};
70+
71+
component.ngOnChanges({
72+
issue: new SimpleChange(expected, expected, null)
73+
});
74+
expect(component.titleControl.value).toEqual('test');
75+
});
76+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { NavigationComponent } from './navigation.component';
2+
3+
describe('NavigationComponent', () => {
4+
let component: NavigationComponent;
5+
6+
beforeEach(() => {
7+
component = new NavigationComponent();
8+
});
9+
10+
it('should be able to toggle', () => {
11+
spyOn(component.manualToggle, 'emit');
12+
component.toggle();
13+
expect(component.manualToggle.emit).toHaveBeenCalled();
14+
});
15+
});
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { ProjectComponent } from '@trungk18/project/project.component';
2+
3+
4+
describe('ProjectComponent', () => {
5+
let component: ProjectComponent;
6+
7+
const projectService: any = {};
8+
const authService: any = {};
9+
10+
beforeEach(() => {
11+
component = new ProjectComponent(
12+
projectService,
13+
authService
14+
);
15+
});
16+
17+
it('should be able to toggle', () => {
18+
component.expanded = false;
19+
component.manualToggle();
20+
expect(component.expanded).toBe(true);
21+
});
22+
});

0 commit comments

Comments
 (0)