@@ -370,7 +370,6 @@ const gridFormSchema = {
370
370
} ,
371
371
} ,
372
372
} as RJSFSchema ;
373
-
374
373
const gridFormUISchema : UiSchema = {
375
374
'ui:field' : 'LayoutGridForm' ,
376
375
'ui:layoutGrid' : {
@@ -565,6 +564,35 @@ const gridFormUISchema: UiSchema = {
565
564
} ,
566
565
} ;
567
566
567
+ const arraySchema : RJSFSchema = {
568
+ type : 'object' ,
569
+ properties : {
570
+ example : {
571
+ type : 'array' ,
572
+ title : 'Examples' ,
573
+ items : {
574
+ type : 'array' ,
575
+ minItems : 3 ,
576
+ maxItems : 3 ,
577
+ items : [
578
+ {
579
+ type : 'integer' ,
580
+ } ,
581
+ {
582
+ type : 'integer' ,
583
+ } ,
584
+ {
585
+ type : 'integer' ,
586
+ } ,
587
+ ] ,
588
+ } ,
589
+ } ,
590
+ } ,
591
+ required : [ 'example' ] ,
592
+ } ;
593
+ const outerArraySchema = arraySchema ?. properties ?. example as RJSFSchema ;
594
+ const innerArraySchema = outerArraySchema ?. items as RJSFSchema ;
595
+
568
596
const ERRORS = [ 'error' ] ;
569
597
const EXTRA_ERROR = new ErrorSchemaBuilder ( ) . addErrors ( ERRORS ) . ErrorSchema ;
570
598
const DEFAULT_ID = 'test-id' ;
@@ -643,9 +671,11 @@ const simpleOneOfRegistry = getTestRegistry(SIMPLE_ONEOF, REGISTRY_FIELDS, {}, {
643
671
const gridFormSchemaRegistry = getTestRegistry ( GRID_FORM_SCHEMA , REGISTRY_FIELDS , { } , { } , REGISTRY_FORM_CONTEXT ) ;
644
672
const sampleSchemaRegistry = getTestRegistry ( SAMPLE_SCHEMA , REGISTRY_FIELDS , { } , { } , REGISTRY_FORM_CONTEXT ) ;
645
673
const readonlySchemaRegistry = getTestRegistry ( readonlySchema , REGISTRY_FIELDS , { } , { } , REGISTRY_FORM_CONTEXT ) ;
674
+ const arraySchemaRegistry = getTestRegistry ( arraySchema , REGISTRY_FIELDS , { } , { } , REGISTRY_FORM_CONTEXT ) ;
646
675
const GRID_FORM_ID_SCHEMA = gridFormSchemaRegistry . schemaUtils . toIdSchema ( GRID_FORM_SCHEMA ) ;
647
676
const SAMPLE_SCHEMA_ID_SCHEMA = sampleSchemaRegistry . schemaUtils . toIdSchema ( SAMPLE_SCHEMA ) ;
648
677
const READONLY_ID_SCHEMA = readonlySchemaRegistry . schemaUtils . toIdSchema ( readonlySchema ) ;
678
+ const ARRAY_ID_SCHEMA = arraySchemaRegistry . schemaUtils . toIdSchema ( arraySchema ) ;
649
679
650
680
/** Simple mock idSchema generator that will take a dotted path string, and return the path joined by the `idSeparator`
651
681
* and appended to `root` (default idPrefix in `toIdSchema`)
@@ -845,6 +875,40 @@ describe('LayoutGridField', () => {
845
875
expect ( LayoutGridField . getIdSchema ( schemaUtils , ID_SCHEMA , { } ) ) . toEqual ( ID_SCHEMA ) ;
846
876
} ) ;
847
877
} ) ;
878
+ describe ( 'LayoutGridField.computeArraySchemasIfPresent()' , ( ) => {
879
+ test ( 'returns undefined rawSchema and given idSchema for non-numeric potentialIndex' , ( ) => {
880
+ expect ( LayoutGridField . computeArraySchemasIfPresent ( undefined , ID_SCHEMA , 'string' ) ) . toEqual ( {
881
+ rawSchema : undefined ,
882
+ idSchema : ID_SCHEMA ,
883
+ } ) ;
884
+ } ) ;
885
+ test ( 'returns undefined rawSchema and given idSchema for numeric potentialIndex, no schema' , ( ) => {
886
+ expect ( LayoutGridField . computeArraySchemasIfPresent ( undefined , ID_SCHEMA , '0' ) ) . toEqual ( {
887
+ rawSchema : undefined ,
888
+ idSchema : ID_SCHEMA ,
889
+ } ) ;
890
+ } ) ;
891
+ test ( 'returns undefined rawSchema and given idSchema for numeric potentialIndex, non-array schema' , ( ) => {
892
+ expect ( LayoutGridField . computeArraySchemasIfPresent ( readonlySchema , ID_SCHEMA , '0' ) ) . toEqual ( {
893
+ rawSchema : undefined ,
894
+ idSchema : ID_SCHEMA ,
895
+ } ) ;
896
+ } ) ;
897
+ test ( 'returns outer array rawSchema and generated idSchema for numeric potentialIndex, array schema' , ( ) => {
898
+ const idSchema = { [ ID_KEY ] : `${ ID_SCHEMA [ ID_KEY ] } _0` } as IdSchema ;
899
+ expect ( LayoutGridField . computeArraySchemasIfPresent ( outerArraySchema , ID_SCHEMA , '0' ) ) . toEqual ( {
900
+ rawSchema : outerArraySchema . items ,
901
+ idSchema,
902
+ } ) ;
903
+ } ) ;
904
+ test ( 'returns inner array rawSchema and generated idSchema for numeric potentialIndex, array schema, idSeparator' , ( ) => {
905
+ const idSchema = { [ ID_KEY ] : `${ ID_SCHEMA [ ID_KEY ] } .1` } as IdSchema ;
906
+ expect ( LayoutGridField . computeArraySchemasIfPresent ( innerArraySchema , ID_SCHEMA , '1' , '.' ) ) . toEqual ( {
907
+ rawSchema : get ( innerArraySchema . items , 1 ) ,
908
+ idSchema,
909
+ } ) ;
910
+ } ) ;
911
+ } ) ;
848
912
describe ( 'LayoutGridField.getSchemaDetailsForField(), blank schema' , ( ) => {
849
913
beforeAll ( ( ) => {
850
914
retrieveSchemaSpy = jest . spyOn ( registry . schemaUtils , 'retrieveSchema' ) ;
@@ -1067,9 +1131,9 @@ describe('LayoutGridField', () => {
1067
1131
} ) ;
1068
1132
describe ( 'LayoutGridField.getSchemaDetailsForField(), readonlySchema' , ( ) => {
1069
1133
beforeAll ( ( ) => {
1070
- retrieveSchemaSpy = jest . spyOn ( gridFormSchemaRegistry . schemaUtils , 'retrieveSchema' ) ;
1071
- toIdSchemaSpy = jest . spyOn ( gridFormSchemaRegistry . schemaUtils , 'toIdSchema' ) ;
1072
- findSelectedOptionInXxxOf = jest . spyOn ( gridFormSchemaRegistry . schemaUtils , 'findSelectedOptionInXxxOf' ) ;
1134
+ retrieveSchemaSpy = jest . spyOn ( readonlySchemaRegistry . schemaUtils , 'retrieveSchema' ) ;
1135
+ toIdSchemaSpy = jest . spyOn ( readonlySchemaRegistry . schemaUtils , 'toIdSchema' ) ;
1136
+ findSelectedOptionInXxxOf = jest . spyOn ( readonlySchemaRegistry . schemaUtils , 'findSelectedOptionInXxxOf' ) ;
1073
1137
} ) ;
1074
1138
afterEach ( ( ) => {
1075
1139
findSelectedOptionInXxxOf . mockClear ( ) ;
@@ -1099,7 +1163,7 @@ describe('LayoutGridField', () => {
1099
1163
idSchema : testGetIdSchema ( path ) ,
1100
1164
optionsInfo : { options : get ( schema , [ ONE_OF_KEY ] ) , hasDiscriminator : false } ,
1101
1165
} ) ;
1102
- expect ( retrieveSchemaSpy ) . not . toHaveBeenCalled ( ) ;
1166
+ expect ( retrieveSchemaSpy ) . toHaveBeenCalledTimes ( 2 ) ;
1103
1167
expect ( toIdSchemaSpy ) . not . toHaveBeenCalled ( ) ;
1104
1168
} ) ;
1105
1169
test ( 'returns schema, isRequired: true, isReadonly: true, options: undefined when selecting readonly field' , ( ) => {
@@ -1120,7 +1184,7 @@ describe('LayoutGridField', () => {
1120
1184
idSchema : testGetIdSchema ( path ) ,
1121
1185
optionsInfo : undefined ,
1122
1186
} ) ;
1123
- expect ( retrieveSchemaSpy ) . not . toHaveBeenCalled ( ) ;
1187
+ expect ( retrieveSchemaSpy ) . toHaveBeenCalledTimes ( 2 ) ;
1124
1188
expect ( toIdSchemaSpy ) . not . toHaveBeenCalled ( ) ;
1125
1189
} ) ;
1126
1190
test ( 'returns schema, isRequired: true, isReadonly: true, options: undefined when selecting field on readonly parent' , ( ) => {
@@ -1141,7 +1205,7 @@ describe('LayoutGridField', () => {
1141
1205
idSchema : testGetIdSchema ( path ) ,
1142
1206
optionsInfo : undefined ,
1143
1207
} ) ;
1144
- expect ( retrieveSchemaSpy ) . not . toHaveBeenCalled ( ) ;
1208
+ expect ( retrieveSchemaSpy ) . toHaveBeenCalledTimes ( 3 ) ;
1145
1209
expect ( toIdSchemaSpy ) . not . toHaveBeenCalled ( ) ;
1146
1210
} ) ;
1147
1211
test ( 'returns schema, isRequired: true, isReadonly: false, options: undefined when selecting explicitly readonly false field' , ( ) => {
@@ -1163,7 +1227,65 @@ describe('LayoutGridField', () => {
1163
1227
idSchema : testGetIdSchema ( path ) ,
1164
1228
optionsInfo : undefined ,
1165
1229
} ) ;
1166
- expect ( retrieveSchemaSpy ) . not . toHaveBeenCalled ( ) ;
1230
+ expect ( retrieveSchemaSpy ) . toHaveBeenCalledTimes ( 3 ) ;
1231
+ expect ( toIdSchemaSpy ) . not . toHaveBeenCalled ( ) ;
1232
+ } ) ;
1233
+ } ) ;
1234
+ describe ( 'LayoutGridField.getSchemaDetailsForField(), arraySchema' , ( ) => {
1235
+ beforeAll ( ( ) => {
1236
+ retrieveSchemaSpy = jest . spyOn ( arraySchemaRegistry . schemaUtils , 'retrieveSchema' ) ;
1237
+ toIdSchemaSpy = jest . spyOn ( arraySchemaRegistry . schemaUtils , 'toIdSchema' ) ;
1238
+ findSelectedOptionInXxxOf = jest . spyOn ( arraySchemaRegistry . schemaUtils , 'findSelectedOptionInXxxOf' ) ;
1239
+ } ) ;
1240
+ afterEach ( ( ) => {
1241
+ findSelectedOptionInXxxOf . mockClear ( ) ;
1242
+ retrieveSchemaSpy . mockClear ( ) ;
1243
+ toIdSchemaSpy . mockClear ( ) ;
1244
+ } ) ;
1245
+ afterAll ( ( ) => {
1246
+ retrieveSchemaSpy . mockRestore ( ) ;
1247
+ toIdSchemaSpy . mockRestore ( ) ;
1248
+ } ) ;
1249
+ test ( 'returns schema, isRequired: false, isReadonly: undefined, options when oneOf schema is requested' , ( ) => {
1250
+ const path = 'example.0' ;
1251
+ const schema = innerArraySchema ;
1252
+ expect (
1253
+ LayoutGridField . getSchemaDetailsForField (
1254
+ arraySchemaRegistry . schemaUtils ,
1255
+ path ,
1256
+ arraySchema ,
1257
+ { } ,
1258
+ ARRAY_ID_SCHEMA ,
1259
+ ) ,
1260
+ ) . toEqual ( {
1261
+ schema,
1262
+ isRequired : false ,
1263
+ isReadonly : undefined ,
1264
+ idSchema : testGetIdSchema ( path ) ,
1265
+ optionsInfo : undefined ,
1266
+ } ) ;
1267
+ expect ( retrieveSchemaSpy ) . toHaveBeenCalledTimes ( 2 ) ;
1268
+ expect ( toIdSchemaSpy ) . not . toHaveBeenCalled ( ) ;
1269
+ } ) ;
1270
+ test ( 'returns schema, isRequired: true, isReadonly: true, options: undefined when selecting readonly field' , ( ) => {
1271
+ const path = 'example.0.1' ;
1272
+ const schema = get ( innerArraySchema . items , '1' ) ;
1273
+ expect (
1274
+ LayoutGridField . getSchemaDetailsForField (
1275
+ arraySchemaRegistry . schemaUtils ,
1276
+ path ,
1277
+ arraySchema ,
1278
+ { } ,
1279
+ ARRAY_ID_SCHEMA ,
1280
+ ) ,
1281
+ ) . toEqual ( {
1282
+ schema,
1283
+ isRequired : false ,
1284
+ isReadonly : undefined ,
1285
+ idSchema : testGetIdSchema ( path ) ,
1286
+ optionsInfo : undefined ,
1287
+ } ) ;
1288
+ expect ( retrieveSchemaSpy ) . toHaveBeenCalledTimes ( 3 ) ;
1167
1289
expect ( toIdSchemaSpy ) . not . toHaveBeenCalled ( ) ;
1168
1290
} ) ;
1169
1291
} ) ;
0 commit comments