/
categories.effects.spec.ts
436 lines (356 loc) · 17.7 KB
/
categories.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
import { Location } from '@angular/common';
import { Component } from '@angular/core';
import { TestBed, fakeAsync, tick } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { provideMockActions } from '@ngrx/effects/testing';
import { Action, Store, combineReducers } from '@ngrx/store';
import { cold, hot } from 'jest-marbles';
import { RouteNavigation } from 'ngrx-router';
import { Observable, noop, of, throwError } from 'rxjs';
import { instance, mock, verify, when } from 'ts-mockito';
import { MAIN_NAVIGATION_MAX_SUB_CATEGORIES_DEPTH } from 'ish-core/configurations/injection-keys';
import { CategoryView } from 'ish-core/models/category-view/category-view.model';
import { Category, CategoryCompletenessLevel } from 'ish-core/models/category/category.model';
import { HttpError } from 'ish-core/models/http-error/http-error.model';
import { Locale } from 'ish-core/models/locale/locale.model';
import { CategoriesService } from 'ish-core/services/categories/categories.service';
import { SetAvailableLocales } from 'ish-core/store/locale';
import { localeReducer } from 'ish-core/store/locale/locale.reducer';
import { LoadMoreProducts } from 'ish-core/store/shopping/product-listing';
import { SelectProduct } from 'ish-core/store/shopping/products/products.actions';
import { shoppingReducers } from 'ish-core/store/shopping/shopping-store.module';
import { ngrxTesting } from 'ish-core/utils/dev/ngrx-testing';
import { categoryTree } from 'ish-core/utils/dev/test-data-utils';
import * as fromActions from './categories.actions';
import { CategoriesEffects } from './categories.effects';
describe('Categories Effects', () => {
let actions$: Observable<Action>;
let effects: CategoriesEffects;
let store$: Store<{}>;
let location: Location;
let categoriesServiceMock: CategoriesService;
const TOP_LEVEL_CATEGORIES = categoryTree([
{ uniqueId: '123', categoryPath: ['123'] },
{ uniqueId: '456', categoryPath: ['456'] },
] as Category[]);
@Component({ template: 'dummy' })
class DummyComponent {}
beforeEach(() => {
categoriesServiceMock = mock(CategoriesService);
when(categoriesServiceMock.getCategory('123')).thenReturn(
of(categoryTree([{ uniqueId: '123', categoryPath: ['123'] } as Category]))
);
when(categoriesServiceMock.getCategory('invalid')).thenReturn(throwError({ message: 'invalid category' }));
when(categoriesServiceMock.getTopLevelCategories(2)).thenReturn(of(TOP_LEVEL_CATEGORIES));
when(categoriesServiceMock.getTopLevelCategories(-1)).thenReturn(throwError({ message: 'invalid number' }));
TestBed.configureTestingModule({
declarations: [DummyComponent],
imports: [
RouterTestingModule.withRoutes([{ path: 'error', component: DummyComponent }]),
ngrxTesting({
reducers: {
shopping: combineReducers(shoppingReducers),
locale: localeReducer,
},
}),
],
providers: [
CategoriesEffects,
provideMockActions(() => actions$),
{ provide: CategoriesService, useFactory: () => instance(categoriesServiceMock) },
{ provide: MAIN_NAVIGATION_MAX_SUB_CATEGORIES_DEPTH, useValue: 1 },
],
});
effects = TestBed.get(CategoriesEffects);
store$ = TestBed.get(Store);
location = TestBed.get(Location);
});
describe('routeListenerForSelectingCategory$', () => {
it('should trigger SelectCategory when /category/XXX is visited', () => {
const action = new RouteNavigation({
path: 'category/:categoryUniqueId',
params: { categoryUniqueId: 'dummy' },
queryParams: {},
});
const expected = new fromActions.SelectCategory({ categoryId: 'dummy' });
actions$ = hot('a', { a: action });
expect(effects.routeListenerForSelectingCategory$).toBeObservable(cold('a', { a: expected }));
});
it('should trigger SelectCategory when /category/XXX/product/YYY is visited', () => {
const action = new RouteNavigation({
path: 'category/:categoryUniqueId/product/:sku',
params: { categoryUniqueId: 'dummy', sku: 'foobar' },
queryParams: {},
});
const expected = new fromActions.SelectCategory({ categoryId: 'dummy' });
actions$ = hot('a', { a: action });
expect(effects.routeListenerForSelectingCategory$).toBeObservable(cold('a', { a: expected }));
});
it('should not trigger SelectCategory when /something is visited', () => {
const action = new RouteNavigation({
path: 'something',
params: {},
queryParams: {},
});
actions$ = hot('a', { a: action });
expect(effects.routeListenerForSelectingCategory$).toBeObservable(cold('-'));
});
it('should not trigger SelectCategory when category is already selected', () => {
const action = new RouteNavigation({
path: 'category/:categoryUniqueId',
params: { categoryUniqueId: 'dummy' },
queryParams: {},
});
const expected = new fromActions.SelectCategory({ categoryId: 'dummy' });
actions$ = hot('-a-a-a', { a: action });
expect(effects.routeListenerForSelectingCategory$).toBeObservable(cold('-a----', { a: expected }));
});
});
describe('selectedCategory$', () => {
describe('for root categories', () => {
let category: CategoryView;
beforeEach(() => {
category = {
uniqueId: '123',
} as CategoryView;
});
it('should reload category if it isnt completely loaded yet', () => {
category.completenessLevel = 0;
store$.dispatch(new fromActions.LoadCategorySuccess({ categories: categoryTree([category]) }));
actions$ = hot('a', { a: new fromActions.SelectCategory({ categoryId: category.uniqueId }) });
const completion = new fromActions.LoadCategory({ categoryId: category.uniqueId });
const expected$ = cold('a', { a: completion });
expect(effects.selectedCategory$).toBeObservable(expected$);
});
it('should do nothing if category is completely loaded', () => {
category.completenessLevel = CategoryCompletenessLevel.Max;
store$.dispatch(new fromActions.LoadCategorySuccess({ categories: categoryTree([category]) }));
actions$ = hot('a', { a: new fromActions.SelectCategory({ categoryId: category.uniqueId }) });
expect(effects.selectedCategory$).toBeObservable(cold('-'));
});
it('should trigger LoadCategory if not exists', () => {
actions$ = hot('a', { a: new fromActions.SelectCategory({ categoryId: category.uniqueId }) });
const completion = new fromActions.LoadCategory({ categoryId: category.uniqueId });
const expected$ = cold('a', { a: completion });
expect(effects.selectedCategory$).toBeObservable(expected$);
});
it('should trigger LoadCategory if category exists but subcategories have not been loaded', () => {
category.hasChildren = () => true;
category.completenessLevel = 0;
store$.dispatch(new fromActions.LoadCategorySuccess({ categories: categoryTree([category]) }));
actions$ = hot('a', { a: new fromActions.SelectCategory({ categoryId: category.uniqueId }) });
const completion = new fromActions.LoadCategory({ categoryId: category.uniqueId });
const expected$ = cold('a', { a: completion });
expect(effects.selectedCategory$).toBeObservable(expected$);
});
});
describe('for leaf categories', () => {
let category: Category;
beforeEach(() => {
category = {
uniqueId: '123.456.789',
} as Category;
});
it('should trigger only one LoadCategory if it doesnt exist', () => {
actions$ = hot('a', { a: new fromActions.SelectCategory({ categoryId: category.uniqueId }) });
const completion = new fromActions.LoadCategory({ categoryId: '123.456.789' });
const expected$ = cold('a', { a: completion });
expect(effects.selectedCategory$).toBeObservable(expected$);
});
it('should not trigger LoadCategory for categories that are completely loaded', () => {
category.completenessLevel = CategoryCompletenessLevel.Max;
store$.dispatch(new fromActions.LoadCategorySuccess({ categories: categoryTree([category]) }));
actions$ = hot('a', { a: new fromActions.SelectCategory({ categoryId: category.uniqueId }) });
expect(effects.selectedCategory$).toBeObservable(cold('-----'));
});
});
});
describe('loadCategory$', () => {
it('should call the categoriesService for LoadCategory action', done => {
const categoryId = '123';
const action = new fromActions.LoadCategory({ categoryId });
actions$ = of(action);
effects.loadCategory$.subscribe(() => {
verify(categoriesServiceMock.getCategory(categoryId)).once();
done();
});
});
it('should map to action of type LoadCategorySuccess', () => {
const categoryId = '123';
const action = new fromActions.LoadCategory({ categoryId });
const response = categoryTree([
{
uniqueId: categoryId,
categoryPath: ['123'],
} as Category,
]);
const completion = new fromActions.LoadCategorySuccess({ categories: response });
actions$ = hot('-a-a-a', { a: action });
const expected$ = cold('-c-c-c', { c: completion });
expect(effects.loadCategory$).toBeObservable(expected$);
});
it('should map invalid request to action of type LoadCategoryFail', () => {
const categoryId = 'invalid';
const action = new fromActions.LoadCategory({ categoryId });
const completion = new fromActions.LoadCategoryFail({ error: { message: 'invalid category' } as HttpError });
actions$ = hot('-a-a-a', { a: action });
const expected$ = cold('-c-c-c', { c: completion });
expect(effects.loadCategory$).toBeObservable(expected$);
});
});
describe('loadTopLevelWhenUnavailable$', () => {
const EN_US = { lang: 'en' } as Locale;
let depth: number;
beforeEach(() => {
depth = TestBed.get(MAIN_NAVIGATION_MAX_SUB_CATEGORIES_DEPTH);
store$.dispatch(new SetAvailableLocales({ locales: [EN_US] }));
});
it('should load top level categories retrying for every routing action', () => {
const completion = new fromActions.LoadTopLevelCategories({ depth });
actions$ = hot(' ----a---a--a', { a: new RouteNavigation({ path: 'any' }) });
const expected$ = cold('----a---a--a', { a: completion });
expect(effects.loadTopLevelWhenUnavailable$).toBeObservable(expected$);
});
it('should not load top level categories when already available', () => {
store$.dispatch(new fromActions.LoadTopLevelCategoriesSuccess({ categories: categoryTree() }));
actions$ = hot(' ----a---a--a', { a: new RouteNavigation({ path: 'any' }) });
const expected$ = cold('------------');
expect(effects.loadTopLevelWhenUnavailable$).toBeObservable(expected$);
});
});
describe('loadTopLevelCategories$', () => {
it('should call the categoriesService for LoadTopLevelCategories action', done => {
const depth = 2;
const action = new fromActions.LoadTopLevelCategories({ depth });
actions$ = of(action);
effects.loadTopLevelCategories$.subscribe(() => {
verify(categoriesServiceMock.getTopLevelCategories(depth)).once();
done();
});
});
it('should map to action of type LoadCategorySuccess', () => {
const depth = 2;
const action = new fromActions.LoadTopLevelCategories({ depth });
const completion = new fromActions.LoadTopLevelCategoriesSuccess({ categories: TOP_LEVEL_CATEGORIES });
actions$ = hot('-a-a-a', { a: action });
const expected$ = cold('-c-c-c', { c: completion });
expect(effects.loadTopLevelCategories$).toBeObservable(expected$);
});
it('should map invalid request to action of type LoadCategoryFail', () => {
const depth = -1;
const action = new fromActions.LoadTopLevelCategories({ depth });
const completion = new fromActions.LoadTopLevelCategoriesFail({
error: { message: 'invalid number' } as HttpError,
});
actions$ = hot('-a-a-a', { a: action });
const expected$ = cold('-c-c-c', { c: completion });
expect(effects.loadTopLevelCategories$).toBeObservable(expected$);
});
});
describe('productOrCategoryChanged$', () => {
let category: Category;
beforeEach(() => {
category = {
uniqueId: '123',
hasOnlineProducts: false,
} as Category;
});
it('should do nothing when product is selected', () => {
store$.dispatch(new fromActions.LoadCategorySuccess({ categories: categoryTree([category]) }));
store$.dispatch(new SelectProduct({ sku: 'P222' }));
expect(effects.productOrCategoryChanged$).toBeObservable(cold('-'));
});
describe('when product is not selected', () => {
it('should do nothing when category doesnt have online products', () => {
category.hasOnlineProducts = false;
store$.dispatch(new fromActions.LoadCategorySuccess({ categories: categoryTree([category]) }));
store$.dispatch(new fromActions.SelectCategory({ categoryId: category.uniqueId }));
expect(effects.productOrCategoryChanged$).toBeObservable(cold('-'));
});
it('should do nothing when no category is selected', () => {
store$.dispatch(new fromActions.LoadCategorySuccess({ categories: categoryTree([category]) }));
expect(effects.productOrCategoryChanged$).toBeObservable(cold('-'));
});
it('should do nothing when selected category is not in the store', () => {
store$.dispatch(new fromActions.SelectCategory({ categoryId: category.uniqueId }));
expect(effects.productOrCategoryChanged$).toBeObservable(cold('-'));
});
it('should trigger action of type LoadProductsForCategory when category is selected', () => {
category.hasOnlineProducts = true;
store$.dispatch(new fromActions.LoadCategorySuccess({ categories: categoryTree([category]) }));
store$.dispatch(new fromActions.SelectCategory({ categoryId: category.uniqueId }));
actions$ = hot('--a)', {
a: new RouteNavigation({
path: 'category/:categoryUniqueId',
params: { categoryUniqueId: category.uniqueId },
queryParams: {},
}),
});
const action = new LoadMoreProducts({ id: { type: 'category', value: category.uniqueId }, page: undefined });
expect(effects.productOrCategoryChanged$).toBeObservable(cold('--a', { a: action }));
});
it('should not trigger action when we are on a product page', () => {
category.hasOnlineProducts = true;
store$.dispatch(new fromActions.LoadCategorySuccess({ categories: categoryTree([category]) }));
store$.dispatch(new fromActions.SelectCategory({ categoryId: category.uniqueId }));
actions$ = hot('(ab)', {
a: new RouteNavigation({
path: 'category/:categoryUniqueId/product/:sku',
params: { categoryUniqueId: category.uniqueId, sku: 'dummy' },
queryParams: {},
}),
b: new fromActions.SelectedCategoryAvailable({ categoryId: category.uniqueId }),
});
expect(effects.productOrCategoryChanged$).toBeObservable(cold('------'));
});
});
});
describe('redirectIfErrorInCategories$', () => {
it('should redirect if triggered', fakeAsync(() => {
const action = new fromActions.LoadCategoryFail({ error: { status: 404 } as HttpError });
actions$ = of(action);
effects.redirectIfErrorInCategories$.subscribe(noop, fail, noop);
tick(500);
expect(location.path()).toEqual('/error');
}));
});
describe('selectedCategoryAvailable$', () => {
it('should not fire when selected category is not available', () => {
store$.dispatch(new fromActions.SelectCategory({ categoryId: 'A' }));
actions$ = of(new fromActions.SelectCategory({ categoryId: 'A' }));
expect(effects.selectedCategoryAvailable$).toBeObservable(cold('-----'));
});
it('should not fire when selected category is available but not completely loaded', () => {
store$.dispatch(
new fromActions.LoadCategorySuccess({ categories: categoryTree([{ uniqueId: 'A' }] as Category[]) })
);
store$.dispatch(new fromActions.SelectCategory({ categoryId: 'A' }));
actions$ = of(new fromActions.SelectCategory({ categoryId: 'A' }));
expect(effects.selectedCategoryAvailable$).toBeObservable(cold('-----'));
});
it('should fire when selected category is available and completely loaded', () => {
store$.dispatch(
new fromActions.LoadCategorySuccess({
categories: categoryTree([{ uniqueId: 'A', completenessLevel: CategoryCompletenessLevel.Max }] as Category[]),
})
);
store$.dispatch(new fromActions.SelectCategory({ categoryId: 'A' }));
actions$ = of(new fromActions.SelectCategory({ categoryId: 'A' }));
expect(effects.selectedCategoryAvailable$).toBeObservable(
cold('a', { a: new fromActions.SelectedCategoryAvailable({ categoryId: 'A' }) })
);
});
it('should not fire twice when category is selected multiple times', () => {
store$.dispatch(
new fromActions.LoadCategorySuccess({
categories: categoryTree([{ uniqueId: 'A', completenessLevel: CategoryCompletenessLevel.Max }] as Category[]),
})
);
store$.dispatch(new fromActions.SelectCategory({ categoryId: 'A' }));
actions$ = hot('-a-a-a', { a: new fromActions.SelectCategory({ categoryId: 'A' }) });
expect(effects.selectedCategoryAvailable$).toBeObservable(
cold('-a-----', { a: new fromActions.SelectedCategoryAvailable({ categoryId: 'A' }) })
);
});
});
});