|
| 1 | +import React from "react"; |
| 2 | +import { render, cleanup, fireEvent } from "@testing-library/react"; |
| 3 | +import "jest-dom/extend-expect"; |
| 4 | +import userEvent from "../src"; |
| 5 | + |
| 6 | +afterEach(cleanup); |
| 7 | + |
| 8 | +describe("userEvent.selectOptions", () => { |
| 9 | + it.each(["select", "select multiple"])( |
| 10 | + "should fire the correct events for <%s>", |
| 11 | + type => { |
| 12 | + const events = []; |
| 13 | + const eventsHandler = jest.fn(evt => events.push(evt.type)); |
| 14 | + const multiple = type === "select multiple"; |
| 15 | + const eventHandlers = { |
| 16 | + onMouseOver: eventsHandler, |
| 17 | + onMouseMove: eventsHandler, |
| 18 | + onMouseDown: eventsHandler, |
| 19 | + onFocus: eventsHandler, |
| 20 | + onMouseUp: eventsHandler, |
| 21 | + onClick: eventsHandler |
| 22 | + }; |
| 23 | + |
| 24 | + const { getByTestId } = render( |
| 25 | + <select {...{ ...eventHandlers, multiple }} data-testid="element"> |
| 26 | + <option value="1">1</option> |
| 27 | + <option value="2">2</option> |
| 28 | + <option value="3">3</option> |
| 29 | + </select> |
| 30 | + ); |
| 31 | + |
| 32 | + userEvent.selectOptions(getByTestId("element"), "1"); |
| 33 | + |
| 34 | + expect(events).toEqual([ |
| 35 | + "mouseover", |
| 36 | + "mousemove", |
| 37 | + "mousedown", |
| 38 | + "focus", |
| 39 | + "mouseup", |
| 40 | + "click", |
| 41 | + "mouseover", // The events repeat because we click on the child OPTION too |
| 42 | + "mousemove", // But these specifically are the events bubbling up to the <select> |
| 43 | + "mousedown", |
| 44 | + "focus", |
| 45 | + "mouseup", |
| 46 | + "click" |
| 47 | + ]); |
| 48 | + } |
| 49 | + ); |
| 50 | + |
| 51 | + it("should fire the correct events on selected OPTION child with <select>", () => { |
| 52 | + function handleEvent(evt) { |
| 53 | + const optValue = parseInt(evt.target.value); |
| 54 | + events[optValue] = [...(events[optValue] || []), evt.type]; |
| 55 | + } |
| 56 | + |
| 57 | + const events = []; |
| 58 | + const eventsHandler = jest.fn(handleEvent); |
| 59 | + const eventHandlers = { |
| 60 | + onMouseOver: eventsHandler, |
| 61 | + onMouseMove: eventsHandler, |
| 62 | + onMouseDown: eventsHandler, |
| 63 | + onFocus: eventsHandler, |
| 64 | + onMouseUp: eventsHandler, |
| 65 | + onClick: eventsHandler |
| 66 | + }; |
| 67 | + |
| 68 | + const { getByTestId } = render( |
| 69 | + <select data-testid="element"> |
| 70 | + <option {...eventHandlers} value="1"> |
| 71 | + 1 |
| 72 | + </option> |
| 73 | + <option {...eventHandlers} value="2"> |
| 74 | + 2 |
| 75 | + </option> |
| 76 | + <option {...eventHandlers} value="3"> |
| 77 | + 3 |
| 78 | + </option> |
| 79 | + </select> |
| 80 | + ); |
| 81 | + |
| 82 | + userEvent.selectOptions(getByTestId("element"), ["2"]); |
| 83 | + |
| 84 | + expect(events[1]).toBe(undefined); |
| 85 | + expect(events[3]).toBe(undefined); |
| 86 | + expect(events[2]).toEqual([ |
| 87 | + "mouseover", |
| 88 | + "mousemove", |
| 89 | + "mousedown", |
| 90 | + "focus", |
| 91 | + "mouseup", |
| 92 | + "click" |
| 93 | + ]); |
| 94 | + }); |
| 95 | + |
| 96 | + it("should fire the correct events on selected OPTION children with <select multiple>", () => { |
| 97 | + function handleEvent(evt) { |
| 98 | + const optValue = parseInt(evt.target.value); |
| 99 | + events[optValue] = [...(events[optValue] || []), evt.type]; |
| 100 | + } |
| 101 | + |
| 102 | + const events = []; |
| 103 | + const eventsHandler = jest.fn(handleEvent); |
| 104 | + const eventHandlers = { |
| 105 | + onMouseOver: eventsHandler, |
| 106 | + onMouseMove: eventsHandler, |
| 107 | + onMouseDown: eventsHandler, |
| 108 | + onFocus: eventsHandler, |
| 109 | + onMouseUp: eventsHandler, |
| 110 | + onClick: eventsHandler |
| 111 | + }; |
| 112 | + |
| 113 | + const { getByTestId } = render( |
| 114 | + <select multiple data-testid="element"> |
| 115 | + <option {...eventHandlers} value="1"> |
| 116 | + 1 |
| 117 | + </option> |
| 118 | + <option {...eventHandlers} value="2"> |
| 119 | + 2 |
| 120 | + </option> |
| 121 | + <option {...eventHandlers} value="3"> |
| 122 | + 3 |
| 123 | + </option> |
| 124 | + </select> |
| 125 | + ); |
| 126 | + |
| 127 | + userEvent.selectOptions(getByTestId("element"), ["1", "3"]); |
| 128 | + |
| 129 | + expect(events[2]).toBe(undefined); |
| 130 | + expect(events[1]).toEqual([ |
| 131 | + "mouseover", |
| 132 | + "mousemove", |
| 133 | + "mousedown", |
| 134 | + "focus", |
| 135 | + "mouseup", |
| 136 | + "click" |
| 137 | + ]); |
| 138 | + |
| 139 | + expect(events[3]).toEqual([ |
| 140 | + "mouseover", |
| 141 | + "mousemove", |
| 142 | + "mousedown", |
| 143 | + "focus", |
| 144 | + "mouseup", |
| 145 | + "click" |
| 146 | + ]); |
| 147 | + }); |
| 148 | + |
| 149 | + it("sets the selected prop on the selected OPTION", () => { |
| 150 | + const onSubmit = jest.fn(); |
| 151 | + |
| 152 | + const { getByTestId } = render( |
| 153 | + <form onSubmit={onSubmit}> |
| 154 | + <select multiple data-testid="element"> |
| 155 | + <option data-testid="val1" value="1"> |
| 156 | + 1 |
| 157 | + </option> |
| 158 | + <option data-testid="val2" value="2"> |
| 159 | + 2 |
| 160 | + </option> |
| 161 | + <option data-testid="val3" value="3"> |
| 162 | + 3 |
| 163 | + </option> |
| 164 | + </select> |
| 165 | + </form> |
| 166 | + ); |
| 167 | + |
| 168 | + userEvent.selectOptions(getByTestId("element"), ["1", "3"]); |
| 169 | + |
| 170 | + expect(getByTestId("val1").selected).toBe(true); |
| 171 | + expect(getByTestId("val2").selected).toBe(false); |
| 172 | + expect(getByTestId("val3").selected).toBe(true); |
| 173 | + }); |
| 174 | +}); |
0 commit comments