From 422c25d6785d3195402057412fc69f226cb82e57 Mon Sep 17 00:00:00 2001 From: Diego Tinitana Date: Fri, 15 May 2020 08:56:33 -0500 Subject: [PATCH 1/2] fix: #237 Move notification messages to ngrx effects --- .../activities-management.component.spec.ts | 109 +----------------- .../pages/activities-management.component.ts | 51 +------- .../store/activity-management.effects.ts | 26 ++++- .../create-customer/create-customer.spec.ts | 8 +- .../create-customer/create-customer.ts | 5 +- .../customer-list.component.spec.ts | 9 +- .../customer-list/customer-list.component.ts | 4 +- .../create-project-type.component.spec.ts | 9 +- .../create-project-type.component.ts | 5 +- .../project-type-list.component.spec.ts | 9 +- .../project-type-list.component.ts | 4 +- .../store/project-type.effects.ts | 26 ++++- .../create-project.component.spec.ts | 9 +- .../create-project.component.ts | 5 +- .../project-list.component.spec.ts | 9 +- .../project-list/project-list.component.ts | 5 +- .../components/store/project.effects.ts | 30 ++++- .../store/customer-management.effects.ts | 26 ++++- .../customer-management.selectors.spec.ts | 28 +++++ .../project-list-hover.component.spec.ts | 7 +- .../project-list-hover.component.ts | 21 ++-- .../pages/time-clock.component.spec.ts | 8 -- .../time-clock/store/entry.selectors.spec.ts | 4 + 23 files changed, 151 insertions(+), 266 deletions(-) create mode 100644 src/app/modules/customer-management/store/customer-management.selectors.spec.ts diff --git a/src/app/modules/activities-management/pages/activities-management.component.spec.ts b/src/app/modules/activities-management/pages/activities-management.component.spec.ts index d20faee04..223e7999b 100644 --- a/src/app/modules/activities-management/pages/activities-management.component.spec.ts +++ b/src/app/modules/activities-management/pages/activities-management.component.spec.ts @@ -1,33 +1,14 @@ -import { HttpClient, HttpHandler } from '@angular/common/http'; import { async, TestBed, ComponentFixture } from '@angular/core/testing'; -import { of, Subscription } from 'rxjs'; -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'; -import { ToastrService, ToastrModule, IndividualConfig } from 'ngx-toastr'; describe('ActivitiesManagementComponent', () => { let component: ActivitiesManagementComponent; let fixture: ComponentFixture; - let activityService: ActivityService; - const activitiesFromApi: Activity[] = [{ id: '123', name: 'aaa', description: 'xxx' }]; - const toastrService = { - success: (message?: string, title?: string, override?: Partial) => { }, - error: (message?: string, title?: string, override?: Partial) => { } - }; - let injectedToastrService; beforeEach(async(() => { - const actionSub: ActionsSubject = new ActionsSubject(); TestBed.configureTestingModule({ - imports: [ToastrModule.forRoot()], - declarations: [ActivitiesManagementComponent], - providers: [ActivityService, HttpClient, HttpHandler, - { provide: ActionsSubject, useValue: actionSub }, - { provide: ToastrService, useValue: toastrService } - ], + imports: [], + declarations: [ActivitiesManagementComponent] }).compileComponents(); })); @@ -35,95 +16,9 @@ describe('ActivitiesManagementComponent', () => { fixture = TestBed.createComponent(ActivitiesManagementComponent); component = fixture.componentInstance; fixture.detectChanges(); - injectedToastrService = TestBed.inject(ToastrService); - console.log(injectedToastrService); - activityService = TestBed.inject(ActivityService); - spyOn(activityService, 'getActivities').and.returnValue(of(activitiesFromApi)); }); it('should create the component', () => { expect(component).toBeTruthy(); }); - - it('should call #setDataNotification with action #ngOnInit', () => { - const actionSubject = TestBed.inject(ActionsSubject) as ActionsSubject; - spyOn(component, 'setDataNotification'); - const action = { - type: '[ActivityManagement] CREATE_ACTIVITY_SUCCESS', - }; - actionSubject.next(action); - - component.ngOnInit(); - expect(component.setDataNotification).toHaveBeenCalledWith(action.type); - }); - - it('has a succesfull message on CREATE_ACTIVITY_SUCCESS', () => { - spyOn(injectedToastrService, 'success'); - component.setDataNotification(ActivityManagementActionTypes.CREATE_ACTIVITY_SUCCESS); - - expect(injectedToastrService.success).toHaveBeenCalled(); - }); - - it('has a succesfull message on UPDATE_ACTIVITY_SUCCESS', () => { - spyOn(injectedToastrService, 'success'); - component.setDataNotification(ActivityManagementActionTypes.UPDATE_ACTIVITY_SUCCESS); - - expect(injectedToastrService.success).toHaveBeenCalled(); - }); - - it('should destroy the subscription', () => { - component.actionsSubscription = new Subscription(); - const subscription = spyOn(component.actionsSubscription, 'unsubscribe'); - - component.ngOnDestroy(); - - expect(subscription).toHaveBeenCalledTimes(1); - }); - - it('#setDataNotification should show an success notification with DELETE_ACTIVITY_SUCCESS case', () => { - spyOn(injectedToastrService, 'success').and.returnValue(of(activitiesFromApi)); - const actionSubject = TestBed.inject(ActionsSubject) as ActionsSubject; - const action = { - type: '[ActivityManagement] DELETE_ACTIVITY_SUCESS', - }; - actionSubject.next(action); - - component.setDataNotification(action.type); - expect(injectedToastrService.success).toHaveBeenCalled(); - }); - - it('shows an error notification with CREATE_ACTIVITY_FAIL case', () => { - spyOn(injectedToastrService, 'error'); - - component.setDataNotification(ActivityManagementActionTypes.CREATE_ACTIVITY_FAIL); - - expect(injectedToastrService.error).toHaveBeenCalled(); - }); - - it('shows an error notification with UPDATE_ACTIVITY_FAIL case', () => { - spyOn(injectedToastrService, 'error'); - - component.setDataNotification(ActivityManagementActionTypes.UPDATE_ACTIVITY_FAIL); - - expect(injectedToastrService.error).toHaveBeenCalled(); - }); - - it('shows an error notification with DELETE_ACTIVITY_FAIL case', () => { - spyOn(injectedToastrService, 'error'); - - component.setDataNotification(ActivityManagementActionTypes.DELETE_ACTIVITY_FAIL); - - expect(injectedToastrService.error).toHaveBeenCalled(); - }); - - it('#setDataNotification should not show an error notification with incorrect action', () => { - spyOn(injectedToastrService, 'success').and.returnValue(of(activitiesFromApi)); - const actionSubject = TestBed.inject(ActionsSubject) as ActionsSubject; - const action = { - type: '[ActivityManagement] TEST', - }; - actionSubject.next(action); - - component.setDataNotification(action.type); - }); }); diff --git a/src/app/modules/activities-management/pages/activities-management.component.ts b/src/app/modules/activities-management/pages/activities-management.component.ts index c35b4f80e..94c1de433 100644 --- a/src/app/modules/activities-management/pages/activities-management.component.ts +++ b/src/app/modules/activities-management/pages/activities-management.component.ts @@ -1,55 +1,10 @@ -import { Component, OnInit, OnDestroy } from '@angular/core'; -import { ActionsSubject } from '@ngrx/store'; -import { Subscription } from 'rxjs'; -import { ActivityManagementActionTypes } from '../store'; -import { ToastrService } from 'ngx-toastr'; +import { Component } from '@angular/core'; @Component({ selector: 'app-activities-management', templateUrl: './activities-management.component.html', styleUrls: ['./activities-management.component.scss'], }) -export class ActivitiesManagementComponent implements OnInit, OnDestroy { - actionsSubscription: Subscription; - - constructor(private actionsSubject$: ActionsSubject, private toastrService: ToastrService) {} - - ngOnInit() { - this.actionsSubscription = this.actionsSubject$.subscribe((action) => { - this.setDataNotification(action.type); - }); - } - - ngOnDestroy() { - this.actionsSubscription.unsubscribe(); - } - - setDataNotification(action: any) { - switch (action) { - case ActivityManagementActionTypes.CREATE_ACTIVITY_SUCCESS: { - this.toastrService.success('The activity has been saved successfully.'); - break; - } - case ActivityManagementActionTypes.UPDATE_ACTIVITY_SUCCESS: { - this.toastrService.success('The activity has been saved successfully.'); - break; - } - case ActivityManagementActionTypes.DELETE_ACTIVITY_SUCCESS: { - this.toastrService.success('The activity has been removed successfully.'); - break; - } - case ActivityManagementActionTypes.CREATE_ACTIVITY_FAIL: { - this.toastrService.error('An unexpected error happened, please try again later.'); - break; - } - case ActivityManagementActionTypes.UPDATE_ACTIVITY_FAIL: { - this.toastrService.error('An unexpected error happened, please try again later.'); - break; - } - case ActivityManagementActionTypes.DELETE_ACTIVITY_FAIL : { - this.toastrService.error('An unexpected error happened, please try again later.'); - break; - } - } - } +export class ActivitiesManagementComponent { + constructor() {} } diff --git a/src/app/modules/activities-management/store/activity-management.effects.ts b/src/app/modules/activities-management/store/activity-management.effects.ts index 8014038cc..7c202d1ad 100644 --- a/src/app/modules/activities-management/store/activity-management.effects.ts +++ b/src/app/modules/activities-management/store/activity-management.effects.ts @@ -1,8 +1,10 @@ +import { INFO_SAVED_SUCCESSFULLY, INFO_DELETE_SUCCESSFULLY, UNEXPECTED_ERROR } from '../../shared/messages'; import { Injectable } from '@angular/core'; import { Actions, Effect, ofType } from '@ngrx/effects'; import { Action } from '@ngrx/store'; import { Observable, of } from 'rxjs'; import { catchError, map, mergeMap } from 'rxjs/operators'; +import { ToastrService } from 'ngx-toastr'; import * as actions from './activity-management.actions'; import { Activity } from './../../shared/models/activity.model'; @@ -10,7 +12,11 @@ import { ActivityService } from './../services/activity.service'; @Injectable() export class ActivityEffects { - constructor(private actions$: Actions, private activityService: ActivityService) {} + constructor( + private actions$: Actions, + private activityService: ActivityService, + private toastrService: ToastrService + ) {} @Effect() getActivities$: Observable = this.actions$.pipe( @@ -32,9 +38,13 @@ export class ActivityEffects { mergeMap((activity) => this.activityService.createActivity(activity).pipe( map((activityData) => { + this.toastrService.success(INFO_SAVED_SUCCESSFULLY); return new actions.CreateActivitySuccess(activityData); }), - catchError((error) => of(new actions.CreateActivityFail(error))) + catchError((error) => { + this.toastrService.error(UNEXPECTED_ERROR); + return of(new actions.CreateActivityFail(error)); + }) ) ) ); @@ -46,9 +56,13 @@ export class ActivityEffects { mergeMap((activityId) => this.activityService.deleteActivity(activityId).pipe( map(() => { + this.toastrService.success(INFO_DELETE_SUCCESSFULLY); return new actions.DeleteActivitySuccess(activityId); }), - catchError((error) => of(new actions.DeleteActivityFail(error))) + catchError((error) => { + this.toastrService.error(UNEXPECTED_ERROR); + return of(new actions.DeleteActivityFail(error)); + }) ) ) ); @@ -60,9 +74,13 @@ export class ActivityEffects { mergeMap((activity) => this.activityService.updateActivity(activity).pipe( map((activityData) => { + this.toastrService.success(INFO_SAVED_SUCCESSFULLY); return new actions.UpdateActivitySuccess(activityData); }), - catchError((error) => of(new actions.UpdateActivityFail(error))) + catchError((error) => { + this.toastrService.error(UNEXPECTED_ERROR); + return of(new actions.UpdateActivityFail(error)); + }) ) ) ); diff --git a/src/app/modules/customer-management/components/customer-info/components/create-customer/create-customer.spec.ts b/src/app/modules/customer-management/components/customer-info/components/create-customer/create-customer.spec.ts index 2ba63c9c5..4ed5bd858 100644 --- a/src/app/modules/customer-management/components/customer-info/components/create-customer/create-customer.spec.ts +++ b/src/app/modules/customer-management/components/customer-info/components/create-customer/create-customer.spec.ts @@ -6,16 +6,11 @@ import { CreateCustomerComponent } from './create-customer'; import { CustomerState, CreateCustomer } from 'src/app/modules/customer-management/store'; import { ResetCustomerToEdit, UpdateCustomer } from './../../../../store/customer-management.actions'; import { Customer } from 'src/app/modules/shared/models'; -import { ToastrService, IndividualConfig } from 'ngx-toastr'; describe('CreateCustomerComponent', () => { let component: CreateCustomerComponent; let fixture: ComponentFixture; let store: MockStore; - const toastrService = { - success: (message?: string, title?: string, override?: Partial) => { }, - error: (message?: string, title?: string, override?: Partial) => { } - }; const state = { data: [], @@ -35,8 +30,7 @@ describe('CreateCustomerComponent', () => { declarations: [CreateCustomerComponent], providers: [ FormBuilder, - provideMockStore({ initialState: state }), - { provide: ToastrService, useValue: toastrService }, + provideMockStore({ initialState: state }) ], }).compileComponents(); diff --git a/src/app/modules/customer-management/components/customer-info/components/create-customer/create-customer.ts b/src/app/modules/customer-management/components/customer-info/components/create-customer/create-customer.ts index 80bf45285..94993a883 100644 --- a/src/app/modules/customer-management/components/customer-info/components/create-customer/create-customer.ts +++ b/src/app/modules/customer-management/components/customer-info/components/create-customer/create-customer.ts @@ -13,7 +13,6 @@ import { } from 'src/app/modules/customer-management/store'; import { LoadProjectTypes } from '../../../projects-type/store'; import { LoadCustomerProjects } from '../../../projects/components/store/project.actions'; -import { ToastrService } from 'ngx-toastr'; @Component({ selector: 'app-create-customer', @@ -28,8 +27,7 @@ export class CreateCustomerComponent implements OnInit, OnDestroy { customerToEdit: Customer; editSubscription: Subscription; - constructor(private formBuilder: FormBuilder, private store: Store, - private toastrService: ToastrService) { + constructor(private formBuilder: FormBuilder, private store: Store) { this.customerForm = this.formBuilder.group({ name: ['', Validators.required], description: ['', Validators.required], @@ -61,7 +59,6 @@ export class CreateCustomerComponent implements OnInit, OnDestroy { } else { this.store.dispatch(new CreateCustomer(customerData)); } - this.toastrService.success('Customer information saved successfully'); this.areTabsActive = true; this.changeValueAreTabsActives.emit(this.areTabsActive); } diff --git a/src/app/modules/customer-management/components/customer-info/components/customer-list/customer-list.component.spec.ts b/src/app/modules/customer-management/components/customer-info/components/customer-list/customer-list.component.spec.ts index 2632076b4..acee2616f 100644 --- a/src/app/modules/customer-management/components/customer-info/components/customer-list/customer-list.component.spec.ts +++ b/src/app/modules/customer-management/components/customer-info/components/customer-list/customer-list.component.spec.ts @@ -1,4 +1,3 @@ -import { ToastrService, IndividualConfig } from 'ngx-toastr'; import { async, ComponentFixture, TestBed } from '@angular/core/testing'; import { MockStore, provideMockStore } from '@ngrx/store/testing'; @@ -21,18 +20,14 @@ describe('CustomerTableListComponent', () => { customerId: '' }; - const toastrService = { - success: (message?: string, title?: string, override?: Partial) => { }, - error: (message?: string, title?: string, override?: Partial) => { } - }; + beforeEach(async(() => { TestBed.configureTestingModule({ imports: [NgxPaginationModule], declarations: [CustomerListComponent], providers: [ - provideMockStore({ initialState: state }), - { provide: ToastrService, useValue: toastrService }, + provideMockStore({ initialState: state }) ], }).compileComponents(); })); diff --git a/src/app/modules/customer-management/components/customer-info/components/customer-list/customer-list.component.ts b/src/app/modules/customer-management/components/customer-info/components/customer-list/customer-list.component.ts index a28a63e2b..095561a8e 100644 --- a/src/app/modules/customer-management/components/customer-info/components/customer-list/customer-list.component.ts +++ b/src/app/modules/customer-management/components/customer-info/components/customer-list/customer-list.component.ts @@ -6,7 +6,6 @@ import { allCustomers } from './../../../../store/customer-management.selectors' import { LoadCustomers, DeleteCustomer, SetCustomerToEdit } from './../../../../store/customer-management.actions'; import { Customer } from './../../../../../shared/models/customer.model'; import { ITEMS_PER_PAGE } from 'src/environments/environment'; -import { ToastrService } from 'ngx-toastr'; @Component({ selector: 'app-customer-list', @@ -22,7 +21,7 @@ export class CustomerListComponent implements OnInit, OnDestroy { customers: Customer[] = []; customerSubscription: Subscription; - constructor(private store: Store, private toastrService: ToastrService) { } + constructor(private store: Store) { } ngOnInit(): void { this.store.dispatch(new LoadCustomers()); @@ -44,6 +43,5 @@ export class CustomerListComponent implements OnInit, OnDestroy { deleteCustomer(customerId: string) { this.store.dispatch(new DeleteCustomer(customerId)); - this.toastrService.success('Customer has been deleted'); } } diff --git a/src/app/modules/customer-management/components/projects-type/components/create-project-type/create-project-type.component.spec.ts b/src/app/modules/customer-management/components/projects-type/components/create-project-type/create-project-type.component.spec.ts index 06003ba68..b85058f16 100644 --- a/src/app/modules/customer-management/components/projects-type/components/create-project-type/create-project-type.component.spec.ts +++ b/src/app/modules/customer-management/components/projects-type/components/create-project-type/create-project-type.component.spec.ts @@ -1,4 +1,3 @@ -import { ToastrService, IndividualConfig } from 'ngx-toastr'; import { async, ComponentFixture, TestBed } from '@angular/core/testing'; import { FormBuilder } from '@angular/forms'; import { provideMockStore, MockStore } from '@ngrx/store/testing'; @@ -39,18 +38,12 @@ describe('InputProjectTypeComponent', () => { description: 'It is good for learning', }; - const toastrService = { - success: (message?: string, title?: string, override?: Partial) => { }, - error: (message?: string, title?: string, override?: Partial) => { } - }; - beforeEach(async(() => { TestBed.configureTestingModule({ declarations: [CreateProjectTypeComponent], providers: [ FormBuilder, - provideMockStore({ initialState: state }), - { provide: ToastrService, useValue: toastrService }, + provideMockStore({ initialState: state }) ], }).compileComponents(); })); diff --git a/src/app/modules/customer-management/components/projects-type/components/create-project-type/create-project-type.component.ts b/src/app/modules/customer-management/components/projects-type/components/create-project-type/create-project-type.component.ts index abdf46db2..b5e3221c8 100644 --- a/src/app/modules/customer-management/components/projects-type/components/create-project-type/create-project-type.component.ts +++ b/src/app/modules/customer-management/components/projects-type/components/create-project-type/create-project-type.component.ts @@ -1,4 +1,3 @@ -import { ToastrService } from 'ngx-toastr'; import { Component, OnInit, OnDestroy } from '@angular/core'; import { FormBuilder, Validators, FormGroup } from '@angular/forms'; import { Store, select } from '@ngrx/store'; @@ -21,8 +20,7 @@ export class CreateProjectTypeComponent implements OnInit, OnDestroy { getCustomerIdSubscription: Subscription; constructor(private formBuilder: FormBuilder, - private store: Store, - private toastrService: ToastrService) { + private store: Store) { this.projectTypeForm = this.formBuilder.group({ name: ['', Validators.required], description: [''], @@ -69,7 +67,6 @@ export class CreateProjectTypeComponent implements OnInit, OnDestroy { this.store.dispatch(new CreateProjectType({ ...projectTypeData, customer_id: this.customerId })); this.projectTypeForm.get('description').setValue(''); } - this.toastrService.success('Information has been saved succesfully'); } cancelButton() { diff --git a/src/app/modules/customer-management/components/projects-type/components/project-type-list/project-type-list.component.spec.ts b/src/app/modules/customer-management/components/projects-type/components/project-type-list/project-type-list.component.spec.ts index ae63ef7f5..cef9a4438 100644 --- a/src/app/modules/customer-management/components/projects-type/components/project-type-list/project-type-list.component.spec.ts +++ b/src/app/modules/customer-management/components/projects-type/components/project-type-list/project-type-list.component.spec.ts @@ -1,4 +1,3 @@ -import { IndividualConfig, ToastrService } from 'ngx-toastr'; import { async, ComponentFixture, TestBed } from '@angular/core/testing'; import { provideMockStore, MockStore } from '@ngrx/store/testing'; import { NgxPaginationModule } from 'ngx-pagination'; @@ -20,18 +19,12 @@ describe('ProjectTypeTableListComponent', () => { projectTypeIdToEdit: '', }; - const toastrService = { - success: (message?: string, title?: string, override?: Partial) => { }, - error: (message?: string, title?: string, override?: Partial) => { } - }; - beforeEach(async(() => { TestBed.configureTestingModule({ imports: [NgxPaginationModule], declarations: [ProjectTypeListComponent], providers: [ - provideMockStore({ initialState: state }), - { provide: ToastrService, useValue: toastrService }, + provideMockStore({ initialState: state }) ], }).compileComponents(); })); diff --git a/src/app/modules/customer-management/components/projects-type/components/project-type-list/project-type-list.component.ts b/src/app/modules/customer-management/components/projects-type/components/project-type-list/project-type-list.component.ts index 48bc7536e..b27a03591 100644 --- a/src/app/modules/customer-management/components/projects-type/components/project-type-list/project-type-list.component.ts +++ b/src/app/modules/customer-management/components/projects-type/components/project-type-list/project-type-list.component.ts @@ -1,4 +1,3 @@ -import { ToastrService } from 'ngx-toastr'; import { Component, OnInit } from '@angular/core'; import { ITEMS_PER_PAGE } from 'src/environments/environment'; import { Store, select } from '@ngrx/store'; @@ -18,7 +17,7 @@ export class ProjectTypeListComponent implements OnInit { initPage2 = 1; itemsPerPage = ITEMS_PER_PAGE; - constructor(private store: Store, private toastrService: ToastrService) {} + constructor(private store: Store) {} ngOnInit(): void { const projectTypes$ = this.store.pipe(select(allProjectTypes)); @@ -29,7 +28,6 @@ export class ProjectTypeListComponent implements OnInit { deleteProjectType(projectTypeId: string) { this.store.dispatch(new DeleteProjectType(projectTypeId)); - this.toastrService.success('Information has been removed successfully'); } updateProjectType(projectTypeId: string) { diff --git a/src/app/modules/customer-management/components/projects-type/store/project-type.effects.ts b/src/app/modules/customer-management/components/projects-type/store/project-type.effects.ts index 12bfa8ab0..8be18fe84 100644 --- a/src/app/modules/customer-management/components/projects-type/store/project-type.effects.ts +++ b/src/app/modules/customer-management/components/projects-type/store/project-type.effects.ts @@ -1,3 +1,4 @@ +import { INFO_SAVED_SUCCESSFULLY, INFO_DELETE_SUCCESSFULLY, UNEXPECTED_ERROR } from '../../../../shared/messages'; import { Injectable } from '@angular/core'; import { Actions, Effect, ofType } from '@ngrx/effects'; import { Action } from '@ngrx/store'; @@ -7,10 +8,15 @@ import { catchError, map, mergeMap } from 'rxjs/operators'; import * as actions from './project-type.actions'; import { ProjectType } from '../../../../shared/models'; import { ProjectTypeService } from '../services/project-type.service'; +import { ToastrService } from 'ngx-toastr'; @Injectable() export class ProjectTypeEffects { - constructor(private actions$: Actions, private projectTypeService: ProjectTypeService) {} + constructor( + private actions$: Actions, + private projectTypeService: ProjectTypeService, + private toastrService: ToastrService + ) {} @Effect() getProjectTypes$: Observable = this.actions$.pipe( @@ -32,9 +38,13 @@ export class ProjectTypeEffects { mergeMap((projectType) => this.projectTypeService.createProjectType(projectType).pipe( map((projectTypeData) => { + this.toastrService.success(INFO_SAVED_SUCCESSFULLY); return new actions.CreateProjectTypeSuccess(projectTypeData); }), - catchError((error) => of(new actions.CreateProjectTypeFail(error))) + catchError((error) => { + this.toastrService.error(UNEXPECTED_ERROR); + return of(new actions.CreateProjectTypeFail(error)); + }) ) ) ); @@ -46,9 +56,13 @@ export class ProjectTypeEffects { mergeMap((protectTypeId) => this.projectTypeService.deleteProjectType(protectTypeId).pipe( map(() => { + this.toastrService.success(INFO_DELETE_SUCCESSFULLY); return new actions.DeleteProjectTypeSuccess(protectTypeId); }), - catchError((error) => of(new actions.DeleteProjectTypeFail(error))) + catchError((error) => { + this.toastrService.error(UNEXPECTED_ERROR); + return of(new actions.DeleteProjectTypeFail(error)); + }) ) ) ); @@ -60,9 +74,13 @@ export class ProjectTypeEffects { mergeMap((projectType) => this.projectTypeService.updateProjectType(projectType).pipe( map((projectTypeData) => { + this.toastrService.success(INFO_SAVED_SUCCESSFULLY); return new actions.UpdateProjectTypeSuccess(projectTypeData); }), - catchError((error) => of(new actions.UpdateProjectTypeFail(error))) + catchError((error) => { + this.toastrService.error(UNEXPECTED_ERROR); + return of(new actions.UpdateProjectTypeFail(error)); + }) ) ) ); diff --git a/src/app/modules/customer-management/components/projects/components/create-project/create-project.component.spec.ts b/src/app/modules/customer-management/components/projects/components/create-project/create-project.component.spec.ts index 4bce1ec25..6e8880c4c 100644 --- a/src/app/modules/customer-management/components/projects/components/create-project/create-project.component.spec.ts +++ b/src/app/modules/customer-management/components/projects/components/create-project/create-project.component.spec.ts @@ -1,4 +1,3 @@ -import { ToastrService, IndividualConfig } from 'ngx-toastr'; import { async, ComponentFixture, TestBed } from '@angular/core/testing'; import { MockStore, provideMockStore } from '@ngrx/store/testing'; import { FormBuilder } from '@angular/forms'; @@ -36,18 +35,12 @@ describe('InputProjectComponent', () => { project_type_id: '123', }; - const toastrService = { - success: (message?: string, title?: string, override?: Partial) => { }, - error: (message?: string, title?: string, override?: Partial) => { } - }; - beforeEach(async(() => { TestBed.configureTestingModule({ declarations: [CreateProjectComponent], providers: [ FormBuilder, - provideMockStore({ initialState: state }), - { provide: ToastrService, useValue: toastrService }, + provideMockStore({ initialState: state }) ], }).compileComponents(); })); diff --git a/src/app/modules/customer-management/components/projects/components/create-project/create-project.component.ts b/src/app/modules/customer-management/components/projects/components/create-project/create-project.component.ts index 296dea23a..fa303e86d 100644 --- a/src/app/modules/customer-management/components/projects/components/create-project/create-project.component.ts +++ b/src/app/modules/customer-management/components/projects/components/create-project/create-project.component.ts @@ -1,4 +1,3 @@ -import { ToastrService } from 'ngx-toastr'; import { Component, OnInit, OnDestroy } from '@angular/core'; import { FormBuilder, Validators } from '@angular/forms'; import { Store, select } from '@ngrx/store'; @@ -28,8 +27,7 @@ export class CreateProjectComponent implements OnInit, OnDestroy { constructor( private formBuilder: FormBuilder, private store: Store, - private projectTypeStore: Store, - private toastrService: ToastrService + private projectTypeStore: Store ) { this.projectForm = this.formBuilder.group({ name: ['', Validators.required], @@ -73,7 +71,6 @@ export class CreateProjectComponent implements OnInit, OnDestroy { this.store.dispatch(new actions.CreateProject({ ...formData, customer_id: this.customerId })); } this.resetValuesForm(); - this.toastrService.success('The information has been saved successfully'); } get name() { diff --git a/src/app/modules/customer-management/components/projects/components/project-list/project-list.component.spec.ts b/src/app/modules/customer-management/components/projects/components/project-list/project-list.component.spec.ts index 692367381..c58432f43 100644 --- a/src/app/modules/customer-management/components/projects/components/project-list/project-list.component.spec.ts +++ b/src/app/modules/customer-management/components/projects/components/project-list/project-list.component.spec.ts @@ -1,4 +1,3 @@ -import { IndividualConfig, ToastrService } from 'ngx-toastr'; import { ComponentFixture, TestBed } from '@angular/core/testing'; import { MockStore, provideMockStore } from '@ngrx/store/testing'; import { NgxPaginationModule } from 'ngx-pagination'; @@ -18,11 +17,6 @@ describe('ProjectListComponent', () => { let getCustomerProjectsSelectorMock; let allCustomerProjectsSelectorMock; - const toastrService = { - success: (message?: string, title?: string, override?: Partial) => { }, - error: (message?: string, title?: string, override?: Partial) => { } - }; - const state: ProjectState = { projects: [], customerProjects: [], @@ -39,8 +33,7 @@ describe('ProjectListComponent', () => { imports: [NgxPaginationModule], declarations: [ProjectListComponent, FilterProjectPipe], providers: [ - provideMockStore({ initialState: state }), - { provide: ToastrService, useValue: toastrService }, + provideMockStore({ initialState: state }) ], }).compileComponents(); diff --git a/src/app/modules/customer-management/components/projects/components/project-list/project-list.component.ts b/src/app/modules/customer-management/components/projects/components/project-list/project-list.component.ts index 6ce099374..36c799727 100644 --- a/src/app/modules/customer-management/components/projects/components/project-list/project-list.component.ts +++ b/src/app/modules/customer-management/components/projects/components/project-list/project-list.component.ts @@ -1,4 +1,3 @@ -import { ToastrService } from 'ngx-toastr'; import { Component, OnInit, OnDestroy } from '@angular/core'; import { Store, select } from '@ngrx/store'; import { Subscription } from 'rxjs'; @@ -22,8 +21,7 @@ export class ProjectListComponent implements OnInit, OnDestroy { projectsSubscription: Subscription; - constructor(private store: Store, - private toastrService: ToastrService) {} + constructor(private store: Store) {} ngOnInit(): void { const projects$ = this.store.pipe(select(getCustomerProjects)); @@ -43,6 +41,5 @@ export class ProjectListComponent implements OnInit, OnDestroy { deleteProject(projectId: string) { this.store.dispatch(new actions.DeleteProject(projectId)); - this.toastrService.success('The project has been removed successfully'); } } diff --git a/src/app/modules/customer-management/components/projects/components/store/project.effects.ts b/src/app/modules/customer-management/components/projects/components/store/project.effects.ts index b4327656d..d6665b293 100644 --- a/src/app/modules/customer-management/components/projects/components/store/project.effects.ts +++ b/src/app/modules/customer-management/components/projects/components/store/project.effects.ts @@ -1,3 +1,4 @@ +import { INFO_SAVED_SUCCESSFULLY, INFO_DELETE_SUCCESSFULLY, UNEXPECTED_ERROR } from '../../../../../shared/messages'; import { Injectable } from '@angular/core'; import { of, Observable } from 'rxjs'; import { catchError, map, mergeMap } from 'rxjs/operators'; @@ -5,10 +6,15 @@ import { Action } from '@ngrx/store'; import { ofType, Actions, Effect } from '@ngrx/effects'; import { ProjectService } from '../services/project.service'; import * as actions from './project.actions'; +import { ToastrService } from 'ngx-toastr'; @Injectable() export class ProjectEffects { - constructor(private actions$: Actions, private projectService: ProjectService) {} + constructor( + private actions$: Actions, + private projectService: ProjectService, + private toastrService: ToastrService + ) {} @Effect() loadProjects$: Observable = this.actions$.pipe( @@ -18,7 +24,9 @@ export class ProjectEffects { map((projects) => { return new actions.LoadProjectsSuccess(projects); }), - catchError((error) => of(new actions.LoadProjectsFail(error))) + catchError((error) => { + return of(new actions.LoadProjectsFail(error)); + }) ) ) ); @@ -43,9 +51,13 @@ export class ProjectEffects { mergeMap((project) => this.projectService.createProject(project).pipe( map((projectData) => { + this.toastrService.success(INFO_DELETE_SUCCESSFULLY); return new actions.CreateProjectSuccess(projectData); }), - catchError((error) => of(new actions.CreateProjectFail(error))) + catchError((error) => { + this.toastrService.error(UNEXPECTED_ERROR); + return of(new actions.CreateProjectFail(error)); + }) ) ) ); @@ -57,9 +69,13 @@ export class ProjectEffects { mergeMap((project) => this.projectService.updateProject(project).pipe( map((projectData) => { + this.toastrService.success(INFO_SAVED_SUCCESSFULLY); return new actions.UpdateProjectSuccess(projectData); }), - catchError((error) => of(new actions.UpdateProjectFail(error))) + catchError((error) => { + this.toastrService.error(UNEXPECTED_ERROR); + return of(new actions.UpdateProjectFail(error)); + }) ) ) ); @@ -71,9 +87,13 @@ export class ProjectEffects { mergeMap((projectId) => this.projectService.deleteProject(projectId).pipe( map(() => { + this.toastrService.success(INFO_DELETE_SUCCESSFULLY); return new actions.DeleteProjectSuccess(projectId); }), - catchError((error) => of(new actions.DeleteProjectFail(error))) + catchError((error) => { + this.toastrService.error(UNEXPECTED_ERROR); + return of(new actions.DeleteProjectFail(error)); + }) ) ) ); diff --git a/src/app/modules/customer-management/store/customer-management.effects.ts b/src/app/modules/customer-management/store/customer-management.effects.ts index b26ad5416..04242e88d 100644 --- a/src/app/modules/customer-management/store/customer-management.effects.ts +++ b/src/app/modules/customer-management/store/customer-management.effects.ts @@ -1,15 +1,21 @@ +import { INFO_SAVED_SUCCESSFULLY, INFO_DELETE_SUCCESSFULLY, UNEXPECTED_ERROR } from '../../shared/messages'; import { Injectable } from '@angular/core'; import { Actions, Effect, ofType } from '@ngrx/effects'; import { Action } from '@ngrx/store'; import { Observable, of } from 'rxjs'; import { map, catchError, mergeMap } from 'rxjs/operators'; +import { ToastrService } from 'ngx-toastr'; import { CustomerService } from '../services/customer.service'; import * as actions from './customer-management.actions'; @Injectable() export class CustomerEffects { - constructor(private actions$: Actions, private customerService: CustomerService) {} + constructor( + private actions$: Actions, + private customerService: CustomerService, + private toastrService: ToastrService + ) {} @Effect() loadCustomers$: Observable = this.actions$.pipe( @@ -31,9 +37,13 @@ export class CustomerEffects { mergeMap((customer) => this.customerService.createCustomer(customer).pipe( map((customerData) => { + this.toastrService.success(INFO_SAVED_SUCCESSFULLY); return new actions.CreateCustomerSuccess(customerData); }), - catchError((error) => of(new actions.CreateCustomerFail(error))) + catchError((error) => { + this.toastrService.error(UNEXPECTED_ERROR); + return of(new actions.CreateCustomerFail(error)); + }) ) ) ); @@ -45,9 +55,13 @@ export class CustomerEffects { mergeMap((customerId) => this.customerService.deleteCustomer(customerId).pipe( map(() => { + this.toastrService.success(INFO_DELETE_SUCCESSFULLY); return new actions.DeleteCustomerSuccesss(customerId); }), - catchError((error) => of(new actions.DeleteCustomerFail(error))) + catchError((error) => { + this.toastrService.error(UNEXPECTED_ERROR); + return of(new actions.DeleteCustomerFail(error)); + }) ) ) ); @@ -59,9 +73,13 @@ export class CustomerEffects { mergeMap((customer) => this.customerService.updateCustomer(customer).pipe( map((customerData) => { + this.toastrService.success(INFO_SAVED_SUCCESSFULLY); return new actions.UpdateCustomerSuccess(customerData); }), - catchError((error) => of(new actions.UpdateCustomerFail(error))) + catchError((error) => { + this.toastrService.error(UNEXPECTED_ERROR); + return of(new actions.UpdateCustomerFail(error)); + }) ) ) ); diff --git a/src/app/modules/customer-management/store/customer-management.selectors.spec.ts b/src/app/modules/customer-management/store/customer-management.selectors.spec.ts new file mode 100644 index 000000000..b0cbd7c6c --- /dev/null +++ b/src/app/modules/customer-management/store/customer-management.selectors.spec.ts @@ -0,0 +1,28 @@ +import * as selectors from './customer-management.selectors'; + +describe('Customer selectors', () => { + it('should select the message', () => { + const anyMessage = 'my-message'; + const customerState = { message: anyMessage }; + + expect(selectors.getStatusMessage.projector(customerState)).toBe(anyMessage); + }); + + it('should select the customer list', () => { + const data = []; + const customerState = { data }; + expect(selectors.allCustomers.projector(customerState)).toBe(data); + }); + + it('should select the customerIdtoEdit', () => { + const customerIdToEdit = 'abc'; + const customerState = { customerIdToEdit }; + expect(selectors.customerIdtoEdit.projector(customerState)).toBe(customerIdToEdit); + }); + + it('should select the customerId', () => { + const customerId = 'abc'; + const customerState = { customerId }; + expect(selectors.getCustomerId.projector(customerState)).toBe(customerId); + }); +}); diff --git a/src/app/modules/time-clock/components/project-list-hover/project-list-hover.component.spec.ts b/src/app/modules/time-clock/components/project-list-hover/project-list-hover.component.spec.ts index 218b9ea65..ba8ee1182 100644 --- a/src/app/modules/time-clock/components/project-list-hover/project-list-hover.component.spec.ts +++ b/src/app/modules/time-clock/components/project-list-hover/project-list-hover.component.spec.ts @@ -2,7 +2,6 @@ import { async, ComponentFixture, TestBed } from '@angular/core/testing'; import { provideMockStore, MockStore } from '@ngrx/store/testing'; import { HttpClientTestingModule } from '@angular/common/http/testing'; -import { ToastrService } from 'ngx-toastr'; import { ProjectListHoverComponent } from './project-list-hover.component'; import { ProjectState } from '../../../customer-management/components/projects/components/store/project.reducer'; import { getCustomerProjects } from '../../../customer-management/components/projects/components/store/project.selectors'; @@ -35,14 +34,10 @@ describe('ProjectListHoverComponent', () => { }, }; - const toastrService = { - success: () => {} - }; - beforeEach(async(() => { TestBed.configureTestingModule({ declarations: [ProjectListHoverComponent, FilterProjectPipe], - providers: [provideMockStore({ initialState: state }), { provide: ToastrService, useValue: toastrService }], + providers: [provideMockStore({ initialState: state })], imports: [HttpClientTestingModule], }).compileComponents(); store = TestBed.inject(MockStore); diff --git a/src/app/modules/time-clock/components/project-list-hover/project-list-hover.component.ts b/src/app/modules/time-clock/components/project-list-hover/project-list-hover.component.ts index 7b3d98d3d..f89a2bf60 100644 --- a/src/app/modules/time-clock/components/project-list-hover/project-list-hover.component.ts +++ b/src/app/modules/time-clock/components/project-list-hover/project-list-hover.component.ts @@ -1,13 +1,12 @@ -import {getProjects} from './../../../customer-management/components/projects/components/store/project.selectors'; -import {Component, OnInit} from '@angular/core'; -import {Store, select} from '@ngrx/store'; +import { getProjects } from './../../../customer-management/components/projects/components/store/project.selectors'; +import { Component, OnInit } from '@angular/core'; +import { Store, select } from '@ngrx/store'; -import {getActiveTimeEntry} from './../../store/entry.selectors'; -import {Project} from 'src/app/modules/shared/models'; -import {ProjectState} from '../../../customer-management/components/projects/components/store/project.reducer'; +import { getActiveTimeEntry } from './../../store/entry.selectors'; +import { Project } from 'src/app/modules/shared/models'; +import { ProjectState } from '../../../customer-management/components/projects/components/store/project.reducer'; import * as actions from '../../../customer-management/components/projects/components/store/project.actions'; import * as entryActions from '../../store/entry.actions'; -import {ToastrService} from 'ngx-toastr'; @Component({ selector: 'app-project-list-hover', @@ -21,8 +20,7 @@ export class ProjectListHoverComponent implements OnInit { nameActiveProject: string; activeEntry; - constructor(private store: Store, private toastr: ToastrService) { - } + constructor(private store: Store) {} ngOnInit(): void { this.store.dispatch(new actions.LoadProjects()); @@ -54,12 +52,11 @@ export class ProjectListHoverComponent implements OnInit { clockIn(id: string) { if (this.activeEntry) { - const entry = {id: this.activeEntry.id, project_id: id}; + const entry = { id: this.activeEntry.id, project_id: id }; this.store.dispatch(new entryActions.UpdateActiveEntry(entry)); } else { - const newEntry = {project_id: id, start_date: new Date().toISOString()}; + const newEntry = { project_id: id, start_date: new Date().toISOString() }; this.store.dispatch(new entryActions.CreateEntry(newEntry)); - this.toastr.success('You just clocked-in'); } } } diff --git a/src/app/modules/time-clock/pages/time-clock.component.spec.ts b/src/app/modules/time-clock/pages/time-clock.component.spec.ts index 5fa3d8e53..7da3f0191 100644 --- a/src/app/modules/time-clock/pages/time-clock.component.spec.ts +++ b/src/app/modules/time-clock/pages/time-clock.component.spec.ts @@ -2,7 +2,6 @@ import { StopTimeEntryRunning } from './../store/entry.actions'; import { async, ComponentFixture, TestBed } from '@angular/core/testing'; import { HttpClientTestingModule } from '@angular/common/http/testing'; import { provideMockStore, MockStore } from '@ngrx/store/testing'; -import { ToastrService } from 'ngx-toastr'; import { TimeClockComponent } from './time-clock.component'; import { ProjectState } from '../../customer-management/components/projects/components/store/project.reducer'; import { ProjectListHoverComponent } from '../components'; @@ -14,11 +13,6 @@ describe('TimeClockComponent', () => { let fixture: ComponentFixture; let store: MockStore; let azureAdB2CService: AzureAdB2CService; - let injectedToastrService; - - const toastrService = { - success: () => {}, - }; const state = { projects: { projects: [{ id: 'id', name: 'name', project_type_id: '' }], @@ -49,7 +43,6 @@ describe('TimeClockComponent', () => { declarations: [TimeClockComponent, ProjectListHoverComponent, FilterProjectPipe], providers: [ AzureAdB2CService, - { provide: ToastrService, useValue: toastrService }, provideMockStore({ initialState: state }), ], }).compileComponents(); @@ -61,7 +54,6 @@ describe('TimeClockComponent', () => { component = fixture.componentInstance; fixture.detectChanges(); azureAdB2CService = TestBed.inject(AzureAdB2CService); - injectedToastrService = TestBed.inject(ToastrService); }); it('should be created', () => { diff --git a/src/app/modules/time-clock/store/entry.selectors.spec.ts b/src/app/modules/time-clock/store/entry.selectors.spec.ts index 3cc4b80d8..2e774b666 100644 --- a/src/app/modules/time-clock/store/entry.selectors.spec.ts +++ b/src/app/modules/time-clock/store/entry.selectors.spec.ts @@ -14,4 +14,8 @@ describe('Entry selectors', () => { expect(selectors.allEntries.projector(entryState)).toBe(entryList); }); + + // it('should select the entry summary', () => { + + // }) }); From 114ce90769d5e359e241dfc0a6dd1366641321ad Mon Sep 17 00:00:00 2001 From: Rene Enriquez Date: Fri, 15 May 2020 10:45:06 -0500 Subject: [PATCH 2/2] fix: #237 removing commented code --- src/app/modules/time-clock/store/entry.selectors.spec.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/app/modules/time-clock/store/entry.selectors.spec.ts b/src/app/modules/time-clock/store/entry.selectors.spec.ts index 2e774b666..ff151cbcc 100644 --- a/src/app/modules/time-clock/store/entry.selectors.spec.ts +++ b/src/app/modules/time-clock/store/entry.selectors.spec.ts @@ -15,7 +15,4 @@ describe('Entry selectors', () => { expect(selectors.allEntries.projector(entryState)).toBe(entryList); }); - // it('should select the entry summary', () => { - - // }) });