Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fix: validate future date #459
  • Loading branch information
Angeluz-07 committed Jul 7, 2020
commit 93ca91e6f35fe3eeaa85cea978f0517d5a81ee35
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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;
Expand All @@ -28,6 +30,9 @@ describe('DetailsFieldsComponent', () => {
let entryToEdit;
let formValues;
const actionSub: ActionsSubject = new ActionsSubject();
const toastrServiceStub = {
error: (message?: string, title?: string, override?: Partial<IndividualConfig>) => { }
};

const state = {
projects: {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

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

constructor(private formBuilder: FormBuilder, private store: Store<Merged>,
private actionsSubject$: ActionsSubject) {
private actionsSubject$: ActionsSubject, private toastrService: ToastrService) {
this.entryForm = this.formBuilder.group({
project_id: ['', Validators.required],
activity_id: ['', Validators.required],
Expand Down Expand Up @@ -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 });
}

Expand Down