1
+ import { ToastrService } from 'ngx-toastr' ;
1
2
import { async , ComponentFixture , TestBed } from '@angular/core/testing' ;
2
3
import { provideMockStore , MockStore } from '@ngrx/store/testing' ;
3
4
import { FormsModule , ReactiveFormsModule } from '@angular/forms' ;
@@ -23,6 +24,11 @@ describe('TimeEntriesComponent', () => {
23
24
let mockTechnologySelector ;
24
25
let mockProjectsSelector ;
25
26
let mockEntriesSelector ;
27
+ let injectedToastrService ;
28
+
29
+ const toastrService = {
30
+ error : ( ) => { } ,
31
+ } ;
26
32
27
33
const state = {
28
34
projects : {
@@ -43,6 +49,10 @@ describe('TimeEntriesComponent', () => {
43
49
} ,
44
50
entries : {
45
51
entryList : [ ] ,
52
+ active : {
53
+ start_date : new Date ( '2019-01-01T15:36:15.887Z' ) ,
54
+ id : 'active-entry' ,
55
+ }
46
56
} ,
47
57
} ;
48
58
@@ -68,13 +78,16 @@ describe('TimeEntriesComponent', () => {
68
78
TechnologiesComponent ,
69
79
TimeEntriesSummaryComponent ,
70
80
] ,
71
- providers : [ provideMockStore ( { initialState : state } ) ] ,
81
+ providers : [ provideMockStore ( { initialState : state } ) ,
82
+ { provide : ToastrService , useValue : toastrService } ,
83
+ ] ,
72
84
imports : [ FormsModule , ReactiveFormsModule ] ,
73
85
} ) . compileComponents ( ) ;
74
86
store = TestBed . inject ( MockStore ) ;
75
87
mockTechnologySelector = store . overrideSelector ( allTechnologies , state . technologies ) ;
76
88
mockProjectsSelector = store . overrideSelector ( getProjects , state . projects . projects ) ;
77
89
mockEntriesSelector = store . overrideSelector ( allEntries , state . entries . entryList ) ;
90
+ injectedToastrService = TestBed . inject ( ToastrService ) ;
78
91
} ) ) ;
79
92
80
93
beforeEach ( ( ) => {
@@ -159,30 +172,51 @@ describe('TimeEntriesComponent', () => {
159
172
expect ( component . entryId ) . toBe ( 'entry_1' ) ;
160
173
} ) ;
161
174
162
- it ( 'should update entry by id ' , ( ) => {
175
+ it ( 'displays an error when start date of entry is > than active entry start date ' , ( ) => {
163
176
const newEntry = {
164
- project_id : 'projectId' ,
165
- start_date : '' ,
177
+ project_id : 'p-id' ,
178
+ start_date : '2020-05-05T10:04' ,
179
+ description : 'description' ,
180
+ technologies : [ ] ,
181
+ uri : 'abc' ,
182
+ } ;
183
+ component . entryId = 'new-entry' ;
184
+ spyOn ( injectedToastrService , 'error' ) ;
185
+
186
+ component . saveEntry ( newEntry ) ;
187
+
188
+ expect ( injectedToastrService . error ) . toHaveBeenCalled ( ) ;
189
+ } ) ;
190
+
191
+ it ( 'should dispatch an action when entry is going to be saved' , ( ) => {
192
+ const newEntry = {
193
+ project_id : 'p-id' ,
194
+ start_date : '2020-05-05T10:04' ,
166
195
description : 'description' ,
167
196
technologies : [ ] ,
168
197
uri : 'abc' ,
169
198
} ;
170
- component . entryId = 'entry_1 ' ;
199
+ component . entryId = 'active-entry ' ;
171
200
spyOn ( store , 'dispatch' ) ;
201
+
172
202
component . saveEntry ( newEntry ) ;
203
+
173
204
expect ( store . dispatch ) . toHaveBeenCalled ( ) ;
174
205
} ) ;
175
206
176
207
it ( 'should create new Entry' , ( ) => {
177
208
const newEntry = {
178
209
project_id : 'projectId' ,
179
- start_date : '' ,
210
+ start_date : '2010-05-05T10:04 ' ,
180
211
description : 'description' ,
181
212
technologies : [ ] ,
182
213
uri : 'abc' ,
183
214
} ;
215
+ component . entryId = undefined ;
184
216
spyOn ( store , 'dispatch' ) ;
217
+
185
218
component . saveEntry ( newEntry ) ;
219
+
186
220
expect ( store . dispatch ) . toHaveBeenCalledWith ( new entryActions . CreateEntry ( newEntry ) ) ;
187
221
} ) ;
188
222
0 commit comments