Skip to content

Commit

Permalink
feat(store): add clearResult to reset a mock selector (ngrx#2270)
Browse files Browse the repository at this point in the history
Closes ngrx#2244 

BREAKING CHANGE:

BEFORE:

Using `mockSelector.setResult(undefined)` resulted in clearing the
return value.

AFTER:

Using `mockSelector.setResult(undefined)` will set the return value of
the selector to `undefined`.
To reset the mock selector, use `mockSelector.clearResult()`.
  • Loading branch information
timdeschryver authored and brandonroberts committed Jan 8, 2020
1 parent 3a683cd commit 803295b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
14 changes: 11 additions & 3 deletions modules/store/src/selector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export type MemoizedProjection = {
memoized: AnyFn;
reset: () => void;
setResult: (result?: any) => void;
clearResult: () => void;
};

export type MemoizeFn = (t: AnyFn) => MemoizedProjection;
Expand All @@ -23,6 +24,7 @@ export interface MemoizedSelector<
release(): void;
projector: ProjectorFn;
setResult: (result?: Result) => void;
clearResult: () => void;
}

export interface MemoizedSelectorWithProps<
Expand All @@ -34,6 +36,7 @@ export interface MemoizedSelectorWithProps<
release(): void;
projector: ProjectorFn;
setResult: (result?: Result) => void;
clearResult: () => void;
}

export function isEqualCheck(a: any, b: any): boolean {
Expand Down Expand Up @@ -76,13 +79,17 @@ export function defaultMemoize(
}

function setResult(result: any = undefined) {
overrideResult = result;
overrideResult = { result };
}

function clearResult() {
overrideResult = undefined;
}

// tslint:disable-next-line:no-any anything could be the result.
function memoized(): any {
if (overrideResult !== undefined) {
return overrideResult;
return overrideResult.result;
}

if (!lastArguments) {
Expand All @@ -107,7 +114,7 @@ export function defaultMemoize(
return newResult;
}

return { memoized, reset, setResult };
return { memoized, reset, setResult, clearResult };
}

export function createSelector<State, S1, Result>(
Expand Down Expand Up @@ -590,6 +597,7 @@ export function createSelectorFactory(
release,
projector: memoizedProjector.memoized,
setResult: memoizedState.setResult,
clearResult: memoizedState.clearResult,
});
};
}
Expand Down
2 changes: 1 addition & 1 deletion modules/store/testing/src/mock_store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export class MockStore<T> extends Store<T> {
MockStore.selectors.forEach((_, selector) => {
if (typeof selector !== 'string') {
selector.release();
selector.setResult();
selector.clearResult();
}
});

Expand Down

0 comments on commit 803295b

Please sign in to comment.