-
Notifications
You must be signed in to change notification settings - Fork 202
/
Copy pathng-redux.mock.ts
105 lines (95 loc) · 2.98 KB
/
ng-redux.mock.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import {
NgRedux,
Selector,
Comparator,
PathSelector,
} from '@angular-redux/store';
import {
AnyAction,
Reducer,
Dispatch,
Middleware,
Store,
StoreEnhancer,
} from 'redux';
import { Observable, Subject } from 'rxjs';
import { MockObservableStore } from './observable-store.mock';
// tslint:disable:member-ordering
/**
* Convenience mock to make it easier to control selector
* behaviour in unit tests.
*/
export class MockNgRedux<T = {}> extends NgRedux<T> {
/** @deprecated Use MockNgRedux.getInstance() instead. */
static mockInstance?: MockNgRedux<any> = undefined;
private mockRootStore = new MockObservableStore<any>();
/**
* Returns a subject that's connected to any observable returned by the
* given selector. You can use this subject to pump values into your
* components or services under test; when they call .select or @select
* in the context of a unit test, MockNgRedux will give them the values
* you pushed onto your stub.
*/
static getSelectorStub<R, S>(
selector?: Selector<R, S>,
comparator?: Comparator
): Subject<S> {
return MockNgRedux.getInstance().mockRootStore.getSelectorStub<S>(
selector,
comparator
);
}
/**
* Returns a mock substore that allows you to set up selectorStubs for
* any 'fractal' stores your app creates with NgRedux.configureSubStore.
*
* If your app creates deeply nested substores from other substores,
* pass the chain of pathSelectors in as ordered arguments to mock
* the nested substores out.
* @param pathSelectors
*/
static getSubStore<S>(
...pathSelectors: PathSelector[]
): MockObservableStore<S> {
return pathSelectors.length
? MockNgRedux.getInstance().mockRootStore.getSubStore(...pathSelectors)
: MockNgRedux.getInstance().mockRootStore;
}
/**
* Reset all previously configured stubs.
*/
static reset(): void {
MockNgRedux.getInstance().mockRootStore.reset();
NgRedux.instance = MockNgRedux.mockInstance;
}
/**
* Gets the singleton MockNgRedux instance. Useful for cases where your
* tests need to spy on store methods, for example.
*/
static getInstance() {
MockNgRedux.mockInstance = MockNgRedux.mockInstance || new MockNgRedux();
return MockNgRedux.mockInstance;
}
provideStore = (_: Store<any>): void => {};
configureStore = (
_: Reducer<any, AnyAction>,
__: any,
___?: Middleware[],
____?: StoreEnhancer<any>[]
): void => {};
configureSubStore = this.mockRootStore.configureSubStore;
select: <SelectedType>(
selector?: Selector<T, SelectedType>,
comparator?: Comparator
) => Observable<SelectedType> = this.mockRootStore.select;
dispatch = this.mockRootStore.dispatch as Dispatch<any>;
getState = this.mockRootStore.getState;
subscribe = this.mockRootStore.subscribe;
replaceReducer = this.mockRootStore.replaceReducer;
/** @hidden */
private constructor() {
super();
// This hooks the mock up to @select.
NgRedux.instance = this;
}
}