2022-11-28 02:00:28 +00:00
|
|
|
import { Rocket } from 'lucide-react';
|
|
|
|
|
2021-12-14 19:14:53 +00:00
|
|
|
import { render, fireEvent } from '@/react-tools/test-utils';
|
|
|
|
|
2023-02-07 03:33:57 +00:00
|
|
|
import { BoxSelector } from './BoxSelector';
|
|
|
|
import { BoxSelectorOption, Value } from './types';
|
2021-12-14 19:14:53 +00:00
|
|
|
|
2023-02-07 03:33:57 +00:00
|
|
|
function renderDefault<T extends Value>({
|
2021-12-14 19:14:53 +00:00
|
|
|
options = [],
|
|
|
|
onChange = () => {},
|
|
|
|
radioName = 'radio',
|
|
|
|
value,
|
2023-02-07 03:33:57 +00:00
|
|
|
}: {
|
|
|
|
options?: BoxSelectorOption<T>[];
|
|
|
|
onChange?: (value: T) => void;
|
|
|
|
radioName?: string;
|
|
|
|
value: T;
|
|
|
|
}) {
|
2021-12-14 19:14:53 +00:00
|
|
|
return render(
|
|
|
|
<BoxSelector
|
|
|
|
options={options}
|
|
|
|
onChange={onChange}
|
|
|
|
radioName={radioName}
|
2023-02-07 03:33:57 +00:00
|
|
|
value={value}
|
2021-12-14 19:14:53 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
test('should render with the initial value selected and call onChange when clicking a different value', async () => {
|
|
|
|
const options: BoxSelectorOption<number>[] = [
|
|
|
|
{
|
|
|
|
description: 'description 1',
|
2022-11-28 02:00:28 +00:00
|
|
|
icon: Rocket,
|
2021-12-14 19:14:53 +00:00
|
|
|
id: '1',
|
|
|
|
value: 3,
|
|
|
|
label: 'option 1',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: 'description 2',
|
2022-11-28 02:00:28 +00:00
|
|
|
icon: Rocket,
|
2021-12-14 19:14:53 +00:00
|
|
|
id: '2',
|
|
|
|
value: 4,
|
|
|
|
label: 'option 2',
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
const onChange = jest.fn();
|
|
|
|
const { getByLabelText } = renderDefault({
|
|
|
|
options,
|
|
|
|
onChange,
|
|
|
|
value: options[0].value,
|
|
|
|
});
|
|
|
|
|
|
|
|
const item1 = getByLabelText(options[0].label, {
|
|
|
|
exact: false,
|
|
|
|
}) as HTMLInputElement;
|
|
|
|
expect(item1.checked).toBeTruthy();
|
|
|
|
|
|
|
|
const item2 = getByLabelText(options[1].label, {
|
|
|
|
exact: false,
|
|
|
|
}) as HTMLInputElement;
|
|
|
|
expect(item2.checked).toBeFalsy();
|
|
|
|
|
|
|
|
fireEvent.click(item2);
|
|
|
|
expect(onChange).toHaveBeenCalledWith(options[1].value, false);
|
|
|
|
});
|