1
- import { Store , createReducer , Action , ofAction } from './index' ;
2
- import { of } from 'rxjs/Observable/of' ;
3
- import { Action as NgRxAction } from '@ngrx/store' ;
1
+ import { Store , createReducer , Action , ofAction , Select , NgrxSelect } from '../index' ;
2
+ import { Action as NgRxAction , createFeatureSelector , createSelector , Store as NgRxStore } from '@ngrx/store' ;
3
+ import { Observable } from 'rxjs/Observable' ;
4
+ import { of } from 'rxjs/observable/of' ;
4
5
5
6
describe ( 'actions' , ( ) => {
6
7
interface FooState {
7
8
foo : boolean | null ;
9
+ bar ?: {
10
+ a ?: {
11
+ b ?: any ;
12
+ } ;
13
+ } ;
8
14
}
9
15
10
16
it ( 'has strict type checking working' , ( ) => {
@@ -20,9 +26,9 @@ describe('actions', () => {
20
26
}
21
27
}
22
28
23
- const reducer = createReducer < FooState > ( Bar ) ;
29
+ const reducer = createReducer < FooState | undefined > ( Bar ) ;
24
30
const res = reducer ( undefined , new MyAction ( ) ) ;
25
- expect ( res . foo ) . toBe ( true ) ;
31
+ expect ( res && res . foo ) . toBe ( true ) ;
26
32
} ) ;
27
33
28
34
it ( 'adds defaults' , ( ) => {
@@ -131,13 +137,72 @@ describe('actions', () => {
131
137
readonly type = 'myaction2' ;
132
138
}
133
139
134
- const action = new MyAction ( 'foo' ) ;
135
- const actions = of < NgRxAction > ( action , new MyAction2 ( ) ) ;
136
- let tappedAction : NgRxAction ;
137
- actions . pipe ( ofAction ( MyAction ) ) . subscribe ( a => {
138
- tappedAction = a ;
140
+ class MyAction3 implements NgRxAction {
141
+ readonly type = 'myaction3' ;
142
+ constructor ( public foo : any , public bar : any ) { }
143
+ }
144
+
145
+ const action = new MyAction ( 'foo' ) ,
146
+ action2 = new MyAction2 ( ) ,
147
+ action3 = new MyAction3 ( 'a' , 0 ) ;
148
+ const actions = of < NgRxAction > ( action , action2 , action3 ) ;
149
+ const tappedActions : NgRxAction [ ] = [ ] ;
150
+ actions . pipe ( ofAction < MyAction | MyAction2 > ( MyAction , MyAction2 ) ) . subscribe ( a => {
151
+ tappedActions . push ( a ) ;
139
152
} ) ;
140
153
141
- expect ( tappedAction ) . toBe ( action ) ;
154
+ expect ( tappedActions . length ) . toEqual ( 2 ) ;
155
+ expect ( tappedActions [ 0 ] ) . toBe ( action ) ;
156
+ expect ( tappedActions [ 1 ] ) . toBe ( action2 ) ;
157
+ } ) ;
158
+
159
+ it ( 'selects sub state' , ( ) => {
160
+ const globalState : {
161
+ myFeature : FooState ;
162
+ } = {
163
+ myFeature : {
164
+ foo : true ,
165
+ bar : {
166
+ a : {
167
+ b : {
168
+ c : {
169
+ d : 'world'
170
+ }
171
+ }
172
+ }
173
+ }
174
+ }
175
+ } ;
176
+
177
+ const msFeature = createFeatureSelector < FooState > ( 'myFeature' ) ;
178
+ const msBar = createSelector ( msFeature , state => state . bar ) ;
179
+
180
+ class MyStateSelector {
181
+ @Select ( 'myFeature.bar.a.b.c.d' ) hello$ : Observable < string > ; // deeply nested props
182
+ @Select ( ) myFeature : Observable < FooState > ; // implied by name
183
+ @Select ( msBar ) bar$ : Observable < any > ; // using MemoizedSelector
184
+ }
185
+
186
+ const store = new NgRxStore ( of ( globalState ) , undefined , undefined ) ;
187
+
188
+ try {
189
+ NgrxSelect . store = store ;
190
+
191
+ const mss = new MyStateSelector ( ) ;
192
+
193
+ mss . hello$ . subscribe ( n => {
194
+ expect ( n ) . toBe ( 'world' ) ;
195
+ } ) ;
196
+
197
+ mss . myFeature . subscribe ( n => {
198
+ expect ( n ) . toBe ( globalState . myFeature ) ;
199
+ } ) ;
200
+
201
+ mss . bar$ . subscribe ( n => {
202
+ expect ( n ) . toBe ( globalState . myFeature . bar ) ;
203
+ } ) ;
204
+ } finally {
205
+ NgrxSelect . store = undefined ;
206
+ }
142
207
} ) ;
143
208
} ) ;
0 commit comments