Skip to content

Commit f3fa2a1

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

File tree

5 files changed

+70
-7
lines changed

5 files changed

+70
-7
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
@@ -1,3 +1,4 @@
1+
import { LoadCustomers } from './../../../../store/customer-management.actions';
12
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
23
import { FormBuilder } from '@angular/forms';
34
import { MockStore, provideMockStore } from '@ngrx/store/testing';
@@ -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/customer-list/customer-list.component.spec.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
import { allCustomers } from './../../../../store/customer-management.selectors';
2+
import { CustomerState } from 'src/app/modules/customer-management/store';
3+
import { Customer } from './../../../../../shared/models/customer.model';
4+
import { ProjectState } from './../../../../../project-management/store/project.reducer';
5+
import { MockStore, provideMockStore } from '@ngrx/store/testing';
16
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
27

38
import { CustomerListComponent } from './customer-list.component';
@@ -6,22 +11,49 @@ import { NgxPaginationModule } from 'ngx-pagination';
611
describe('CustomerTableListComponent', () => {
712
let component: CustomerListComponent;
813
let fixture: ComponentFixture<CustomerListComponent>;
14+
let store: MockStore<CustomerState>;
15+
let mockCustomerSelector;
16+
17+
18+
const state = {
19+
data: [{ tenant_id: 'id', name: 'name', description: 'description' }],
20+
isLoading: false,
21+
message: '',
22+
};
923

1024
beforeEach(async(() => {
1125
TestBed.configureTestingModule({
1226
imports: [NgxPaginationModule],
1327
declarations: [CustomerListComponent],
28+
providers: [provideMockStore({ initialState: state })],
1429
}).compileComponents();
1530
}));
1631

1732
beforeEach(() => {
1833
fixture = TestBed.createComponent(CustomerListComponent);
1934
component = fixture.componentInstance;
2035
fixture.detectChanges();
36+
37+
store = TestBed.inject(MockStore);
38+
store.setState(state);
39+
mockCustomerSelector = store.overrideSelector(allCustomers, state.data);
2140
});
2241

2342
it('component should be created', () => {
2443
expect(component).toBeTruthy();
2544
});
2645

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

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)