Skip to content

Commit

Permalink
Adjust the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-okrushko committed Jul 16, 2018
1 parent 1033dfb commit 45c9b8d
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 55 deletions.
113 changes: 58 additions & 55 deletions modules/store/spec/selector.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
createFeatureSelector,
defaultMemoize,
createSelectorFactory,
resultMemoize,
MemoizedProjection,
} from '@ngrx/store';
import { map, distinctUntilChanged } from 'rxjs/operators';
Expand Down Expand Up @@ -251,14 +252,16 @@ describe('Selectors', () => {
it('should allow a custom memoization function', () => {
const projectFn = jasmine.createSpy('projectionFn');
const anyFn = jasmine.createSpy('t').and.callFake(() => true);
const customMemoizer = (aFn: any = anyFn) => defaultMemoize(anyFn);
const equalFn = jasmine.createSpy('isEqual').and.callFake(() => true);
const customMemoizer = (aFn: any = anyFn, eFn: any = equalFn) =>
defaultMemoize(anyFn, equalFn);
const customSelector = createSelectorFactory(customMemoizer);

const selector = customSelector(incrementOne, incrementTwo, projectFn);
selector(1);
selector(2);

expect(anyFn.calls.count()).toEqual(2);
expect(anyFn.calls.count()).toEqual(1);
});

it('should allow a custom state memoization function', () => {
Expand All @@ -283,73 +286,73 @@ describe('Selectors', () => {

expect(anyFn.calls.count()).toEqual(1);
});
});

describe('resultMemoize', () => {
let projectionFnSpy: jasmine.Spy;
const ARRAY = ['a', 'ab', 'b'];
const ARRAY_CHANGED = [...ARRAY, 'bc'];
const A_FILTER: { by: string } = { by: 'a' };
const B_FILTER: { by: string } = { by: 'b' };

let arrayMemoizer: MemoizedProjection;

describe('with custom isResultEqual', () => {
let projectionFnSpy: jasmine.Spy;
const ARRAY = ['a', 'ab', 'b'];
const ARRAY_CHANGED = [...ARRAY, 'bc'];
const A_FILTER: { by: string } = { by: 'a' };
const B_FILTER: { by: string } = { by: 'b' };

let arrayMemoizer: MemoizedProjection;

// Compare a and b on equality. If a and b are Arrays then compare them
// on their content.
function isResultEqual(a: any, b: any) {
if (a instanceof Array) {
return a.length === b.length && a.every(fromA => b.includes(fromA));
}
// Default comparison
return a === b;
// Compare a and b on equality. If a and b are Arrays then compare them
// on their content.
function isResultEqual(a: any, b: any) {
if (a instanceof Array) {
return a.length === b.length && a.every(fromA => b.includes(fromA));
}
// Default comparison
return a === b;
}

beforeEach(() => {
projectionFnSpy = jasmine
.createSpy('projectionFn')
.and.callFake((arr: string[], filter: { by: string }) =>
arr.filter(item => item.startsWith(filter.by))
);
beforeEach(() => {
projectionFnSpy = jasmine
.createSpy('projectionFn')
.and.callFake((arr: string[], filter: { by: string }) =>
arr.filter(item => item.startsWith(filter.by))
);

arrayMemoizer = defaultMemoize(projectionFnSpy, isResultEqual);
});
arrayMemoizer = resultMemoize(projectionFnSpy, isResultEqual);
});

it('should not rerun projector function when arguments stayed the same', () => {
arrayMemoizer.memoized(ARRAY, A_FILTER);
arrayMemoizer.memoized(ARRAY, A_FILTER);
it('should not rerun projector function when arguments stayed the same', () => {
arrayMemoizer.memoized(ARRAY, A_FILTER);
arrayMemoizer.memoized(ARRAY, A_FILTER);

expect(projectionFnSpy.calls.count()).toBe(1);
});
expect(projectionFnSpy.calls.count()).toBe(1);
});

it('should rerun projector function when arguments changed', () => {
arrayMemoizer.memoized(ARRAY, A_FILTER);
arrayMemoizer.memoized(ARRAY_CHANGED, A_FILTER);
it('should rerun projector function when arguments changed', () => {
arrayMemoizer.memoized(ARRAY, A_FILTER);
arrayMemoizer.memoized(ARRAY_CHANGED, A_FILTER);

expect(projectionFnSpy.calls.count()).toBe(2);
});
expect(projectionFnSpy.calls.count()).toBe(2);
});

it('should return the same instance of results when projector function produces the same results array', () => {
const result1 = arrayMemoizer.memoized(ARRAY, A_FILTER);
const result2 = arrayMemoizer.memoized(ARRAY, A_FILTER);
it('should return the same instance of results when projector function produces the same results array', () => {
const result1 = arrayMemoizer.memoized(ARRAY, A_FILTER);
const result2 = arrayMemoizer.memoized(ARRAY, A_FILTER);

expect(result1).toBe(result2);
});
expect(result1).toBe(result2);
});

it('should return the same instance of results when projector function produces similar results array', () => {
const result1 = arrayMemoizer.memoized(ARRAY, A_FILTER);
const result2 = arrayMemoizer.memoized(ARRAY_CHANGED, A_FILTER);
it('should return the same instance of results when projector function produces similar results array', () => {
const result1 = arrayMemoizer.memoized(ARRAY, A_FILTER);
const result2 = arrayMemoizer.memoized(ARRAY_CHANGED, A_FILTER);

expect(result1).toBe(result2);
});
expect(result1).toBe(result2);
});

it('should return the new instance of results when projector function produces different result', () => {
const result1 = arrayMemoizer.memoized(ARRAY, A_FILTER);
const result2 = arrayMemoizer.memoized(ARRAY_CHANGED, B_FILTER);
it('should return the new instance of results when projector function produces different result', () => {
const result1 = arrayMemoizer.memoized(ARRAY, A_FILTER);
const result2 = arrayMemoizer.memoized(ARRAY_CHANGED, B_FILTER);

expect(result1).toBeDefined();
expect(result2).toBeDefined();
expect(result1).not.toBe(result2);
expect(result1).not.toEqual(result2);
});
expect(result1).toBeDefined();
expect(result2).toBeDefined();
expect(result1).not.toBe(result2);
expect(result1).not.toEqual(result2);
});
});
});
1 change: 1 addition & 0 deletions modules/store/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export {
MemoizeFn,
MemoizedProjection,
MemoizedSelector,
resultMemoize,
} from './selector';
export { State, StateObservable, reduceState } from './state';
export {
Expand Down

0 comments on commit 45c9b8d

Please sign in to comment.