Skip to content

Commit

Permalink
Add Select tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewbaldwin44 committed Dec 7, 2023
1 parent e0e8043 commit bf3d9c6
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions locust/webui/src/components/Form/tests/Select.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { render } from '@testing-library/react';
import { describe, expect, test } from 'vitest';

import Select from 'components/Form/Select';

const selectedOptionsToArray = (selectElement: HTMLSelectElement) =>
Array.from(selectElement.selectedOptions).map(option => option.value);

describe('Select component', () => {
test('should set defaultValue as first option', () => {
const options = ['Option 1', 'Option 2', 'Option 3'];

const { getByLabelText } = render(
<Select label='Select Input' name='selectInput' options={options} />,
);

expect((getByLabelText('Select Input') as HTMLSelectElement).value).toBe(options[0]);
});

test('should set defaultValue as all provided option in multi-select', () => {
const options = ['Option 1', 'Option 2', 'Option 3'];

const { getByLabelText } = render(
<Select label='Select Input' multiple name='selectInput' options={options} />,
);

expect(selectedOptionsToArray(getByLabelText('Select Input') as HTMLSelectElement)).toEqual(
options,
);
});

test('should allow defaultValue to be set', async () => {
const options = ['Option 1', 'Option 2', 'Option 3'];

const { getByLabelText } = render(
<Select
defaultValue={options[1]}
label='Select Input'
name='selectInput'
options={options}
/>,
);

expect((getByLabelText('Select Input') as HTMLSelectElement).value).toBe(options[1]);
});

test('should allow defaultValue to be set in multi-select', async () => {
const options = ['Option 1', 'Option 2', 'Option 3'];

const { getByLabelText } = render(
<Select
defaultValue={options[1]}
label='Select Input'
name='selectInput'
options={options}
/>,
);

expect(selectedOptionsToArray(getByLabelText('Select Input') as HTMLSelectElement)).toEqual([
options[1],
]);
});
});

0 comments on commit bf3d9c6

Please sign in to comment.