@@ -42,36 +42,36 @@ describe('Null values', () => {
4242 } ) ;
4343
4444 test ( 'Type changes with null' , ( ) => {
45- store . setCell ( 't1' , 'r1' , 'c1' , 'hello ' ) ;
46- expect ( store . getCell ( 't1' , 'r1' , 'c1' ) ) . toBe ( 'hello ' ) ;
45+ store . setCell ( 't1' , 'r1' , 'c1' , '1 ' ) ;
46+ expect ( store . getCell ( 't1' , 'r1' , 'c1' ) ) . toBe ( '1 ' ) ;
4747
4848 store . setCell ( 't1' , 'r1' , 'c1' , null ) ;
4949 expect ( store . getCell ( 't1' , 'r1' , 'c1' ) ) . toBe ( null ) ;
5050
51- store . setCell ( 't1' , 'r1' , 'c1' , 123 ) ;
52- expect ( store . getCell ( 't1' , 'r1' , 'c1' ) ) . toBe ( 123 ) ;
51+ store . setCell ( 't1' , 'r1' , 'c1' , 2 ) ;
52+ expect ( store . getCell ( 't1' , 'r1' , 'c1' ) ) . toBe ( 2 ) ;
5353 } ) ;
5454
5555 test ( 'Null in table data' , ( ) => {
5656 store . setTables ( {
5757 t1 : {
58- r1 : { c1 : 'hello ' , c2 : null , c3 : 42 } ,
58+ r1 : { c1 : '1 ' , c2 : null , c3 : 3 } ,
5959 r2 : { c1 : null } ,
6060 } ,
6161 } ) ;
6262
63- expect ( store . getCell ( 't1' , 'r1' , 'c1' ) ) . toBe ( 'hello ' ) ;
63+ expect ( store . getCell ( 't1' , 'r1' , 'c1' ) ) . toBe ( '1 ' ) ;
6464 expect ( store . getCell ( 't1' , 'r1' , 'c2' ) ) . toBe ( null ) ;
65- expect ( store . getCell ( 't1' , 'r1' , 'c3' ) ) . toBe ( 42 ) ;
65+ expect ( store . getCell ( 't1' , 'r1' , 'c3' ) ) . toBe ( 3 ) ;
6666 expect ( store . getCell ( 't1' , 'r2' , 'c1' ) ) . toBe ( null ) ;
6767 } ) ;
6868
6969 test ( 'Null in values data' , ( ) => {
70- store . setValues ( { v1 : 'test ' , v2 : null , v3 : 100 } ) ;
70+ store . setValues ( { v1 : '1 ' , v2 : null , v3 : 3 } ) ;
7171
72- expect ( store . getValue ( 'v1' ) ) . toBe ( 'test ' ) ;
72+ expect ( store . getValue ( 'v1' ) ) . toBe ( '1 ' ) ;
7373 expect ( store . getValue ( 'v2' ) ) . toBe ( null ) ;
74- expect ( store . getValue ( 'v3' ) ) . toBe ( 100 ) ;
74+ expect ( store . getValue ( 'v3' ) ) . toBe ( 3 ) ;
7575 } ) ;
7676
7777 test ( 'JSON serialization with null' , ( ) => {
@@ -87,6 +87,30 @@ describe('Null values', () => {
8787 expect ( newStore . getCell ( 't1' , 'r1' , 'c1' ) ) . toBe ( null ) ;
8888 expect ( newStore . getValue ( 'v1' ) ) . toBe ( null ) ;
8989 } ) ;
90+
91+ test ( 'JSON round-trip with mixed types including null' , ( ) => {
92+ store . setTables ( {
93+ t1 : {
94+ r1 : { c1 : '1' , c2 : 2 , c3 : null } ,
95+ r2 : { c1 : '2' , c2 : null , c3 : false } ,
96+ } ,
97+ } ) ;
98+ store . setValues ( {
99+ v1 : null ,
100+ v2 : 3 ,
101+ v3 : true ,
102+ } ) ;
103+
104+ const json = store . getJson ( ) ;
105+ const store2 = createStore ( ) ;
106+ store2 . setJson ( json ) ;
107+
108+ expect ( store2 . getTables ( ) ) . toEqual ( store . getTables ( ) ) ;
109+ expect ( store2 . getValues ( ) ) . toEqual ( store . getValues ( ) ) ;
110+ expect ( store2 . getCell ( 't1' , 'r1' , 'c3' ) ) . toBe ( null ) ;
111+ expect ( store2 . getCell ( 't1' , 'r2' , 'c2' ) ) . toBe ( null ) ;
112+ expect ( store2 . getValue ( 'v1' ) ) . toBe ( null ) ;
113+ } ) ;
90114} ) ;
91115
92116describe ( 'Null with schemas' , ( ) => {
@@ -167,24 +191,24 @@ describe('Null with schemas', () => {
167191 test ( 'Multiple types with different null settings' , ( ) => {
168192 store . setTablesSchema ( {
169193 t1 : {
170- name : { type : 'string' } ,
171- age : { type : 'number' , allowNull : true } ,
172- active : { type : 'boolean' , default : true } ,
194+ c1 : { type : 'string' } ,
195+ c2 : { type : 'number' , allowNull : true } ,
196+ c3 : { type : 'boolean' , default : true } ,
173197 } ,
174198 } ) ;
175199
176200 store . setRow ( 't1' , 'r1' , {
177- name : 'Alice ' ,
178- age : null ,
179- active : true ,
201+ c1 : '1 ' ,
202+ c2 : null ,
203+ c3 : true ,
180204 } ) ;
181205
182- expect ( store . getCell ( 't1' , 'r1' , 'name ' ) ) . toBe ( 'Alice ' ) ;
183- expect ( store . getCell ( 't1' , 'r1' , 'age ' ) ) . toBe ( null ) ;
184- expect ( store . getCell ( 't1' , 'r1' , 'active ' ) ) . toBe ( true ) ;
206+ expect ( store . getCell ( 't1' , 'r1' , 'c1 ' ) ) . toBe ( '1 ' ) ;
207+ expect ( store . getCell ( 't1' , 'r1' , 'c2 ' ) ) . toBe ( null ) ;
208+ expect ( store . getCell ( 't1' , 'r1' , 'c3 ' ) ) . toBe ( true ) ;
185209
186- store . setCell ( 't1' , 'r1' , 'name ' , null ) ;
187- expect ( store . getCell ( 't1' , 'r1' , 'name ' ) ) . toBe ( 'Alice ' ) ;
210+ store . setCell ( 't1' , 'r1' , 'c1 ' , null ) ;
211+ expect ( store . getCell ( 't1' , 'r1' , 'c1 ' ) ) . toBe ( '1 ' ) ;
188212 } ) ;
189213} ) ;
190214
@@ -197,23 +221,33 @@ describe('Null listeners and events', () => {
197221
198222 test ( 'Cell listener fires when setting to null' , ( ) => {
199223 expect . assertions ( 2 ) ;
200- store . addCellListener ( 't1' , 'r1' , 'c1' , ( _store , _table , _row , _cell , newCell , oldCell ) => {
201- expect ( newCell ) . toBe ( null ) ;
202- expect ( oldCell ) . toBeUndefined ( ) ;
203- } ) ;
224+ store . addCellListener (
225+ 't1' ,
226+ 'r1' ,
227+ 'c1' ,
228+ ( _store , _table , _row , _cell , newCell , oldCell ) => {
229+ expect ( newCell ) . toBe ( null ) ;
230+ expect ( oldCell ) . toBeUndefined ( ) ;
231+ } ,
232+ ) ;
204233
205234 store . setCell ( 't1' , 'r1' , 'c1' , null ) ;
206235 } ) ;
207236
208237 test ( 'Cell listener fires when changing from null' , ( ) => {
209238 store . setCell ( 't1' , 'r1' , 'c1' , null ) ;
210239 expect . assertions ( 2 ) ;
211- store . addCellListener ( 't1' , 'r1' , 'c1' , ( _store , _table , _row , _cell , newCell , oldCell ) => {
212- expect ( newCell ) . toBe ( 'hello' ) ;
213- expect ( oldCell ) . toBe ( null ) ;
214- } ) ;
240+ store . addCellListener (
241+ 't1' ,
242+ 'r1' ,
243+ 'c1' ,
244+ ( _store , _table , _row , _cell , newCell , oldCell ) => {
245+ expect ( newCell ) . toBe ( '1' ) ;
246+ expect ( oldCell ) . toBe ( null ) ;
247+ } ,
248+ ) ;
215249
216- store . setCell ( 't1' , 'r1' , 'c1' , 'hello ' ) ;
250+ store . setCell ( 't1' , 'r1' , 'c1' , '1 ' ) ;
217251 } ) ;
218252
219253 test ( 'Value listener fires when setting to null' , ( ) => {
@@ -229,14 +263,19 @@ describe('Null listeners and events', () => {
229263 test ( 'HasCell listener distinguishes null from deleted' , ( ) => {
230264 expect . assertions ( 2 ) ;
231265 let callCount = 0 ;
232- store . addHasCellListener ( 't1' , 'r1' , 'c1' , ( _store , _table , _row , _cell , hasCell ) => {
233- if ( callCount === 0 ) {
234- expect ( hasCell ) . toBe ( true ) ;
235- } else if ( callCount === 1 ) {
236- expect ( hasCell ) . toBe ( false ) ;
237- }
238- callCount ++ ;
239- } ) ;
266+ store . addHasCellListener (
267+ 't1' ,
268+ 'r1' ,
269+ 'c1' ,
270+ ( _store , _table , _row , _cell , hasCell ) => {
271+ if ( callCount === 0 ) {
272+ expect ( hasCell ) . toBe ( true ) ;
273+ } else if ( callCount === 1 ) {
274+ expect ( hasCell ) . toBe ( false ) ;
275+ }
276+ callCount ++ ;
277+ } ,
278+ ) ;
240279
241280 store . setCell ( 't1' , 'r1' , 'c1' , null ) ;
242281 store . delCell ( 't1' , 'r1' , 'c1' ) ;
@@ -277,7 +316,7 @@ describe('Null vs Delete semantics', () => {
277316 test ( 'Null cells appear in iteration' , ( ) => {
278317 store . setTables ( {
279318 t1 : {
280- r1 : { c1 : 'hello ' , c2 : null , c3 : 42 } ,
319+ r1 : { c1 : '1 ' , c2 : null , c3 : 3 } ,
281320 } ,
282321 } ) ;
283322
@@ -287,24 +326,24 @@ describe('Null vs Delete semantics', () => {
287326 } ) ;
288327
289328 expect ( cells ) . toEqual ( [
290- [ 'c1' , 'hello ' ] ,
329+ [ 'c1' , '1 ' ] ,
291330 [ 'c2' , null ] ,
292- [ 'c3' , 42 ] ,
331+ [ 'c3' , 3 ] ,
293332 ] ) ;
294333 } ) ;
295334
296335 test ( 'Null values appear in iteration' , ( ) => {
297- store . setValues ( { v1 : 'test ' , v2 : null , v3 : 123 } ) ;
336+ store . setValues ( { v1 : '1 ' , v2 : null , v3 : 3 } ) ;
298337
299338 const values : [ string , any ] [ ] = [ ] ;
300339 store . forEachValue ( ( valueId , value ) => {
301340 values . push ( [ valueId , value ] ) ;
302341 } ) ;
303342
304343 expect ( values ) . toEqual ( [
305- [ 'v1' , 'test ' ] ,
344+ [ 'v1' , '1 ' ] ,
306345 [ 'v2' , null ] ,
307- [ 'v3' , 123 ] ,
346+ [ 'v3' , 3 ] ,
308347 ] ) ;
309348 } ) ;
310349} ) ;
0 commit comments