fix: select tests

This commit is contained in:
Daniel Lautzenheiser 2023-04-17 14:32:14 -04:00
parent 290d708b12
commit b39092c21c
3 changed files with 20 additions and 18 deletions

View File

@ -40,6 +40,22 @@ const Select = forwardRef(
{ label, placeholder, value, options, required = false, disabled, onChange }: SelectProps, { label, placeholder, value, options, required = false, disabled, onChange }: SelectProps,
ref: Ref<HTMLButtonElement>, ref: Ref<HTMLButtonElement>,
) => { ) => {
const { width } = useElementSize<HTMLButtonElement>(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( const handleChange = useCallback(
(_event: MouseEvent | KeyboardEvent | FocusEvent | null, selectedValue: number | string) => { (_event: MouseEvent | KeyboardEvent | FocusEvent | null, selectedValue: number | string) => {
if (Array.isArray(value)) { if (Array.isArray(value)) {
@ -52,31 +68,16 @@ const Select = forwardRef(
} }
onChange(newValue); onChange(newValue);
setOpen(false);
return; return;
} }
onChange(selectedValue); onChange(selectedValue);
setOpen(false);
}, },
[onChange, value], [onChange, value],
); );
const { width } = useElementSize<HTMLButtonElement>(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 ( return (
<div className="relative w-full"> <div className="relative w-full">
{/* eslint-disable-next-line @typescript-eslint/no-explicit-any */} {/* eslint-disable-next-line @typescript-eslint/no-explicit-any */}

View File

@ -2,8 +2,8 @@
* @jest-environment jsdom * @jest-environment jsdom
*/ */
import '@testing-library/jest-dom'; import '@testing-library/jest-dom';
import userEvent from '@testing-library/user-event';
import { act } from '@testing-library/react'; import { act } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { mockSelectField } from '@staticcms/test/data/fields.mock'; import { mockSelectField } from '@staticcms/test/data/fields.mock';
import { createWidgetControlHarness } from '@staticcms/test/harnesses/widget.harness'; import { createWidgetControlHarness } from '@staticcms/test/harnesses/widget.harness';

View File

@ -70,6 +70,7 @@ export const createMockWidgetControlProps = <
i18n: undefined, i18n: undefined,
duplicate: false, duplicate: false,
hidden: false, hidden: false,
controlled: false,
theme: 'light', theme: 'light',
onChange: jest.fn(), onChange: jest.fn(),
openMediaLibrary: jest.fn(), openMediaLibrary: jest.fn(),