diff --git a/web/src/components/jsonjoy-builder/components/schema-editor/types/array-editor.test.tsx b/web/src/components/jsonjoy-builder/components/schema-editor/types/array-editor.test.tsx new file mode 100644 index 0000000000..f3c705cf23 --- /dev/null +++ b/web/src/components/jsonjoy-builder/components/schema-editor/types/array-editor.test.tsx @@ -0,0 +1,37 @@ +import { + act, + fireEvent, + render, + screen, + waitFor, +} from '@testing-library/react'; +import ArrayEditor from './array-editor'; + +jest.mock('../type-editor', () => ({ + __esModule: true, + default: () => null, +})); + +describe('ArrayEditor', () => { + it('writes the latest uniqueItems switch value to the schema', async () => { + const onChange = jest.fn(); + + render( + , + ); + + await act(async () => { + fireEvent.click(screen.getByRole('switch')); + }); + + await waitFor(() => { + expect(onChange).toHaveBeenCalledWith( + expect.objectContaining({ uniqueItems: true }), + ); + }); + }); +}); diff --git a/web/src/components/jsonjoy-builder/components/schema-editor/types/array-editor.tsx b/web/src/components/jsonjoy-builder/components/schema-editor/types/array-editor.tsx index b50a2add3e..908f9ffb2f 100644 --- a/web/src/components/jsonjoy-builder/components/schema-editor/types/array-editor.tsx +++ b/web/src/components/jsonjoy-builder/components/schema-editor/types/array-editor.tsx @@ -43,13 +43,13 @@ const ArrayEditor: React.FC = ({ ); // Handle validation settings change - const handleValidationChange = () => { + const handleValidationChange = (nextUniqueItems = uniqueItems) => { const validationProps: ObjectJSONSchema = { type: 'array', ...(isBooleanSchema(schema) ? {} : schema), minItems: minItems, maxItems: maxItems, - uniqueItems: uniqueItems || undefined, + uniqueItems: nextUniqueItems || undefined, }; // Keep the items schema @@ -68,6 +68,10 @@ const ArrayEditor: React.FC = ({ onChange(propsToKeep as ObjectJSONSchema); }; + const handleValidationBlur = () => { + handleValidationChange(); + }; + // Handle item schema changes const handleItemSchemaChange = (updatedItemSchema: ObjectJSONSchema) => { const updatedSchema: ObjectJSONSchema = { @@ -125,7 +129,7 @@ const ArrayEditor: React.FC = ({ setMinItems(value); // Don't update immediately to avoid too many rerenders }} - onBlur={handleValidationChange} + onBlur={handleValidationBlur} placeholder={t.arrayMinimumPlaceholder} className={cn('h-8', !!minMaxError && 'border-destructive')} /> @@ -150,7 +154,7 @@ const ArrayEditor: React.FC = ({ setMaxItems(value); // Don't update immediately to avoid too many rerenders }} - onBlur={handleValidationChange} + onBlur={handleValidationBlur} placeholder={t.arrayMaximumPlaceholder} className={cn('h-8', !!minMaxError && 'border-destructive')} /> @@ -170,7 +174,7 @@ const ArrayEditor: React.FC = ({ checked={uniqueItems} onCheckedChange={(checked) => { setUniqueItems(checked); - setTimeout(handleValidationChange, 0); + handleValidationChange(checked); }} />