Skip to content

Commit

Permalink
fix(store): adjust mock store to handle selectors with props (#1878)
Browse files Browse the repository at this point in the history
Closes #1864, #1873
  • Loading branch information
alex-okrushko authored and brandonroberts committed May 21, 2019
1 parent 3454e70 commit a7ded00
Show file tree
Hide file tree
Showing 6 changed files with 306 additions and 183 deletions.
5 changes: 3 additions & 2 deletions modules/store/spec/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ ts_test_library(
name = "test_lib",
srcs = glob(
[
"**/*.ts",
"*.ts",
"meta-reducers/**/*.ts",
],
exclude = ["ngc/**/*.ts"],
),
deps = [
"//modules/store",
"//modules/store/spec/fixtures",
"//modules/store/testing",
"@npm//rxjs",
"@npm//ts-snippet",
Expand Down
17 changes: 17 additions & 0 deletions modules/store/spec/fixtures/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package(default_visibility = ["//visibility:public"])

exports_files(["package.json"])

load("//tools:defaults.bzl", "ng_module")

ng_module(
name = "fixtures",
srcs = glob([
"*.ts",
"src/**/*.ts",
]),
visibility = ["//visibility:public"],
deps = [
"//modules/store",
],
)
180 changes: 1 addition & 179 deletions modules/store/spec/store.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
select,
ReducerManagerDispatcher,
UPDATE,
REDUCER_FACTORY,
ActionReducer,
Action,
} from '../';
Expand All @@ -23,10 +22,7 @@ import {
counterReducer2,
} from './fixtures/counter';
import Spy = jasmine.Spy;
import any = jasmine.any;
import { skip, take } from 'rxjs/operators';
import { MockStore, provideMockStore } from '../testing';
import { createSelector } from '../src/selector';
import { take } from 'rxjs/operators';

interface TestAppSchema {
counter1: number;
Expand Down Expand Up @@ -447,180 +443,6 @@ describe('ngRx Store', () => {
}
});

describe('Mock Store', () => {
let mockStore: MockStore<TestAppSchema>;
const initialState = { counter1: 0, counter2: 1, counter4: 3 };
const stringSelector = 'counter4';
const memoizedSelector = createSelector(
() => initialState,
state => state.counter4
);

beforeEach(() => {
TestBed.configureTestingModule({
providers: [
provideMockStore({
initialState,
selectors: [
{ selector: stringSelector, value: 87 },
{ selector: memoizedSelector, value: 98 },
],
}),
],
});

mockStore = TestBed.get(Store);
});

it('should set the initial state to a mocked one', (done: DoneFn) => {
const fixedState = {
counter1: 17,
counter2: 11,
counter3: 25,
};
mockStore.setState(fixedState);
mockStore.pipe(take(1)).subscribe({
next(val) {
expect(val).toEqual(fixedState);
},
error: done.fail,
complete: done,
});
});

it('should allow tracing dispatched actions', () => {
const action = { type: INCREMENT };
mockStore.scannedActions$
.pipe(skip(1))
.subscribe(scannedAction => expect(scannedAction).toEqual(action));
mockStore.dispatch(action);
});

it('should allow mocking of store.select with string selector using provideMockStore', () => {
const expectedValue = 87;

mockStore
.select(stringSelector)
.subscribe(result => expect(result).toBe(expectedValue));
});

it('should allow mocking of store.select with a memoized selector using provideMockStore', () => {
const expectedValue = 98;

mockStore
.select(memoizedSelector)
.subscribe(result => expect(result).toBe(expectedValue));
});

it('should allow mocking of store.pipe(select()) with a memoized selector using provideMockStore', () => {
const expectedValue = 98;

mockStore
.pipe(select(memoizedSelector))
.subscribe(result => expect(result).toBe(expectedValue));
});

it('should allow mocking of store.select with string selector using overrideSelector', () => {
const mockValue = 5;

mockStore.overrideSelector('counter1', mockValue);

mockStore
.select('counter1')
.subscribe(result => expect(result).toBe(mockValue));
});

it('should allow mocking of store.select with a memoized selector using overrideSelector', () => {
const mockValue = 5;
const selector = createSelector(
() => initialState,
state => state.counter1
);

mockStore.overrideSelector(selector, mockValue);

mockStore
.select(selector)
.subscribe(result => expect(result).toBe(mockValue));
});

it('should allow mocking of store.pipe(select()) with a memoized selector using overrideSelector', () => {
const mockValue = 5;
const selector = createSelector(
() => initialState,
state => state.counter2
);

mockStore.overrideSelector(selector, mockValue);

mockStore
.pipe(select(selector))
.subscribe(result => expect(result).toBe(mockValue));
});

it('should pass through unmocked selectors', () => {
const mockValue = 5;
const selector = createSelector(
() => initialState,
state => state.counter1
);
const selector2 = createSelector(
() => initialState,
state => state.counter2
);
const selector3 = createSelector(
selector,
selector2,
(sel1, sel2) => sel1 + sel2
);

mockStore.overrideSelector(selector, mockValue);

mockStore
.pipe(select(selector2))
.subscribe(result => expect(result).toBe(1));
mockStore
.pipe(select(selector3))
.subscribe(result => expect(result).toBe(6));
});

it('should allow you reset mocked selectors', () => {
const mockValue = 5;
const selector = createSelector(
() => initialState,
state => state.counter1
);
const selector2 = createSelector(
() => initialState,
state => state.counter2
);
const selector3 = createSelector(
selector,
selector2,
(sel1, sel2) => sel1 + sel2
);

mockStore
.pipe(select(selector3))
.subscribe(result => expect(result).toBe(1));

mockStore.overrideSelector(selector, mockValue);
mockStore.overrideSelector(selector2, mockValue);
selector3.release();

mockStore
.pipe(select(selector3))
.subscribe(result => expect(result).toBe(10));

mockStore.resetSelectors();
selector3.release();

mockStore
.pipe(select(selector3))
.subscribe(result => expect(result).toBe(1));
});
});

describe('Meta Reducers', () => {
let metaReducerContainer: any;
let metaReducerSpy1: Spy;
Expand Down
22 changes: 22 additions & 0 deletions modules/store/testing/spec/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
load("//tools:defaults.bzl", "jasmine_node_test", "ts_test_library")

ts_test_library(
name = "test_lib",
srcs = glob(
["**/*.ts"],
),
deps = [
"//modules/store",
"//modules/store/spec/fixtures",
"//modules/store/testing",
"@npm//rxjs",
],
)

jasmine_node_test(
name = "test",
deps = [
":test_lib",
"//modules/store",
],
)
Loading

0 comments on commit a7ded00

Please sign in to comment.