@@ -13,8 +13,11 @@ import { TestScheduler } from 'rxjs/testing';
13
13
import { createStateChecker } from './fixtures' ;
14
14
15
15
function setupState < T extends object > ( cfg : { initialState ?: T } ) {
16
+ const testBed = TestBed . configureTestingModule ( {
17
+ providers : [ RxState ] ,
18
+ } ) ;
19
+ const state = testBed . inject ( RxState < T > ) ;
16
20
const { initialState } = { ...cfg } ;
17
- const state = new RxState < T > ( ) ;
18
21
if ( initialState ) {
19
22
state . set ( initialState ) ;
20
23
}
@@ -36,56 +39,56 @@ beforeEach(() => {
36
39
} ) ;
37
40
38
41
describe ( 'RxStateService' , ( ) => {
39
- let service : RxState < PrimitiveState > ;
40
-
41
- beforeEach ( ( ) => {
42
- TestBed . configureTestingModule ( {
43
- teardown : { destroyAfterEach : true } ,
44
- } ) ;
45
- service = setupState ( { } ) ;
46
- } ) ;
42
+ let state : RxState < PrimitiveState > ;
47
43
48
44
it ( 'should be created' , ( ) => {
49
- expect ( service ) . toBeTruthy ( ) ;
45
+ state = setupState ( { } ) ;
46
+ expect ( state ) . toBeTruthy ( ) ;
50
47
} ) ;
51
48
52
49
it ( 'should be hot on instantiation' , ( ) => {
53
- stateChecker . checkSubscriptions ( service , 1 ) ;
50
+ state = setupState ( { } ) ;
51
+ stateChecker . checkSubscriptions ( state , 1 ) ;
54
52
} ) ;
55
53
56
54
it ( 'should unsubscribe on ngOnDestroy call' , ( ) => {
57
- stateChecker . checkSubscriptions ( service , 1 ) ;
58
- service . ngOnDestroy ( ) ;
59
- stateChecker . checkSubscriptions ( service , 0 ) ;
55
+ state = setupState ( { } ) ;
56
+
57
+ stateChecker . checkSubscriptions ( state , 1 ) ;
58
+ state . ngOnDestroy ( ) ;
59
+ stateChecker . checkSubscriptions ( state , 0 ) ;
60
60
} ) ;
61
61
62
62
describe ( 'State' , ( ) => {
63
+ beforeEach ( ( ) => {
64
+ state = setupState ( { } ) ;
65
+ } ) ;
66
+
63
67
it ( 'should create new instance' , ( ) => {
64
- const state = new RxState < PrimitiveState > ( ) ;
65
- expect ( state ) . toBeDefined ( ) ;
68
+ expect ( state ) . toBeInstanceOf ( RxState ) ;
66
69
} ) ;
67
70
} ) ;
68
71
69
72
describe ( '$' , ( ) => {
73
+ beforeEach ( ( ) => {
74
+ state = setupState ( { } ) ;
75
+ } ) ;
76
+
70
77
it ( 'should return NO empty state after init when subscribing late' , ( ) => {
71
78
testScheduler . run ( ( { expectObservable } ) => {
72
- const state = setupState ( { } ) ;
73
79
expectObservable ( state . $ ) . toBe ( '' ) ;
74
80
} ) ;
75
81
} ) ;
76
82
77
83
it ( 'should return No changes when subscribing late' , ( ) => {
78
84
testScheduler . run ( ( { expectObservable } ) => {
79
- const state = new RxState < PrimitiveState > ( ) ;
80
85
state . subscribe ( ) ;
81
-
82
86
state . set ( { num : 42 } ) ;
83
87
expectObservable ( state . $ . pipe ( pluck ( 'num' ) ) ) . toBe ( '' ) ;
84
88
} ) ;
85
89
} ) ;
86
90
87
91
it ( 'should return new changes' , ( ) => {
88
- const state = new RxState < PrimitiveState > ( ) ;
89
92
state . subscribe ( ) ;
90
93
state . set ( { num : 42 } ) ;
91
94
const slice$ = state . $ . pipe ( select ( 'num' ) ) ;
@@ -97,26 +100,26 @@ describe('RxStateService', () => {
97
100
} ) ;
98
101
99
102
describe ( 'stateful with select' , ( ) => {
103
+ beforeEach ( ( ) => {
104
+ state = setupState ( { } ) ;
105
+ } ) ;
106
+
100
107
it ( 'should return empty state after init when subscribing late' , ( ) => {
101
108
testScheduler . run ( ( { expectObservable } ) => {
102
- const state = setupState ( { } ) ;
103
109
expectObservable ( state . select ( ) ) . toBe ( '' ) ;
104
110
} ) ;
105
111
} ) ;
106
112
107
113
it ( 'should return changes when subscribing late' , ( ) => {
108
114
testScheduler . run ( ( { expectObservable } ) => {
109
- const state = new RxState < PrimitiveState > ( ) ;
110
115
state . subscribe ( ) ;
111
-
112
116
state . set ( { num : 42 } ) ;
113
117
expectObservable ( state . select ( 'num' ) ) . toBe ( 'n' , { n : 42 } ) ;
114
118
} ) ;
115
119
} ) ;
116
120
117
121
it ( 'should return new changes' , ( ) => {
118
- testScheduler . run ( ( { expectObservable } ) => {
119
- const state = new RxState < PrimitiveState > ( ) ;
122
+ testScheduler . run ( ( ) => {
120
123
state . subscribe ( ) ;
121
124
state . set ( { num : 42 } ) ;
122
125
const slice$ = state . select ( 'num' ) ;
@@ -189,7 +192,7 @@ describe('RxStateService', () => {
189
192
190
193
it ( 'should return initial state' , ( ) => {
191
194
testScheduler . run ( ( { expectObservable } ) => {
192
- const state = new RxState < PrimitiveState > ( ) ;
195
+ const state = setupState ( { } ) ;
193
196
state . subscribe ( ) ;
194
197
195
198
state . set ( { num : 42 } ) ;
@@ -282,6 +285,7 @@ describe('RxStateService', () => {
282
285
state . set ( initialPrimitiveState ) ;
283
286
state . select ( ) . subscribe ( ( s ) => expect ( s ) . toBe ( initialPrimitiveState ) ) ;
284
287
} ) ;
288
+
285
289
it ( 'should override previous state slices' , ( ) => {
286
290
const state = setupState ( { initialState : initialPrimitiveState } ) ;
287
291
state . select ( ) . subscribe ( ( s ) => {
@@ -301,6 +305,7 @@ describe('RxStateService', () => {
301
305
) . toThrowError ( 'wrong param' ) ;
302
306
} ) ;
303
307
} ) ;
308
+
304
309
describe ( 'with state project partial' , ( ) => {
305
310
it ( 'should add new slices' , ( ) => {
306
311
const state = setupState ( { } ) ;
@@ -319,6 +324,7 @@ describe('RxStateService', () => {
319
324
state . select ( ) . subscribe ( ( s ) => expect ( state ) . toBe ( { num : 43 } ) ) ;
320
325
} ) ;
321
326
} ) ;
327
+
322
328
describe ( 'with state key and value partial' , ( ) => {
323
329
it ( 'should add new slices' , ( ) => {
324
330
const state = setupState < PrimitiveState > ( { } ) ;
0 commit comments