@@ -16,7 +16,7 @@ import {
16
16
withState ,
17
17
} from '../src' ;
18
18
import { STATE_SOURCE } from '../src/state-source' ;
19
- import { createLocalService } from './helpers' ;
19
+ import { assertStateSource , createLocalService } from './helpers' ;
20
20
21
21
describe ( 'signalStore' , ( ) => {
22
22
describe ( 'creation' , ( ) => {
@@ -47,42 +47,56 @@ describe('signalStore', () => {
47
47
expect ( store1 . foo ( ) ) . toBe ( 'bar' ) ;
48
48
} ) ;
49
49
50
- it ( 'creates a store with readonly state source by default' , ( ) => {
50
+ it ( 'creates a store with state source as Record holding slices as signals by default' , ( ) => {
51
51
const Store = signalStore ( withState ( { foo : 'bar' } ) ) ;
52
52
const store = new Store ( ) ;
53
53
const stateSource = store [ STATE_SOURCE ] ;
54
54
55
- expect ( isSignal ( stateSource ) ) . toBe ( true ) ;
56
- expect ( stateSource ( ) ) . toEqual ( { foo : 'bar' } ) ;
55
+ expect ( isSignal ( stateSource ) ) . toBe ( false ) ;
56
+ expect ( Object . keys ( stateSource ) ) . toEqual ( [ 'foo' ] ) ;
57
+ expect ( isSignal ( stateSource . foo ) ) . toBe ( true ) ;
58
+ assertStateSource ( stateSource , {
59
+ foo : signal ( 'bar' ) ,
60
+ } ) ;
57
61
} ) ;
58
62
59
- it ( 'creates a store with readonly state source when protectedState option is true' , ( ) => {
63
+ it ( 'creates a store with state source as Record holding slices as signals when protectedState option is true' , ( ) => {
60
64
const Store = signalStore (
61
65
{ protectedState : true } ,
62
66
withState ( { foo : 'bar' } )
63
67
) ;
64
68
const store = new Store ( ) ;
65
69
const stateSource = store [ STATE_SOURCE ] ;
66
70
67
- expect ( isSignal ( stateSource ) ) . toBe ( true ) ;
68
- expect ( stateSource ( ) ) . toEqual ( { foo : 'bar' } ) ;
71
+ expect ( isSignal ( stateSource ) ) . toBe ( false ) ;
72
+ expect ( Object . keys ( stateSource ) ) . toEqual ( [ 'foo' ] ) ;
73
+ expect ( isSignal ( stateSource . foo ) ) . toBe ( true ) ;
74
+ assertStateSource ( stateSource , {
75
+ foo : signal ( 'bar' ) ,
76
+ } ) ;
69
77
} ) ;
70
78
71
- it ( 'creates a store with writable state source when protectedState option is false' , ( ) => {
79
+ it ( 'creates a store with state source as Record holding slices as writeable signals when protectedState option is false' , ( ) => {
72
80
const Store = signalStore (
73
81
{ protectedState : false } ,
74
82
withState ( { foo : 'bar' } )
75
83
) ;
76
84
const store = new Store ( ) ;
77
85
const stateSource = store [ STATE_SOURCE ] ;
78
86
79
- expect ( isSignal ( stateSource ) ) . toBe ( true ) ;
80
- expect ( stateSource ( ) ) . toEqual ( { foo : 'bar' } ) ;
81
- expect ( typeof stateSource . update === 'function' ) . toBe ( true ) ;
87
+ expect ( isSignal ( stateSource ) ) . toBe ( false ) ;
88
+ expect ( Object . keys ( stateSource ) ) . toEqual ( [ 'foo' ] ) ;
89
+ expect ( isSignal ( stateSource . foo ) ) . toBe ( true ) ;
90
+ assertStateSource ( stateSource , {
91
+ foo : signal ( 'bar' ) ,
92
+ } ) ;
93
+ expect ( typeof stateSource . foo . update === 'function' ) . toBe ( true ) ;
82
94
83
95
patchState ( store , { foo : 'baz' } ) ;
84
96
85
- expect ( stateSource ( ) ) . toEqual ( { foo : 'baz' } ) ;
97
+ assertStateSource ( stateSource , {
98
+ foo : signal ( 'baz' ) ,
99
+ } ) ;
86
100
} ) ;
87
101
} ) ;
88
102
@@ -97,10 +111,11 @@ describe('signalStore', () => {
97
111
98
112
const store = new Store ( ) ;
99
113
100
- expect ( store [ STATE_SOURCE ] ( ) ) . toEqual ( {
101
- foo : 'foo' ,
102
- x : { y : { z : 10 } } ,
114
+ assertStateSource ( store [ STATE_SOURCE ] , {
115
+ foo : signal ( 'foo' ) ,
116
+ x : signal ( { y : { z : 10 } } ) ,
103
117
} ) ;
118
+
104
119
expect ( store . foo ( ) ) . toBe ( 'foo' ) ;
105
120
expect ( store . x ( ) ) . toEqual ( { y : { z : 10 } } ) ;
106
121
expect ( store . x . y ( ) ) . toEqual ( { z : 10 } ) ;
@@ -178,7 +193,9 @@ describe('signalStore', () => {
178
193
179
194
const store = new Store ( ) ;
180
195
181
- expect ( store [ STATE_SOURCE ] ( ) ) . toEqual ( { foo : 'foo' } ) ;
196
+ assertStateSource ( store [ STATE_SOURCE ] , {
197
+ foo : signal ( 'foo' ) ,
198
+ } ) ;
182
199
expect ( store . foo ( ) ) . toBe ( 'foo' ) ;
183
200
expect ( store . bar ( ) ) . toBe ( 'bar' ) ;
184
201
expect ( store . num ) . toBe ( 10 ) ;
@@ -236,7 +253,9 @@ describe('signalStore', () => {
236
253
237
254
const store = new Store ( ) ;
238
255
239
- expect ( store [ STATE_SOURCE ] ( ) ) . toEqual ( { foo : 'foo' } ) ;
256
+ assertStateSource ( store [ STATE_SOURCE ] , {
257
+ foo : signal ( 'foo' ) ,
258
+ } ) ;
240
259
expect ( store . foo ( ) ) . toBe ( 'foo' ) ;
241
260
expect ( store . bar ( ) ) . toBe ( 'bar' ) ;
242
261
expect ( store . num ) . toBe ( 10 ) ;
@@ -279,7 +298,9 @@ describe('signalStore', () => {
279
298
withMethods ( ( ) => ( { baz : ( ) => 'baz' } ) ) ,
280
299
withProps ( ( ) => ( { num : 100 } ) ) ,
281
300
withMethods ( ( store ) => {
282
- expect ( store [ STATE_SOURCE ] ( ) ) . toEqual ( { foo : 'foo' } ) ;
301
+ assertStateSource ( store [ STATE_SOURCE ] , {
302
+ foo : signal ( 'foo' ) ,
303
+ } ) ;
283
304
expect ( store . foo ( ) ) . toBe ( 'foo' ) ;
284
305
expect ( store . bar ( ) ) . toBe ( 'bar' ) ;
285
306
expect ( store . baz ( ) ) . toBe ( 'baz' ) ;
@@ -291,7 +312,9 @@ describe('signalStore', () => {
291
312
292
313
const store = new Store ( ) ;
293
314
294
- expect ( store [ STATE_SOURCE ] ( ) ) . toEqual ( { foo : 'foo' } ) ;
315
+ assertStateSource ( store [ STATE_SOURCE ] , {
316
+ foo : signal ( 'foo' ) ,
317
+ } ) ;
295
318
expect ( store . foo ( ) ) . toBe ( 'foo' ) ;
296
319
expect ( store . bar ( ) ) . toBe ( 'bar' ) ;
297
320
expect ( store . baz ( ) ) . toBe ( 'baz' ) ;
@@ -372,7 +395,9 @@ describe('signalStore', () => {
372
395
withProps ( ( ) => ( { num : 10 } ) ) ,
373
396
withHooks ( {
374
397
onInit ( store ) {
375
- expect ( store [ STATE_SOURCE ] ( ) ) . toEqual ( { foo : 'foo' } ) ;
398
+ assertStateSource ( store [ STATE_SOURCE ] , {
399
+ foo : signal ( 'foo' ) ,
400
+ } ) ;
376
401
expect ( store . foo ( ) ) . toBe ( 'foo' ) ;
377
402
expect ( store . bar ( ) ) . toBe ( 'bar' ) ;
378
403
expect ( store . baz ( ) ) . toBe ( 'baz' ) ;
0 commit comments