-
Notifications
You must be signed in to change notification settings - Fork 79
/
addresses.effects.spec.ts
202 lines (172 loc) · 8.04 KB
/
addresses.effects.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import { TestBed } from '@angular/core/testing';
import { provideMockActions } from '@ngrx/effects/testing';
import { Action, Store } from '@ngrx/store';
import { cold, hot } from 'jasmine-marbles';
import { Observable, of, throwError } from 'rxjs';
import { anyString, anything, instance, mock, verify, when } from 'ts-mockito';
import { Address } from 'ish-core/models/address/address.model';
import { Customer } from 'ish-core/models/customer/customer.model';
import { AddressService } from 'ish-core/services/address/address.service';
import { CoreStoreModule } from 'ish-core/store/core/core-store.module';
import { displaySuccessMessage } from 'ish-core/store/core/messages';
import { CustomerStoreModule } from 'ish-core/store/customer/customer-store.module';
import { loginUserSuccess } from 'ish-core/store/customer/user';
import { makeHttpError } from 'ish-core/utils/dev/api-service-utils';
import {
createCustomerAddress,
createCustomerAddressFail,
createCustomerAddressSuccess,
deleteCustomerAddress,
deleteCustomerAddressFail,
deleteCustomerAddressSuccess,
loadAddresses,
loadAddressesSuccess,
updateCustomerAddress,
updateCustomerAddressFail,
updateCustomerAddressSuccess,
} from './addresses.actions';
import { AddressesEffects } from './addresses.effects';
describe('Addresses Effects', () => {
let actions$: Observable<Action>;
let addressServiceMock: AddressService;
let effects: AddressesEffects;
let store: Store;
beforeEach(() => {
addressServiceMock = mock(AddressService);
when(addressServiceMock.getCustomerAddresses()).thenReturn(of([{ urn: 'test' } as Address]));
when(addressServiceMock.createCustomerAddress(anyString(), anything())).thenReturn(of({ urn: 'test' } as Address));
when(addressServiceMock.updateCustomerAddress(anyString(), anything())).thenReturn(of({ urn: 'test' } as Address));
when(addressServiceMock.deleteCustomerAddress(anyString(), anything())).thenReturn(of('123'));
TestBed.configureTestingModule({
imports: [CoreStoreModule.forTesting(), CustomerStoreModule.forTesting('user')],
providers: [
{ provide: AddressService, useFactory: () => instance(addressServiceMock) },
AddressesEffects,
provideMockActions(() => actions$),
],
});
effects = TestBed.inject(AddressesEffects);
store = TestBed.inject(Store);
const customer = { customerNo: 'patricia' } as Customer;
store.dispatch(loginUserSuccess({ customer }));
});
describe('loadAddresses$', () => {
it('should call the addressService for loadAddresses', done => {
const action = loadAddresses();
actions$ = of(action);
effects.loadAddresses$.subscribe(() => {
verify(addressServiceMock.getCustomerAddresses()).once();
done();
});
});
it('should map to action of type LoadAddressesSuccess', () => {
const action = loadAddresses();
const completion = loadAddressesSuccess({ addresses: [{ urn: 'test' } as Address] });
actions$ = hot('-a-a-a', { a: action });
const expected$ = cold('-c-c-c', { c: completion });
expect(effects.loadAddresses$).toBeObservable(expected$);
});
});
describe('createCustomerAddress$', () => {
it('should call the addressService for createCustomerAddress', done => {
const address = { urn: '123' } as Address;
const action = createCustomerAddress({ address });
actions$ = of(action);
effects.createCustomerAddress$.subscribe(() => {
verify(addressServiceMock.createCustomerAddress('patricia', anything())).once();
done();
});
});
it('should map to action of type CreateCustomerSuccess', () => {
const address = { urn: '123' } as Address;
const action = createCustomerAddress({ address });
const completion = createCustomerAddressSuccess({ address: { urn: 'test' } as Address });
const completion2 = displaySuccessMessage({
message: 'account.addresses.new_address_created.message',
});
actions$ = hot('-a----a----a----|', { a: action });
const expected$ = cold('-(cd)-(cd)-(cd)-|', { c: completion, d: completion2 });
expect(effects.createCustomerAddress$).toBeObservable(expected$);
});
it('should map invalid request to action of type CreateCustomerFail', () => {
when(addressServiceMock.createCustomerAddress(anyString(), anything())).thenReturn(
throwError(() => makeHttpError({ message: 'invalid' }))
);
const address = { urn: '123' } as Address;
const action = createCustomerAddress({ address });
const error = makeHttpError({ message: 'invalid' });
const completion = createCustomerAddressFail({ error });
actions$ = hot('-a-a-a', { a: action });
const expected$ = cold('-c-c-c', { c: completion });
expect(effects.createCustomerAddress$).toBeObservable(expected$);
});
});
describe('deleteCustomerAddress$', () => {
it('should call the addressService for deleteCustomerAddress', done => {
const addressId = '123';
const action = deleteCustomerAddress({ addressId });
actions$ = of(action);
effects.deleteCustomerAddress$.subscribe(() => {
verify(addressServiceMock.deleteCustomerAddress('patricia', '123')).once();
done();
});
});
it('should map to action of type DeleteCustomerSuccess', () => {
const addressId = '123';
const action = deleteCustomerAddress({ addressId });
const completion = deleteCustomerAddressSuccess({ addressId });
const completion2 = displaySuccessMessage({
message: 'account.addresses.new_address_deleted.message',
});
actions$ = hot('-a----a----a----|', { a: action });
const expected$ = cold('-(cd)-(cd)-(cd)-|', { c: completion, d: completion2 });
expect(effects.deleteCustomerAddress$).toBeObservable(expected$);
});
it('should map invalid request to action of type DeleteCustomerFail', () => {
when(addressServiceMock.deleteCustomerAddress(anyString(), anyString())).thenReturn(
throwError(() => makeHttpError({ message: 'invalid' }))
);
const addressId = '123';
const action = deleteCustomerAddress({ addressId });
const error = makeHttpError({ message: 'invalid' });
const completion = deleteCustomerAddressFail({ error });
actions$ = hot('-a-a-a', { a: action });
const expected$ = cold('-c-c-c', { c: completion });
expect(effects.deleteCustomerAddress$).toBeObservable(expected$);
});
});
describe('updateCustomerAddress$', () => {
it('should call the addressService for updateCustomerAddress', done => {
const address = { urn: '123' } as Address;
const action = updateCustomerAddress({ address });
actions$ = of(action);
effects.updateCustomerAddress$.subscribe(() => {
verify(addressServiceMock.updateCustomerAddress('-', anything())).once();
done();
});
});
it('should map to action of type updateCustomerSuccess', () => {
const address = { urn: 'test' } as Address;
const action = updateCustomerAddress({ address });
const completion = updateCustomerAddressSuccess({ address });
const completion2 = displaySuccessMessage({
message: 'account.addresses.address_updated.message',
});
actions$ = hot('-a-', { a: action });
const expected$ = cold('-(cd)', { c: completion, d: completion2 });
expect(effects.updateCustomerAddress$).toBeObservable(expected$);
});
it('should map invalid request to action of type updateCustomerFail', () => {
when(addressServiceMock.updateCustomerAddress('-', anything())).thenReturn(
throwError(() => makeHttpError({ message: 'invalid' }))
);
const address = { urn: '123' } as Address;
const action = updateCustomerAddress({ address });
const error = makeHttpError({ message: 'invalid' });
const completion = updateCustomerAddressFail({ error });
actions$ = hot('-a-a-a', { a: action });
const expected$ = cold('-c-c-c', { c: completion });
expect(effects.updateCustomerAddress$).toBeObservable(expected$);
});
});
});