Skip to content

Commit 0099756

Browse files
committed
service test
1 parent d899566 commit 0099756

File tree

4 files changed

+66
-19
lines changed

4 files changed

+66
-19
lines changed

src/app/components/options-sidebar/project-management/project-management.component.spec.ts

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
1+
import { async, ComponentFixture, TestBed, inject } from '@angular/core/testing';
22

33
import { ProjectManagementComponent } from './project-management.component';
44
import { Project } from '../../../interfaces/project';
5+
import { ProjectService } from '../../../services/project.service';
6+
import { of } from 'rxjs';
57

68
describe('ProjectManagementComponent', () => {
79
let component: ProjectManagementComponent;
810
let fixture: ComponentFixture<ProjectManagementComponent>;
11+
let projectService: ProjectService;
12+
913
const projects: Project[] = [{
1014
id: 1,
1115
name: 'app 1',
@@ -29,9 +33,17 @@ describe('ProjectManagementComponent', () => {
2933
}
3034
];
3135

36+
const projectServiceStub = {
37+
getProjects() {
38+
const projectsMock = projects;
39+
return of(projectsMock);
40+
}
41+
};
42+
3243
beforeEach(async(() => {
3344
TestBed.configureTestingModule({
34-
declarations: [ ProjectManagementComponent ]
45+
declarations: [ ProjectManagementComponent ],
46+
providers: [ { provide: ProjectService, useValue: projectServiceStub }]
3547
})
3648
.compileComponents();
3749
}));
@@ -40,9 +52,18 @@ describe('ProjectManagementComponent', () => {
4052
fixture = TestBed.createComponent(ProjectManagementComponent);
4153
component = fixture.componentInstance;
4254
fixture.detectChanges();
55+
56+
projectService = TestBed.inject(ProjectService);
4357
component.projects = projects;
4458
});
4559

60+
61+
it('Service injected via inject(...) and TestBed.get(...) should be the same instance',
62+
inject([ProjectService], (injectService: ProjectService) => {
63+
expect(injectService).toBe(projectService);
64+
})
65+
);
66+
4667
it('should create', () => {
4768
expect(component).toBeTruthy();
4869
});
@@ -97,4 +118,34 @@ describe('ProjectManagementComponent', () => {
97118
component.cancelForm();
98119
expect(component.project).toBe(null);
99120
});
121+
122+
it('should call getProjects method on init', () => {
123+
const preojectServiceSpy = spyOn(projectService, 'getProjects').and.callThrough();
124+
const componentSpy = spyOn(component, 'getProjects').and.callThrough();
125+
126+
expect(preojectServiceSpy).not.toHaveBeenCalled();
127+
expect(componentSpy).not.toHaveBeenCalled();
128+
129+
component.ngOnInit();
130+
131+
expect(preojectServiceSpy).toHaveBeenCalledTimes(1);
132+
expect(componentSpy).toHaveBeenCalledTimes(1);
133+
});
134+
135+
136+
it('should call getProjects and return a list of projects', async(() => {
137+
138+
spyOn(projectService, 'getProjects').and.returnValue(of(projects));
139+
140+
component.ngOnInit();
141+
142+
expect(component.projects).toEqual(projects);
143+
}));
144+
145+
it('should delete a project #deleteProject', () => {
146+
const projectId = 1;
147+
148+
component.deleteProject(projectId);
149+
expect(component.projects.length).toEqual(2);
150+
});
100151
});

src/app/components/options-sidebar/project-management/project-management.component.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ export class ProjectManagementComponent implements OnInit {
1616
projects: Project[] = [];
1717

1818
constructor(private projectService: ProjectService) {
19-
this.getProjects();
2019
}
2120

2221
ngOnInit(): void {
Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
1-
import { TestBed } from '@angular/core/testing';
1+
import { TestBed, inject, async } from '@angular/core/testing';
2+
import { HttpClientTestingModule } from '@angular/common/http/testing';
23

34
import { ProjectService } from './project.service';
45

56
describe('ProjectService', () => {
67
let service: ProjectService;
78

89
beforeEach(() => {
9-
TestBed.configureTestingModule({});
10+
TestBed.configureTestingModule({
11+
imports: [HttpClientTestingModule]
12+
});
1013
service = TestBed.inject(ProjectService);
1114
});
1215

13-
it('should be created', () => {
14-
expect(service).toBeTruthy();
15-
});
16+
17+
it('should create', inject([HttpClientTestingModule, ProjectService],
18+
(httpClient: HttpClientTestingModule, apiService: ProjectService) => {
19+
expect(apiService).toBeTruthy();
20+
expect(httpClient).toBeTruthy();
21+
}));
22+
1623
});

src/app/services/project.service.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import { Injectable } from '@angular/core';
22
import { Project } from '../interfaces/project';
33
import { HttpClient } from '@angular/common/http';
4-
import { Observable, of } from 'rxjs';
5-
import { map, filter } from 'rxjs/operators';
6-
7-
4+
import { Observable } from 'rxjs';
85

96
@Injectable({
107
providedIn: 'root'
@@ -19,11 +16,4 @@ export class ProjectService {
1916
getProjects(): Observable<Project[]> {
2017
return this.http.get<Project[]>(this.url);
2118
}
22-
23-
// getProject(projectId): Observable<Project> {
24-
// return this.http.get<Project>(this.url).pipe(
25-
// filter((project: Project) => project.id === projectId)[0]
26-
// );
27-
// }
28-
2919
}

0 commit comments

Comments
 (0)