1- import { async , ComponentFixture , TestBed } from '@angular/core/testing' ;
1+ import { async , ComponentFixture , TestBed , inject } from '@angular/core/testing' ;
22
33import { ProjectManagementComponent } from './project-management.component' ;
44import { Project } from '../../../interfaces/project' ;
5+ import { ProjectService } from '../../../services/project.service' ;
6+ import { of } from 'rxjs' ;
57
68describe ( '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} ) ;
0 commit comments