|
10 | 10 | * governing permissions and limitations under the License.
|
11 | 11 | */
|
12 | 12 |
|
13 |
| -import {fireEvent, installPointerEvent, render} from '@react-spectrum/test-utils'; |
| 13 | +import {fireEvent, installPointerEvent, render, waitFor} from '@react-spectrum/test-utils'; |
14 | 14 | import React, {useRef} from 'react';
|
| 15 | +import {render as ReactDOMRender} from 'react-dom'; |
15 | 16 | import {useInteractOutside} from '../';
|
16 | 17 |
|
17 | 18 | function Example(props) {
|
18 | 19 | let ref = useRef();
|
19 | 20 | useInteractOutside({ref, ...props});
|
20 |
| - return <div ref={ref}>test</div>; |
| 21 | + return <div ref={ref} data-testid="example">test</div>; |
21 | 22 | }
|
22 | 23 |
|
23 | 24 | function pointerEvent(type, opts) {
|
@@ -206,3 +207,238 @@ describe('useInteractOutside', function () {
|
206 | 207 | });
|
207 | 208 | });
|
208 | 209 | });
|
| 210 | + |
| 211 | +describe('useInteractOutside (iframes)', function () { |
| 212 | + let iframe; |
| 213 | + let iframeRoot; |
| 214 | + let iframeDocument; |
| 215 | + beforeEach(() => { |
| 216 | + iframe = document.createElement('iframe'); |
| 217 | + window.document.body.appendChild(iframe); |
| 218 | + iframeDocument = iframe.contentWindow.document; |
| 219 | + iframeRoot = iframeDocument.createElement('div'); |
| 220 | + iframeDocument.body.appendChild(iframeRoot); |
| 221 | + }); |
| 222 | + |
| 223 | + afterEach(() => { |
| 224 | + iframe.remove(); |
| 225 | + }); |
| 226 | + |
| 227 | + const IframeExample = (props) => { |
| 228 | + React.useEffect(() => { |
| 229 | + ReactDOMRender(<Example {...props} />, iframeRoot); |
| 230 | + }, [props]); |
| 231 | + |
| 232 | + return null; |
| 233 | + }; |
| 234 | + |
| 235 | + // TODO: JSDOM doesn't yet support pointer events. Once they do, convert these tests. |
| 236 | + // https://github.com/jsdom/jsdom/issues/2527 |
| 237 | + describe('pointer events', function () { |
| 238 | + installPointerEvent(); |
| 239 | + |
| 240 | + it('should fire interact outside events based on pointer events', async function () { |
| 241 | + let onInteractOutside = jest.fn(); |
| 242 | + render( |
| 243 | + <IframeExample onInteractOutside={onInteractOutside} /> |
| 244 | + ); |
| 245 | + |
| 246 | + await waitFor(() => { |
| 247 | + expect(document.querySelector('iframe').contentWindow.document.body.querySelector('div[data-testid="example"]')).toBeTruthy(); |
| 248 | + }); |
| 249 | + |
| 250 | + const el = document.querySelector('iframe').contentWindow.document.body.querySelector('div[data-testid="example"]'); |
| 251 | + fireEvent(el, pointerEvent('pointerdown')); |
| 252 | + fireEvent(el, pointerEvent('pointerup')); |
| 253 | + expect(onInteractOutside).not.toHaveBeenCalled(); |
| 254 | + |
| 255 | + fireEvent(iframeDocument.body, pointerEvent('pointerdown')); |
| 256 | + fireEvent(iframeDocument.body, pointerEvent('pointerup')); |
| 257 | + expect(onInteractOutside).toHaveBeenCalledTimes(1); |
| 258 | + }); |
| 259 | + |
| 260 | + it('should only listen for the left mouse button', async function () { |
| 261 | + let onInteractOutside = jest.fn(); |
| 262 | + render( |
| 263 | + <IframeExample onInteractOutside={onInteractOutside} /> |
| 264 | + ); |
| 265 | + |
| 266 | + await waitFor(() => { |
| 267 | + expect(document.querySelector('iframe').contentWindow.document.body.querySelector('div[data-testid="example"]')).toBeTruthy(); |
| 268 | + }); |
| 269 | + |
| 270 | + fireEvent(iframeDocument.body, pointerEvent('pointerdown', {button: 1})); |
| 271 | + fireEvent(iframeDocument.body, pointerEvent('pointerup', {button: 1})); |
| 272 | + expect(onInteractOutside).not.toHaveBeenCalled(); |
| 273 | + |
| 274 | + fireEvent(iframeDocument.body, pointerEvent('pointerdown', {button: 0})); |
| 275 | + fireEvent(iframeDocument.body, pointerEvent('pointerup', {button: 0})); |
| 276 | + expect(onInteractOutside).toHaveBeenCalledTimes(1); |
| 277 | + }); |
| 278 | + |
| 279 | + it('should not fire interact outside if there is a pointer up event without a pointer down first', async function () { |
| 280 | + // Fire pointer down before component with useInteractOutside is mounted |
| 281 | + fireEvent(iframeDocument.body, pointerEvent('pointerdown')); |
| 282 | + |
| 283 | + let onInteractOutside = jest.fn(); |
| 284 | + render( |
| 285 | + <IframeExample onInteractOutside={onInteractOutside} /> |
| 286 | + ); |
| 287 | + |
| 288 | + await waitFor(() => { |
| 289 | + expect(document.querySelector('iframe').contentWindow.document.body.querySelector('div[data-testid="example"]')).toBeTruthy(); |
| 290 | + }); |
| 291 | + fireEvent(iframeDocument.body, pointerEvent('pointerup')); |
| 292 | + expect(onInteractOutside).not.toHaveBeenCalled(); |
| 293 | + }); |
| 294 | + }); |
| 295 | + |
| 296 | + describe('mouse events', function () { |
| 297 | + it('should fire interact outside events based on mouse events', async function () { |
| 298 | + let onInteractOutside = jest.fn(); |
| 299 | + render( |
| 300 | + <IframeExample onInteractOutside={onInteractOutside} /> |
| 301 | + ); |
| 302 | + |
| 303 | + await waitFor(() => { |
| 304 | + expect(document.querySelector('iframe').contentWindow.document.body.querySelector('div[data-testid="example"]')).toBeTruthy(); |
| 305 | + }); |
| 306 | + |
| 307 | + const el = document.querySelector('iframe').contentWindow.document.body.querySelector('div[data-testid="example"]'); |
| 308 | + fireEvent.mouseDown(el); |
| 309 | + fireEvent.mouseUp(el); |
| 310 | + expect(onInteractOutside).not.toHaveBeenCalled(); |
| 311 | + |
| 312 | + fireEvent.mouseDown(iframeDocument.body); |
| 313 | + fireEvent.mouseUp(iframeDocument.body); |
| 314 | + expect(onInteractOutside).toHaveBeenCalledTimes(1); |
| 315 | + }); |
| 316 | + |
| 317 | + it('should only listen for the left mouse button', async function () { |
| 318 | + let onInteractOutside = jest.fn(); |
| 319 | + render( |
| 320 | + <IframeExample onInteractOutside={onInteractOutside} /> |
| 321 | + ); |
| 322 | + |
| 323 | + await waitFor(() => { |
| 324 | + expect(document.querySelector('iframe').contentWindow.document.body.querySelector('div[data-testid="example"]')).toBeTruthy(); |
| 325 | + }); |
| 326 | + |
| 327 | + fireEvent.mouseDown(iframeDocument.body, {button: 1}); |
| 328 | + fireEvent.mouseUp(iframeDocument.body, {button: 1}); |
| 329 | + expect(onInteractOutside).not.toHaveBeenCalled(); |
| 330 | + |
| 331 | + fireEvent.mouseDown(iframeDocument.body, {button: 0}); |
| 332 | + fireEvent.mouseUp(iframeDocument.body, {button: 0}); |
| 333 | + expect(onInteractOutside).toHaveBeenCalledTimes(1); |
| 334 | + }); |
| 335 | + |
| 336 | + it('should not fire interact outside if there is a mouse up event without a mouse down first', async function () { |
| 337 | + // Fire mouse down before component with useInteractOutside is mounted |
| 338 | + fireEvent.mouseDown(iframeDocument.body); |
| 339 | + |
| 340 | + let onInteractOutside = jest.fn(); |
| 341 | + render( |
| 342 | + <IframeExample onInteractOutside={onInteractOutside} /> |
| 343 | + ); |
| 344 | + |
| 345 | + await waitFor(() => { |
| 346 | + expect(document.querySelector('iframe').contentWindow.document.body.querySelector('div[data-testid="example"]')).toBeTruthy(); |
| 347 | + }); |
| 348 | + fireEvent.mouseUp(iframeDocument.body); |
| 349 | + expect(onInteractOutside).not.toHaveBeenCalled(); |
| 350 | + }); |
| 351 | + }); |
| 352 | + |
| 353 | + describe('touch events', function () { |
| 354 | + it('should fire interact outside events based on mouse events', async function () { |
| 355 | + let onInteractOutside = jest.fn(); |
| 356 | + render( |
| 357 | + <IframeExample onInteractOutside={onInteractOutside} /> |
| 358 | + ); |
| 359 | + |
| 360 | + await waitFor(() => { |
| 361 | + expect(document.querySelector('iframe').contentWindow.document.body.querySelector('div[data-testid="example"]')).toBeTruthy(); |
| 362 | + }); |
| 363 | + |
| 364 | + const el = document.querySelector('iframe').contentWindow.document.body.querySelector('div[data-testid="example"]'); |
| 365 | + fireEvent.touchStart(el); |
| 366 | + fireEvent.touchEnd(el); |
| 367 | + expect(onInteractOutside).not.toHaveBeenCalled(); |
| 368 | + |
| 369 | + fireEvent.touchStart(iframeDocument.body); |
| 370 | + fireEvent.touchEnd(iframeDocument.body); |
| 371 | + expect(onInteractOutside).toHaveBeenCalledTimes(1); |
| 372 | + }); |
| 373 | + |
| 374 | + it('should ignore emulated mouse events', async function () { |
| 375 | + let onInteractOutside = jest.fn(); |
| 376 | + render( |
| 377 | + <IframeExample onInteractOutside={onInteractOutside} /> |
| 378 | + ); |
| 379 | + |
| 380 | + await waitFor(() => { |
| 381 | + expect(document.querySelector('iframe').contentWindow.document.body.querySelector('div[data-testid="example"]')).toBeTruthy(); |
| 382 | + }); |
| 383 | + |
| 384 | + const el = document.querySelector('iframe').contentWindow.document.body.querySelector('div[data-testid="example"]'); |
| 385 | + fireEvent.touchStart(el); |
| 386 | + fireEvent.touchEnd(el); |
| 387 | + fireEvent.mouseUp(el); |
| 388 | + expect(onInteractOutside).not.toHaveBeenCalled(); |
| 389 | + |
| 390 | + fireEvent.touchStart(iframeDocument.body); |
| 391 | + fireEvent.touchEnd(iframeDocument.body); |
| 392 | + fireEvent.mouseUp(iframeDocument.body); |
| 393 | + expect(onInteractOutside).toHaveBeenCalledTimes(1); |
| 394 | + }); |
| 395 | + |
| 396 | + it('should not fire interact outside if there is a touch end event without a touch start first', function () { |
| 397 | + // Fire mouse down before component with useInteractOutside is mounted |
| 398 | + fireEvent.touchStart(iframeDocument.body); |
| 399 | + |
| 400 | + let onInteractOutside = jest.fn(); |
| 401 | + render( |
| 402 | + <IframeExample onInteractOutside={onInteractOutside} /> |
| 403 | + ); |
| 404 | + |
| 405 | + fireEvent.touchEnd(iframeDocument.body); |
| 406 | + expect(onInteractOutside).not.toHaveBeenCalled(); |
| 407 | + }); |
| 408 | + }); |
| 409 | + |
| 410 | + describe('disable interact outside events', function () { |
| 411 | + it('does not handle pointer events if disabled', function () { |
| 412 | + let onInteractOutside = jest.fn(); |
| 413 | + render( |
| 414 | + <IframeExample isDisabled onInteractOutside={onInteractOutside} /> |
| 415 | + ); |
| 416 | + |
| 417 | + fireEvent(iframeDocument.body, pointerEvent('mousedown')); |
| 418 | + fireEvent(iframeDocument.body, pointerEvent('mouseup')); |
| 419 | + expect(onInteractOutside).not.toHaveBeenCalled(); |
| 420 | + }); |
| 421 | + |
| 422 | + it('does not handle touch events if disabled', function () { |
| 423 | + let onInteractOutside = jest.fn(); |
| 424 | + render( |
| 425 | + <IframeExample isDisabled onInteractOutside={onInteractOutside} /> |
| 426 | + ); |
| 427 | + |
| 428 | + fireEvent.touchStart(iframeDocument.body); |
| 429 | + fireEvent.touchEnd(iframeDocument.body); |
| 430 | + expect(onInteractOutside).not.toHaveBeenCalled(); |
| 431 | + }); |
| 432 | + |
| 433 | + it('does not handle mouse events if disabled', function () { |
| 434 | + let onInteractOutside = jest.fn(); |
| 435 | + render( |
| 436 | + <IframeExample isDisabled onInteractOutside={onInteractOutside} /> |
| 437 | + ); |
| 438 | + |
| 439 | + fireEvent.mouseDown(iframeDocument.body); |
| 440 | + fireEvent.mouseUp(iframeDocument.body); |
| 441 | + expect(onInteractOutside).not.toHaveBeenCalled(); |
| 442 | + }); |
| 443 | + }); |
| 444 | +}); |
0 commit comments