import React, { createRef, useState } from 'react';
import {
act,
fireEvent,
render,
RenderResult,
waitFor,
waitForElementToBeRemoved,
} from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import BeakerIcon from '@leafygreen-ui/icon/dist/Beaker';
import * as LeafyGreenProviderModule from '@leafygreen-ui/leafygreen-provider';
import { PopoverProvider } from '@leafygreen-ui/leafygreen-provider';
import { keyMap } from '@leafygreen-ui/lib';
import { RenderMode } from '@leafygreen-ui/popover';
import { Context, jest as Jest } from '@leafygreen-ui/testing-lib';
import { getTestUtils, type GetTestUtilsReturnType } from '../testing';
import { Option, OptionGroup, Select } from '..';
import { SelectProps, State } from './Select.types';
const Color = {
Red: 'Explicit value: Red',
Blue: 'Blue',
Green: 'Green',
Yellow: 'Yellow',
Orange: 'Orange',
Indigo: 'Indigo',
Violet: 'Violet',
} as const;
const errorMessage = 'This is the error message';
const enabledOptions = ['Select', 'Red', 'Blue', 'Green', 'Yellow'] as const;
const defaultProps = {
label: 'Label',
name: 'pet',
description: 'Description',
children: [
,
,
,
,
<>
>
,
],
} as const;
function Controller({
children,
initialValue,
}: {
children: (
value: string,
setValue: (value: React.SetStateAction) => void,
) => React.ReactNode;
initialValue: string;
}) {
const [value, setValue] = useState(initialValue);
return <>{children(value, setValue)}>;
}
function renderSelect(props = {}) {
const renderUtils = render();
const utils = getTestUtils();
const rerenderSelect = (newProps?: Partial) => {
const allProps = { ...props, ...newProps };
renderUtils.rerender();
};
return {
...renderUtils,
...utils,
rerenderSelect,
};
}
describe('packages/select', () => {
test('renders label and description', () => {
const { getLabel, getDescription } = renderSelect();
expect(getLabel()).toBeVisible();
expect(getDescription()).toBeVisible();
});
test('renders placeholder', async () => {
const { getInput, getOptionByValue, rerenderSelect } = renderSelect();
expect(getInput()).toHaveTextContent('Select');
rerenderSelect({ placeholder: 'Explicit placeholder' });
expect(getInput()).toHaveTextContent('Explicit placeholder');
userEvent.click(getInput());
expect(getOptionByValue('Explicit placeholder')).toBeInTheDocument();
});
test('accepts a ref', () => {
const ref = createRef();
const { container } = render();
expect(ref.current).toBeDefined();
expect(ref.current).toBe(container.firstElementChild);
});
test('must render options in