-
-
Notifications
You must be signed in to change notification settings - Fork 245
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrated
SelectField
tests to @testing-library/react (#1351)
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
1 parent
1bfe92a
commit e2c77bf
Showing
6 changed files
with
559 additions
and
905 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
Oops, something went wrong.