@@ -2,7 +2,7 @@ import { TechnologiesComponent } from './../technologies/technologies.component'
22import { async , ComponentFixture , TestBed } from '@angular/core/testing' ;
33import { provideMockStore , MockStore } from '@ngrx/store/testing' ;
44import { FormsModule , ReactiveFormsModule } from '@angular/forms' ;
5- import { formatDate } from '@angular/common' ;
5+ import { DatePipe , formatDate } from '@angular/common' ;
66
77import { TechnologyState } from '../../store/technology.reducers' ;
88import { allTechnologies } from '../../store/technology.selectors' ;
@@ -22,21 +22,23 @@ describe('DetailsFieldsComponent', () => {
2222 let mockProjectsSelector ;
2323 let mockEntriesUpdateErrorSelector ;
2424 let mockEntriesCreateErrorSelector ;
25+ let entryToEdit ;
26+ let formValues ;
2527
2628 const state = {
2729 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' } ] ,
3032 isLoading : false ,
3133 message : '' ,
3234 projectToEdit : undefined ,
3335 } ,
3436 technologies : {
35- technologyList : { items : [ { name : 'java' } ] } ,
37+ technologyList : { items : [ { name : 'java' } ] } ,
3638 isLoading : false ,
3739 } ,
3840 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' } ] ,
4042 isLoading : false ,
4143 message : 'Data fetch successfully!' ,
4244 activityIdToEdit : '' ,
@@ -61,7 +63,7 @@ describe('DetailsFieldsComponent', () => {
6163 beforeEach ( async ( ( ) => {
6264 TestBed . configureTestingModule ( {
6365 declarations : [ DetailsFieldsComponent , TechnologiesComponent ] ,
64- providers : [ provideMockStore ( { initialState : state } ) ] ,
66+ providers : [ provideMockStore ( { initialState : state } ) ] ,
6567 imports : [ FormsModule , ReactiveFormsModule ] ,
6668 } ) . compileComponents ( ) ;
6769 store = TestBed . inject ( MockStore ) ;
@@ -74,28 +76,17 @@ describe('DetailsFieldsComponent', () => {
7476 beforeEach ( ( ) => {
7577 fixture = TestBed . createComponent ( DetailsFieldsComponent ) ;
7678 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 = {
9180 project_id : '' ,
9281 activity_id : '' ,
9382 uri : 'ticketUri' ,
9483 start_date : null ,
9584 end_date : null ,
9685 description : '' ,
86+ technologies : [ ] ,
87+ id : 'xyz'
9788 } ;
98- const formValue = {
89+ formValues = {
9990 project_id : '' ,
10091 activity_id : '' ,
10192 uri : 'ticketUri' ,
@@ -105,9 +96,24 @@ describe('DetailsFieldsComponent', () => {
10596 description : '' ,
10697 technology : '' ,
10798 } ;
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' , ( ) => {
108112 component . entryToEdit = entryToEdit ;
113+
109114 component . ngOnChanges ( ) ;
110- expect ( component . entryForm . value ) . toEqual ( formValue ) ;
115+
116+ expect ( component . entryForm . value ) . toEqual ( formValues ) ;
111117 } ) ;
112118
113119 it ( 'should emit ngOnChange with new data' , ( ) => {
@@ -170,4 +176,79 @@ describe('DetailsFieldsComponent', () => {
170176 } ;
171177 expect ( component . saveEntry . emit ) . toHaveBeenCalledWith ( data ) ;
172178 } ) ;
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+ } ) ;
173254} ) ;
0 commit comments