@@ -45,11 +45,11 @@ export default function getClosestMatchingOptionTest(testValidator: TestValidato
4545 it ( 'returns 0 when formData is empty object' , ( ) => {
4646 expect ( calculateIndexScore ( testValidator , oneOfSchema , firstOption , { } ) ) . toEqual ( 0 ) ;
4747 } ) ;
48- it ( 'returns 1 for first option in oneOf schema' , ( ) => {
49- expect ( calculateIndexScore ( testValidator , oneOfSchema , firstOption , ONE_OF_SCHEMA_DATA ) ) . toEqual ( 1 ) ;
48+ it ( 'returns 2 for first option in oneOf schema' , ( ) => {
49+ expect ( calculateIndexScore ( testValidator , oneOfSchema , firstOption , ONE_OF_SCHEMA_DATA ) ) . toEqual ( 2 ) ;
5050 } ) ;
51- it ( 'returns 8 for second option in oneOf schema' , ( ) => {
52- expect ( calculateIndexScore ( testValidator , oneOfSchema , secondOption , ONE_OF_SCHEMA_DATA ) ) . toEqual ( 9 ) ;
51+ it ( 'returns 10 for second option in oneOf schema' , ( ) => {
52+ expect ( calculateIndexScore ( testValidator , oneOfSchema , secondOption , ONE_OF_SCHEMA_DATA ) ) . toEqual ( 10 ) ;
5353 } ) ;
5454 it ( 'returns 1 for a schema that has a type matching the formData type' , ( ) => {
5555 expect ( calculateIndexScore ( testValidator , oneOfSchema , { type : 'boolean' } , true ) ) . toEqual ( 1 ) ;
@@ -74,6 +74,34 @@ export default function getClosestMatchingOptionTest(testValidator: TestValidato
7474 ) ,
7575 ) . toEqual ( 0 ) ;
7676 } ) ;
77+ it ( 'scores nested oneOf by finding the first matching option and recursing' , ( ) => {
78+ const schema : RJSFSchema = {
79+ properties : {
80+ choice : {
81+ oneOf : [
82+ { properties : { day : { type : 'string' } } } ,
83+ { properties : { night : { type : 'string' } } } ,
84+ ] ,
85+ } ,
86+ } ,
87+ } ;
88+ testValidator . setReturnValues ( { isValid : [ true ] } ) ;
89+ expect ( calculateIndexScore ( testValidator , oneOfSchema , schema , { choice : { day : 'monday' } } ) ) . toEqual ( 1 ) ;
90+ } ) ;
91+ it ( 'scores nested anyOf by finding the first matching option and recursing' , ( ) => {
92+ const schema : RJSFSchema = {
93+ properties : {
94+ choice : {
95+ anyOf : [
96+ { properties : { x : { type : 'number' } } } ,
97+ { properties : { y : { type : 'number' } } } ,
98+ ] ,
99+ } ,
100+ } ,
101+ } ;
102+ testValidator . setReturnValues ( { isValid : [ true ] } ) ;
103+ expect ( calculateIndexScore ( testValidator , oneOfSchema , schema , { choice : { x : 42 } } ) ) . toEqual ( 1 ) ;
104+ } ) ;
77105 } ) ;
78106 describe ( 'oneOfMatchingOption' , ( ) => {
79107 it ( 'oneOfSchema, oneOfData data, no options, returns -1' , ( ) => {
@@ -161,9 +189,10 @@ export default function getClosestMatchingOptionTest(testValidator: TestValidato
161189 } ,
162190 } ;
163191 const formData = { ipsum : { night : 'nicht' } } ;
164- // Mock to return true for the last of the second one-ofs
192+ // Mock: first 3 calls (JUNK for lorem, lorem itself, JUNK for ipsum) fail;
193+ // 4th call (ipsum with oneOf stripped to {}) succeeds, giving a unique valid match
165194 testValidator . setReturnValues ( {
166- isValid : [ false , false , false , false , false , false , false , true ] ,
195+ isValid : [ false , false , false , true ] ,
167196 } ) ;
168197 expect ( getClosestMatchingOption ( testValidator , schema , formData , get ( schema , 'items.oneOf' ) ) ) . toEqual ( 1 ) ;
169198 } ) ;
@@ -207,9 +236,10 @@ export default function getClosestMatchingOptionTest(testValidator: TestValidato
207236 } ,
208237 } ;
209238 const formData = { ipsum : { night : 'nicht' } } ;
210- // Mock to return true for the last of the second anyOfs
239+ // Mock: first 3 calls (JUNK for lorem, lorem itself, JUNK for ipsum) fail;
240+ // 4th call (ipsum with anyOf stripped to {}) succeeds, giving a unique valid match
211241 testValidator . setReturnValues ( {
212- isValid : [ false , false , false , false , false , false , false , true ] ,
242+ isValid : [ false , false , false , true ] ,
213243 } ) ;
214244 expect ( getClosestMatchingOption ( testValidator , schema , formData , get ( schema , 'items.anyOf' ) ) ) . toEqual ( 1 ) ;
215245 } ) ;
0 commit comments