Skip to content

Commit

Permalink
Migrated SelectField tests to @testing-library/react (#1351)
Browse files Browse the repository at this point in the history
Co-authored-by: Konrad Bosak <[email protected]>
Co-authored-by: Konrad Bosak <[email protected]>
Co-authored-by: Ernest Teluk <[email protected]>
Co-authored-by: Piotr Pośpiech <[email protected]>
  • Loading branch information
5 people authored Jul 25, 2024
1 parent 1bfe92a commit e2c77bf
Show file tree
Hide file tree
Showing 6 changed files with 559 additions and 905 deletions.
64 changes: 34 additions & 30 deletions packages/uniforms-antd/__tests__/SelectField.tsx
Original file line number Diff line number Diff line change
@@ -1,44 +1,48 @@
import Select from 'antd/lib/select';
import { fireEvent, screen } from '@testing-library/react';
import React from 'react';
import { SelectField } from 'uniforms-antd';

import createContext from './_createContext';
import mount from './_mount';
import { renderWithZod } from 'uniforms/__suites__';
import { z } from 'zod';

test('<SelectField> - renders a select which correctly reacts on change (array)', () => {
const onChange = jest.fn();

const element = <SelectField name="x" value={undefined} />;
const wrapper = mount(
element,
createContext(
{
x: { type: Array },
'x.$': { type: String, allowedValues: ['a', 'b'] },
},
{ onChange },
),
);
renderWithZod({
element: <SelectField name="x" />,
schema: z.object({
x: z.string().uniforms({
fieldType: Array,
options: [
{ label: 'A', value: 'a' },
{ label: 'B', value: 'b' },
],
}),
}),
onChange,
});

fireEvent.mouseDown(screen.getByRole('combobox'));
fireEvent.click(screen.getByText('B'));

expect(wrapper.find(Select)).toHaveLength(1);
// FIXME: Provide a valid option.
expect(
wrapper.find(Select).prop('onChange')!(['b'], null as any),
).toBeFalsy();
expect(onChange).toHaveBeenLastCalledWith('x', ['b']);
});

test('<SelectField> - renders a select (undefined values)', () => {
const element = <SelectField name="x" value={[undefined, 'a', undefined]} />;
const wrapper = mount(
element,
createContext({
x: { type: Array },
'x.$': { type: String, allowedValues: ['a', 'b'] },
const { container } = renderWithZod({
element: <SelectField name="x" value={[undefined, 'a', undefined]} />,
schema: z.object({
x: z.string().uniforms({
fieldType: Array,
options: [
{ label: 'A', value: 'a' },
{ label: 'B', value: 'b' },
],
}),
}),
);
});

expect(wrapper.find(Select)).toHaveLength(1);
expect(wrapper.find(Select).prop('value')).not.toContain(undefined);
expect(wrapper.find(Select).prop('value')).toContain('a');
expect(
container.getElementsByClassName('ant-select-selection-item-content')
.length,
).toBe(1);
});
Loading

0 comments on commit e2c77bf

Please sign in to comment.