@@ -2,7 +2,7 @@ import { TechnologiesComponent } from './../technologies/technologies.component'
2
2
import { async , ComponentFixture , TestBed } from '@angular/core/testing' ;
3
3
import { provideMockStore , MockStore } from '@ngrx/store/testing' ;
4
4
import { FormsModule , ReactiveFormsModule } from '@angular/forms' ;
5
- import { formatDate } from '@angular/common' ;
5
+ import { DatePipe , formatDate } from '@angular/common' ;
6
6
7
7
import { TechnologyState } from '../../store/technology.reducers' ;
8
8
import { allTechnologies } from '../../store/technology.selectors' ;
@@ -22,21 +22,23 @@ describe('DetailsFieldsComponent', () => {
22
22
let mockProjectsSelector ;
23
23
let mockEntriesUpdateErrorSelector ;
24
24
let mockEntriesCreateErrorSelector ;
25
+ let entryToEdit ;
26
+ let formValues ;
25
27
26
28
const state = {
27
29
projects : {
28
- projects : [ { id : 'id' , name : 'name' , project_type_id : '' } ] ,
29
- customerProjects : [ { id : 'id' , name : 'name' , description : 'description' , project_type_id : '123' } ] ,
30
+ projects : [ { id : 'id' , name : 'name' , project_type_id : '' } ] ,
31
+ customerProjects : [ { id : 'id' , name : 'name' , description : 'description' , project_type_id : '123' } ] ,
30
32
isLoading : false ,
31
33
message : '' ,
32
34
projectToEdit : undefined ,
33
35
} ,
34
36
technologies : {
35
- technologyList : { items : [ { name : 'java' } ] } ,
37
+ technologyList : { items : [ { name : 'java' } ] } ,
36
38
isLoading : false ,
37
39
} ,
38
40
activities : {
39
- data : [ { id : 'fc5fab41-a21e-4155-9d05-511b956ebd05' , tenant_id : 'ioet' , deleted : null , name : 'abc' } ] ,
41
+ data : [ { id : 'fc5fab41-a21e-4155-9d05-511b956ebd05' , tenant_id : 'ioet' , deleted : null , name : 'abc' } ] ,
40
42
isLoading : false ,
41
43
message : 'Data fetch successfully!' ,
42
44
activityIdToEdit : '' ,
@@ -61,7 +63,7 @@ describe('DetailsFieldsComponent', () => {
61
63
beforeEach ( async ( ( ) => {
62
64
TestBed . configureTestingModule ( {
63
65
declarations : [ DetailsFieldsComponent , TechnologiesComponent ] ,
64
- providers : [ provideMockStore ( { initialState : state } ) ] ,
66
+ providers : [ provideMockStore ( { initialState : state } ) ] ,
65
67
imports : [ FormsModule , ReactiveFormsModule ] ,
66
68
} ) . compileComponents ( ) ;
67
69
store = TestBed . inject ( MockStore ) ;
@@ -74,28 +76,17 @@ describe('DetailsFieldsComponent', () => {
74
76
beforeEach ( ( ) => {
75
77
fixture = TestBed . createComponent ( DetailsFieldsComponent ) ;
76
78
component = fixture . componentInstance ;
77
- } ) ;
78
-
79
- it ( 'should create' , ( ) => {
80
- expect ( component ) . toBeTruthy ( ) ;
81
- } ) ;
82
-
83
- it ( 'should emit ngOnChange without data' , ( ) => {
84
- component . entryToEdit = null ;
85
- component . ngOnChanges ( ) ;
86
- expect ( component . entryForm . value ) . toEqual ( initialData ) ;
87
- } ) ;
88
-
89
- it ( 'should emit ngOnChange with new data' , ( ) => {
90
- const entryToEdit = {
79
+ entryToEdit = {
91
80
project_id : '' ,
92
81
activity_id : '' ,
93
82
uri : 'ticketUri' ,
94
83
start_date : null ,
95
84
end_date : null ,
96
85
description : '' ,
86
+ technologies : [ ] ,
87
+ id : 'xyz'
97
88
} ;
98
- const formValue = {
89
+ formValues = {
99
90
project_id : '' ,
100
91
activity_id : '' ,
101
92
uri : 'ticketUri' ,
@@ -105,9 +96,24 @@ describe('DetailsFieldsComponent', () => {
105
96
description : '' ,
106
97
technology : '' ,
107
98
} ;
99
+ } ) ;
100
+
101
+ it ( 'should create' , ( ) => {
102
+ expect ( component ) . toBeTruthy ( ) ;
103
+ } ) ;
104
+
105
+ it ( 'should emit ngOnChange without data' , ( ) => {
106
+ component . entryToEdit = null ;
107
+ component . ngOnChanges ( ) ;
108
+ expect ( component . entryForm . value ) . toEqual ( initialData ) ;
109
+ } ) ;
110
+
111
+ it ( 'should emit ngOnChange with new data' , ( ) => {
108
112
component . entryToEdit = entryToEdit ;
113
+
109
114
component . ngOnChanges ( ) ;
110
- expect ( component . entryForm . value ) . toEqual ( formValue ) ;
115
+
116
+ expect ( component . entryForm . value ) . toEqual ( formValues ) ;
111
117
} ) ;
112
118
113
119
it ( 'should emit ngOnChange with new data' , ( ) => {
@@ -170,4 +176,79 @@ describe('DetailsFieldsComponent', () => {
170
176
} ;
171
177
expect ( component . saveEntry . emit ) . toHaveBeenCalledWith ( data ) ;
172
178
} ) ;
179
+
180
+ it ( 'when the current entry is not running, then the end hour input should be rendered' , ( ) => {
181
+ component . isEntryRunning = false ;
182
+ fixture . detectChanges ( ) ;
183
+
184
+ const endHourInput = fixture . debugElement . nativeElement . querySelector ( '#end_hour' ) ;
185
+ expect ( endHourInput ) . toBeDefined ( ) ;
186
+ } ) ;
187
+
188
+ it ( 'when the current entry is running, then the end hour input should not be rendered' , ( ) => {
189
+ component . isEntryRunning = true ;
190
+ fixture . detectChanges ( ) ;
191
+
192
+ const endHourInput = fixture . debugElement . nativeElement . querySelector ( '#end_hour' ) ;
193
+ expect ( endHourInput ) . toBeNull ( ) ;
194
+ } ) ;
195
+
196
+ it ( 'when creating a new entry, then the new entry should be marked as not running' , ( ) => {
197
+ component . entryToEdit = null ;
198
+
199
+ expect ( component . isEntryRunning ) . toBeFalse ( ) ;
200
+ } ) ;
201
+
202
+ it ( 'when editing entry that is currently running, then the entry should be marked as running' , ( ) => {
203
+ component . entryToEdit = { ...entryToEdit , running : true } ;
204
+
205
+ fixture . componentInstance . ngOnChanges ( ) ;
206
+
207
+ expect ( component . isEntryRunning ) . toBeTrue ( ) ;
208
+ } ) ;
209
+
210
+ it ( 'when editing entry that already finished, then the entry should not be marked as running' , ( ) => {
211
+ component . entryToEdit = { ...entryToEdit , running : false } ;
212
+
213
+ fixture . componentInstance . ngOnChanges ( ) ;
214
+
215
+ expect ( component . isEntryRunning ) . toBeFalse ( ) ;
216
+ } ) ;
217
+
218
+ it ( 'when editing entry that already finished, then the entry should not be marked as running' , ( ) => {
219
+ component . entryToEdit = { ...entryToEdit , running : false } ;
220
+
221
+ fixture . componentInstance . ngOnChanges ( ) ;
222
+
223
+ expect ( component . isEntryRunning ) . toBeFalse ( ) ;
224
+ } ) ;
225
+
226
+ it ( 'when submitting a entry that is currently running, the end date should not be sent ' , ( ) => {
227
+ component . isEntryRunning = true ;
228
+ spyOn ( component . saveEntry , 'emit' ) ;
229
+
230
+ component . entryForm . setValue ( { ...formValues , entry_date : '2020-06-11' } ) ;
231
+ component . onSubmit ( ) ;
232
+ const data = {
233
+ project_id : '' ,
234
+ activity_id : '' ,
235
+ technologies : [ ] ,
236
+ description : '' ,
237
+ start_date : '2020-06-11T00:00' ,
238
+ uri : 'ticketUri' ,
239
+ } ;
240
+ expect ( component . saveEntry . emit ) . toHaveBeenCalledWith ( data ) ;
241
+ } ) ;
242
+
243
+ it ( 'when disabling current entry is running, then the end hour should be set to the current time' , ( ) => {
244
+ const datePipe : DatePipe = new DatePipe ( 'en' ) ;
245
+ const currentTime = datePipe . transform ( new Date ( ) , 'HH:mm' ) ;
246
+
247
+ const checkIsEntryRunning : Element = fixture . debugElement . nativeElement . querySelector ( '#isEntryRunning' ) ;
248
+ checkIsEntryRunning . dispatchEvent ( new Event ( 'change' ) ) ;
249
+ fixture . detectChanges ( ) ;
250
+
251
+ const endHourInput : HTMLInputElement = fixture . debugElement . nativeElement . querySelector ( '#end_hour' ) ;
252
+ expect ( endHourInput . value ) . toEqual ( currentTime ) ;
253
+ } ) ;
173
254
} ) ;
0 commit comments