Skip to content

Commit

Permalink
fix: Update getSelectedOption to return correct option for enums
Browse files Browse the repository at this point in the history
  • Loading branch information
tdurnford committed Aug 27, 2020
1 parent 0a1958e commit fa67de1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,16 @@ describe('getSelectedOption', () => {
schema: { type: 'string' as const },
},
},
{
key: 'enum',
text: 'enum',
data: {
schema: {
type: 'string' as const,
enum: ['value1', 'value2', 'value3'],
},
},
},
];

it('returns undefined if there are no options', () => {
Expand Down Expand Up @@ -222,4 +232,10 @@ describe('getSelectedOption', () => {
expect(getSelectedOption(['foo'], options)).toEqual(options[3]);
});
});

describe('when the value is included in an enum', () => {
it('returns the enum option', () => {
expect(getSelectedOption('value2', options)).toEqual(options[6]);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,14 @@ export function getOptions(schema: JSONSchema7, definitions): SchemaOption[] {

export function getSelectedOption(value: any | undefined, options: SchemaOption[]): SchemaOption | undefined {
const expressionOption = options.find(({ key }) => key === 'expression');
const enumOption = options.find(({ data: { schema } }) => schema.enum?.includes(value));
let valueType = getValueType(value);

// return the enumOption if the value is included in the option's enum schema
if (enumOption) {
return enumOption;
}

// if its an array, we know it's not an expression

if (valueType === 'integer') {
Expand Down

0 comments on commit fa67de1

Please sign in to comment.