@@ -370,7 +370,6 @@ const gridFormSchema = {
370370 } ,
371371 } ,
372372} as RJSFSchema ;
373-
374373const gridFormUISchema : UiSchema = {
375374 'ui:field' : 'LayoutGridForm' ,
376375 'ui:layoutGrid' : {
@@ -565,6 +564,35 @@ const gridFormUISchema: UiSchema = {
565564 } ,
566565} ;
567566
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+
568596const ERRORS = [ 'error' ] ;
569597const EXTRA_ERROR = new ErrorSchemaBuilder ( ) . addErrors ( ERRORS ) . ErrorSchema ;
570598const DEFAULT_ID = 'test-id' ;
@@ -643,9 +671,11 @@ const simpleOneOfRegistry = getTestRegistry(SIMPLE_ONEOF, REGISTRY_FIELDS, {}, {
643671const gridFormSchemaRegistry = getTestRegistry ( GRID_FORM_SCHEMA , REGISTRY_FIELDS , { } , { } , REGISTRY_FORM_CONTEXT ) ;
644672const sampleSchemaRegistry = getTestRegistry ( SAMPLE_SCHEMA , REGISTRY_FIELDS , { } , { } , REGISTRY_FORM_CONTEXT ) ;
645673const readonlySchemaRegistry = getTestRegistry ( readonlySchema , REGISTRY_FIELDS , { } , { } , REGISTRY_FORM_CONTEXT ) ;
674+ const arraySchemaRegistry = getTestRegistry ( arraySchema , REGISTRY_FIELDS , { } , { } , REGISTRY_FORM_CONTEXT ) ;
646675const GRID_FORM_ID_SCHEMA = gridFormSchemaRegistry . schemaUtils . toIdSchema ( GRID_FORM_SCHEMA ) ;
647676const SAMPLE_SCHEMA_ID_SCHEMA = sampleSchemaRegistry . schemaUtils . toIdSchema ( SAMPLE_SCHEMA ) ;
648677const READONLY_ID_SCHEMA = readonlySchemaRegistry . schemaUtils . toIdSchema ( readonlySchema ) ;
678+ const ARRAY_ID_SCHEMA = arraySchemaRegistry . schemaUtils . toIdSchema ( arraySchema ) ;
649679
650680/** Simple mock idSchema generator that will take a dotted path string, and return the path joined by the `idSeparator`
651681 * and appended to `root` (default idPrefix in `toIdSchema`)
@@ -845,6 +875,40 @@ describe('LayoutGridField', () => {
845875 expect ( LayoutGridField . getIdSchema ( schemaUtils , ID_SCHEMA , { } ) ) . toEqual ( ID_SCHEMA ) ;
846876 } ) ;
847877 } ) ;
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+ } ) ;
848912 describe ( 'LayoutGridField.getSchemaDetailsForField(), blank schema' , ( ) => {
849913 beforeAll ( ( ) => {
850914 retrieveSchemaSpy = jest . spyOn ( registry . schemaUtils , 'retrieveSchema' ) ;
@@ -1067,9 +1131,9 @@ describe('LayoutGridField', () => {
10671131 } ) ;
10681132 describe ( 'LayoutGridField.getSchemaDetailsForField(), readonlySchema' , ( ) => {
10691133 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' ) ;
10731137 } ) ;
10741138 afterEach ( ( ) => {
10751139 findSelectedOptionInXxxOf . mockClear ( ) ;
@@ -1099,7 +1163,7 @@ describe('LayoutGridField', () => {
10991163 idSchema : testGetIdSchema ( path ) ,
11001164 optionsInfo : { options : get ( schema , [ ONE_OF_KEY ] ) , hasDiscriminator : false } ,
11011165 } ) ;
1102- expect ( retrieveSchemaSpy ) . not . toHaveBeenCalled ( ) ;
1166+ expect ( retrieveSchemaSpy ) . toHaveBeenCalledTimes ( 2 ) ;
11031167 expect ( toIdSchemaSpy ) . not . toHaveBeenCalled ( ) ;
11041168 } ) ;
11051169 test ( 'returns schema, isRequired: true, isReadonly: true, options: undefined when selecting readonly field' , ( ) => {
@@ -1120,7 +1184,7 @@ describe('LayoutGridField', () => {
11201184 idSchema : testGetIdSchema ( path ) ,
11211185 optionsInfo : undefined ,
11221186 } ) ;
1123- expect ( retrieveSchemaSpy ) . not . toHaveBeenCalled ( ) ;
1187+ expect ( retrieveSchemaSpy ) . toHaveBeenCalledTimes ( 2 ) ;
11241188 expect ( toIdSchemaSpy ) . not . toHaveBeenCalled ( ) ;
11251189 } ) ;
11261190 test ( 'returns schema, isRequired: true, isReadonly: true, options: undefined when selecting field on readonly parent' , ( ) => {
@@ -1141,7 +1205,7 @@ describe('LayoutGridField', () => {
11411205 idSchema : testGetIdSchema ( path ) ,
11421206 optionsInfo : undefined ,
11431207 } ) ;
1144- expect ( retrieveSchemaSpy ) . not . toHaveBeenCalled ( ) ;
1208+ expect ( retrieveSchemaSpy ) . toHaveBeenCalledTimes ( 3 ) ;
11451209 expect ( toIdSchemaSpy ) . not . toHaveBeenCalled ( ) ;
11461210 } ) ;
11471211 test ( 'returns schema, isRequired: true, isReadonly: false, options: undefined when selecting explicitly readonly false field' , ( ) => {
@@ -1163,7 +1227,65 @@ describe('LayoutGridField', () => {
11631227 idSchema : testGetIdSchema ( path ) ,
11641228 optionsInfo : undefined ,
11651229 } ) ;
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 undefined when 1d array 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: false, isReadonly: undefined, options: undefined when 2d array schema is requested' , ( ) => {
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 ) ;
11671289 expect ( toIdSchemaSpy ) . not . toHaveBeenCalled ( ) ;
11681290 } ) ;
11691291 } ) ;
0 commit comments