// SPDX-FileCopyrightText: Copyright Orangebot, Inc. and Medplum contributors
// SPDX-License-Identifier: Apache-2.0
import { MockClient } from '@medplum/mock';
import { MedplumProvider } from '@medplum/react-hooks';
import type { ReactNode } from 'react';
import { act, fireEvent, render, screen } from '../test-utils/render';
import { CodeInput } from './CodeInput';
const medplum = new MockClient();
const binding = 'https://example.com/test';
describe('CodeInput', () => {
beforeEach(() => {
jest.useFakeTimers();
});
afterEach(async () => {
await act(async () => {
jest.runOnlyPendingTimers();
});
jest.useRealTimers();
});
const defaultProps = { maxValues: 1, binding, name: 'test', onChange: undefined };
async function setup(child: ReactNode): Promise<void> {
await act(async () => {
render(<MedplumProvider medplum={medplum}>{child}</MedplumProvider>);
});
}
test('Renders', async () => {
await setup(<CodeInput {...defaultProps} />);
expect(screen.getByRole('searchbox')).toBeInTheDocument();
});
test('Renders string default value', async () => {
await act(async () => {
render(
<MedplumProvider medplum={medplum}>
<CodeInput {...defaultProps} defaultValue="xyz" maxValues={undefined} />
</MedplumProvider>
);
});
expect(screen.getByRole('searchbox')).toBeInTheDocument();
expect(screen.getByText('xyz')).toBeDefined();
});
test('Searches for results', async () => {
await setup(<CodeInput {...defaultProps} />);
const input = screen.getByRole('searchbox') as HTMLInputElement;
// Enter random text
await act(async () => {
fireEvent.change(input, { target: { value: 'Test' } });
});
// Wait for the drop down
await act(async () => {
jest.advanceTimersByTime(1000);
});
// Press the down arrow
await act(async () => {
fireEvent.keyDown(input, { key: 'ArrowDown', code: 'ArrowDown' });
});
// Press "Enter"
await act(async () => {
fireEvent.keyDown(input, { key: 'Enter', code: 'Enter' });
});
expect(screen.getByText('Test Display')).toBeDefined();
});
test('Searches for results with creatable set to false', async () => {
await setup(<CodeInput {...defaultProps} creatable={false} clearable={false} />);
const input = screen.getByRole('searchbox') as HTMLInputElement;
// Enter random text
await act(async () => {
fireEvent.change(input, { target: { value: 'Test' } });
});
// Wait for the drop down
await act(async () => {
jest.advanceTimersByTime(1000);
});
// Press the down arrow
await act(async () => {
fireEvent.keyDown(input, { key: 'ArrowDown', code: 'ArrowDown' });
});
// Press "Enter"
await act(async () => {
fireEvent.keyDown(input, { key: 'Enter', code: 'Enter' });
});
expect(screen.getByText('Test Display')).toBeDefined();
});
});