From 93ca91e6f35fe3eeaa85cea978f0517d5a81ee35 Mon Sep 17 00:00:00 2001 From: roberto Date: Tue, 7 Jul 2020 14:46:34 -0500 Subject: [PATCH] fix: validate future date #459 --- .../details-fields.component.spec.ts | 22 ++++++++++++++++++- .../details-fields.component.ts | 10 ++++++++- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/app/modules/shared/components/details-fields/details-fields.component.spec.ts b/src/app/modules/shared/components/details-fields/details-fields.component.spec.ts index 2e407eeab..c69d23912 100644 --- a/src/app/modules/shared/components/details-fields/details-fields.component.spec.ts +++ b/src/app/modules/shared/components/details-fields/details-fields.component.spec.ts @@ -5,6 +5,7 @@ import { MockStore, provideMockStore } from '@ngrx/store/testing'; import { FormsModule, ReactiveFormsModule } from '@angular/forms'; import { formatDate } from '@angular/common'; import { ActionsSubject } from '@ngrx/store'; +import { ToastrService, IndividualConfig } from 'ngx-toastr'; import { TechnologyState } from '../../store/technology.reducers'; import { allTechnologies } from '../../store/technology.selectors'; @@ -15,6 +16,7 @@ import { EntryState } from '../../../time-clock/store/entry.reducer'; import * as entryActions from '../../../time-clock/store/entry.actions'; import { getCreateError, getUpdateError } from 'src/app/modules/time-clock/store/entry.selectors'; import { SaveEntryEvent } from './save-entry-event'; +import * as moment from 'moment'; describe('DetailsFieldsComponent', () => { type Merged = TechnologyState & ProjectState & EntryState; @@ -28,6 +30,9 @@ describe('DetailsFieldsComponent', () => { let entryToEdit; let formValues; const actionSub: ActionsSubject = new ActionsSubject(); + const toastrServiceStub = { + error: (message?: string, title?: string, override?: Partial) => { } + }; const state = { projects: { @@ -67,7 +72,11 @@ describe('DetailsFieldsComponent', () => { beforeEach(async(() => { TestBed.configureTestingModule({ declarations: [DetailsFieldsComponent, TechnologiesComponent], - providers: [provideMockStore({ initialState: state }), { provide: ActionsSubject, useValue: actionSub }], + providers: [ + provideMockStore({ initialState: state }), + { provide: ActionsSubject, useValue: actionSub }, + { provide: ToastrService, useValue: toastrServiceStub } + ], imports: [FormsModule, ReactiveFormsModule], }).compileComponents(); store = TestBed.inject(MockStore); @@ -273,6 +282,17 @@ describe('DetailsFieldsComponent', () => { expect(component.saveEntry.emit).toHaveBeenCalledWith(data); }); + + it('displays error message when the date selected is in the future', () => { + spyOn(toastrServiceStub, 'error'); + + const futureDate = moment().add(1, 'days').format('YYYY-MM-DD'); + component.entryForm.setValue({ ...formValues, entry_date: futureDate }); + component.onSubmit(); + + expect(toastrServiceStub.error).toHaveBeenCalled(); + }); + /* TODO As part of https://github.com/ioet/time-tracker-ui/issues/424 a new parameter was added to the details-field-component, and now these couple of tests are failing. A solution to this error might be generate a Test Wrapper Component. More details here: diff --git a/src/app/modules/shared/components/details-fields/details-fields.component.ts b/src/app/modules/shared/components/details-fields/details-fields.component.ts index 214dbb9ce..4e999be2e 100644 --- a/src/app/modules/shared/components/details-fields/details-fields.component.ts +++ b/src/app/modules/shared/components/details-fields/details-fields.component.ts @@ -15,6 +15,7 @@ import { EntryState } from '../../../time-clock/store/entry.reducer'; import * as entryActions from '../../../time-clock/store/entry.actions'; import { getCreateError, getUpdateError } from 'src/app/modules/time-clock/store/entry.selectors'; import { SaveEntryEvent } from './save-entry-event'; +import { ToastrService } from 'ngx-toastr'; type Merged = TechnologyState & ProjectState & ActivityState & EntryState; @@ -37,7 +38,7 @@ export class DetailsFieldsComponent implements OnChanges, OnInit { shouldRestartEntry = false; constructor(private formBuilder: FormBuilder, private store: Store, - private actionsSubject$: ActionsSubject) { + private actionsSubject$: ActionsSubject, private toastrService: ToastrService) { this.entryForm = this.formBuilder.group({ project_id: ['', Validators.required], activity_id: ['', Validators.required], @@ -168,6 +169,13 @@ export class DetailsFieldsComponent implements OnChanges, OnInit { if (this.goingToWorkOnThis) { delete entry.end_date; } + + const isEntryDateInTheFuture = new Date(entryDate) > new Date(); + if (isEntryDateInTheFuture) { + this.toastrService.error('You cannot start a time-entry in the future'); + return; + } + this.saveEntry.emit({ entry, shouldRestartEntry: this.shouldRestartEntry }); }