@@ -2,6 +2,8 @@ import { Buffer } from 'buffer';
2
2
3
3
import { ClassConverter } from './class_converter.js' ;
4
4
import { convertBigintsInObj , convertFromJsonObj , convertToJsonObj } from './convert.js' ;
5
+ import { ToStringClass as ToStringClassA } from './fixtures/class_a.js' ;
6
+ import { ToStringClass as ToStringClassB } from './fixtures/class_b.js' ;
5
7
import { TestNote } from './fixtures/test_state.js' ;
6
8
7
9
const TEST_BASE64 = 'YmFzZTY0IGRlY29kZXI=' ;
@@ -24,3 +26,33 @@ test('does not convert a string', () => {
24
26
expect ( convertBigintsInObj ( 'hello' ) ) . toEqual ( 'hello' ) ;
25
27
expect ( convertBigintsInObj ( { msg : 'hello' } ) ) . toEqual ( { msg : 'hello' } ) ;
26
28
} ) ;
29
+
30
+ test ( 'converts a registered class' , ( ) => {
31
+ const cc = new ClassConverter ( { ToStringClass : ToStringClassA } ) ;
32
+ const obj = { content : new ToStringClassA ( 'a' , 'b' ) } ;
33
+ const serialised = convertToJsonObj ( cc , obj ) ;
34
+ const deserialised = convertFromJsonObj ( cc , serialised ) as { content : ToStringClassA } ;
35
+ expect ( deserialised . content ) . toBeInstanceOf ( ToStringClassA ) ;
36
+ expect ( deserialised . content . x ) . toEqual ( 'a' ) ;
37
+ expect ( deserialised . content . y ) . toEqual ( 'b' ) ;
38
+ } ) ;
39
+
40
+ test ( 'converts a class by name in the event of duplicate modules being loaded' , ( ) => {
41
+ const cc = new ClassConverter ( { ToStringClass : ToStringClassA } ) ;
42
+ const obj = { content : new ToStringClassB ( 'a' , 'b' ) } ;
43
+ const serialised = convertToJsonObj ( cc , obj ) ;
44
+ const deserialised = convertFromJsonObj ( cc , serialised ) as { content : ToStringClassA } ;
45
+ expect ( deserialised . content ) . toBeInstanceOf ( ToStringClassA ) ;
46
+ expect ( deserialised . content . x ) . toEqual ( 'a' ) ;
47
+ expect ( deserialised . content . y ) . toEqual ( 'b' ) ;
48
+ } ) ;
49
+
50
+ test ( 'converts a class by constructor instead of name in the event of minified bundle' , ( ) => {
51
+ const cc = new ClassConverter ( { NotMinifiedToStringClassName : ToStringClassA } ) ;
52
+ const obj = { content : new ToStringClassA ( 'a' , 'b' ) } ;
53
+ const serialised = convertToJsonObj ( cc , obj ) ;
54
+ const deserialised = convertFromJsonObj ( cc , serialised ) as { content : ToStringClassA } ;
55
+ expect ( deserialised . content ) . toBeInstanceOf ( ToStringClassA ) ;
56
+ expect ( deserialised . content . x ) . toEqual ( 'a' ) ;
57
+ expect ( deserialised . content . y ) . toEqual ( 'b' ) ;
58
+ } ) ;
0 commit comments