|
| 1 | +import { fireEvent, render, screen } from "solid-testing-library"; |
| 2 | + |
| 3 | +import { createInteractOutside } from "../src"; |
| 4 | +import { installPointerEvent } from "../src/test-utils"; |
| 5 | + |
| 6 | +function Example(props: any) { |
| 7 | + let ref: any; |
| 8 | + |
| 9 | + createInteractOutside(props, () => ref); |
| 10 | + |
| 11 | + return <div ref={ref}>test</div>; |
| 12 | +} |
| 13 | + |
| 14 | +function pointerEvent(type: any, opts?: any) { |
| 15 | + const evt = new Event(type, { bubbles: true, cancelable: true }); |
| 16 | + Object.assign(evt, opts); |
| 17 | + return evt; |
| 18 | +} |
| 19 | + |
| 20 | +describe("createInteractOutside", () => { |
| 21 | + // TODO: JSDOM doesn't yet support pointer events. Once they do, convert these tests. |
| 22 | + // https://github.com/jsdom/jsdom/issues/2527 |
| 23 | + describe("pointer events", () => { |
| 24 | + installPointerEvent(); |
| 25 | + |
| 26 | + it("should fire interact outside events based on pointer events", async () => { |
| 27 | + const onInteractOutside = jest.fn(); |
| 28 | + render(() => <Example onInteractOutside={onInteractOutside} />); |
| 29 | + |
| 30 | + const el = screen.getByText("test"); |
| 31 | + |
| 32 | + fireEvent(el, pointerEvent("pointerdown")); |
| 33 | + await Promise.resolve(); |
| 34 | + |
| 35 | + fireEvent(el, pointerEvent("pointerup")); |
| 36 | + await Promise.resolve(); |
| 37 | + |
| 38 | + expect(onInteractOutside).not.toHaveBeenCalled(); |
| 39 | + |
| 40 | + fireEvent(document.body, pointerEvent("pointerdown")); |
| 41 | + await Promise.resolve(); |
| 42 | + |
| 43 | + fireEvent(document.body, pointerEvent("pointerup")); |
| 44 | + await Promise.resolve(); |
| 45 | + |
| 46 | + expect(onInteractOutside).toHaveBeenCalledTimes(1); |
| 47 | + }); |
| 48 | + |
| 49 | + it("should only listen for the left mouse button", async () => { |
| 50 | + const onInteractOutside = jest.fn(); |
| 51 | + render(() => <Example onInteractOutside={onInteractOutside} />); |
| 52 | + |
| 53 | + fireEvent(document.body, pointerEvent("pointerdown", { button: 1 })); |
| 54 | + await Promise.resolve(); |
| 55 | + |
| 56 | + fireEvent(document.body, pointerEvent("pointerup", { button: 1 })); |
| 57 | + await Promise.resolve(); |
| 58 | + |
| 59 | + expect(onInteractOutside).not.toHaveBeenCalled(); |
| 60 | + |
| 61 | + fireEvent(document.body, pointerEvent("pointerdown", { button: 0 })); |
| 62 | + await Promise.resolve(); |
| 63 | + |
| 64 | + fireEvent(document.body, pointerEvent("pointerup", { button: 0 })); |
| 65 | + await Promise.resolve(); |
| 66 | + |
| 67 | + expect(onInteractOutside).toHaveBeenCalledTimes(1); |
| 68 | + }); |
| 69 | + |
| 70 | + it("should not fire interact outside if there is a pointer up event without a pointer down first", async () => { |
| 71 | + // Fire pointer down before component with useInteractOutside is mounted |
| 72 | + fireEvent(document.body, pointerEvent("pointerdown")); |
| 73 | + await Promise.resolve(); |
| 74 | + |
| 75 | + const onInteractOutside = jest.fn(); |
| 76 | + render(() => <Example onInteractOutside={onInteractOutside} />); |
| 77 | + |
| 78 | + fireEvent(document.body, pointerEvent("pointerup")); |
| 79 | + await Promise.resolve(); |
| 80 | + |
| 81 | + expect(onInteractOutside).not.toHaveBeenCalled(); |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + describe("mouse events", () => { |
| 86 | + it("should fire interact outside events based on mouse events", async () => { |
| 87 | + const onInteractOutside = jest.fn(); |
| 88 | + render(() => <Example onInteractOutside={onInteractOutside} />); |
| 89 | + |
| 90 | + const el = screen.getByText("test"); |
| 91 | + |
| 92 | + fireEvent.mouseDown(el); |
| 93 | + await Promise.resolve(); |
| 94 | + |
| 95 | + fireEvent.mouseUp(el); |
| 96 | + await Promise.resolve(); |
| 97 | + |
| 98 | + expect(onInteractOutside).not.toHaveBeenCalled(); |
| 99 | + |
| 100 | + fireEvent.mouseDown(document.body); |
| 101 | + await Promise.resolve(); |
| 102 | + |
| 103 | + fireEvent.mouseUp(document.body); |
| 104 | + await Promise.resolve(); |
| 105 | + |
| 106 | + expect(onInteractOutside).toHaveBeenCalledTimes(1); |
| 107 | + }); |
| 108 | + |
| 109 | + it("should only listen for the left mouse button", async () => { |
| 110 | + const onInteractOutside = jest.fn(); |
| 111 | + render(() => <Example onInteractOutside={onInteractOutside} />); |
| 112 | + |
| 113 | + fireEvent.mouseDown(document.body, { button: 1 }); |
| 114 | + await Promise.resolve(); |
| 115 | + |
| 116 | + fireEvent.mouseUp(document.body, { button: 1 }); |
| 117 | + await Promise.resolve(); |
| 118 | + |
| 119 | + expect(onInteractOutside).not.toHaveBeenCalled(); |
| 120 | + |
| 121 | + fireEvent.mouseDown(document.body, { button: 0 }); |
| 122 | + await Promise.resolve(); |
| 123 | + |
| 124 | + fireEvent.mouseUp(document.body, { button: 0 }); |
| 125 | + await Promise.resolve(); |
| 126 | + |
| 127 | + expect(onInteractOutside).toHaveBeenCalledTimes(1); |
| 128 | + }); |
| 129 | + |
| 130 | + it("should not fire interact outside if there is a mouse up event without a mouse down first", async () => { |
| 131 | + // Fire mouse down before component with useInteractOutside is mounted |
| 132 | + fireEvent.mouseDown(document.body); |
| 133 | + await Promise.resolve(); |
| 134 | + |
| 135 | + const onInteractOutside = jest.fn(); |
| 136 | + render(() => <Example onInteractOutside={onInteractOutside} />); |
| 137 | + |
| 138 | + fireEvent.mouseUp(document.body); |
| 139 | + await Promise.resolve(); |
| 140 | + |
| 141 | + expect(onInteractOutside).not.toHaveBeenCalled(); |
| 142 | + }); |
| 143 | + }); |
| 144 | + |
| 145 | + describe("touch events", () => { |
| 146 | + it("should fire interact outside events based on mouse events", async () => { |
| 147 | + const onInteractOutside = jest.fn(); |
| 148 | + render(() => <Example onInteractOutside={onInteractOutside} />); |
| 149 | + |
| 150 | + const el = screen.getByText("test"); |
| 151 | + |
| 152 | + fireEvent.touchStart(el); |
| 153 | + await Promise.resolve(); |
| 154 | + |
| 155 | + fireEvent.touchEnd(el); |
| 156 | + await Promise.resolve(); |
| 157 | + |
| 158 | + expect(onInteractOutside).not.toHaveBeenCalled(); |
| 159 | + |
| 160 | + fireEvent.touchStart(document.body); |
| 161 | + await Promise.resolve(); |
| 162 | + |
| 163 | + fireEvent.touchEnd(document.body); |
| 164 | + await Promise.resolve(); |
| 165 | + |
| 166 | + expect(onInteractOutside).toHaveBeenCalledTimes(1); |
| 167 | + }); |
| 168 | + |
| 169 | + it("should ignore emulated mouse events", async () => { |
| 170 | + const onInteractOutside = jest.fn(); |
| 171 | + render(() => <Example onInteractOutside={onInteractOutside} />); |
| 172 | + |
| 173 | + const el = screen.getByText("test"); |
| 174 | + |
| 175 | + fireEvent.touchStart(el); |
| 176 | + await Promise.resolve(); |
| 177 | + |
| 178 | + fireEvent.touchEnd(el); |
| 179 | + await Promise.resolve(); |
| 180 | + |
| 181 | + fireEvent.mouseUp(el); |
| 182 | + await Promise.resolve(); |
| 183 | + |
| 184 | + expect(onInteractOutside).not.toHaveBeenCalled(); |
| 185 | + |
| 186 | + fireEvent.touchStart(document.body); |
| 187 | + await Promise.resolve(); |
| 188 | + |
| 189 | + fireEvent.touchEnd(document.body); |
| 190 | + await Promise.resolve(); |
| 191 | + |
| 192 | + fireEvent.mouseUp(document.body); |
| 193 | + await Promise.resolve(); |
| 194 | + |
| 195 | + expect(onInteractOutside).toHaveBeenCalledTimes(1); |
| 196 | + }); |
| 197 | + |
| 198 | + it("should not fire interact outside if there is a touch end event without a touch start first", async () => { |
| 199 | + // Fire mouse down before component with useInteractOutside is mounted |
| 200 | + fireEvent.touchStart(document.body); |
| 201 | + await Promise.resolve(); |
| 202 | + |
| 203 | + const onInteractOutside = jest.fn(); |
| 204 | + render(() => <Example onInteractOutside={onInteractOutside} />); |
| 205 | + |
| 206 | + fireEvent.touchEnd(document.body); |
| 207 | + await Promise.resolve(); |
| 208 | + |
| 209 | + expect(onInteractOutside).not.toHaveBeenCalled(); |
| 210 | + }); |
| 211 | + }); |
| 212 | + |
| 213 | + describe("disable interact outside events", () => { |
| 214 | + it("does not handle pointer events if disabled", async () => { |
| 215 | + const onInteractOutside = jest.fn(); |
| 216 | + render(() => <Example isDisabled onInteractOutside={onInteractOutside} />); |
| 217 | + |
| 218 | + fireEvent(document.body, pointerEvent("mousedown")); |
| 219 | + await Promise.resolve(); |
| 220 | + |
| 221 | + fireEvent(document.body, pointerEvent("mouseup")); |
| 222 | + await Promise.resolve(); |
| 223 | + |
| 224 | + expect(onInteractOutside).not.toHaveBeenCalled(); |
| 225 | + }); |
| 226 | + |
| 227 | + it("does not handle touch events if disabled", async () => { |
| 228 | + const onInteractOutside = jest.fn(); |
| 229 | + render(() => <Example isDisabled onInteractOutside={onInteractOutside} />); |
| 230 | + |
| 231 | + fireEvent.touchStart(document.body); |
| 232 | + await Promise.resolve(); |
| 233 | + |
| 234 | + fireEvent.touchEnd(document.body); |
| 235 | + await Promise.resolve(); |
| 236 | + |
| 237 | + expect(onInteractOutside).not.toHaveBeenCalled(); |
| 238 | + }); |
| 239 | + |
| 240 | + it("does not handle mouse events if disabled", async () => { |
| 241 | + const onInteractOutside = jest.fn(); |
| 242 | + render(() => <Example isDisabled onInteractOutside={onInteractOutside} />); |
| 243 | + |
| 244 | + fireEvent.mouseDown(document.body); |
| 245 | + await Promise.resolve(); |
| 246 | + |
| 247 | + fireEvent.mouseUp(document.body); |
| 248 | + await Promise.resolve(); |
| 249 | + |
| 250 | + expect(onInteractOutside).not.toHaveBeenCalled(); |
| 251 | + }); |
| 252 | + }); |
| 253 | +}); |
0 commit comments