Skip to content

Commit 9e8bd14

Browse files
committed
added test for getValidFormData method
1 parent be667a2 commit 9e8bd14

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

packages/utils/src/schema/getDefaultFormState.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ export function computeDefaults<T = any, S extends StrictRJSFSchema = RJSFSchema
353353
* @param formData The current formData
354354
* @returns valid formData
355355
*/
356-
function getValidFormData<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(
356+
export function getValidFormData<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(
357357
validator: ValidatorType<T, S, F>,
358358
schema: S,
359359
rootSchema: S,
@@ -364,7 +364,7 @@ function getValidFormData<T = any, S extends StrictRJSFSchema = RJSFSchema, F ex
364364

365365
if (isSelectField) {
366366
const getOptionsList = optionsList(schema);
367-
const isValid = getOptionsList?.some((option) => isEqual(option.value, formData)) ?? false;
367+
const isValid = getOptionsList?.some((option) => isEqual(option.value, formData));
368368
validFormData = isValid ? formData : undefined;
369369
}
370370
return validFormData;

packages/utils/test/schema/getDefaultFormStateTest.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
getDefaultBasedOnSchemaType,
77
getInnerSchemaForArrayItem,
88
getObjectDefaults,
9+
getValidFormData,
910
} from '../../src/schema/getDefaultFormState';
1011
import { RECURSIVE_REF, RECURSIVE_REF_ALLOF } from '../testUtils/testData';
1112
import { IExpectType, TestValidatorType } from './types';
@@ -2058,6 +2059,29 @@ export default function getDefaultFormStateTest(testValidator: TestValidatorType
20582059
},
20592060
]);
20602061
});
2062+
describe('getValidFormData', () => {
2063+
let schema: RJSFSchema;
2064+
it('Test schema with non valid formData for enum property', () => {
2065+
schema = {
2066+
type: 'string',
2067+
enum: ['a', 'b', 'c'],
2068+
};
2069+
2070+
expect(getValidFormData(testValidator, schema, schema, 'd')).toBeUndefined();
2071+
});
2072+
it('Test schema with valid formData for enum property', () => {
2073+
expect(getValidFormData(testValidator, schema, schema, 'b')).toEqual('b');
2074+
});
2075+
it('Test schema with const property', () => {
2076+
schema = {
2077+
type: 'string',
2078+
enum: ['a', 'b', 'c'],
2079+
const: 'a',
2080+
};
2081+
2082+
expect(getValidFormData(testValidator, schema, schema, 'a')).toEqual('a');
2083+
});
2084+
});
20612085
describe('default form state behavior: ignore min items unless required', () => {
20622086
it('should return empty data for an optional array property with minItems', () => {
20632087
const schema: RJSFSchema = {

0 commit comments

Comments
 (0)