1
- import { jestMatcher } from '@test-helpers/rx-angular' ;
1
+ import { TestBed } from '@angular/core/testing' ;
2
+ import {
3
+ createAccumulationObservable ,
4
+ select ,
5
+ } from '@rx-angular/state/selections' ;
2
6
import {
3
- initialPrimitiveState ,
4
7
PrimitiveState ,
8
+ initialPrimitiveState ,
9
+ jestMatcher ,
5
10
} from '@test-helpers/rx-angular' ;
6
11
import { of , throwError } from 'rxjs' ;
7
12
import { map } from 'rxjs/operators' ;
8
13
import { TestScheduler } from 'rxjs/testing' ;
9
- import { select } from '../src' ;
10
- import { createAccumulationObservable } from '../src/lib/accumulation-observable' ;
11
14
12
15
function setupAccumulationObservable < T extends object > ( cfg : {
13
16
initialState ?: T ;
@@ -24,165 +27,199 @@ function setupAccumulationObservable<T extends object>(cfg: {
24
27
return acc ;
25
28
}
26
29
30
+ let testBed : TestBed ;
27
31
let testScheduler : TestScheduler ;
28
32
29
33
beforeEach ( ( ) => {
34
+ testBed = TestBed . configureTestingModule ( { } ) ;
30
35
testScheduler = new TestScheduler ( jestMatcher ) ;
31
36
} ) ;
32
37
33
38
describe ( 'createAccumulationObservable' , ( ) => {
34
39
it ( 'should return object' , ( ) => {
35
- const acc = createAccumulationObservable ( ) ;
36
- expect ( acc ) . toBeDefined ( ) ;
40
+ testBed . runInInjectionContext ( ( ) => {
41
+ const acc = createAccumulationObservable ( ) ;
42
+ expect ( acc ) . toBeDefined ( ) ;
43
+ } ) ;
37
44
} ) ;
38
45
39
46
describe ( 'signal$' , ( ) => {
40
47
it ( 'should return NO empty base-state after init when subscribing late' , ( ) => {
41
48
testScheduler . run ( ( { expectObservable } ) => {
42
- const state = setupAccumulationObservable ( { } ) ;
43
- expectObservable ( state . signal$ ) . toBe ( '' ) ;
49
+ testBed . runInInjectionContext ( ( ) => {
50
+ const state = setupAccumulationObservable ( { } ) ;
51
+ expectObservable ( state . signal$ ) . toBe ( '' ) ;
52
+ } ) ;
44
53
} ) ;
45
54
} ) ;
46
55
47
56
it ( 'should return No changes when subscribing late' , ( ) => {
48
57
testScheduler . run ( ( { expectObservable } ) => {
49
- const state = setupAccumulationObservable < PrimitiveState > ( { } ) ;
50
- state . subscribe ( ) ;
58
+ testBed . runInInjectionContext ( ( ) => {
59
+ const state = setupAccumulationObservable < PrimitiveState > ( { } ) ;
60
+ state . subscribe ( ) ;
51
61
52
- state . nextSlice ( { num : 42 } ) ;
53
- expectObservable ( state . signal$ . pipe ( map ( ( s ) => s . num ) ) ) . toBe ( '' ) ;
62
+ state . nextSlice ( { num : 42 } ) ;
63
+ expectObservable ( state . signal$ . pipe ( map ( ( s ) => s . num ) ) ) . toBe ( '' ) ;
64
+ } ) ;
54
65
} ) ;
55
66
} ) ;
56
67
57
68
it ( 'should return changes after subscription' , ( ) => {
58
- const state = setupAccumulationObservable < PrimitiveState > ( { } ) ;
59
- state . subscribe ( ) ;
60
- state . nextSlice ( { num : 42 } ) ;
61
- const slice$ = state . signal$ . pipe ( select ( 'num' ) ) ;
69
+ testBed . runInInjectionContext ( ( ) => {
70
+ const state = setupAccumulationObservable < PrimitiveState > ( { } ) ;
71
+ state . subscribe ( ) ;
72
+ state . nextSlice ( { num : 42 } ) ;
73
+ const slice$ = state . signal$ . pipe ( select ( 'num' ) ) ;
62
74
63
- let i = - 1 ;
64
- const valuesInOrder = [ '' , { num : 777 } ] ;
65
- slice$ . subscribe ( ( next ) => expect ( next ) . toBe ( valuesInOrder [ ++ i ] ) ) ;
66
- state . nextSlice ( { num : 777 } ) ;
75
+ let i = - 1 ;
76
+ const valuesInOrder = [ '' , { num : 777 } ] ;
77
+ slice$ . subscribe ( ( next ) => expect ( next ) . toBe ( valuesInOrder [ ++ i ] ) ) ;
78
+ state . nextSlice ( { num : 777 } ) ;
79
+ } ) ;
67
80
} ) ;
68
81
69
82
it ( 'should log error if getting error' , ( ) => {
70
- const spy = jest . spyOn ( console , 'error' ) . mockImplementation ( ) ;
71
- const state = setupAccumulationObservable < PrimitiveState > ( { } ) ;
72
- state . nextSliceObservable ( throwError ( 'test' ) as any ) ;
73
- state . subscribe ( ) ;
83
+ testBed . runInInjectionContext ( ( ) => {
84
+ const spy = jest . spyOn ( console , 'error' ) . mockImplementation ( ) ;
85
+ const state = setupAccumulationObservable < PrimitiveState > ( { } ) ;
86
+ state . nextSliceObservable ( throwError ( 'test' ) as any ) ;
87
+ state . subscribe ( ) ;
74
88
75
- expect ( spy ) . toBeCalled ( ) ;
89
+ expect ( spy ) . toBeCalled ( ) ;
90
+ } ) ;
76
91
} ) ;
77
92
} ) ;
78
93
79
94
describe ( 'state$' , ( ) => {
80
95
it ( 'should return nothing without subscriber' , ( ) => {
81
96
testScheduler . run ( ( { expectObservable } ) => {
82
- const acc = setupAccumulationObservable < PrimitiveState > ( {
83
- initialState : initialPrimitiveState ,
84
- initialize : false ,
97
+ testBed . runInInjectionContext ( ( ) => {
98
+ const acc = setupAccumulationObservable < PrimitiveState > ( {
99
+ initialState : initialPrimitiveState ,
100
+ initialize : false ,
101
+ } ) ;
102
+ expectObservable ( acc . state$ ) . toBe ( '' ) ;
85
103
} ) ;
86
- expectObservable ( acc . state$ ) . toBe ( '' ) ;
87
104
} ) ;
88
105
} ) ;
89
106
90
107
it ( 'should return nothing after init' , ( ) => {
91
108
testScheduler . run ( ( { expectObservable } ) => {
92
- const acc = setupAccumulationObservable < PrimitiveState > ( {
93
- initialize : true ,
109
+ testBed . runInInjectionContext ( ( ) => {
110
+ const acc = setupAccumulationObservable < PrimitiveState > ( {
111
+ initialize : true ,
112
+ } ) ;
113
+ expectObservable ( acc . state$ ) . toBe ( '' ) ;
94
114
} ) ;
95
- expectObservable ( acc . state$ ) . toBe ( '' ) ;
96
115
} ) ;
97
116
} ) ;
98
117
99
118
it ( 'should return initial base-state' , ( ) => {
100
119
testScheduler . run ( ( { expectObservable } ) => {
101
- const acc = setupAccumulationObservable < PrimitiveState > ( {
102
- initialState : initialPrimitiveState ,
120
+ testBed . runInInjectionContext ( ( ) => {
121
+ const acc = setupAccumulationObservable < PrimitiveState > ( {
122
+ initialState : initialPrimitiveState ,
123
+ } ) ;
124
+ expectObservable ( acc . state$ ) . toBe ( 's' , { s : initialPrimitiveState } ) ;
103
125
} ) ;
104
- expectObservable ( acc . state$ ) . toBe ( 's' , { s : initialPrimitiveState } ) ;
105
126
} ) ;
106
127
} ) ;
107
128
} ) ;
108
129
109
130
describe ( 'state' , ( ) => {
110
131
it ( 'should return {} without subscriber' , ( ) => {
111
- const acc = setupAccumulationObservable < PrimitiveState > ( {
112
- initialize : false ,
132
+ testBed . runInInjectionContext ( ( ) => {
133
+ const acc = setupAccumulationObservable < PrimitiveState > ( {
134
+ initialize : false ,
135
+ } ) ;
136
+ expect ( acc . state ) . toStrictEqual ( { } ) ;
113
137
} ) ;
114
- expect ( acc . state ) . toStrictEqual ( { } ) ;
115
138
} ) ;
116
139
117
140
it ( 'should return {} with subscriber' , ( ) => {
118
- const acc = setupAccumulationObservable < PrimitiveState > ( {
119
- initialize : true ,
141
+ testBed . runInInjectionContext ( ( ) => {
142
+ const acc = setupAccumulationObservable < PrimitiveState > ( {
143
+ initialize : true ,
144
+ } ) ;
145
+ expect ( acc . state ) . toStrictEqual ( { } ) ;
120
146
} ) ;
121
- expect ( acc . state ) . toStrictEqual ( { } ) ;
122
147
} ) ;
123
148
124
149
it ( 'should return {} after init' , ( ) => {
125
- const acc = setupAccumulationObservable < PrimitiveState > ( {
126
- initialize : true ,
150
+ testBed . runInInjectionContext ( ( ) => {
151
+ const acc = setupAccumulationObservable < PrimitiveState > ( {
152
+ initialize : true ,
153
+ } ) ;
154
+ expect ( acc . state ) . toStrictEqual ( { } ) ;
127
155
} ) ;
128
- expect ( acc . state ) . toStrictEqual ( { } ) ;
129
156
} ) ;
130
157
131
158
it ( 'should return initial base-state' , ( ) => {
132
- const acc = setupAccumulationObservable < PrimitiveState > ( {
133
- initialState : initialPrimitiveState ,
159
+ testBed . runInInjectionContext ( ( ) => {
160
+ const acc = setupAccumulationObservable < PrimitiveState > ( {
161
+ initialState : initialPrimitiveState ,
162
+ } ) ;
163
+ expect ( acc . state ) . toEqual ( initialPrimitiveState ) ;
134
164
} ) ;
135
- expect ( acc . state ) . toEqual ( initialPrimitiveState ) ;
136
165
} ) ;
137
166
} ) ;
138
167
139
168
describe ( 'nextSlice' , ( ) => {
140
169
it ( 'should add new base-state by partial' , ( ) => {
141
170
testScheduler . run ( ( { expectObservable } ) => {
142
- const acc = setupAccumulationObservable < PrimitiveState > ( { } ) ;
143
- acc . nextSlice ( { num : 42 } ) ;
144
- expectObservable ( acc . state$ . pipe ( map ( ( s ) => s . num ) ) ) . toBe ( 's' , {
145
- s : 42 ,
171
+ testBed . runInInjectionContext ( ( ) => {
172
+ const acc = setupAccumulationObservable < PrimitiveState > ( { } ) ;
173
+ acc . nextSlice ( { num : 42 } ) ;
174
+ expectObservable ( acc . state$ . pipe ( map ( ( s ) => s . num ) ) ) . toBe ( 's' , {
175
+ s : 42 ,
176
+ } ) ;
146
177
} ) ;
147
178
} ) ;
148
179
} ) ;
149
180
150
181
it ( 'should override previous base-state by partial' , ( ) => {
151
- const acc = setupAccumulationObservable < PrimitiveState > ( {
152
- initialState : initialPrimitiveState ,
182
+ testBed . runInInjectionContext ( ( ) => {
183
+ const acc = setupAccumulationObservable < PrimitiveState > ( {
184
+ initialState : initialPrimitiveState ,
185
+ } ) ;
186
+ acc . state$
187
+ . pipe ( map ( ( s ) => s . num ) )
188
+ . subscribe ( ( res ) => expect ( res ) . toBe ( { s : 42 } ) ) ;
189
+ acc . nextSlice ( { num : 43 } ) ;
190
+ acc . state$
191
+ . pipe ( map ( ( s ) => s . num ) )
192
+ . subscribe ( ( res ) => expect ( res ) . toBe ( { s : 43 } ) ) ;
153
193
} ) ;
154
- acc . state$
155
- . pipe ( map ( ( s ) => s . num ) )
156
- . subscribe ( ( res ) => expect ( res ) . toBe ( { s : 42 } ) ) ;
157
- acc . nextSlice ( { num : 43 } ) ;
158
- acc . state$
159
- . pipe ( map ( ( s ) => s . num ) )
160
- . subscribe ( ( res ) => expect ( res ) . toBe ( { s : 43 } ) ) ;
161
194
} ) ;
162
195
} ) ;
163
196
164
197
describe ( 'connectState' , ( ) => {
165
198
it ( 'should add new slices' , ( ) => {
166
199
testScheduler . run ( ( { expectObservable } ) => {
167
- const acc = setupAccumulationObservable < PrimitiveState > ( { } ) ;
168
- acc . nextSliceObservable ( of ( { num : 42 } ) ) ;
169
- expectObservable ( acc . state$ . pipe ( map ( ( s ) => s . num ) ) ) . toBe ( 's' , {
170
- s : 42 ,
200
+ testBed . runInInjectionContext ( ( ) => {
201
+ const acc = setupAccumulationObservable < PrimitiveState > ( { } ) ;
202
+ acc . nextSliceObservable ( of ( { num : 42 } ) ) ;
203
+ expectObservable ( acc . state$ . pipe ( map ( ( s ) => s . num ) ) ) . toBe ( 's' , {
204
+ s : 42 ,
205
+ } ) ;
171
206
} ) ;
172
207
} ) ;
173
208
} ) ;
174
209
175
210
it ( 'should override previous base-state slices' , ( ) => {
176
- const acc = setupAccumulationObservable < PrimitiveState > ( {
177
- initialState : initialPrimitiveState ,
211
+ testBed . runInInjectionContext ( ( ) => {
212
+ const acc = setupAccumulationObservable < PrimitiveState > ( {
213
+ initialState : initialPrimitiveState ,
214
+ } ) ;
215
+ acc . state$
216
+ . pipe ( map ( ( s ) => s . num ) )
217
+ . subscribe ( ( res ) => expect ( res ) . toBe ( { s : 42 } ) ) ;
218
+ acc . nextSliceObservable ( of ( { num : 43 } ) ) ;
219
+ acc . state$
220
+ . pipe ( map ( ( s ) => s . num ) )
221
+ . subscribe ( ( res ) => expect ( res ) . toBe ( { s : 42 } ) ) ;
178
222
} ) ;
179
- acc . state$
180
- . pipe ( map ( ( s ) => s . num ) )
181
- . subscribe ( ( res ) => expect ( res ) . toBe ( { s : 42 } ) ) ;
182
- acc . nextSliceObservable ( of ( { num : 43 } ) ) ;
183
- acc . state$
184
- . pipe ( map ( ( s ) => s . num ) )
185
- . subscribe ( ( res ) => expect ( res ) . toBe ( { s : 42 } ) ) ;
186
223
} ) ;
187
224
} ) ;
188
225
@@ -196,19 +233,21 @@ describe('createAccumulationObservable', () => {
196
233
...sl ,
197
234
} ;
198
235
} ;
199
- const acc = setupAccumulationObservable < PrimitiveState > ( { } ) ;
200
- testScheduler . run ( ( { expectObservable } ) => {
201
- acc . nextSlice ( { num : 42 } ) ;
202
- expectObservable ( acc . state$ . pipe ( map ( ( s ) => s . num ) ) ) . toBe ( '(a)' , {
203
- a : 44 ,
236
+ testBed . runInInjectionContext ( ( ) => {
237
+ const acc = setupAccumulationObservable < PrimitiveState > ( { } ) ;
238
+ testScheduler . run ( ( { expectObservable } ) => {
239
+ acc . nextSlice ( { num : 42 } ) ;
240
+ expectObservable ( acc . state$ . pipe ( map ( ( s ) => s . num ) ) ) . toBe ( '(a)' , {
241
+ a : 44 ,
242
+ } ) ;
243
+
244
+ acc . nextAccumulator ( customAcc ) ;
245
+ acc . nextSlice ( { num : 43 } ) ;
246
+ acc . nextSlice ( { num : 44 } ) ;
204
247
} ) ;
205
248
206
- acc . nextAccumulator ( customAcc ) ;
207
- acc . nextSlice ( { num : 43 } ) ;
208
- acc . nextSlice ( { num : 44 } ) ;
249
+ expect ( numAccCalls ) . toBe ( 2 ) ;
209
250
} ) ;
210
-
211
- expect ( numAccCalls ) . toBe ( 2 ) ;
212
251
} ) ;
213
252
} ) ;
214
253
@@ -217,25 +256,29 @@ describe('createAccumulationObservable', () => {
217
256
// the latest value emitted
218
257
xit ( 'should stop on unsubscribe from state' , ( ) => {
219
258
testScheduler . run ( ( { expectObservable } ) => {
220
- const acc = createAccumulationObservable < PrimitiveState > ( ) ;
221
- const sub = acc . subscribe ( ) ;
222
- acc . nextSlice ( initialPrimitiveState ) ;
223
- sub . unsubscribe ( ) ;
224
- expectObservable ( acc . state$ ) . toBe ( '' ) ;
259
+ testBed . runInInjectionContext ( ( ) => {
260
+ const acc = createAccumulationObservable < PrimitiveState > ( ) ;
261
+ const sub = acc . subscribe ( ) ;
262
+ acc . nextSlice ( initialPrimitiveState ) ;
263
+ sub . unsubscribe ( ) ;
264
+ expectObservable ( acc . state$ ) . toBe ( '' ) ;
265
+ } ) ;
225
266
} ) ;
226
267
} ) ;
227
268
228
269
it ( 'should stop from connect observable' , ( ) => {
229
270
testScheduler . run ( ( { expectObservable, hot, expectSubscriptions } ) => {
230
- const acc = createAccumulationObservable < PrimitiveState > ( ) ;
231
- const sub = acc . subscribe ( ) ;
232
- const tick$ = hot ( 'aaaaaaaaaaaaaaa|' , { a : 1 } ) ;
233
- const interval$ = tick$ . pipe ( map ( ( num ) => ( { num } ) ) ) ;
234
- const subs = '(^!)' ;
235
- acc . nextSliceObservable ( interval$ ) ;
236
- sub . unsubscribe ( ) ;
237
- expectObservable ( acc . state$ ) . toBe ( '' ) ;
238
- expectSubscriptions ( tick$ . subscriptions ) . toBe ( subs ) ;
271
+ testBed . runInInjectionContext ( ( ) => {
272
+ const acc = createAccumulationObservable < PrimitiveState > ( ) ;
273
+ const sub = acc . subscribe ( ) ;
274
+ const tick$ = hot ( 'aaaaaaaaaaaaaaa|' , { a : 1 } ) ;
275
+ const interval$ = tick$ . pipe ( map ( ( num ) => ( { num } ) ) ) ;
276
+ const subs = '(^!)' ;
277
+ acc . nextSliceObservable ( interval$ ) ;
278
+ sub . unsubscribe ( ) ;
279
+ expectObservable ( acc . state$ ) . toBe ( '' ) ;
280
+ expectSubscriptions ( tick$ . subscriptions ) . toBe ( subs ) ;
281
+ } ) ;
239
282
} ) ;
240
283
} ) ;
241
284
} ) ;
0 commit comments