import React from 'react'; import { render, screen } from '@testing-library/react'; import { Select, InlineField } from '@grafana/ui'; import { useCreatableSelectPersistedBehaviour } from './useCreatableSelectPersistedBehaviour'; import userEvent from '@testing-library/user-event'; describe('useCreatableSelectPersistedBehaviour', () => { it('Should make a Select accept custom values', () => { const MyComp = (_: { force?: boolean }) => ( ); render(); const input = screen.getByLabelText('label') as HTMLInputElement; expect(input).toBeInTheDocument(); // we open the menu userEvent.click(input); const option1 = screen.getByText('Option 1'); expect(option1).toBeInTheDocument(); // Should call onChange when selecting an already existing option userEvent.click(option1); expect(onChange).toHaveBeenLastCalledWith( { value: 'Option 1', label: 'Option 1' }, { action: 'select-option', name: undefined, option: undefined } ); userEvent.click(input); // we type in the input 'Option 2', which should prompt an option creation userEvent.type(input, 'Option 2'); userEvent.click(screen.getByLabelText('Select option')); expect(onChange).toHaveBeenLastCalledWith({ value: 'Option 2' }); }); it('Should create an option for value if value is not in options', () => { const MyComp = (_: { force?: boolean }) => (