123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- import selectEvent from 'react-select-event';
- import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
- import {Form, SelectField} from 'sentry/components/deprecatedforms';
- import FormModel from 'sentry/components/forms/model';
- describe('SelectField', function () {
- it('renders without form context', function () {
- const {container} = render(
- <SelectField
- options={[
- {label: 'a', value: 'a'},
- {label: 'b', value: 'b'},
- ]}
- name="fieldName"
- value="a"
- />
- );
- expect(container).toSnapshot();
- });
- it('renders with flat choices', function () {
- const {container} = render(
- <Form
- value={
- new FormModel({
- initialData: {
- fieldName: 'fieldValue',
- },
- })
- }
- >
- <SelectField choices={['a', 'b', 'c']} name="fieldName" />
- </Form>
- );
- expect(container).toSnapshot();
- });
- it('renders with paired choices', function () {
- const {container} = render(
- <Form
- value={
- new FormModel({
- initialData: {
- fieldName: 'fieldValue',
- },
- })
- }
- >
- <SelectField
- choices={[
- ['a', 'abc'],
- ['b', 'bcd'],
- ['c', 'cde'],
- ]}
- name="fieldName"
- />
- </Form>
- );
- expect(container).toSnapshot();
- });
- it('can change value and submit', async function () {
- const mock = jest.fn();
- render(
- <Form onSubmit={mock}>
- <SelectField
- options={[
- {label: 'a', value: 'a'},
- {label: 'b', value: 'b'},
- ]}
- name="fieldName"
- />
- <button type="submit">submit</button>
- </Form>
- );
- await selectEvent.select(screen.getByText('Select...'), 'a');
- userEvent.click(screen.getByText('submit'));
- expect(mock).toHaveBeenCalledWith(
- {fieldName: 'a'},
- expect.anything(),
- expect.anything()
- );
- });
- it('can set the value to empty string via props with no options', async function () {
- const mock = jest.fn();
- const {rerender} = render(
- <SelectField
- options={[
- {label: 'a', value: 'a'},
- {label: 'b', value: 'b'},
- ]}
- name="fieldName"
- onChange={mock}
- />
- );
- // Select a value so there is an option selected.
- await selectEvent.select(screen.getByText('Select...'), 'a');
- expect(mock).toHaveBeenCalledTimes(1);
- expect(mock).toHaveBeenLastCalledWith('a');
- // Update props to remove value and options.
- rerender(<SelectField options={[]} value="" name="fieldName" onChange={mock} />);
- // second update.
- expect(mock).toHaveBeenCalledTimes(2);
- expect(mock).toHaveBeenLastCalledWith('');
- });
- describe('Multiple', function () {
- it('selects multiple values and submits', async function () {
- const mock = jest.fn();
- render(
- <Form onSubmit={mock}>
- <SelectField
- multiple
- options={[
- {label: 'a', value: 'a'},
- {label: 'b', value: 'b'},
- ]}
- name="fieldName"
- />
- <button type="submit">submit</button>
- </Form>
- );
- await selectEvent.select(screen.getByText('Select...'), 'a');
- userEvent.click(screen.getByText('submit'));
- expect(mock).toHaveBeenCalledWith(
- {fieldName: ['a']},
- expect.anything(),
- expect.anything()
- );
- });
- });
- });
|