1
1
import { beforeEach , describe , expect , it } from 'vitest' ;
2
- import type { Schema } from '@sjsf/form/core' ;
2
+ import type { RPath , Schema } from '@sjsf/form/core' ;
3
3
import { createMerger } from '@sjsf/form/mergers/modern' ;
4
4
import { createFormValidator } from '@sjsf/ajv8-validator' ;
5
5
6
6
import {
7
- createFormDataEntriesConverter ,
7
+ createFormDataEntryConverter ,
8
8
type FormDataConverterOptions
9
- } from './convert-form-data-entries.js' ;
10
- import type { Entries } from './entry.js' ;
9
+ } from './convert-form-data-entry.js' ;
11
10
12
11
const defaultOptions : FormDataConverterOptions = {
13
12
validator : createFormValidator ( ) ,
@@ -16,7 +15,7 @@ const defaultOptions: FormDataConverterOptions = {
16
15
rootUiSchema : { }
17
16
} ;
18
17
19
- const convert = createFormDataEntriesConverter ( defaultOptions ) ;
18
+ const convert = createFormDataEntryConverter ( defaultOptions ) ;
20
19
21
20
describe ( 'convertFormDataEntries' , async ( ) => {
22
21
let c : AbortController ;
@@ -32,45 +31,48 @@ describe('convertFormDataEntries', async () => {
32
31
type : [ 'string' , 'null' ]
33
32
} ,
34
33
uiSchema : { } ,
35
- entries : [ ]
34
+ path : [ ] ,
35
+ value : undefined
36
36
} )
37
37
) . toBeNull ( ) ;
38
38
} ) ;
39
39
it ( 'Should convert boolean' , async ( ) => {
40
40
const schema : Schema = { title : 'A boolean' , type : 'boolean' , default : false } ;
41
- const entries : Entries < string > = [ [ 'root.fixedNoToolbar.1' , 'on' ] ] ;
42
- await expect ( convert ( c . signal , { schema, uiSchema : { } , entries } ) ) . resolves . toEqual ( true ) ;
41
+ await expect (
42
+ convert ( c . signal , { schema, uiSchema : { } , path : [ 'fixedNoToolbar' , 1 ] , value : 'on' } )
43
+ ) . resolves . toEqual ( true ) ;
43
44
} ) ;
44
45
it ( 'Should convert number' , async ( ) => {
45
46
const schema : Schema = { title : 'A number' , type : 'number' , default : 42 } ;
46
- const entries : Entries < string > = [ [ 'root.fixedNoToolbar.0' , '42' ] ] ;
47
- await expect ( convert ( c . signal , { schema, uiSchema : { } , entries } ) ) . resolves . toEqual ( 42 ) ;
47
+ await expect (
48
+ convert ( c . signal , { schema, uiSchema : { } , path : [ 'fixedNoToolbar' , 0 ] , value : '42' } )
49
+ ) . resolves . toEqual ( 42 ) ;
48
50
} ) ;
49
51
it ( 'Should convert enum value' , async ( ) => {
50
52
const schema : Schema = {
51
53
type : 'string' ,
52
54
enum : [ 'foo' , 'bar' , 'baz' ]
53
55
} ;
54
- const entries : Entries < string > = [ [ 'root' , 'bar' ] ] ;
55
- await expect ( convert ( c . signal , { schema, uiSchema : { } , entries } ) ) . resolves . toEqual ( 'bar' ) ;
56
+ await expect (
57
+ convert ( c . signal , { schema, uiSchema : { } , path : [ ] , value : 'bar' } )
58
+ ) . resolves . toEqual ( 'bar' ) ;
56
59
} ) ;
57
60
it ( 'Should return correct value from enum of string numbers' , async ( ) => {
58
61
const schema : Schema = {
59
62
type : 'string' ,
60
63
enum : [ '1' , '2' , '3' ]
61
64
} ;
65
+ const path : RPath = [ ] ;
62
66
// First, the value is treated as an index
63
- await expect (
64
- convert ( c . signal , { schema , uiSchema : { } , entries : [ [ 'root' , '0' ] ] } )
65
- ) . resolves . toEqual ( '1' ) ;
67
+ await expect ( convert ( c . signal , { schema , uiSchema : { } , path , value : '0' } ) ) . resolves . toEqual (
68
+ '1'
69
+ ) ;
66
70
// Then we check if there is a value in the enumeration
67
- await expect (
68
- convert ( c . signal , { schema , uiSchema : { } , entries : [ [ 'root' , '3' ] ] } )
69
- ) . resolves . toEqual ( '3' ) ;
71
+ await expect ( convert ( c . signal , { schema , uiSchema : { } , path , value : '3' } ) ) . resolves . toEqual (
72
+ '3'
73
+ ) ;
70
74
// If nothing fits, it's an error
71
- await expect (
72
- convert ( c . signal , { schema, uiSchema : { } , entries : [ [ 'root' , '4' ] ] } )
73
- ) . rejects . toThrow ( ) ;
75
+ await expect ( convert ( c . signal , { schema, uiSchema : { } , path, value : '4' } ) ) . rejects . toThrow ( ) ;
74
76
} ) ;
75
77
it ( 'Should return correct value from anyOf' , async ( ) => {
76
78
const schema : Schema = {
@@ -94,8 +96,9 @@ describe('convertFormDataEntries', async () => {
94
96
}
95
97
]
96
98
} ;
97
- const entries : Entries < string > = [ [ 'root.currentColor' , '1' ] ] ;
98
- await expect ( convert ( c . signal , { schema, uiSchema : { } , entries } ) ) . resolves . toEqual ( '#00ff00' ) ;
99
+ await expect (
100
+ convert ( c . signal , { schema, uiSchema : { } , path : [ 'currentColor' ] , value : '1' } )
101
+ ) . resolves . toEqual ( '#00ff00' ) ;
99
102
} ) ;
100
103
it ( 'Should return correct value from oneOf' , async ( ) => {
101
104
const schema : Schema = {
@@ -106,12 +109,14 @@ describe('convertFormDataEntries', async () => {
106
109
{ title : 'Disable' , const : false }
107
110
]
108
111
} ;
109
- const entries : Entries < string > = [ [ 'root.toggleMask' , '0' ] ] ;
110
- await expect ( convert ( c . signal , { schema, uiSchema : { } , entries } ) ) . resolves . toEqual ( true ) ;
112
+ await expect (
113
+ convert ( c . signal , { schema, uiSchema : { } , path : [ 'toggleMask' ] , value : '0' } )
114
+ ) . resolves . toEqual ( true ) ;
111
115
} ) ;
112
116
it ( 'Should return correct value from enum' , async ( ) => {
113
117
const schema : Schema = { type : 'string' , enum : [ 'foo' , 'bar' , 'fuzz' , 'qux' ] } ;
114
- const entries : Entries < string > = [ [ 'root.multipleChoicesList' , '1' ] ] ;
115
- await expect ( convert ( c . signal , { schema, uiSchema : { } , entries } ) ) . resolves . toEqual ( 'bar' ) ;
118
+ await expect (
119
+ convert ( c . signal , { schema, uiSchema : { } , path : [ 'multipleChoicesList' ] , value : '1' } )
120
+ ) . resolves . toEqual ( 'bar' ) ;
116
121
} ) ;
117
122
} ) ;
0 commit comments