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 3ae5f0c20..9c110a818 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 @@ -5,6 +5,7 @@ import { MockStore, provideMockStore } from '@ngrx/store/testing'; import { CreateCustomerComponent } from './create-customer'; import { CustomerState, CreateCustomer } from 'src/app/modules/customer-management/store'; import * as models from 'src/app/modules/shared/models/index'; +import { LoadCustomers } from './../../../../store/customer-management.actions'; describe('CreateCustomerComponent', () => { let component: CreateCustomerComponent; @@ -55,13 +56,14 @@ describe('CreateCustomerComponent', () => { expect(component.customerForm.reset).toHaveBeenCalled(); }); - it('onSubmit and dispatch CreateCustomer action', () => { + it('onSubmit, dispatch CreateCustomer and LoadCustomers actions', () => { spyOn(store, 'dispatch'); component.onSubmit(customerData); - expect(store.dispatch).toHaveBeenCalledTimes(1); + expect(store.dispatch).toHaveBeenCalledTimes(2); expect(store.dispatch).toHaveBeenCalledWith(new CreateCustomer(customerData)); + expect(store.dispatch).toHaveBeenCalledWith(new LoadCustomers()); }); it('should call resetCustomerForm', () => { 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 0cabbc120..35a494dac 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 @@ -1,11 +1,10 @@ -import { LoadCustomers } from './../../../../store/customer-management.actions'; import { Component, Input, Output, EventEmitter, OnDestroy, OnInit } from '@angular/core'; import { FormGroup, FormBuilder, Validators } from '@angular/forms'; import { Store, select } from '@ngrx/store'; import { Subscription } from 'rxjs'; -import { CustomerState, CreateCustomer } from 'src/app/modules/customer-management/store'; -import { getStatusMessage } from 'src/app/modules/customer-management/store/customer-management.selectors'; +import { CustomerState, CreateCustomer, LoadCustomers } from 'src/app/modules/customer-management/store'; +import { getStatusMessage } from './../../../../store/customer-management.selectors'; @Component({ selector: 'app-create-customer', 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 1de2cb053..5ff5a7497 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,16 +1,29 @@ +import { MockStore, provideMockStore } from '@ngrx/store/testing'; import { async, ComponentFixture, TestBed } from '@angular/core/testing'; import { CustomerListComponent } from './customer-list.component'; import { NgxPaginationModule } from 'ngx-pagination'; +import { allCustomers } from './../../../../store/customer-management.selectors'; +import { CustomerState } from 'src/app/modules/customer-management/store'; describe('CustomerTableListComponent', () => { let component: CustomerListComponent; let fixture: ComponentFixture; + let store: MockStore; + let mockCustomerSelector; + + + const state = { + data: [{ tenant_id: 'id', name: 'name', description: 'description' }], + isLoading: false, + message: '', + }; beforeEach(async(() => { TestBed.configureTestingModule({ imports: [NgxPaginationModule], declarations: [CustomerListComponent], + providers: [provideMockStore({ initialState: state })], }).compileComponents(); })); @@ -18,10 +31,27 @@ describe('CustomerTableListComponent', () => { fixture = TestBed.createComponent(CustomerListComponent); component = fixture.componentInstance; fixture.detectChanges(); + + store = TestBed.inject(MockStore); + store.setState(state); + mockCustomerSelector = store.overrideSelector(allCustomers, state.data); }); it('component should be created', () => { expect(component).toBeTruthy(); }); + it('onNgInit customers are loaded from store executing an action', () => { + spyOn(store, 'dispatch'); + + component.ngOnInit(); + + expect(store.dispatch).toHaveBeenCalled(); + expect(component.customers).toEqual(state.data); + }); + + afterEach(() => { + fixture.destroy(); + }); + }); 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 ae6b9fb62..6356f7767 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 @@ -1,7 +1,7 @@ -import { allCustomers } from './../../../../store/customer-management.selectors'; import { Component, OnInit } from '@angular/core'; import { Store, select } from '@ngrx/store'; +import { allCustomers } from './../../../../store/customer-management.selectors'; import { LoadCustomers } from './../../../../store/customer-management.actions'; import { Customer } from './../../../../../shared/models/customer.model'; import { ITEMS_PER_PAGE } from 'src/environments/environment'; diff --git a/src/app/modules/customer-management/store/customer-management.actions.spec.ts b/src/app/modules/customer-management/store/customer-management.actions.spec.ts index 80dab4f7d..f9d019c68 100644 --- a/src/app/modules/customer-management/store/customer-management.actions.spec.ts +++ b/src/app/modules/customer-management/store/customer-management.actions.spec.ts @@ -23,4 +23,14 @@ describe('CustomerManagmentActions', () => { const createActivityFail = new actions.CreateCustomerFail('error'); expect(createActivityFail.type).toEqual(actions.CustomerManagementActionTypes.CREATE_CUSTOMER_FAIL); }); + + it('LoadCustomersSuccess type is CustomerManagementActionTypes.LOAD_CUSTOMERS_SUCCESS', () => { + const action = new actions.LoadCustomersSuccess([]); + expect(action.type).toEqual(actions.CustomerManagementActionTypes.LOAD_CUSTOMERS_SUCCESS); + }); + + it('LoadCustomersFail type is CustomerManagementActionTypes.LOAD_CUSTOMERS_FAIL', () => { + const action = new actions.LoadCustomersFail('error'); + expect(action.type).toEqual(actions.CustomerManagementActionTypes.LOAD_CUSTOMERS_FAIL); + }); }); diff --git a/src/app/modules/customer-management/store/customer-management.reducers.spec.ts b/src/app/modules/customer-management/store/customer-management.reducers.spec.ts index ffe193acb..d3ad42a20 100644 --- a/src/app/modules/customer-management/store/customer-management.reducers.spec.ts +++ b/src/app/modules/customer-management/store/customer-management.reducers.spec.ts @@ -6,6 +6,30 @@ describe('customerManagementReducer', () => { const initialState: CustomerState = { data: [], isLoading: false, message: '' }; const customer: Customer = { name: 'aa', description: 'bb', tenant_id: 'cc' }; + it('on LoadCustomer, isLoading is true ', () => { + const action = new actions.LoadCustomers(); + const state = customerManagementReducer(initialState, action); + + expect(state.isLoading).toEqual(true); + }); + + it('on LoadCustomerSucess, isLoading is false and state has data', () => { + const data = []; + const action = new actions.LoadCustomersSuccess(data); + const state = customerManagementReducer(initialState, action); + + expect(state.isLoading).toEqual(false); + expect(state.data).toEqual(data); + }); + + it('on LoadCustomerFail, isLoading is false and state has empty data', () => { + const action = new actions.LoadCustomersFail('doh!!!'); + const state = customerManagementReducer(initialState, action); + + expect(state.isLoading).toEqual(false); + expect(state.data.length).toBe(0); + }); + it('on CreateCustomer, isLoading is true ', () => { const action = new actions.CreateCustomer(customer); const state = customerManagementReducer(initialState, action); diff --git a/src/app/modules/customer-management/store/customer-management.reducers.ts b/src/app/modules/customer-management/store/customer-management.reducers.ts index cb77c0a9a..e46a81fb0 100644 --- a/src/app/modules/customer-management/store/customer-management.reducers.ts +++ b/src/app/modules/customer-management/store/customer-management.reducers.ts @@ -5,14 +5,12 @@ export interface CustomerState { data: Customer[]; isLoading: boolean; message: string; - customers: Customer[]; } export const initialState: CustomerState = { data: [], isLoading: false, message: '', - customers: [], }; export function customerManagementReducer( @@ -65,8 +63,5 @@ export function customerManagementReducer( message: 'An error occurred, try again later.', }; } - - default: - return state; } }