From b39092c21ccb601efbea065ebf6f8706243f6cfe Mon Sep 17 00:00:00 2001 From: Daniel Lautzenheiser Date: Mon, 17 Apr 2023 14:32:14 -0400 Subject: [PATCH] fix: select tests --- .../src/components/common/select/Select.tsx | 35 ++++++++++--------- .../select/__tests__/SelectControl.spec.ts | 2 +- packages/core/test/data/widgets.mock.ts | 1 + 3 files changed, 20 insertions(+), 18 deletions(-) diff --git a/packages/core/src/components/common/select/Select.tsx b/packages/core/src/components/common/select/Select.tsx index 81c8307b..509dff53 100644 --- a/packages/core/src/components/common/select/Select.tsx +++ b/packages/core/src/components/common/select/Select.tsx @@ -40,6 +40,22 @@ const Select = forwardRef( { label, placeholder, value, options, required = false, disabled, onChange }: SelectProps, ref: Ref, ) => { + const { width } = useElementSize(ref); + + const [open, setOpen] = useState(false); + const debouncedOpen = useDebounce(open, 200); + const handleOpenChange = useCallback( + (newOpen: boolean) => { + if (debouncedOpen !== open) { + return; + } + + setOpen(newOpen); + }, + [debouncedOpen, open], + ); + const handleButtonClick = useCallback(() => handleOpenChange(!open), [handleOpenChange, open]); + const handleChange = useCallback( (_event: MouseEvent | KeyboardEvent | FocusEvent | null, selectedValue: number | string) => { if (Array.isArray(value)) { @@ -52,31 +68,16 @@ const Select = forwardRef( } onChange(newValue); + setOpen(false); return; } onChange(selectedValue); + setOpen(false); }, [onChange, value], ); - const { width } = useElementSize(ref); - - const [open, setOpen] = useState(false); - const debouncedOpen = useDebounce(open, 250); - const handleOpenChange = useCallback( - (newOpen: boolean) => { - if (debouncedOpen !== open) { - return; - } - - setOpen(newOpen); - }, - [debouncedOpen, open], - ); - - const handleButtonClick = useCallback(() => handleOpenChange(!open), [handleOpenChange, open]); - return (
{/* eslint-disable-next-line @typescript-eslint/no-explicit-any */} diff --git a/packages/core/src/widgets/select/__tests__/SelectControl.spec.ts b/packages/core/src/widgets/select/__tests__/SelectControl.spec.ts index 5657df59..63a6f1f1 100644 --- a/packages/core/src/widgets/select/__tests__/SelectControl.spec.ts +++ b/packages/core/src/widgets/select/__tests__/SelectControl.spec.ts @@ -2,8 +2,8 @@ * @jest-environment jsdom */ import '@testing-library/jest-dom'; -import userEvent from '@testing-library/user-event'; import { act } from '@testing-library/react'; +import userEvent from '@testing-library/user-event'; import { mockSelectField } from '@staticcms/test/data/fields.mock'; import { createWidgetControlHarness } from '@staticcms/test/harnesses/widget.harness'; diff --git a/packages/core/test/data/widgets.mock.ts b/packages/core/test/data/widgets.mock.ts index 412cc17c..1ed192f0 100644 --- a/packages/core/test/data/widgets.mock.ts +++ b/packages/core/test/data/widgets.mock.ts @@ -70,6 +70,7 @@ export const createMockWidgetControlProps = < i18n: undefined, duplicate: false, hidden: false, + controlled: false, theme: 'light', onChange: jest.fn(), openMediaLibrary: jest.fn(),