@@ -42,7 +42,42 @@ describe('toDeepSignal', () => {
42
42
expect ( deepSig . user . firstName ( ) ) . toBe ( 'John' ) ;
43
43
} ) ;
44
44
45
- it ( 'does not create deep signals for primitives' , ( ) => {
45
+ it ( 'allows lazy initialization' , ( ) => {
46
+ const sig = signal ( undefined as unknown as { m : { s : 't' } } ) ;
47
+ const deepSig = toDeepSignal ( sig ) ;
48
+
49
+ sig . set ( { m : { s : 't' } } ) ;
50
+
51
+ expect ( deepSig ( ) ) . toEqual ( { m : { s : 't' } } ) ;
52
+ expect ( deepSig . m ( ) ) . toEqual ( { s : 't' } ) ;
53
+ expect ( deepSig . m . s ( ) ) . toBe ( 't' ) ;
54
+ } ) ;
55
+
56
+ it ( 'creates a deep signal when value is a union of objects' , ( ) => {
57
+ const sig = signal ( { m : { s : 't' } } as
58
+ | { s : 'asdf' }
59
+ | { m : { s : string } } ) ;
60
+ const deepSig = toDeepSignal ( sig ) ;
61
+
62
+ expect ( 'm' in deepSig ) . toBe ( true ) ;
63
+ expect ( 'm' in deepSig && deepSig . m ( ) ) . toEqual ( { s : 't' } ) ;
64
+ expect ( 'm' in deepSig && deepSig . m . s ( ) ) . toBe ( 't' ) ;
65
+
66
+ sig . set ( { s : 'asdf' } ) ;
67
+
68
+ expect ( 'm' in deepSig ) . toBe ( false ) ;
69
+ expect ( 's' in deepSig ) . toBe ( true ) ;
70
+ expect ( 's' in deepSig && deepSig . s ( ) ) . toBe ( 'asdf' ) ;
71
+
72
+ sig . set ( { m : { s : 'ngrx' } } ) ;
73
+
74
+ expect ( 's' in deepSig ) . toBe ( false ) ;
75
+ expect ( 'm' in deepSig ) . toBe ( true ) ;
76
+ expect ( 'm' in deepSig && deepSig . m ( ) ) . toEqual ( { s : 'ngrx' } ) ;
77
+ expect ( 'm' in deepSig && deepSig . m . s ( ) ) . toBe ( 'ngrx' ) ;
78
+ } ) ;
79
+
80
+ it ( 'does not affect signals with primitives as values' , ( ) => {
46
81
const num = signal ( 0 ) ;
47
82
const str = signal ( 'str' ) ;
48
83
const bool = signal ( true ) ;
@@ -51,12 +86,17 @@ describe('toDeepSignal', () => {
51
86
const deepStr = toDeepSignal ( str ) ;
52
87
const deepBool = toDeepSignal ( bool ) ;
53
88
54
- expect ( deepNum ) . toBe ( num ) ;
55
- expect ( deepStr ) . toBe ( str ) ;
56
- expect ( deepBool ) . toBe ( bool ) ;
89
+ expect ( isSignal ( deepNum ) ) . toBe ( true ) ;
90
+ expect ( deepNum ( ) ) . toBe ( num ( ) ) ;
91
+
92
+ expect ( isSignal ( deepStr ) ) . toBe ( true ) ;
93
+ expect ( deepStr ( ) ) . toBe ( str ( ) ) ;
94
+
95
+ expect ( isSignal ( deepBool ) ) . toBe ( true ) ;
96
+ expect ( deepBool ( ) ) . toBe ( bool ( ) ) ;
57
97
} ) ;
58
98
59
- it ( 'does not create deep signals for iterables' , ( ) => {
99
+ it ( 'does not affect signals with iterables as values ' , ( ) => {
60
100
const array = signal ( [ ] ) ;
61
101
const set = signal ( new Set ( ) ) ;
62
102
const map = signal ( new Map ( ) ) ;
@@ -69,14 +109,23 @@ describe('toDeepSignal', () => {
69
109
const deepUintArray = toDeepSignal ( uintArray ) ;
70
110
const deepFloatArray = toDeepSignal ( floatArray ) ;
71
111
72
- expect ( deepArray ) . toBe ( array ) ;
73
- expect ( deepSet ) . toBe ( set ) ;
74
- expect ( deepMap ) . toBe ( map ) ;
75
- expect ( deepUintArray ) . toBe ( uintArray ) ;
76
- expect ( deepFloatArray ) . toBe ( floatArray ) ;
112
+ expect ( isSignal ( deepArray ) ) . toBe ( true ) ;
113
+ expect ( deepArray ( ) ) . toBe ( array ( ) ) ;
114
+
115
+ expect ( isSignal ( deepSet ) ) . toBe ( true ) ;
116
+ expect ( deepSet ( ) ) . toBe ( set ( ) ) ;
117
+
118
+ expect ( isSignal ( deepMap ) ) . toBe ( true ) ;
119
+ expect ( deepMap ( ) ) . toBe ( map ( ) ) ;
120
+
121
+ expect ( isSignal ( deepUintArray ) ) . toBe ( true ) ;
122
+ expect ( deepUintArray ( ) ) . toBe ( uintArray ( ) ) ;
123
+
124
+ expect ( isSignal ( deepFloatArray ) ) . toBe ( true ) ;
125
+ expect ( deepFloatArray ( ) ) . toBe ( floatArray ( ) ) ;
77
126
} ) ;
78
127
79
- it ( 'does not create deep signals for built-in object types' , ( ) => {
128
+ it ( 'does not affect signals with built-in object types as values ' , ( ) => {
80
129
const weakSet = signal ( new WeakSet ( ) ) ;
81
130
const weakMap = signal ( new WeakMap ( ) ) ;
82
131
const promise = signal ( Promise . resolve ( 10 ) ) ;
@@ -95,17 +144,32 @@ describe('toDeepSignal', () => {
95
144
const deepArrayBuffer = toDeepSignal ( arrayBuffer ) ;
96
145
const deepDataView = toDeepSignal ( dataView ) ;
97
146
98
- expect ( deepWeakSet ) . toBe ( weakSet ) ;
99
- expect ( deepWeakMap ) . toBe ( weakMap ) ;
100
- expect ( deepPromise ) . toBe ( promise ) ;
101
- expect ( deepDate ) . toBe ( date ) ;
102
- expect ( deepError ) . toBe ( error ) ;
103
- expect ( deepRegExp ) . toBe ( regExp ) ;
104
- expect ( deepArrayBuffer ) . toBe ( arrayBuffer ) ;
105
- expect ( deepDataView ) . toBe ( dataView ) ;
147
+ expect ( isSignal ( deepWeakSet ) ) . toBe ( true ) ;
148
+ expect ( deepWeakSet ( ) ) . toBe ( weakSet ( ) ) ;
149
+
150
+ expect ( isSignal ( deepWeakMap ) ) . toBe ( true ) ;
151
+ expect ( deepWeakMap ( ) ) . toBe ( weakMap ( ) ) ;
152
+
153
+ expect ( isSignal ( deepPromise ) ) . toBe ( true ) ;
154
+ expect ( deepPromise ( ) ) . toBe ( promise ( ) ) ;
155
+
156
+ expect ( isSignal ( deepDate ) ) . toBe ( true ) ;
157
+ expect ( deepDate ( ) ) . toBe ( date ( ) ) ;
158
+
159
+ expect ( isSignal ( deepError ) ) . toBe ( true ) ;
160
+ expect ( deepError ( ) ) . toBe ( error ( ) ) ;
161
+
162
+ expect ( isSignal ( deepRegExp ) ) . toBe ( true ) ;
163
+ expect ( deepRegExp ( ) ) . toBe ( regExp ( ) ) ;
164
+
165
+ expect ( isSignal ( deepArrayBuffer ) ) . toBe ( true ) ;
166
+ expect ( deepArrayBuffer ( ) ) . toBe ( arrayBuffer ( ) ) ;
167
+
168
+ expect ( isSignal ( deepDataView ) ) . toBe ( true ) ;
169
+ expect ( deepDataView ( ) ) . toBe ( dataView ( ) ) ;
106
170
} ) ;
107
171
108
- it ( 'does not create deep signals for functions' , ( ) => {
172
+ it ( 'does not affect signals with functions as values ' , ( ) => {
109
173
const fn1 = signal ( new Function ( ) ) ;
110
174
const fn2 = signal ( function ( ) { } ) ;
111
175
const fn3 = signal ( ( ) => { } ) ;
@@ -114,12 +178,17 @@ describe('toDeepSignal', () => {
114
178
const deepFn2 = toDeepSignal ( fn2 ) ;
115
179
const deepFn3 = toDeepSignal ( fn3 ) ;
116
180
117
- expect ( deepFn1 ) . toBe ( fn1 ) ;
118
- expect ( deepFn2 ) . toBe ( fn2 ) ;
119
- expect ( deepFn3 ) . toBe ( fn3 ) ;
181
+ expect ( isSignal ( deepFn1 ) ) . toBe ( true ) ;
182
+ expect ( deepFn1 ( ) ) . toBe ( fn1 ( ) ) ;
183
+
184
+ expect ( isSignal ( deepFn2 ) ) . toBe ( true ) ;
185
+ expect ( deepFn2 ( ) ) . toBe ( fn2 ( ) ) ;
186
+
187
+ expect ( isSignal ( deepFn3 ) ) . toBe ( true ) ;
188
+ expect ( deepFn3 ( ) ) . toBe ( fn3 ( ) ) ;
120
189
} ) ;
121
190
122
- it ( 'does not create deep signals for custom class instances that are iterables' , ( ) => {
191
+ it ( 'does not affect signals with custom class instances that are iterables as values ' , ( ) => {
123
192
class CustomArray extends Array { }
124
193
125
194
class CustomSet extends Set { }
@@ -134,12 +203,17 @@ describe('toDeepSignal', () => {
134
203
const deepFloatArray = toDeepSignal ( floatArray ) ;
135
204
const deepSet = toDeepSignal ( set ) ;
136
205
137
- expect ( deepArray ) . toBe ( array ) ;
138
- expect ( deepFloatArray ) . toBe ( floatArray ) ;
139
- expect ( deepSet ) . toBe ( set ) ;
206
+ expect ( isSignal ( deepArray ) ) . toBe ( true ) ;
207
+ expect ( deepArray ( ) ) . toBe ( array ( ) ) ;
208
+
209
+ expect ( isSignal ( deepFloatArray ) ) . toBe ( true ) ;
210
+ expect ( deepFloatArray ( ) ) . toBe ( floatArray ( ) ) ;
211
+
212
+ expect ( isSignal ( deepSet ) ) . toBe ( true ) ;
213
+ expect ( deepSet ( ) ) . toBe ( set ( ) ) ;
140
214
} ) ;
141
215
142
- it ( 'does not create deep signals for custom class instances that extend built-in object types' , ( ) => {
216
+ it ( 'does not affect signals with custom class instances that extend built-in object types as values ' , ( ) => {
143
217
class CustomWeakMap extends WeakMap { }
144
218
145
219
class CustomError extends Error { }
@@ -154,8 +228,13 @@ describe('toDeepSignal', () => {
154
228
const deepError = toDeepSignal ( error ) ;
155
229
const deepArrayBuffer = toDeepSignal ( arrayBuffer ) ;
156
230
157
- expect ( deepWeakMap ) . toBe ( weakMap ) ;
158
- expect ( deepError ) . toBe ( error ) ;
159
- expect ( deepArrayBuffer ) . toBe ( arrayBuffer ) ;
231
+ expect ( isSignal ( deepWeakMap ) ) . toBe ( true ) ;
232
+ expect ( deepWeakMap ( ) ) . toBe ( weakMap ( ) ) ;
233
+
234
+ expect ( isSignal ( deepError ) ) . toBe ( true ) ;
235
+ expect ( deepError ( ) ) . toBe ( error ( ) ) ;
236
+
237
+ expect ( isSignal ( deepArrayBuffer ) ) . toBe ( true ) ;
238
+ expect ( deepArrayBuffer ( ) ) . toBe ( arrayBuffer ( ) ) ;
160
239
} ) ;
161
240
} ) ;
0 commit comments