Skip to content

Commit 0e4e102

Browse files
authored
Add example of firing select event (#472)
1 parent df5f8f0 commit 0e4e102

File tree

1 file changed

+14
-0
lines changed
  • docs/react-testing-library

1 file changed

+14
-0
lines changed

docs/react-testing-library/faq.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,20 @@ test('change values via the fireEvent.change method', () => {
2828
expect(input.value).toBe('a')
2929
})
3030

31+
test('select drop-downs must use the fireEvent.change', () => {
32+
const handleChange = jest.fn()
33+
const { container } = render(<select onChange={handleChange}><option value="1">1</option><option value="2">2</option></select>)
34+
const select = container.firstChild;
35+
const option1 = container.getElementsByTagName("option").item(0);
36+
const option2 = container.getElementsByTagName("option").item(1);
37+
38+
fireEvent.change(select, {target: {value: "2"}});
39+
40+
expect(handleChange).toHaveBeenCalledTimes(1);
41+
expect(option1.selected).toBe(false);
42+
expect(option2.selected).toBe(true);
43+
})
44+
3145
test('checkboxes (and radios) must use fireEvent.click', () => {
3246
const handleChange = jest.fn()
3347
const { container } = render(

0 commit comments

Comments
 (0)