From ec932db942dd046640303056c89e3501b16ec469 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=A8=E3=81=BE=E3=81=A8=E3=81=BF?= Date: Sat, 25 May 2024 04:41:09 +0900 Subject: [PATCH] feat: treat default field in additionalProperties (#4199) * feat: treat "default" in additionalProperties * test: add test cases for the case there's a default in additionalProperties --- .../src/components/fields/ObjectField.tsx | 5 ++- packages/core/test/ObjectField.test.jsx | 44 +++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/packages/core/src/components/fields/ObjectField.tsx b/packages/core/src/components/fields/ObjectField.tsx index 107cda80df..dfde7fe1c3 100644 --- a/packages/core/src/components/fields/ObjectField.tsx +++ b/packages/core/src/components/fields/ObjectField.tsx @@ -202,13 +202,16 @@ class ObjectField { }); }); + it("should add a new default item if default is provided in the additionalProperties' schema", () => { + const customSchema = { + ...schema, + additionalProperties: { + type: 'string', + default: 'default value', + }, + }; + const { node, onChange } = createFormComponent({ + schema: customSchema, + formData: {}, + }); + + fireEvent.click(node.querySelector('.object-property-expand button')); + + sinon.assert.calledWithMatch(onChange.lastCall, { + formData: { + newKey: 'default value', + }, + }); + }); + + it('should add a new default item even if the schema of default value is invalid', () => { + const customSchema = { + ...schema, + additionalProperties: { + type: 'string', + default: 1, + }, + }; + const { node, onChange } = createFormComponent({ + schema: customSchema, + formData: {}, + }); + + fireEvent.click(node.querySelector('.object-property-expand button')); + + sinon.assert.calledWithMatch(onChange.lastCall, { + formData: { + newKey: 1, + }, + }); + }); + it('should not provide an expand button if length equals maxProperties', () => { const { node } = createFormComponent({ schema: { maxProperties: 1, ...schema },