@@ -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' ;
@@ -12,6 +12,7 @@ import { getCustomerProjects } from '../../../customer-management/components/pro
1212import { EntryState } from '../../../time-clock/store/entry.reducer' ;
1313import * as entryActions from '../../../time-clock/store/entry.actions' ;
1414import { getUpdateError , getCreateError } from 'src/app/modules/time-clock/store/entry.selectors' ;
15+ import DateTimeFormat = Intl . DateTimeFormat ;
1516
1617describe ( 'DetailsFieldsComponent' , ( ) => {
1718 type Merged = TechnologyState & ProjectState & EntryState ;
@@ -22,21 +23,23 @@ describe('DetailsFieldsComponent', () => {
2223 let mockProjectsSelector ;
2324 let mockEntriesUpdateErrorSelector ;
2425 let mockEntriesCreateErrorSelector ;
26+ let entryToEdit ;
27+ let formValues ;
2528
2629 const state = {
2730 projects : {
28- projects : [ { id : 'id' , name : 'name' , project_type_id : '' } ] ,
29- customerProjects : [ { id : 'id' , name : 'name' , description : 'description' , project_type_id : '123' } ] ,
31+ projects : [ { id : 'id' , name : 'name' , project_type_id : '' } ] ,
32+ customerProjects : [ { id : 'id' , name : 'name' , description : 'description' , project_type_id : '123' } ] ,
3033 isLoading : false ,
3134 message : '' ,
3235 projectToEdit : undefined ,
3336 } ,
3437 technologies : {
35- technologyList : { items : [ { name : 'java' } ] } ,
38+ technologyList : { items : [ { name : 'java' } ] } ,
3639 isLoading : false ,
3740 } ,
3841 activities : {
39- data : [ { id : 'fc5fab41-a21e-4155-9d05-511b956ebd05' , tenant_id : 'ioet' , deleted : null , name : 'abc' } ] ,
42+ data : [ { id : 'fc5fab41-a21e-4155-9d05-511b956ebd05' , tenant_id : 'ioet' , deleted : null , name : 'abc' } ] ,
4043 isLoading : false ,
4144 message : 'Data fetch successfully!' ,
4245 activityIdToEdit : '' ,
@@ -61,7 +64,7 @@ describe('DetailsFieldsComponent', () => {
6164 beforeEach ( async ( ( ) => {
6265 TestBed . configureTestingModule ( {
6366 declarations : [ DetailsFieldsComponent , TechnologiesComponent ] ,
64- providers : [ provideMockStore ( { initialState : state } ) ] ,
67+ providers : [ provideMockStore ( { initialState : state } ) ] ,
6568 imports : [ FormsModule , ReactiveFormsModule ] ,
6669 } ) . compileComponents ( ) ;
6770 store = TestBed . inject ( MockStore ) ;
@@ -74,28 +77,17 @@ describe('DetailsFieldsComponent', () => {
7477 beforeEach ( ( ) => {
7578 fixture = TestBed . createComponent ( DetailsFieldsComponent ) ;
7679 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 = {
80+ entryToEdit = {
9181 project_id : '' ,
9282 activity_id : '' ,
9383 uri : 'ticketUri' ,
9484 start_date : null ,
9585 end_date : null ,
9686 description : '' ,
87+ technologies : [ ] ,
88+ id : 'xyz'
9789 } ;
98- const formValue = {
90+ formValues = {
9991 project_id : '' ,
10092 activity_id : '' ,
10193 uri : 'ticketUri' ,
@@ -105,9 +97,24 @@ describe('DetailsFieldsComponent', () => {
10597 description : '' ,
10698 technology : '' ,
10799 } ;
100+ } ) ;
101+
102+ it ( 'should create' , ( ) => {
103+ expect ( component ) . toBeTruthy ( ) ;
104+ } ) ;
105+
106+ it ( 'should emit ngOnChange without data' , ( ) => {
107+ component . entryToEdit = null ;
108+ component . ngOnChanges ( ) ;
109+ expect ( component . entryForm . value ) . toEqual ( initialData ) ;
110+ } ) ;
111+
112+ it ( 'should emit ngOnChange with new data' , ( ) => {
108113 component . entryToEdit = entryToEdit ;
114+
109115 component . ngOnChanges ( ) ;
110- expect ( component . entryForm . value ) . toEqual ( formValue ) ;
116+
117+ expect ( component . entryForm . value ) . toEqual ( formValues ) ;
111118 } ) ;
112119
113120 it ( 'should emit ngOnChange with new data' , ( ) => {
@@ -170,4 +177,79 @@ describe('DetailsFieldsComponent', () => {
170177 } ;
171178 expect ( component . saveEntry . emit ) . toHaveBeenCalledWith ( data ) ;
172179 } ) ;
180+
181+ it ( 'when the current entry is not running, then the end hour input should be rendered' , ( ) => {
182+ component . isEntryRunning = false ;
183+ fixture . detectChanges ( ) ;
184+
185+ const endHourInput = fixture . debugElement . nativeElement . querySelector ( '#end_hour' ) ;
186+ expect ( endHourInput ) . toBeDefined ( ) ;
187+ } ) ;
188+
189+ it ( 'when the current entry is running, then the end hour input should not be rendered' , ( ) => {
190+ component . isEntryRunning = true ;
191+ fixture . detectChanges ( ) ;
192+
193+ const endHourInput = fixture . debugElement . nativeElement . querySelector ( '#end_hour' ) ;
194+ expect ( endHourInput ) . toBeNull ( ) ;
195+ } ) ;
196+
197+ it ( 'when creating a new entry, then the new entry should be marked as not running' , ( ) => {
198+ component . entryToEdit = null ;
199+
200+ expect ( component . isEntryRunning ) . toBeFalse ( ) ;
201+ } ) ;
202+
203+ it ( 'when editing entry that is currently running, then the entry should be marked as running' , ( ) => {
204+ component . entryToEdit = { ...entryToEdit , running : true } ;
205+
206+ fixture . componentInstance . ngOnChanges ( ) ;
207+
208+ expect ( component . isEntryRunning ) . toBeTrue ( ) ;
209+ } ) ;
210+
211+ it ( 'when editing entry that already finished, then the entry should not be marked as running' , ( ) => {
212+ component . entryToEdit = { ...entryToEdit , running : false } ;
213+
214+ fixture . componentInstance . ngOnChanges ( ) ;
215+
216+ expect ( component . isEntryRunning ) . toBeFalse ( ) ;
217+ } ) ;
218+
219+ it ( 'when editing entry that already finished, then the entry should not be marked as running' , ( ) => {
220+ component . entryToEdit = { ...entryToEdit , running : false } ;
221+
222+ fixture . componentInstance . ngOnChanges ( ) ;
223+
224+ expect ( component . isEntryRunning ) . toBeFalse ( ) ;
225+ } ) ;
226+
227+ it ( 'when submitting a entry that is currently running, the end date should not be sent ' , ( ) => {
228+ component . isEntryRunning = true ;
229+ spyOn ( component . saveEntry , 'emit' ) ;
230+
231+ component . entryForm . setValue ( { ...formValues , entry_date : '2020-06-11' } ) ;
232+ component . onSubmit ( ) ;
233+ const data = {
234+ project_id : '' ,
235+ activity_id : '' ,
236+ technologies : [ ] ,
237+ description : '' ,
238+ start_date : '2020-06-11T00:00' ,
239+ uri : 'ticketUri' ,
240+ } ;
241+ expect ( component . saveEntry . emit ) . toHaveBeenCalledWith ( data ) ;
242+ } ) ;
243+
244+ it ( 'when disabling current entry is running, then the end hour should be set to the current time' , ( ) => {
245+ const datePipe : DatePipe = new DatePipe ( 'en' ) ;
246+ const currentTime = datePipe . transform ( new Date ( ) , 'HH:mm' ) ;
247+
248+ const checkIsEntryRunning : Element = fixture . debugElement . nativeElement . querySelector ( '#isEntryRunning' ) ;
249+ checkIsEntryRunning . dispatchEvent ( new Event ( 'change' ) ) ;
250+ fixture . detectChanges ( ) ;
251+
252+ const endHourInput : HTMLInputElement = fixture . debugElement . nativeElement . querySelector ( '#end_hour' ) ;
253+ expect ( endHourInput . value ) . toEqual ( currentTime ) ;
254+ } ) ;
173255} ) ;
0 commit comments