1
- import { async , ComponentFixture , TestBed } from '@angular/core/testing' ;
1
+ import { async , ComponentFixture , TestBed , inject } from '@angular/core/testing' ;
2
2
3
3
import { ProjectManagementComponent } from './project-management.component' ;
4
4
import { Project } from '../../../interfaces/project' ;
5
+ import { ProjectService } from '../../../services/project.service' ;
6
+ import { of } from 'rxjs' ;
5
7
6
8
describe ( 'ProjectManagementComponent' , ( ) => {
7
9
let component : ProjectManagementComponent ;
8
10
let fixture : ComponentFixture < ProjectManagementComponent > ;
11
+ let projectService : ProjectService ;
12
+
9
13
const projects : Project [ ] = [ {
10
14
id : 1 ,
11
15
name : 'app 1' ,
@@ -29,9 +33,17 @@ describe('ProjectManagementComponent', () => {
29
33
}
30
34
] ;
31
35
36
+ const projectServiceStub = {
37
+ getProjects ( ) {
38
+ const projectsMock = projects ;
39
+ return of ( projectsMock ) ;
40
+ }
41
+ } ;
42
+
32
43
beforeEach ( async ( ( ) => {
33
44
TestBed . configureTestingModule ( {
34
- declarations : [ ProjectManagementComponent ]
45
+ declarations : [ ProjectManagementComponent ] ,
46
+ providers : [ { provide : ProjectService , useValue : projectServiceStub } ]
35
47
} )
36
48
. compileComponents ( ) ;
37
49
} ) ) ;
@@ -40,9 +52,18 @@ describe('ProjectManagementComponent', () => {
40
52
fixture = TestBed . createComponent ( ProjectManagementComponent ) ;
41
53
component = fixture . componentInstance ;
42
54
fixture . detectChanges ( ) ;
55
+
56
+ projectService = TestBed . inject ( ProjectService ) ;
43
57
component . projects = projects ;
44
58
} ) ;
45
59
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
+
46
67
it ( 'should create' , ( ) => {
47
68
expect ( component ) . toBeTruthy ( ) ;
48
69
} ) ;
@@ -97,4 +118,34 @@ describe('ProjectManagementComponent', () => {
97
118
component . cancelForm ( ) ;
98
119
expect ( component . project ) . toBe ( null ) ;
99
120
} ) ;
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
+ } ) ;
100
151
} ) ;
0 commit comments