Skip to content

Commit a7ded00

Browse files
alex-okrushkobrandonroberts
authored andcommitted
fix(store): adjust mock store to handle selectors with props (#1878)
Closes #1864, #1873
1 parent 3454e70 commit a7ded00

File tree

6 files changed

+306
-183
lines changed

6 files changed

+306
-183
lines changed

modules/store/spec/BUILD

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ ts_test_library(
44
name = "test_lib",
55
srcs = glob(
66
[
7-
"**/*.ts",
7+
"*.ts",
8+
"meta-reducers/**/*.ts",
89
],
9-
exclude = ["ngc/**/*.ts"],
1010
),
1111
deps = [
1212
"//modules/store",
13+
"//modules/store/spec/fixtures",
1314
"//modules/store/testing",
1415
"@npm//rxjs",
1516
"@npm//ts-snippet",

modules/store/spec/fixtures/BUILD

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package(default_visibility = ["//visibility:public"])
2+
3+
exports_files(["package.json"])
4+
5+
load("//tools:defaults.bzl", "ng_module")
6+
7+
ng_module(
8+
name = "fixtures",
9+
srcs = glob([
10+
"*.ts",
11+
"src/**/*.ts",
12+
]),
13+
visibility = ["//visibility:public"],
14+
deps = [
15+
"//modules/store",
16+
],
17+
)

modules/store/spec/store.spec.ts

Lines changed: 1 addition & 179 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {
99
select,
1010
ReducerManagerDispatcher,
1111
UPDATE,
12-
REDUCER_FACTORY,
1312
ActionReducer,
1413
Action,
1514
} from '../';
@@ -23,10 +22,7 @@ import {
2322
counterReducer2,
2423
} from './fixtures/counter';
2524
import Spy = jasmine.Spy;
26-
import any = jasmine.any;
27-
import { skip, take } from 'rxjs/operators';
28-
import { MockStore, provideMockStore } from '../testing';
29-
import { createSelector } from '../src/selector';
25+
import { take } from 'rxjs/operators';
3026

3127
interface TestAppSchema {
3228
counter1: number;
@@ -447,180 +443,6 @@ describe('ngRx Store', () => {
447443
}
448444
});
449445

450-
describe('Mock Store', () => {
451-
let mockStore: MockStore<TestAppSchema>;
452-
const initialState = { counter1: 0, counter2: 1, counter4: 3 };
453-
const stringSelector = 'counter4';
454-
const memoizedSelector = createSelector(
455-
() => initialState,
456-
state => state.counter4
457-
);
458-
459-
beforeEach(() => {
460-
TestBed.configureTestingModule({
461-
providers: [
462-
provideMockStore({
463-
initialState,
464-
selectors: [
465-
{ selector: stringSelector, value: 87 },
466-
{ selector: memoizedSelector, value: 98 },
467-
],
468-
}),
469-
],
470-
});
471-
472-
mockStore = TestBed.get(Store);
473-
});
474-
475-
it('should set the initial state to a mocked one', (done: DoneFn) => {
476-
const fixedState = {
477-
counter1: 17,
478-
counter2: 11,
479-
counter3: 25,
480-
};
481-
mockStore.setState(fixedState);
482-
mockStore.pipe(take(1)).subscribe({
483-
next(val) {
484-
expect(val).toEqual(fixedState);
485-
},
486-
error: done.fail,
487-
complete: done,
488-
});
489-
});
490-
491-
it('should allow tracing dispatched actions', () => {
492-
const action = { type: INCREMENT };
493-
mockStore.scannedActions$
494-
.pipe(skip(1))
495-
.subscribe(scannedAction => expect(scannedAction).toEqual(action));
496-
mockStore.dispatch(action);
497-
});
498-
499-
it('should allow mocking of store.select with string selector using provideMockStore', () => {
500-
const expectedValue = 87;
501-
502-
mockStore
503-
.select(stringSelector)
504-
.subscribe(result => expect(result).toBe(expectedValue));
505-
});
506-
507-
it('should allow mocking of store.select with a memoized selector using provideMockStore', () => {
508-
const expectedValue = 98;
509-
510-
mockStore
511-
.select(memoizedSelector)
512-
.subscribe(result => expect(result).toBe(expectedValue));
513-
});
514-
515-
it('should allow mocking of store.pipe(select()) with a memoized selector using provideMockStore', () => {
516-
const expectedValue = 98;
517-
518-
mockStore
519-
.pipe(select(memoizedSelector))
520-
.subscribe(result => expect(result).toBe(expectedValue));
521-
});
522-
523-
it('should allow mocking of store.select with string selector using overrideSelector', () => {
524-
const mockValue = 5;
525-
526-
mockStore.overrideSelector('counter1', mockValue);
527-
528-
mockStore
529-
.select('counter1')
530-
.subscribe(result => expect(result).toBe(mockValue));
531-
});
532-
533-
it('should allow mocking of store.select with a memoized selector using overrideSelector', () => {
534-
const mockValue = 5;
535-
const selector = createSelector(
536-
() => initialState,
537-
state => state.counter1
538-
);
539-
540-
mockStore.overrideSelector(selector, mockValue);
541-
542-
mockStore
543-
.select(selector)
544-
.subscribe(result => expect(result).toBe(mockValue));
545-
});
546-
547-
it('should allow mocking of store.pipe(select()) with a memoized selector using overrideSelector', () => {
548-
const mockValue = 5;
549-
const selector = createSelector(
550-
() => initialState,
551-
state => state.counter2
552-
);
553-
554-
mockStore.overrideSelector(selector, mockValue);
555-
556-
mockStore
557-
.pipe(select(selector))
558-
.subscribe(result => expect(result).toBe(mockValue));
559-
});
560-
561-
it('should pass through unmocked selectors', () => {
562-
const mockValue = 5;
563-
const selector = createSelector(
564-
() => initialState,
565-
state => state.counter1
566-
);
567-
const selector2 = createSelector(
568-
() => initialState,
569-
state => state.counter2
570-
);
571-
const selector3 = createSelector(
572-
selector,
573-
selector2,
574-
(sel1, sel2) => sel1 + sel2
575-
);
576-
577-
mockStore.overrideSelector(selector, mockValue);
578-
579-
mockStore
580-
.pipe(select(selector2))
581-
.subscribe(result => expect(result).toBe(1));
582-
mockStore
583-
.pipe(select(selector3))
584-
.subscribe(result => expect(result).toBe(6));
585-
});
586-
587-
it('should allow you reset mocked selectors', () => {
588-
const mockValue = 5;
589-
const selector = createSelector(
590-
() => initialState,
591-
state => state.counter1
592-
);
593-
const selector2 = createSelector(
594-
() => initialState,
595-
state => state.counter2
596-
);
597-
const selector3 = createSelector(
598-
selector,
599-
selector2,
600-
(sel1, sel2) => sel1 + sel2
601-
);
602-
603-
mockStore
604-
.pipe(select(selector3))
605-
.subscribe(result => expect(result).toBe(1));
606-
607-
mockStore.overrideSelector(selector, mockValue);
608-
mockStore.overrideSelector(selector2, mockValue);
609-
selector3.release();
610-
611-
mockStore
612-
.pipe(select(selector3))
613-
.subscribe(result => expect(result).toBe(10));
614-
615-
mockStore.resetSelectors();
616-
selector3.release();
617-
618-
mockStore
619-
.pipe(select(selector3))
620-
.subscribe(result => expect(result).toBe(1));
621-
});
622-
});
623-
624446
describe('Meta Reducers', () => {
625447
let metaReducerContainer: any;
626448
let metaReducerSpy1: Spy;

modules/store/testing/spec/BUILD

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
load("//tools:defaults.bzl", "jasmine_node_test", "ts_test_library")
2+
3+
ts_test_library(
4+
name = "test_lib",
5+
srcs = glob(
6+
["**/*.ts"],
7+
),
8+
deps = [
9+
"//modules/store",
10+
"//modules/store/spec/fixtures",
11+
"//modules/store/testing",
12+
"@npm//rxjs",
13+
],
14+
)
15+
16+
jasmine_node_test(
17+
name = "test",
18+
deps = [
19+
":test_lib",
20+
"//modules/store",
21+
],
22+
)

0 commit comments

Comments
 (0)