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
6 changes: 3 additions & 3 deletions src/app/modules/shared/models/entry.model.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
export interface Entry {
running?: boolean;
id: string;
id?: string;
start_date: Date;
end_date?: Date;
activity_id?: string;
technologies: string[];
technologies?: string[];
uri?: string;
activity_name?: string;
description?: string;
owner_email?: string;

project_id?: string;
project_name: string;
project_name?: string;

customer_id?: string;
customer_name?: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { LoadActiveEntry, EntryActionTypes } 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';
import {FormsModule, ReactiveFormsModule} from '@angular/forms';
import { FormsModule, ReactiveFormsModule, FormBuilder } from '@angular/forms';

import {TechnologyState} from '../../../shared/store/technology.reducers';
import {allTechnologies} from '../../../shared/store/technology.selectors';
import {EntryFieldsComponent} from './entry-fields.component';
import {ProjectState} from '../../../customer-management/components/projects/components/store/project.reducer';
import {getCustomerProjects} from '../../../customer-management/components/projects/components/store/project.selectors';
import * as entryActions from '../../store/entry.actions';
import { ActionsSubject } from '@ngrx/store';

describe('EntryFieldsComponent', () => {
type Merged = TechnologyState & ProjectState;
Expand All @@ -16,6 +18,8 @@ describe('EntryFieldsComponent', () => {
let store: MockStore<Merged>;
let mockTechnologySelector;
let mockProjectsSelector;
let entryForm;
const actionSub: ActionsSubject = new ActionsSubject();

const state = {
projects: {
Expand Down Expand Up @@ -60,10 +64,11 @@ describe('EntryFieldsComponent', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [EntryFieldsComponent],
providers: [provideMockStore({initialState: state})],
providers: [provideMockStore({initialState: state}), { provide: ActionsSubject, useValue: actionSub }],
imports: [FormsModule, ReactiveFormsModule],
}).compileComponents();
store = TestBed.inject(MockStore);
entryForm = TestBed.inject(FormBuilder);
mockTechnologySelector = store.overrideSelector(allTechnologies, state.technologies);
mockProjectsSelector = store.overrideSelector(getCustomerProjects, state.projects);
}));
Expand Down Expand Up @@ -96,12 +101,6 @@ describe('EntryFieldsComponent', () => {
expect(component.selectedTechnologies).toEqual([]);
});

it('should dispatch UpdateActiveEntry action #onSubmit', () => {
spyOn(store, 'dispatch');
component.onSubmit();
expect(store.dispatch).toHaveBeenCalledWith(new entryActions.UpdateEntryRunning(entry));
});

it('when a technology is added, then dispatch UpdateActiveEntry', () => {
const addedTechnologies = ['react'];
spyOn(store, 'dispatch');
Expand All @@ -120,4 +119,114 @@ describe('EntryFieldsComponent', () => {
expect(store.dispatch).toHaveBeenCalled();

});

it('uses the form to check if is valid or not', () => {
entryForm.valid = false;

const result = component.entryFormIsValidate();

expect(result).toBe(entryForm.valid);
});

it('dispatches an action when onSubmit is called', () => {
spyOn(store, 'dispatch');

component.onSubmit();

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

it('dispatches an action when onTechnologyRemoved is called', () => {
spyOn(store, 'dispatch');

component.onTechnologyRemoved(['foo']);

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


it('sets the technologies on the class when entry has technologies', () => {
const entryData = { ...entry, technologies: ['foo']};

component.setDataToUpdate(entryData);

expect(component.selectedTechnologies).toEqual(entryData.technologies);
});


it('activites are populated using the payload of the action', () => {
const actionSubject = TestBed.inject(ActionsSubject) as ActionsSubject;
const action = {
type: ActivityManagementActionTypes.LOAD_ACTIVITIES_SUCCESS,
payload: [],
};

actionSubject.next(action);

expect(component.activities).toEqual(action.payload);
});

it('LoadActiveEntry is dispatchen after LOAD_ACTIVITIES_SUCCESS', () => {
spyOn(store, 'dispatch');

const actionSubject = TestBed.inject(ActionsSubject) as ActionsSubject;
const action = {
type: ActivityManagementActionTypes.LOAD_ACTIVITIES_SUCCESS,
payload: [],
};

actionSubject.next(action);

expect(store.dispatch).toHaveBeenCalledWith(new LoadActiveEntry());
});

it('when entry has an end_date null then LoadActiveEntry is dispatched', () => {
spyOn(store, 'dispatch');

const actionSubject = TestBed.inject(ActionsSubject) as ActionsSubject;
const action = {
type: EntryActionTypes.CREATE_ENTRY_SUCCESS,
payload: {end_date: null},
};

actionSubject.next(action);

expect(store.dispatch).toHaveBeenCalledWith(new LoadActiveEntry());
});

it('when entry has an end_date then nothing is dispatched', () => {
spyOn(store, 'dispatch');

const actionSubject = TestBed.inject(ActionsSubject) as ActionsSubject;
const action = {
type: EntryActionTypes.CREATE_ENTRY_SUCCESS,
payload: {end_date: new Date()},
};

actionSubject.next(action);

expect(store.dispatch).toHaveBeenCalledTimes(0);
});

it('activeEntry is populated using the payload of LOAD_ACTIVE_ENTRY_SUCCESS', () => {
const actionSubject = TestBed.inject(ActionsSubject) as ActionsSubject;
const action = {
type: EntryActionTypes.LOAD_ACTIVE_ENTRY_SUCCESS,
payload: entry,
};

actionSubject.next(action);

expect(component.activeEntry).toBe(action.payload);
});

it('if entryData is null selectedTechnologies is not modified', () => {
const initialTechnologies = ['foo', 'bar'];
component.selectedTechnologies = initialTechnologies;

component.setDataToUpdate(null);

expect(component.selectedTechnologies).toBe(initialTechnologies);
});

});
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { getActiveTimeEntry } from './../../store/entry.selectors';
import { ActivityManagementActionTypes } from './../../../activities-management/store/activity-management.actions';
import { EntryActionTypes, LoadActiveEntry } from './../../store/entry.actions';
import { filter } from 'rxjs/operators';
import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup } from '@angular/forms';
import { select, Store } from '@ngrx/store';
import { Store, ActionsSubject } from '@ngrx/store';

import { Activity, NewEntry } from '../../../shared/models';
import { ProjectState } from '../../../customer-management/components/projects/components/store/project.reducer';
import { TechnologyState } from '../../../shared/store/technology.reducers';
import { ActivityState, allActivities, LoadActivities } from '../../../activities-management/store';
import { ActivityState, LoadActivities } from '../../../activities-management/store';

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

Expand All @@ -24,37 +26,44 @@ export class EntryFieldsComponent implements OnInit {
activeEntry;
newData;

constructor(private formBuilder: FormBuilder, private store: Store<Merged>) {
constructor(private formBuilder: FormBuilder, private store: Store<Merged>, private actionsSubject$: ActionsSubject) {
this.entryForm = this.formBuilder.group({
description: '',
uri: '',
activity_id: '-1',
activity_id: '',
});
}

ngOnInit(): void {
this.store.dispatch(new LoadActivities());
const activities$ = this.store.pipe(select(allActivities));
activities$.subscribe((response) => {
this.activities = response;
this.loadActiveEntry();

this.actionsSubject$.pipe(
filter((action: any) => (action.type === ActivityManagementActionTypes.LOAD_ACTIVITIES_SUCCESS))
).subscribe((action) => {
this.activities = action.payload;
this.store.dispatch(new LoadActiveEntry());
});
}

loadActiveEntry() {
const activeEntry$ = this.store.pipe(select(getActiveTimeEntry));
activeEntry$.subscribe((response) => {
if (response) {
this.activeEntry = response;
this.setDataToUpdate(this.activeEntry);
this.newData = {
id: this.activeEntry.id,
project_id: this.activeEntry.project_id,
uri: this.activeEntry.uri,
activity_id: this.activeEntry.activity_id,
};
this.actionsSubject$.pipe(
filter((action: any) => (action.type === EntryActionTypes.CREATE_ENTRY_SUCCESS))
).subscribe((action) => {
if (!action.payload.end_date) {
this.store.dispatch(new LoadActiveEntry());
}
});

this.actionsSubject$.pipe(
filter((action: any) => ( action.type === EntryActionTypes.LOAD_ACTIVE_ENTRY_SUCCESS ))
).subscribe((action) => {
this.activeEntry = action.payload;
this.setDataToUpdate(this.activeEntry);
this.newData = {
id: this.activeEntry.id,
project_id: this.activeEntry.project_id,
uri: this.activeEntry.uri,
activity_id: this.activeEntry.activity_id,
};
});
}

get activity_id() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { ToastrService, IndividualConfig } from 'ngx-toastr';
import { SwitchTimeEntry, ClockIn } from './../../store/entry.actions';
import { FormBuilder } from '@angular/forms';
import { MockStore, provideMockStore } from '@ngrx/store/testing';
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { provideMockStore, MockStore } from '@ngrx/store/testing';
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { AutocompleteLibModule } from 'angular-ng-autocomplete';
import { IndividualConfig, ToastrService } from 'ngx-toastr';
import { Subscription } from 'rxjs';
import { ProjectState } from '../../../customer-management/components/projects/components/store/project.reducer';
import { getCustomerProjects } from '../../../customer-management/components/projects/components/store/project.selectors';
import { FilterProjectPipe } from '../../../shared/pipes';
import { CreateEntry, UpdateEntryRunning } from '../../store/entry.actions';
import { SwitchTimeEntry } from './../../store/entry.actions';
import { UpdateEntryRunning } from '../../store/entry.actions';
import { ProjectListHoverComponent } from './project-list-hover.component';

describe('ProjectListHoverComponent', () => {
Expand Down Expand Up @@ -68,7 +68,7 @@ describe('ProjectListHoverComponent', () => {

component.clockIn(1, 'customer', 'project');

expect(store.dispatch).toHaveBeenCalledWith(jasmine.any(CreateEntry));
expect(store.dispatch).toHaveBeenCalledWith(jasmine.any(ClockIn));
});

it('dispatch a UpdateEntryRunning action on updateProject', () => {
Expand Down Expand Up @@ -118,6 +118,7 @@ describe('ProjectListHoverComponent', () => {
.toHaveBeenCalledWith({ project_id: 'customer - xyz'});
});


// TODO Fix this test since it is throwing this error
// Expected spy dispatch to have been called with:
// [CreateEntry({ payload: Object({ project_id: '1', start_date: '2020-07-27T22:30:26.743Z', timezone_offset: 300 }),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export class ProjectListHoverComponent implements OnInit, OnDestroy {
start_date: new Date().toISOString(),
timezone_offset: new Date().getTimezoneOffset(),
};
this.store.dispatch(new entryActions.CreateEntry(entry));
this.store.dispatch(new entryActions.ClockIn(entry));
this.projectsForm.setValue( { project_id: `${customerName} - ${name}`, } );
}

Expand Down
9 changes: 9 additions & 0 deletions src/app/modules/time-clock/services/entry.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,13 @@ describe('EntryService', () => {
const restartEntryRequest = httpMock.expectOne( `${service.baseUrl}/${entry}/restart`);
expect(restartEntryRequest.request.method).toBe('POST');
});

it('entries are found by project id with a limit 2 by default', () => {
const projectId = 'project-id';

service.findEntriesByProjectId(projectId).subscribe();

const restartEntryRequest = httpMock.expectOne( `${service.baseUrl}?limit=2&project_id=${projectId}`);
expect(restartEntryRequest.request.method).toBe('GET');
});
});
5 changes: 5 additions & 0 deletions src/app/modules/time-clock/services/entry.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ export class EntryService {
return this.http.get<TimeEntriesSummary>(summaryUrl);
}

findEntriesByProjectId(projectId: string): Observable<Entry[]> {
const findEntriesByProjectURL = `${this.baseUrl}?limit=2&project_id=${projectId}`;
return this.http.get<Entry[]>(findEntriesByProjectURL);
}

loadEntriesByTimeRange(range: TimeEntriesTimeRange, userId: string): Observable<any> {
const MAX_NUMBER_OF_ENTRIES_FOR_REPORTS = 9999;
return this.http.get(this.baseUrl,
Expand Down
14 changes: 14 additions & 0 deletions src/app/modules/time-clock/store/entry.actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export enum EntryActionTypes {
LOAD_ENTRIES = '[Entry] LOAD_ENTRIES',
LOAD_ENTRIES_SUCCESS = '[Entry] LOAD_ENTRIES_SUCCESS',
LOAD_ENTRIES_FAIL = '[Entry] LOAD_ENTRIES_FAIL',
CLOCK_IN = '[Entry] CLOCK_IN',
CLOCK_IN_SUCCESS = '[Entry] CLOCK_IN_SUCCESS',
CREATE_ENTRY = '[Entry] CREATE_ENTRY',
CREATE_ENTRY_SUCCESS = '[Entry] CREATE_ENTRY_SUCCESS',
CREATE_ENTRY_FAIL = '[Entry] CREATE_ENTRY_FAIL',
Expand All @@ -39,6 +41,16 @@ export enum EntryActionTypes {
RESTART_ENTRY_FAIL = '[Entry] RESTART_ENTRY_FAIL',
}

export class ClockIn implements Action {
public readonly type = EntryActionTypes.CLOCK_IN;
constructor(readonly payload: NewEntry) {}
}

export class ClockInSuccess implements Action {
public readonly type = EntryActionTypes.CLOCK_IN_SUCCESS;
constructor() {}
}

export class SwitchTimeEntry implements Action {
public readonly type = EntryActionTypes.SWITCH_TIME_ENTRY;
constructor(readonly idEntrySwitching: string, readonly idProjectSwitching) {}
Expand Down Expand Up @@ -250,6 +262,8 @@ export class RestartEntryFail implements Action {
}

export type EntryActions =
| ClockIn
| ClockInSuccess
| LoadEntriesSummary
| LoadEntriesSummarySuccess
| LoadEntriesSummaryFail
Expand Down
Loading