|
4 | 4 | createFeatureSelector,
|
5 | 5 | defaultMemoize,
|
6 | 6 | createSelectorFactory,
|
| 7 | + resultMemoize, |
| 8 | + MemoizedProjection, |
7 | 9 | } from '@ngrx/store';
|
8 | 10 | import { map, distinctUntilChanged } from 'rxjs/operators';
|
9 | 11 |
|
@@ -285,4 +287,72 @@ describe('Selectors', () => {
|
285 | 287 | expect(anyFn.calls.count()).toEqual(1);
|
286 | 288 | });
|
287 | 289 | });
|
| 290 | + |
| 291 | + describe('resultMemoize', () => { |
| 292 | + let projectionFnSpy: jasmine.Spy; |
| 293 | + const ARRAY = ['a', 'ab', 'b']; |
| 294 | + const ARRAY_CHANGED = [...ARRAY, 'bc']; |
| 295 | + const A_FILTER: { by: string } = { by: 'a' }; |
| 296 | + const B_FILTER: { by: string } = { by: 'b' }; |
| 297 | + |
| 298 | + let arrayMemoizer: MemoizedProjection; |
| 299 | + |
| 300 | + // Compare a and b on equality. If a and b are Arrays then compare them |
| 301 | + // on their content. |
| 302 | + function isResultEqual(a: any, b: any) { |
| 303 | + if (a instanceof Array) { |
| 304 | + return a.length === b.length && a.every(fromA => b.includes(fromA)); |
| 305 | + } |
| 306 | + // Default comparison |
| 307 | + return a === b; |
| 308 | + } |
| 309 | + |
| 310 | + beforeEach(() => { |
| 311 | + projectionFnSpy = jasmine |
| 312 | + .createSpy('projectionFn') |
| 313 | + .and.callFake((arr: string[], filter: { by: string }) => |
| 314 | + arr.filter(item => item.startsWith(filter.by)) |
| 315 | + ); |
| 316 | + |
| 317 | + arrayMemoizer = resultMemoize(projectionFnSpy, isResultEqual); |
| 318 | + }); |
| 319 | + |
| 320 | + it('should not rerun projector function when arguments stayed the same', () => { |
| 321 | + arrayMemoizer.memoized(ARRAY, A_FILTER); |
| 322 | + arrayMemoizer.memoized(ARRAY, A_FILTER); |
| 323 | + |
| 324 | + expect(projectionFnSpy.calls.count()).toBe(1); |
| 325 | + }); |
| 326 | + |
| 327 | + it('should rerun projector function when arguments changed', () => { |
| 328 | + arrayMemoizer.memoized(ARRAY, A_FILTER); |
| 329 | + arrayMemoizer.memoized(ARRAY_CHANGED, A_FILTER); |
| 330 | + |
| 331 | + expect(projectionFnSpy.calls.count()).toBe(2); |
| 332 | + }); |
| 333 | + |
| 334 | + it('should return the same instance of results when projector function produces the same results array', () => { |
| 335 | + const result1 = arrayMemoizer.memoized(ARRAY, A_FILTER); |
| 336 | + const result2 = arrayMemoizer.memoized(ARRAY, A_FILTER); |
| 337 | + |
| 338 | + expect(result1).toBe(result2); |
| 339 | + }); |
| 340 | + |
| 341 | + it('should return the same instance of results when projector function produces similar results array', () => { |
| 342 | + const result1 = arrayMemoizer.memoized(ARRAY, A_FILTER); |
| 343 | + const result2 = arrayMemoizer.memoized(ARRAY_CHANGED, A_FILTER); |
| 344 | + |
| 345 | + expect(result1).toBe(result2); |
| 346 | + }); |
| 347 | + |
| 348 | + it('should return the new instance of results when projector function produces different result', () => { |
| 349 | + const result1 = arrayMemoizer.memoized(ARRAY, A_FILTER); |
| 350 | + const result2 = arrayMemoizer.memoized(ARRAY_CHANGED, B_FILTER); |
| 351 | + |
| 352 | + expect(result1).toBeDefined(); |
| 353 | + expect(result2).toBeDefined(); |
| 354 | + expect(result1).not.toBe(result2); |
| 355 | + expect(result1).not.toEqual(result2); |
| 356 | + }); |
| 357 | + }); |
288 | 358 | });
|
0 commit comments