1- import { async , ComponentFixture , TestBed } from '@angular/core/testing' ;
2- import { MockStore , provideMockStore } from '@ngrx/store/testing' ;
3- import { TimeRangeFormComponent } from './time-range-form.component' ;
4- import { EntryState } from '../../../time-clock/store/entry.reducer' ;
5- import { FormsModule , ReactiveFormsModule } from '@angular/forms' ;
6- import { InputDateComponent } from '../../../shared/components/input-date/input-date.component' ;
1+ import { ToastrService , IndividualConfig } from 'ngx-toastr' ;
2+ import { async , ComponentFixture , TestBed } from '@angular/core/testing' ;
3+ import { MockStore , provideMockStore } from '@ngrx/store/testing' ;
4+ import { TimeRangeFormComponent } from './time-range-form.component' ;
5+ import { EntryState } from '../../../time-clock/store/entry.reducer' ;
6+ import { FormsModule , ReactiveFormsModule } from '@angular/forms' ;
7+ import { InputDateComponent } from '../../../shared/components/input-date/input-date.component' ;
78import * as entryActions from '../../../time-clock/store/entry.actions' ;
89import * as moment from 'moment' ;
910
@@ -12,6 +13,10 @@ describe('Reports Page', () => {
1213 let component : TimeRangeFormComponent ;
1314 let fixture : ComponentFixture < TimeRangeFormComponent > ;
1415 let store : MockStore < EntryState > ;
16+ const toastrServiceStub = {
17+ error : ( message ?: string , title ?: string , override ?: Partial < IndividualConfig > ) => { }
18+ } ;
19+
1520 const timeEntry = {
1621 id : '123' ,
1722 start_date : new Date ( ) ,
@@ -38,7 +43,10 @@ describe('Reports Page', () => {
3843 TestBed . configureTestingModule ( {
3944 imports : [ FormsModule , ReactiveFormsModule ] ,
4045 declarations : [ TimeRangeFormComponent , InputDateComponent ] ,
41- providers : [ provideMockStore ( { initialState : state } ) ] ,
46+ providers : [
47+ provideMockStore ( { initialState : state } ) ,
48+ { provide : ToastrService , useValue : toastrServiceStub }
49+ ] ,
4250 } ) . compileComponents ( ) ;
4351 store = TestBed . inject ( MockStore ) ;
4452
@@ -54,7 +62,7 @@ describe('Reports Page', () => {
5462 expect ( component ) . toBeTruthy ( ) ;
5563 } ) ;
5664
57- it ( 'when submitting form a new LoadEntriesByTimeRange action is triggered ' , ( ) => {
65+ it ( 'LoadEntriesByTimeRange action is triggered when start date is before end date ' , ( ) => {
5866 const yesterday = moment ( new Date ( ) ) . subtract ( 1 , 'days' ) ;
5967 const today = moment ( new Date ( ) ) ;
6068 spyOn ( store , 'dispatch' ) ;
@@ -77,6 +85,19 @@ describe('Reports Page', () => {
7785 expect ( component . setInitialDataOnScreen ) . toHaveBeenCalled ( ) ;
7886 } ) ;
7987
88+ it ( 'shows an error when the end date is before the start date' , ( ) => {
89+ spyOn ( toastrServiceStub , 'error' ) ;
90+ const yesterday = moment ( new Date ( ) ) . subtract ( 1 , 'days' ) ;
91+ const today = moment ( new Date ( ) ) ;
92+ spyOn ( store , 'dispatch' ) ;
93+ component . reportForm . controls . startDate . setValue ( today ) ;
94+ component . reportForm . controls . endDate . setValue ( yesterday ) ;
95+
96+ component . onSubmit ( ) ;
97+
98+ expect ( toastrServiceStub . error ) . toHaveBeenCalled ( ) ;
99+ } ) ;
100+
80101 it ( 'setInitialDataOnScreen sets dates in form' , ( ) => {
81102 spyOn ( component . reportForm , 'setValue' ) ;
82103
0 commit comments