forked from johnpapa/angular-ngrx-data
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentity-reducer.spec.ts
488 lines (424 loc) · 15.4 KB
/
entity-reducer.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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
import { Action, ActionReducer, MetaReducer } from '@ngrx/store';
import { EntityAdapter } from '@ngrx/entity';
import { EntityAction, EntityActionFactory } from '../actions/entity-action';
import { EntityOp } from '../actions/entity-op';
import { EntityCache } from './entity-cache';
import {
EntityCacheMerge,
EntityCacheSet
} from '../actions/entity-cache-actions';
import { EntityCollection } from './entity-collection';
import { EntityCollectionCreator } from './entity-collection-creator';
import { DefaultEntityCollectionReducerMethodsFactory } from './default-entity-collection-reducer-methods';
import { EntityDefinitionService } from '../entity-metadata/entity-definition.service';
import { EntityMetadataMap } from '../entity-metadata/entity-metadata';
import { Logger } from '../utils/interfaces';
import { IdSelector, Update } from '../utils/ngrx-entity-models';
import {
EntityCollectionReducer,
EntityCollectionReducerFactory
} from './entity-collection-reducer';
import {
EntityCollectionReducers,
EntityReducerFactory
} from './entity-reducer';
class Bar {
id: number;
bar: string;
}
class Foo {
id: string;
foo: string;
}
class Hero {
id: number;
name: string;
power?: string;
}
class Villain {
key: string;
name: string;
}
const metadata: EntityMetadataMap = {
Hero: {},
Villain: { selectId: (villain: Villain) => villain.key }
};
describe('EntityReducer', () => {
// action factory never changes in these tests
const entityActionFactory = new EntityActionFactory();
const createAction: <P = any>(
entityName: string,
op: EntityOp,
payload?: P
) => EntityAction = entityActionFactory.create.bind(entityActionFactory);
let collectionCreator: EntityCollectionCreator;
let collectionReducerFactory: EntityCollectionReducerFactory;
let eds: EntityDefinitionService;
let entityReducer: ActionReducer<EntityCache, Action>;
let entityReducerFactory: EntityReducerFactory;
let logger: Logger;
beforeEach(() => {
eds = new EntityDefinitionService([metadata]);
collectionCreator = new EntityCollectionCreator(eds);
const collectionReducerMethodsFactory = new DefaultEntityCollectionReducerMethodsFactory(
eds
);
collectionReducerFactory = new EntityCollectionReducerFactory(
collectionReducerMethodsFactory
);
logger = jasmine.createSpyObj('Logger', ['error', 'log', 'warn']);
entityReducerFactory = new EntityReducerFactory(
collectionCreator,
collectionReducerFactory,
logger
);
});
describe('#create', () => {
beforeEach(() => {
entityReducer = entityReducerFactory.create();
});
it('creates a default hero reducer when QUERY_ALL for hero', () => {
const hero: Hero = { id: 42, name: 'Bobby' };
const action = createAction<Hero>('Hero', EntityOp.ADD_ONE, hero);
const state = entityReducer({}, action);
const collection = state['Hero'];
expect(collection.ids.length).toBe(1, 'should have added one');
expect(collection.entities[42]).toEqual(hero, 'should be added hero');
});
it('throws when ask for reducer of unknown entity type', () => {
const action = entityActionFactory.create('Foo', EntityOp.QUERY_ALL);
expect(() => entityReducer({}, action)).toThrowError(
/no EntityDefinition/i
);
});
});
describe('#registerReducer', () => {
beforeEach(() => {
entityReducer = entityReducerFactory.create();
});
it('can register a new reducer', () => {
const reducer = createNoopReducer();
entityReducerFactory.registerReducer('Foo', reducer);
const action = entityActionFactory.create<Foo>('Foo', EntityOp.ADD_ONE, {
id: 'forty-two',
foo: 'fooz'
});
// Must initialize the state by hand
const state = entityReducer({}, action);
const collection = state['Foo'];
expect(collection.ids.length).toBe(0, 'ADD_ONE should not add');
});
it('can replace existing reducer by registering with same name', () => {
// Just like ADD_ONE test above with default reducer
// but this time should not add the hero.
const hero: Hero = { id: 42, name: 'Bobby' };
const reducer = createNoopReducer();
entityReducerFactory.registerReducer('Hero', reducer);
const action = entityActionFactory.create<Hero>(
'Hero',
EntityOp.ADD_ONE,
hero
);
const state = entityReducer({}, action);
const collection = state['Hero'];
expect(collection.ids.length).toBe(0, 'ADD_ONE should not add');
});
});
/**
* Test the EntityCache-level actions, SET and MERGE, which can
* be used to restore the entity cache from a know state such as
* re-hydrating from browser storage.
* Useful for an offline-capable app.
*/
describe('EntityCache-level actions', () => {
let initialHeroes: Hero[];
let initialCache: EntityCache;
beforeEach(() => {
entityReducer = entityReducerFactory.create();
initialHeroes = [
{ id: 2, name: 'B', power: 'Fast' },
{ id: 1, name: 'A', power: 'invisible' }
];
initialCache = createInitialCache({ Hero: initialHeroes });
});
describe('#SET_ENTITY_CACHE', () => {
it('should initialize cache', () => {
const cache = createInitialCache({
Hero: initialHeroes,
Villain: [{ key: 'DE', name: 'Dr. Evil' }]
});
const action = new EntityCacheSet(cache);
// const action = { // equivalent
// type: SET_ENTITY_CACHE,
// payload: cache
// };
const state = entityReducer(cache, action);
expect(state['Hero'].ids).toEqual([2, 1], 'Hero ids');
expect(state['Hero'].entities).toEqual({
1: initialHeroes[1],
2: initialHeroes[0]
});
expect(state['Villain'].ids).toEqual(['DE'], 'Villain ids');
});
it('should clear the cache when set with empty object', () => {
const action = new EntityCacheSet({});
const state = entityReducer(initialCache, action);
expect(Object.keys(state)).toEqual([]);
});
it('should replace prior cache with new cache', () => {
const priorCache = createInitialCache({
Hero: initialHeroes,
Villain: [{ key: 'DE', name: 'Dr. Evil' }]
});
const newHeroes = [{ id: 42, name: 'Bobby' }];
const newCache = createInitialCache({ Hero: newHeroes });
const action = new EntityCacheSet(newCache);
const state = entityReducer(priorCache, action);
expect(state['Villain']).toBeUndefined('No villains');
const heroCollection = state['Hero'];
expect(heroCollection.ids).toEqual([42], 'hero ids');
expect(heroCollection.entities[42]).toEqual(newHeroes[0], 'heroes');
});
});
describe('#MERGE_ENTITY_CACHE', () => {
function shouldHaveExpectedHeroes(state: EntityCache) {
expect(state['Hero'].ids).toEqual([2, 1], 'Hero ids');
expect(state['Hero'].entities).toEqual({
1: initialHeroes[1],
2: initialHeroes[0]
});
}
it('should initialize an empty cache', () => {
const cache = createInitialCache({
Hero: initialHeroes,
Villain: [{ key: 'DE', name: 'Dr. Evil' }]
});
const action = new EntityCacheMerge(cache);
// const action = {
// type: MERGE_ENTITY_CACHE,
// payload: cache
// };
const state = entityReducer({}, action);
shouldHaveExpectedHeroes(state);
expect(state['Villain'].ids).toEqual(['DE'], 'Villain ids');
});
it('should return cache matching existing cache when merge empty', () => {
const action = new EntityCacheMerge({});
const state = entityReducer(initialCache, action);
shouldHaveExpectedHeroes(state);
});
it('should add a new collection to existing cache', () => {
const mergeCache = createInitialCache({
Villain: [{ key: 'DE', name: 'Dr. Evil' }]
});
const action = new EntityCacheMerge(mergeCache);
const state = entityReducer(initialCache, action);
shouldHaveExpectedHeroes(state);
expect(state['Villain'].ids).toEqual(['DE'], 'Villain ids');
});
it('should overwrite an existing cached collection', () => {
const mergeCache = createInitialCache({
Hero: [{ id: 42, name: 'Bobby' }]
});
const action = new EntityCacheMerge(mergeCache);
const state = entityReducer(initialCache, action);
const heroCollection = state['Hero'];
expect(heroCollection.ids).toEqual([42], 'revised ids');
expect(heroCollection.entities[42]).toEqual(
{ id: 42, name: 'Bobby' },
'revised heroes'
);
});
});
});
describe('#registerReducers', () => {
beforeEach(() => {
entityReducer = entityReducerFactory.create();
});
it('can register several reducers at the same time.', () => {
const reducer = createNoopReducer();
const reducers: EntityCollectionReducers = {
Foo: reducer,
Bar: reducer
};
entityReducerFactory.registerReducers(reducers);
const fooAction = entityActionFactory.create<Foo>(
'Foo',
EntityOp.ADD_ONE,
{ id: 'forty-two', foo: 'fooz' }
);
const barAction = entityActionFactory.create<Bar>(
'Bar',
EntityOp.ADD_ONE,
{ id: 84, bar: 'baz' }
);
let state = entityReducer({}, fooAction);
state = entityReducer(state, barAction);
expect(state['Foo'].ids.length).toBe(0, 'ADD_ONE Foo should not add');
expect(state['Bar'].ids.length).toBe(0, 'ADD_ONE Bar should not add');
});
it('can register several reducers that may override.', () => {
const reducer = createNoopReducer();
const reducers: EntityCollectionReducers = {
Foo: reducer,
Hero: reducer
};
entityReducerFactory.registerReducers(reducers);
const fooAction = entityActionFactory.create<Foo>(
'Foo',
EntityOp.ADD_ONE,
{ id: 'forty-two', foo: 'fooz' }
);
const heroAction = entityActionFactory.create<Hero>(
'Hero',
EntityOp.ADD_ONE,
{ id: 84, name: 'Alex' }
);
let state = entityReducer({}, fooAction);
state = entityReducer(state, heroAction);
expect(state['Foo'].ids.length).toBe(0, 'ADD_ONE Foo should not add');
expect(state['Hero'].ids.length).toBe(0, 'ADD_ONE Hero should not add');
});
});
describe('with EntityCollectionMetadataReducers', () => {
let metaReducerA: MetaReducer<EntityCollection, EntityAction>;
let metaReducerB: MetaReducer<EntityCollection, EntityAction>;
let metaReducerOutput: any[];
// Create MetaReducer that reports how it was called on the way in and out
function testMetadataReducerFactory(name: string) {
// Return the MetaReducer
return (r: ActionReducer<EntityCollection, EntityAction>) => {
// Return the wrapped reducer
return (state: EntityCollection, action: EntityAction) => {
// entered
metaReducerOutput.push({ metaReducer: name, inOut: 'in', action });
// called reducer
const newState = r(state, action);
// exited
metaReducerOutput.push({ metaReducer: name, inOut: 'out', action });
return newState;
};
};
}
let addOneAction: EntityAction<Hero>;
let hero: Hero;
beforeEach(() => {
metaReducerOutput = [];
metaReducerA = jasmine
.createSpy('metaReducerA')
.and.callFake(testMetadataReducerFactory('A'));
metaReducerB = jasmine
.createSpy('metaReducerA')
.and.callFake(testMetadataReducerFactory('B'));
const metaReducers = [metaReducerA, metaReducerB];
entityReducerFactory = new EntityReducerFactory(
collectionCreator,
collectionReducerFactory,
logger,
metaReducers
);
entityReducer = entityReducerFactory.create();
hero = { id: 42, name: 'Bobby' };
addOneAction = entityActionFactory.create<Hero>(
'Hero',
EntityOp.ADD_ONE,
hero
);
});
it('should run inner default reducer as expected', () => {
const state = entityReducer({}, addOneAction);
// inner default reducer worked as expected
const collection = state['Hero'];
expect(collection.ids.length).toBe(1, 'should have added one');
expect(collection.entities[42]).toEqual(hero, 'should be added hero');
});
it('should call meta reducers for inner default reducer as expected', () => {
const expected = [
{ metaReducer: 'A', inOut: 'in', action: addOneAction },
{ metaReducer: 'B', inOut: 'in', action: addOneAction },
{ metaReducer: 'B', inOut: 'out', action: addOneAction },
{ metaReducer: 'A', inOut: 'out', action: addOneAction }
];
const state = entityReducer({}, addOneAction);
expect(metaReducerA).toHaveBeenCalled();
expect(metaReducerB).toHaveBeenCalled();
expect(metaReducerOutput).toEqual(expected);
});
it('should call meta reducers for custom registered reducer', () => {
const reducer = createNoopReducer();
entityReducerFactory.registerReducer('Foo', reducer);
const action = entityActionFactory.create<Foo>('Foo', EntityOp.ADD_ONE, {
id: 'forty-two',
foo: 'fooz'
});
const state = entityReducer({}, action);
expect(metaReducerA).toHaveBeenCalled();
expect(metaReducerB).toHaveBeenCalled();
});
it('should call meta reducers for multiple registered reducers', () => {
const reducer = createNoopReducer();
const reducers: EntityCollectionReducers = {
Foo: reducer,
Hero: reducer
};
entityReducerFactory.registerReducers(reducers);
const fooAction = entityActionFactory.create<Foo>(
'Foo',
EntityOp.ADD_ONE,
{ id: 'forty-two', foo: 'fooz' }
);
entityReducer({}, fooAction);
expect(metaReducerA).toHaveBeenCalled();
expect(metaReducerB).toHaveBeenCalled();
const heroAction = entityActionFactory.create<Hero>(
'Hero',
EntityOp.ADD_ONE,
{ id: 84, name: 'Alex' }
);
entityReducer({}, heroAction);
expect(metaReducerA).toHaveBeenCalledTimes(2);
expect(metaReducerB).toHaveBeenCalledTimes(2);
});
});
// #region helpers
function createCollection<T = any>(
entityName: string,
data: T[],
selectId: IdSelector<any>
) {
return {
...collectionCreator.create<T>(entityName),
ids: data.map(e => selectId(e)) as string[] | number[],
entities: data.reduce(
(acc, e) => {
acc[selectId(e)] = e;
return acc;
},
{} as any
)
} as EntityCollection<T>;
}
function createInitialCache(entityMap: { [entityName: string]: any[] }) {
const cache: EntityCache = {};
// tslint:disable-next-line:forin
for (const entityName in entityMap) {
const selectId =
metadata[entityName].selectId || ((entity: any) => entity.id);
cache[entityName] = createCollection(
entityName,
entityMap[entityName],
selectId
);
}
return cache;
}
function createNoopReducer<T>() {
return function NoopReducer(
collection: EntityCollection<T>,
action: EntityAction
): EntityCollection<T> {
return collection;
};
}
// #endregion helpers
});