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
Next Next commit
fix: #198 include all list projects on time-clock
  • Loading branch information
enriquezrene committed Apr 30, 2020
commit 9bf4304a8a5ca8055909e57d45c1c45c170e1499
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Activity } from '../../shared/models';
import { ActivityService } from './../services/activity.service';
import { ActivitiesManagementComponent } from './activities-management.component';
import { ActionsSubject } from '@ngrx/store';
import { ActivityManagementActionTypes } from '../store';

describe('ActivitiesManagementComponent', () => {
let component: ActivitiesManagementComponent;
Expand Down Expand Up @@ -45,6 +46,12 @@ describe('ActivitiesManagementComponent', () => {
expect(component.setDataNotification).toHaveBeenCalledWith(action.type);
});

it('has a succesfull message on CREATE_ACTIVITY_SUCCESS', () => {
component.setDataNotification(ActivityManagementActionTypes.CREATE_ACTIVITY_SUCCESS);

expect(component.notificationMsg).toBe('The activity has been saved successfully.');
});

it('should destroy the subscription', () => {
component.actionsSubscription = new Subscription();
const subscription = spyOn(component.actionsSubscription, 'unsubscribe');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
ResetCustomerToEdit,
} from 'src/app/modules/customer-management/store';
import { LoadProjectTypes } from '../../../projects-type/store';
import { LoadProjects } from '../../../projects/components/store/project.actions';
import { LoadCustomerProjects } from '../../../projects/components/store/project.actions';

@Component({
selector: 'app-create-customer',
Expand Down Expand Up @@ -86,7 +86,7 @@ export class CreateCustomerComponent implements OnInit, OnDestroy {
setDataToUpdate(customerData: Customer) {
if (customerData) {
this.store.dispatch(new LoadProjectTypes(customerData.id));
this.store.dispatch(new LoadProjects(customerData.id));
this.store.dispatch(new LoadCustomerProjects(customerData.id));
this.changeValueAreTabsActives.emit(true);
this.customerForm.setValue({
name: customerData.name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ describe('InputProjectComponent', () => {
let getProjectToEditMock;

const state = {
projectList: [{ id: '', name: '', project_type_id: '' }],
projects: [{ id: '', name: '', project_type_id: '' }],
customerProjects: [{ id: '', name: '', project_type_id: '' }],
isLoading: false,
message: '',
projectToEdit: undefined,
Expand Down Expand Up @@ -75,6 +76,8 @@ describe('InputProjectComponent', () => {

it('should reset form #onSubmit and dispatch UpdateProject action', () => {
const currentState = {
projects: [],
customerProjects: [],
data: [project],
isLoading: false,
message: '',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,44 +1,46 @@
import { getProjects } from './../store/project.selectors';
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { MockStore, provideMockStore } from '@ngrx/store/testing';
import { NgxPaginationModule } from 'ngx-pagination';
import { Subscription } from 'rxjs';
import { ProjectListComponent } from './project-list.component';
import { ProjectState } from '../store/project.reducer';
import { allProjects } from '../store/project.selectors';
import { getCustomerProjects } from '../store/project.selectors';
import { SetProjectToEdit, DeleteProject } from '../store/project.actions';
import { FilterProjectPipe } from '../../../../../shared/pipes';

describe('ProjectListComponent', () => {
let component: ProjectListComponent;
let fixture: ComponentFixture<ProjectListComponent>;
let store: MockStore<ProjectState>;
let allProjectsSelectorMock;
let getCustomerProjectsSelectorMock;
let allCustomerProjectsSelectorMock;

const state = {
projectList: [{ id: 'id', name: 'name', project_type_id: '' }],
const state: ProjectState = {
projects: [],
customerProjects: [],
isLoading: false,
message: '',
projectToEdit: undefined,
};

const project = { id: '123', name: 'aaa', description: 'xxx', project_type_id: '1234' };

beforeEach(async(() => {
beforeEach(
() => {
TestBed.configureTestingModule({
imports: [NgxPaginationModule],
declarations: [ProjectListComponent, FilterProjectPipe],
providers: [provideMockStore({ initialState: state })],
}).compileComponents();
}));

beforeEach(() => {
fixture = TestBed.createComponent(ProjectListComponent);
component = fixture.componentInstance;
fixture.detectChanges();

store = TestBed.inject(MockStore);
store.setState(state);
allProjectsSelectorMock = store.overrideSelector(allProjects, state);
getCustomerProjectsSelectorMock = store.overrideSelector(getCustomerProjects, state);
allCustomerProjectsSelectorMock = store.overrideSelector(getProjects, state.projects);
component.projectsSubscription = new Subscription();
});

Expand All @@ -50,6 +52,12 @@ describe('ProjectListComponent', () => {
expect(component).toBeTruthy();
});

it('loads projects from state onInit', () => {
component.ngOnInit();

expect(component.projects).toBe(state.customerProjects);
});

it('should destroy the subscriptions', () => {
component.projectsSubscription = new Subscription();
const subscription = spyOn(component.projectsSubscription, 'unsubscribe');
Expand All @@ -60,10 +68,12 @@ describe('ProjectListComponent', () => {
});

it('updateProject, should dispatch SetProjectToEdit action', () => {
spyOn(store, 'dispatch');
component.projectsSubscription = new Subscription();
const subscription = spyOn(component.projectsSubscription, 'unsubscribe');
spyOn(store, 'dispatch');

component.updateProject(project);

component.ngOnDestroy();

expect(subscription).toHaveBeenCalledTimes(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Subscription } from 'rxjs';
import { ITEMS_PER_PAGE } from 'src/environments/environment';
import { Project } from 'src/app/modules/shared/models';
import { ProjectState } from '../store/project.reducer';
import { allProjects } from '../store/project.selectors';
import { getCustomerProjects } from '../store/project.selectors';
import * as actions from '../store/project.actions';

@Component({
Expand All @@ -24,10 +24,10 @@ export class ProjectListComponent implements OnInit, OnDestroy {
constructor(private store: Store<ProjectState>) {}

ngOnInit(): void {
const projects$ = this.store.pipe(select(allProjects));
const projects$ = this.store.pipe(select(getCustomerProjects));
this.projectsSubscription = projects$.subscribe((response) => {
this.isLoading = response.isLoading;
this.projects = response.projectList;
this.projects = response.customerProjects;
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ export class ProjectService {
return this.http.get<Project[]>(this.url, { params });
}

getAllProjects(): Observable<Project[]> {
return this.http.get<Project[]>(this.url);
}

createProject(projectData): Observable<any> {
return this.http.post<Project[]>(this.url, projectData);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import * as actions from './project.actions';

describe('Actions for Projects', () => {
it('LoadProjectsSuccess type is ProjectActionTypes.LOAD_PROJECTS_SUCCESS', () => {
const loadProjectsSuccess = new actions.LoadProjectsSuccess([]);
expect(loadProjectsSuccess.type).toEqual(actions.ProjectActionTypes.LOAD_PROJECTS_SUCCESS);
it('LoadCustomerProjectsSuccess type is ProjectActionTypes.LOAD_CUSTOMER_PROJECTS_SUCCESS', () => {
const LoadCustomerProjectsSuccess = new actions.LoadCustomerProjectsSuccess([]);
expect(LoadCustomerProjectsSuccess.type).toEqual(actions.ProjectActionTypes.LOAD_CUSTOMER_PROJECTS_SUCCESS);
});

it('LoadProjectsFail type is ProjectActionTypes.LOAD_PROJECTS_FAIL', () => {
const loadProjectsFail = new actions.LoadProjectsFail('error');
expect(loadProjectsFail.type).toEqual(actions.ProjectActionTypes.LOAD_PROJECTS_FAIL);
it('LoadCustomerProjectsFail type is ProjectActionTypes.LOAD_CUSTOMER_PROJECTS_FAIL', () => {
const LoadCustomerProjectsFail = new actions.LoadCustomerProjectsFail('error');
expect(LoadCustomerProjectsFail.type).toEqual(actions.ProjectActionTypes.LOAD_CUSTOMER_PROJECTS_FAIL);
});

it('CreateProjectSuccess type is ProjectActionTypes.CREATE_PROJECT_SUCCESS', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ export enum ProjectActionTypes {
LOAD_PROJECTS = '[Projects] LOAD_PROJECTS',
LOAD_PROJECTS_SUCCESS = '[Projects] LOAD_PROJECTS_SUCCESS',
LOAD_PROJECTS_FAIL = '[Projects] LOAD_PROJECTS_FAIL',
LOAD_CUSTOMER_PROJECTS = '[Projects] LOAD_CUSTOMER_PROJECTS',
LOAD_CUSTOMER_PROJECTS_SUCCESS = '[Projects] LOAD_CUSTOMER_PROJECTS_SUCCESS',
LOAD_CUSTOMER_PROJECTS_FAIL = '[Projects] LOAD_CUSTOMER_PROJECTS_FAIL',
CREATE_PROJECT = '[Projects] CREATE_PROJECT',
CREATE_PROJECT_SUCCESS = '[Projects] CREATE_PROJECT_SUCCESS',
CREATE_PROJECT_FAIL = '[Projects] CREATE_PROJECT_FAIL',
Expand All @@ -20,7 +23,7 @@ export enum ProjectActionTypes {

export class LoadProjects implements Action {
public readonly type = ProjectActionTypes.LOAD_PROJECTS;
constructor(public customerId?: string) {}
constructor() {}
}

export class LoadProjectsSuccess implements Action {
Expand All @@ -30,7 +33,22 @@ export class LoadProjectsSuccess implements Action {

export class LoadProjectsFail implements Action {
public readonly type = ProjectActionTypes.LOAD_PROJECTS_FAIL;
constructor(public error: string) {}
}


export class LoadCustomerProjects implements Action {
public readonly type = ProjectActionTypes.LOAD_CUSTOMER_PROJECTS;
constructor(public customerId: string) {}
}

export class LoadCustomerProjectsSuccess implements Action {
readonly type = ProjectActionTypes.LOAD_CUSTOMER_PROJECTS_SUCCESS;
constructor(readonly payload: Project[]) {}
}

export class LoadCustomerProjectsFail implements Action {
public readonly type = ProjectActionTypes.LOAD_CUSTOMER_PROJECTS_FAIL;
constructor(public error: string) {}
}

Expand Down Expand Up @@ -102,6 +120,9 @@ export type ProjectActions =
| LoadProjects
| LoadProjectsSuccess
| LoadProjectsFail
| LoadCustomerProjects
| LoadCustomerProjectsSuccess
| LoadCustomerProjectsFail
| CreateProject
| CreateProjectSuccess
| CreateProjectFail
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,25 @@ export class ProjectEffects {
@Effect()
loadProjects$: Observable<Action> = this.actions$.pipe(
ofType(actions.ProjectActionTypes.LOAD_PROJECTS),
mergeMap(() =>
this.projectService.getAllProjects().pipe(
map((projects) => {
return new actions.LoadProjectsSuccess(projects);
}),
catchError((error) => of(new actions.LoadProjectsFail(error)))
)
)
);

@Effect()
loadCustomerProjects$: Observable<Action> = this.actions$.pipe(
ofType(actions.ProjectActionTypes.LOAD_CUSTOMER_PROJECTS),
mergeMap((customerId) =>
this.projectService.getProjects(customerId).pipe(
map((project) => {
return new actions.LoadProjectsSuccess(project);
return new actions.LoadCustomerProjectsSuccess(project);
}),
catchError((error) => of(new actions.LoadProjectsFail(error)))
catchError((error) => of(new actions.LoadCustomerProjectsFail(error)))
)
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,29 @@ import * as actions from './project.actions';
import { projectReducer, ProjectState } from './project.reducer';

describe('projectReducer', () => {
const initialState: ProjectState = { projectList: [], isLoading: false, message: '', projectToEdit: undefined };
const initialState: ProjectState = {
projects: [{ id: 'id', name: 'name', project_type_id: '' }],
customerProjects: [], isLoading: false, message: '', projectToEdit: undefined
};
const project: Project = { id: '1', name: 'aaa', description: 'bbb', project_type_id: '123' };

it('on LoadProjects, isLoading is true', () => {
const action = new actions.LoadProjects();
const action = new actions.LoadCustomerProjects('1');
const state = projectReducer(initialState, action);
expect(state.isLoading).toEqual(true);
});

it('on LoadProjectsSuccess, projectsFound are saved in the store', () => {
it('on LoadCustomerProjectsSuccess, projectsFound are saved in the store', () => {
const projectsFound: Project[] = [{ id: '', name: '', description: '', project_type_id: '123' }];
const action = new actions.LoadProjectsSuccess(projectsFound);
const action = new actions.LoadCustomerProjectsSuccess(projectsFound);
const state = projectReducer(initialState, action);
expect(state.projectList).toEqual(projectsFound);
expect(state.customerProjects).toEqual(projectsFound);
});

it('on LoadProjectsFail, projectList equal []', () => {
const action = new actions.LoadProjectsFail('error');
it('on LoadCustomerProjectsFail, customerProjects equal []', () => {
const action = new actions.LoadCustomerProjectsFail('error');
const state = projectReducer(initialState, action);
expect(state.projectList).toEqual([]);
expect(state.customerProjects).toEqual([]);
});

it('on CreateProject, isLoading is true', () => {
Expand All @@ -36,15 +39,15 @@ describe('projectReducer', () => {
const action = new actions.CreateProjectSuccess(project);
const state = projectReducer(initialState, action);

expect(state.projectList).toEqual([project]);
expect(state.customerProjects).toEqual([project]);
expect(state.isLoading).toEqual(false);
});

it('on CreateProjectFail, projectList equal []', () => {
it('on CreateProjectFail, customerProjects equal []', () => {
const action = new actions.CreateProjectFail('error');
const state = projectReducer(initialState, action);

expect(state.projectList).toEqual([]);
expect(state.customerProjects).toEqual([]);
expect(state.isLoading).toEqual(false);
});

Expand All @@ -57,23 +60,24 @@ describe('projectReducer', () => {

it('on UpdateProjectSuccess, project is saved in the store', () => {
const currentState: ProjectState = {
projectList: [project],
projects: [project],
customerProjects: [project],
isLoading: false,
message: '',
projectToEdit: project,
};
const action = new actions.UpdateProjectSuccess(project);
const state = projectReducer(currentState, action);

expect(state.projectList).toEqual([project]);
expect(state.customerProjects).toEqual([project]);
expect(state.isLoading).toEqual(false);
});

it('on UpdateProjectFail, projectList equal []', () => {
it('on UpdateProjectFail, customerProjects equal []', () => {
const action = new actions.UpdateProjectFail('error');
const state = projectReducer(initialState, action);

expect(state.projectList).toEqual([]);
expect(state.customerProjects).toEqual(state.customerProjects);
expect(state.isLoading).toEqual(false);
});

Expand Down Expand Up @@ -106,7 +110,8 @@ describe('projectReducer', () => {

it('on DeleteProjectSuccess, message equal to Project removed successfully!', () => {
const currentState: ProjectState = {
projectList: [project],
projects: [project],
customerProjects: [project],
isLoading: false,
message: '',
projectToEdit: undefined,
Expand All @@ -115,7 +120,7 @@ describe('projectReducer', () => {
const action = new actions.DeleteProjectSuccess(projectIdToDelete);

const state = projectReducer(currentState, action);
expect(state.projectList).toEqual([]);
expect(state.customerProjects).toEqual([]);
expect(state.message).toEqual('Project removed successfully!');
});

Expand All @@ -124,7 +129,7 @@ describe('projectReducer', () => {
const action = new actions.DeleteProjectFail(projectToEdit);

const state = projectReducer(initialState, action);
expect(state.projectList).toEqual([]);
expect(state.customerProjects).toEqual([]);
expect(state.isLoading).toEqual(false);
expect(state.message).toEqual('Something went wrong deleting the project!');
});
Expand Down
Loading