Skip to content

Commit ff87824

Browse files
committed
fix: ioet#141 loading customers reading data from API
1 parent cb40ae2 commit ff87824

File tree

7 files changed

+71
-11
lines changed

7 files changed

+71
-11
lines changed

src/app/modules/customer-management/components/customer-info/components/create-customer/create-customer.spec.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { MockStore, provideMockStore } from '@ngrx/store/testing';
55
import { CreateCustomerComponent } from './create-customer';
66
import { CustomerState, CreateCustomer } from 'src/app/modules/customer-management/store';
77
import * as models from 'src/app/modules/shared/models/index';
8+
import { LoadCustomers } from './../../../../store/customer-management.actions';
89

910
describe('CreateCustomerComponent', () => {
1011
let component: CreateCustomerComponent;
@@ -55,13 +56,14 @@ describe('CreateCustomerComponent', () => {
5556
expect(component.customerForm.reset).toHaveBeenCalled();
5657
});
5758

58-
it('onSubmit and dispatch CreateCustomer action', () => {
59+
it('onSubmit, dispatch CreateCustomer and LoadCustomers actions', () => {
5960
spyOn(store, 'dispatch');
6061

6162
component.onSubmit(customerData);
6263

63-
expect(store.dispatch).toHaveBeenCalledTimes(1);
64+
expect(store.dispatch).toHaveBeenCalledTimes(2);
6465
expect(store.dispatch).toHaveBeenCalledWith(new CreateCustomer(customerData));
66+
expect(store.dispatch).toHaveBeenCalledWith(new LoadCustomers());
6567
});
6668

6769
it('should call resetCustomerForm', () => {

src/app/modules/customer-management/components/customer-info/components/create-customer/create-customer.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
import { LoadCustomers } from './../../../../store/customer-management.actions';
21
import { Component, Input, Output, EventEmitter, OnDestroy, OnInit } from '@angular/core';
32
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
43
import { Store, select } from '@ngrx/store';
54

65
import { Subscription } from 'rxjs';
7-
import { CustomerState, CreateCustomer } from 'src/app/modules/customer-management/store';
8-
import { getStatusMessage } from 'src/app/modules/customer-management/store/customer-management.selectors';
6+
import { CustomerState, CreateCustomer, LoadCustomers } from 'src/app/modules/customer-management/store';
7+
import { getStatusMessage } from './../../../../store/customer-management.selectors';
98

109
@Component({
1110
selector: 'app-create-customer',
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,57 @@
1+
import { MockStore, provideMockStore } from '@ngrx/store/testing';
12
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
23

34
import { CustomerListComponent } from './customer-list.component';
45
import { NgxPaginationModule } from 'ngx-pagination';
6+
import { allCustomers } from './../../../../store/customer-management.selectors';
7+
import { CustomerState } from 'src/app/modules/customer-management/store';
58

69
describe('CustomerTableListComponent', () => {
710
let component: CustomerListComponent;
811
let fixture: ComponentFixture<CustomerListComponent>;
12+
let store: MockStore<CustomerState>;
13+
let mockCustomerSelector;
14+
15+
16+
const state = {
17+
data: [{ tenant_id: 'id', name: 'name', description: 'description' }],
18+
isLoading: false,
19+
message: '',
20+
};
921

1022
beforeEach(async(() => {
1123
TestBed.configureTestingModule({
1224
imports: [NgxPaginationModule],
1325
declarations: [CustomerListComponent],
26+
providers: [provideMockStore({ initialState: state })],
1427
}).compileComponents();
1528
}));
1629

1730
beforeEach(() => {
1831
fixture = TestBed.createComponent(CustomerListComponent);
1932
component = fixture.componentInstance;
2033
fixture.detectChanges();
34+
35+
store = TestBed.inject(MockStore);
36+
store.setState(state);
37+
mockCustomerSelector = store.overrideSelector(allCustomers, state.data);
2138
});
2239

2340
it('component should be created', () => {
2441
expect(component).toBeTruthy();
2542
});
2643

44+
it('onNgInit customers are loaded from store executing an action', () => {
45+
spyOn(store, 'dispatch');
46+
47+
component.ngOnInit();
48+
49+
expect(store.dispatch).toHaveBeenCalled();
50+
expect(component.customers).toEqual(state.data);
51+
});
52+
53+
afterEach(() => {
54+
fixture.destroy();
55+
});
56+
2757
});

src/app/modules/customer-management/components/customer-info/components/customer-list/customer-list.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { allCustomers } from './../../../../store/customer-management.selectors';
21
import { Component, OnInit } from '@angular/core';
32
import { Store, select } from '@ngrx/store';
43

4+
import { allCustomers } from './../../../../store/customer-management.selectors';
55
import { LoadCustomers } from './../../../../store/customer-management.actions';
66
import { Customer } from './../../../../../shared/models/customer.model';
77
import { ITEMS_PER_PAGE } from 'src/environments/environment';

src/app/modules/customer-management/store/customer-management.actions.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,14 @@ describe('CustomerManagmentActions', () => {
2323
const createActivityFail = new actions.CreateCustomerFail('error');
2424
expect(createActivityFail.type).toEqual(actions.CustomerManagementActionTypes.CREATE_CUSTOMER_FAIL);
2525
});
26+
27+
it('LoadCustomersSuccess type is CustomerManagementActionTypes.LOAD_CUSTOMERS_SUCCESS', () => {
28+
const action = new actions.LoadCustomersSuccess([]);
29+
expect(action.type).toEqual(actions.CustomerManagementActionTypes.LOAD_CUSTOMERS_SUCCESS);
30+
});
31+
32+
it('LoadCustomersFail type is CustomerManagementActionTypes.LOAD_CUSTOMERS_FAIL', () => {
33+
const action = new actions.LoadCustomersFail('error');
34+
expect(action.type).toEqual(actions.CustomerManagementActionTypes.LOAD_CUSTOMERS_FAIL);
35+
});
2636
});

src/app/modules/customer-management/store/customer-management.reducers.spec.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,30 @@ describe('customerManagementReducer', () => {
66
const initialState: CustomerState = { data: [], isLoading: false, message: '' };
77
const customer: Customer = { name: 'aa', description: 'bb', tenant_id: 'cc' };
88

9+
it('on LoadCustomer, isLoading is true ', () => {
10+
const action = new actions.LoadCustomers();
11+
const state = customerManagementReducer(initialState, action);
12+
13+
expect(state.isLoading).toEqual(true);
14+
});
15+
16+
it('on LoadCustomerSucess, isLoading is false and state has data', () => {
17+
const data = [];
18+
const action = new actions.LoadCustomersSuccess(data);
19+
const state = customerManagementReducer(initialState, action);
20+
21+
expect(state.isLoading).toEqual(false);
22+
expect(state.data).toEqual(data);
23+
});
24+
25+
it('on LoadCustomerFail, isLoading is false and state has empty data', () => {
26+
const action = new actions.LoadCustomersFail('doh!!!');
27+
const state = customerManagementReducer(initialState, action);
28+
29+
expect(state.isLoading).toEqual(false);
30+
expect(state.data.length).toBe(0);
31+
});
32+
933
it('on CreateCustomer, isLoading is true ', () => {
1034
const action = new actions.CreateCustomer(customer);
1135
const state = customerManagementReducer(initialState, action);

src/app/modules/customer-management/store/customer-management.reducers.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,12 @@ export interface CustomerState {
55
data: Customer[];
66
isLoading: boolean;
77
message: string;
8-
customers: Customer[];
98
}
109

1110
export const initialState: CustomerState = {
1211
data: [],
1312
isLoading: false,
1413
message: '',
15-
customers: [],
1614
};
1715

1816
export function customerManagementReducer(
@@ -65,8 +63,5 @@ export function customerManagementReducer(
6563
message: 'An error occurred, try again later.',
6664
};
6765
}
68-
69-
default:
70-
return state;
7166
}
7267
}

0 commit comments

Comments
 (0)