From c939d29a412afbf4e5b276e9e111be79b7f47dd4 Mon Sep 17 00:00:00 2001 From: qsmr Date: Fri, 22 Nov 2024 16:44:50 -0500 Subject: [PATCH] review: add oneOf test --- packages/core/test/anyOf.test.jsx | 10 ++------- packages/core/test/oneOf.test.jsx | 34 +++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/packages/core/test/anyOf.test.jsx b/packages/core/test/anyOf.test.jsx index c62a661abb..0c30e92113 100644 --- a/packages/core/test/anyOf.test.jsx +++ b/packages/core/test/anyOf.test.jsx @@ -172,14 +172,8 @@ describe('anyOf', () => { properties: { foo: { anyOf: [ - { - type: 'string', - default: 'defaultfoo', - }, - { - type: 'boolean', - default: true, - }, + { type: 'string', default: 'defaultfoo' }, + { type: 'boolean', default: true }, ], }, }, diff --git a/packages/core/test/oneOf.test.jsx b/packages/core/test/oneOf.test.jsx index 3c76c6cfd5..4c021b2464 100644 --- a/packages/core/test/oneOf.test.jsx +++ b/packages/core/test/oneOf.test.jsx @@ -222,6 +222,37 @@ describe('oneOf', () => { ); }); + it('should assign a default value and set defaults on option change for scalar types schemas', () => { + const { node, onChange } = createFormComponent({ + schema: { + type: 'object', + properties: { + foo: { + oneOf: [ + { type: 'string', default: 'defaultfoo' }, + { type: 'boolean', default: true }, + ], + }, + }, + }, + }); + sinon.assert.calledWithMatch(onChange.lastCall, { + formData: { foo: 'defaultfoo' }, + }); + + const $select = node.querySelector('select'); + + act(() => { + fireEvent.change($select, { + target: { value: $select.options[1].value }, + }); + }); + + sinon.assert.calledWithMatch(onChange.lastCall, { + formData: { foo: true }, + }); + }); + it('should render a custom widget', () => { const schema = { type: 'object', @@ -573,6 +604,7 @@ describe('oneOf', () => { }, }; const formContext = { root: 'root-id', root_userId: 'userId-id' }; + function CustomSchemaField(props) { const { formContext, idSchema } = props; return ( @@ -582,6 +614,7 @@ describe('oneOf', () => { ); } + const { node } = createFormComponent({ schema, formData: { userId: 'foobarbaz' }, @@ -1598,6 +1631,7 @@ describe('oneOf', () => { }, }, }; + function customValidate(formData, errors) { errors.userId.addError('test'); return errors;