Skip to content

Commit 445dba4

Browse files
authored
Merge pull request #466 from ioet/459-working-this-not-future-date
fix: #459 validate future date
2 parents 2bf3fdf + 93ca91e commit 445dba4

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

src/app/modules/shared/components/details-fields/details-fields.component.spec.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { MockStore, provideMockStore } from '@ngrx/store/testing';
55
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
66
import { formatDate } from '@angular/common';
77
import { ActionsSubject } from '@ngrx/store';
8+
import { ToastrService, IndividualConfig } from 'ngx-toastr';
89

910
import { TechnologyState } from '../../store/technology.reducers';
1011
import { allTechnologies } from '../../store/technology.selectors';
@@ -15,6 +16,7 @@ import { EntryState } from '../../../time-clock/store/entry.reducer';
1516
import * as entryActions from '../../../time-clock/store/entry.actions';
1617
import { getCreateError, getUpdateError } from 'src/app/modules/time-clock/store/entry.selectors';
1718
import { SaveEntryEvent } from './save-entry-event';
19+
import * as moment from 'moment';
1820

1921
describe('DetailsFieldsComponent', () => {
2022
type Merged = TechnologyState & ProjectState & EntryState;
@@ -28,6 +30,9 @@ describe('DetailsFieldsComponent', () => {
2830
let entryToEdit;
2931
let formValues;
3032
const actionSub: ActionsSubject = new ActionsSubject();
33+
const toastrServiceStub = {
34+
error: (message?: string, title?: string, override?: Partial<IndividualConfig>) => { }
35+
};
3136

3237
const state = {
3338
projects: {
@@ -67,7 +72,11 @@ describe('DetailsFieldsComponent', () => {
6772
beforeEach(async(() => {
6873
TestBed.configureTestingModule({
6974
declarations: [DetailsFieldsComponent, TechnologiesComponent],
70-
providers: [provideMockStore({ initialState: state }), { provide: ActionsSubject, useValue: actionSub }],
75+
providers: [
76+
provideMockStore({ initialState: state }),
77+
{ provide: ActionsSubject, useValue: actionSub },
78+
{ provide: ToastrService, useValue: toastrServiceStub }
79+
],
7180
imports: [FormsModule, ReactiveFormsModule],
7281
}).compileComponents();
7382
store = TestBed.inject(MockStore);
@@ -273,6 +282,17 @@ describe('DetailsFieldsComponent', () => {
273282

274283
expect(component.saveEntry.emit).toHaveBeenCalledWith(data);
275284
});
285+
286+
it('displays error message when the date selected is in the future', () => {
287+
spyOn(toastrServiceStub, 'error');
288+
289+
const futureDate = moment().add(1, 'days').format('YYYY-MM-DD');
290+
component.entryForm.setValue({ ...formValues, entry_date: futureDate });
291+
component.onSubmit();
292+
293+
expect(toastrServiceStub.error).toHaveBeenCalled();
294+
});
295+
276296
/*
277297
TODO As part of https://github.com/ioet/time-tracker-ui/issues/424 a new parameter was added to the details-field-component,
278298
and now these couple of tests are failing. A solution to this error might be generate a Test Wrapper Component. More details here:

src/app/modules/shared/components/details-fields/details-fields.component.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { EntryState } from '../../../time-clock/store/entry.reducer';
1515
import * as entryActions from '../../../time-clock/store/entry.actions';
1616
import { getCreateError, getUpdateError } from 'src/app/modules/time-clock/store/entry.selectors';
1717
import { SaveEntryEvent } from './save-entry-event';
18+
import { ToastrService } from 'ngx-toastr';
1819

1920
type Merged = TechnologyState & ProjectState & ActivityState & EntryState;
2021

@@ -37,7 +38,7 @@ export class DetailsFieldsComponent implements OnChanges, OnInit {
3738
shouldRestartEntry = false;
3839

3940
constructor(private formBuilder: FormBuilder, private store: Store<Merged>,
40-
private actionsSubject$: ActionsSubject) {
41+
private actionsSubject$: ActionsSubject, private toastrService: ToastrService) {
4142
this.entryForm = this.formBuilder.group({
4243
project_id: ['', Validators.required],
4344
activity_id: ['', Validators.required],
@@ -168,6 +169,13 @@ export class DetailsFieldsComponent implements OnChanges, OnInit {
168169
if (this.goingToWorkOnThis) {
169170
delete entry.end_date;
170171
}
172+
173+
const isEntryDateInTheFuture = new Date(entryDate) > new Date();
174+
if (isEntryDateInTheFuture) {
175+
this.toastrService.error('You cannot start a time-entry in the future');
176+
return;
177+
}
178+
171179
this.saveEntry.emit({ entry, shouldRestartEntry: this.shouldRestartEntry });
172180
}
173181

0 commit comments

Comments
 (0)