Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
fix: #572 correct names of tests, messages and variables
  • Loading branch information
PaulRC-ioet committed Nov 30, 2020
commit a5c28f8083860c76f15156120f1339bbe7af3ad9
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { LoadActiveEntry, EntryActionTypes } from './../../store/entry.actions';
import { LoadActiveEntry, EntryActionTypes, UpdateEntry } from './../../store/entry.actions';
import { ActivityManagementActionTypes } from './../../../activities-management/store/activity-management.actions';
import {async, ComponentFixture, TestBed} from '@angular/core/testing';
import {MockStore, provideMockStore} from '@ngrx/store/testing';
Expand Down Expand Up @@ -28,8 +28,8 @@ describe('EntryFieldsComponent', () => {
warning: (message?: string, title?: string, override?: Partial<IndividualConfig>) => { }
};
const lastDate = moment().format('YYYY-MM-DD');
const startHourTest = moment().add(-5, 'hours').format('HH:mm:ss');
const endHourTest = moment().add(-3, 'hours').format('HH:mm:ss');
const startHourTest = moment().subtract(5, 'hours').format('HH:mm:ss');
const endHourTest = moment().subtract(3, 'hours').format('HH:mm:ss');
const lastStartHourEntryEntered = new Date(`${lastDate}T${startHourTest.trim()}`).toISOString();
const lastEndHourEntryEntered = new Date(`${lastDate}T${endHourTest.trim()}`).toISOString();

Expand Down Expand Up @@ -161,26 +161,26 @@ describe('EntryFieldsComponent', () => {
expect(toastrServiceStub.error).toHaveBeenCalled();
});

it('displays error message when new hour entered is in the past of other entry', () => {
it('Displays an error message when the active entry has start_time before the start_time of another entry', () => {
component.newData = entry;
component.activeEntry = entry ;
component.setDataToUpdate(entry);
spyOn(toastrServiceStub, 'error');

const hourInTheFuture = moment().add(-6, 'hour').format('HH:mm:ss');
component.entryForm.patchValue({ start_hour : hourInTheFuture});
const hourInThePast = moment().subtract(6, 'hour').format('HH:mm:ss');
component.entryForm.patchValue({ start_hour : hourInThePast});
component.onUpdateStartHour();

expect(toastrServiceStub.error).toHaveBeenCalled();
});

it('If start hour is in the past of other entry, reset to initial start_date in form', () => {
it('should reset to current start_date when start_date has an error', () => {
component.newData = entry;
component.activeEntry = entry ;
component.setDataToUpdate(entry);

const newHour = moment().add(-6, 'hours').format('HH:mm:ss');
component.entryForm.patchValue({ start_hour : newHour});
const updatedTime = moment().subtract(6, 'hours').format('HH:mm:ss');
component.entryForm.patchValue({ start_hour : updatedTime});

spyOn(component.entryForm, 'patchValue');
component.onUpdateStartHour();
Expand Down Expand Up @@ -213,35 +213,38 @@ describe('EntryFieldsComponent', () => {
it('when a start hour is updated, then dispatch UpdateActiveEntry', () => {
component.activeEntry = entry ;
component.setDataToUpdate(entry);
const newHour = moment().format('HH:mm:ss');
component.entryForm.patchValue({ start_hour : newHour});
const updatedTime = moment().format('HH:mm:ss');
component.entryForm.patchValue({ start_hour : updatedTime});
spyOn(store, 'dispatch');

component.onUpdateStartHour();
expect(store.dispatch).toHaveBeenCalled();
});

it('when a start hour is update, then select the last time entry', async(() => {
it('When start_time is updated, component.last_entry is equal to time entry in the position 1', async(() => {
component.activeEntry = entry ;
component.setDataToUpdate(entry);
const newHour = moment().format('HH:mm:ss');
const updatedTime = moment().format('HH:mm:ss');

component.entryForm.patchValue({ start_hour : newHour});
component.entryForm.patchValue({ start_hour : updatedTime});
component.onUpdateStartHour();

expect(component.lastEntry).toBe(state.entries.timeEntriesDataSource.data[1]);
}));

it('when a start hour is updated in other time entry, then dispatch UpdateEntry and UpdateEntryRunning', () => {
it('When start_time is updated for a time entry. UpdateEntry and UpdateEntryRuning actions are dispatched', () => {
component.activeEntry = entry ;
component.setDataToUpdate(entry);

const newHour = moment().add(-4, 'hours').format('HH:mm:ss');
component.entryForm.patchValue({ start_hour : newHour});
const lastEntry = state.entries.timeEntriesDataSource.data[1];
const updatedTime = moment().subtract(4, 'hours').format('HH:mm:ss');
const lastStartHourEntryEnteredTest = new Date(`${lastDate}T${updatedTime.trim()}`).toISOString();
component.entryForm.patchValue({ start_hour : updatedTime});
spyOn(store, 'dispatch');

component.onUpdateStartHour();

expect(store.dispatch).toHaveBeenCalledTimes(2);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might want to verify here the above-mentioned actions have been dispatched.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, it's tested

expect(store.dispatch).toHaveBeenCalledWith(new UpdateEntry({id: lastEntry.id, end_date: lastStartHourEntryEnteredTest}));
});

it('when a technology is added, then dispatch UpdateActiveEntry', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { TechnologyState } from '../../../shared/store/technology.reducers';
import { ActivityState, LoadActivities } from '../../../activities-management/store';

import * as entryActions from '../../store/entry.actions';

import { get } from 'lodash';
import * as moment from 'moment';
import { ToastrService } from 'ngx-toastr';
import { formatDate } from '@angular/common';
Expand All @@ -37,7 +37,6 @@ export class EntryFieldsComponent implements OnInit {
private actionsSubject$: ActionsSubject,
private toastrService: ToastrService
) {
this.store.dispatch(new entryActions.LoadEntries(new Date().getMonth() + 1));
this.entryForm = this.formBuilder.group({
description: '',
uri: '',
Expand All @@ -49,7 +48,7 @@ export class EntryFieldsComponent implements OnInit {

ngOnInit(): void {
this.store.dispatch(new LoadActivities());

this.store.dispatch(new entryActions.LoadEntries(new Date().getMonth() + 1));
this.actionsSubject$
.pipe(filter((action: any) => action.type === ActivityManagementActionTypes.LOAD_ACTIVITIES_SUCCESS))
.subscribe((action) => {
Expand Down Expand Up @@ -122,6 +121,7 @@ export class EntryFieldsComponent implements OnInit {
}

onUpdateStartHour() {
this.getLastEntry();
const startDate = formatDate(this.activeEntry.start_date, 'yyyy-MM-dd', 'en');
const newHourEntered = new Date(`${startDate}T${this.entryForm.value.start_hour.trim()}`).toISOString();
const isEntryDateInTheFuture = moment(newHourEntered).isAfter(moment());
Expand All @@ -131,12 +131,10 @@ export class EntryFieldsComponent implements OnInit {
return;
}

this.getLastEntry();

const isFirstEntry = this.lastEntry !== undefined ? this.lastEntry.start_date : moment().add(-1, 'hours');
const isEntryDateInLastStartDate = moment(newHourEntered).isBefore(isFirstEntry);
const lastEntryStartDate = get(this.lastEntry, 'start_date', moment().subtract(1, 'hours'));
const isEntryDateInLastStartDate = moment(newHourEntered).isSameOrBefore(lastEntryStartDate);
if (isEntryDateInLastStartDate) {
this.toastrService.error('You cannot start a time-entry before another time-entry');
this.toastrService.error('There is another time entry registered in this time range');
this.entryForm.patchValue({ start_hour: this.newData.start_hour });
return;
}
Expand All @@ -145,8 +143,8 @@ export class EntryFieldsComponent implements OnInit {
}

private dispatchEntries(newHourEntered) {
const isFirstEntry = this.lastEntry !== undefined ? this.lastEntry.end_date : moment().add(-1, 'hours');
const isInLastEntry = moment(newHourEntered).isBefore(isFirstEntry);
const lastEntryEndDate = get(this.lastEntry, 'end_date', moment().subtract(1, 'hours'));
const isInLastEntry = moment(newHourEntered).isBefore(lastEntryEndDate);
if (isInLastEntry) {
this.store.dispatch(new entryActions.UpdateEntry({ id: this.lastEntry.id, end_date: newHourEntered }));
}
Expand Down
7 changes: 6 additions & 1 deletion src/app/modules/users/store/user.actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export enum UserActionTypes {
LOAD_USERS = '[User] LOAD_USERS',
LOAD_USERS_SUCCESS = '[User] LOAD_USERS_SUCCESS',
LOAD_USERS_FAIL = '[User] LOAD_USERS_FAIL',
DEFAULT_USER = '[USER] DEFAULT_USER',
}

export class LoadUsers implements Action {
Expand All @@ -21,4 +22,8 @@ export class LoadUsersFail implements Action {
constructor(public error: string) {}
}

export type UserActions = LoadUsers | LoadUsersSuccess | LoadUsersFail;
export class DefaultUser implements Action {
public readonly type = UserActionTypes.DEFAULT_USER;
}

export type UserActions = LoadUsers | LoadUsersSuccess | LoadUsersFail | DefaultUser;
6 changes: 6 additions & 0 deletions src/app/modules/users/store/user.reducer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,10 @@ describe('userReducer', () => {
expect(state.isLoading).toEqual(false);
expect(state.data.length).toBe(0);
});

it('on Default, ', () => {
const action = new actions.DefaultUser();
const state = userReducer(initialState, action);
expect(state).toEqual(initialState);
});
});