| 1 | /** |
| 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | * |
| 4 | * This source code is licensed under the MIT license found in the |
| 5 | * LICENSE file in the root directory of this source tree. |
| 6 | * |
| 7 | * @emails react-core |
| 8 | */ |
| 9 | |
| 10 | 'use strict'; |
| 11 | |
| 12 | let React; |
| 13 | let ReactDOMClient; |
| 14 | let act; |
| 15 | |
| 16 | describe('SyntheticWheelEvent', () => { |
| 17 | let container; |
| 18 | let root; |
| 19 | |
| 20 | beforeEach(() => { |
| 21 | React = require('react'); |
| 22 | ReactDOMClient = require('react-dom/client'); |
| 23 | act = require('internal-test-utils').act; |
| 24 | |
| 25 | // The container has to be attached for events to fire. |
| 26 | container = document.createElement('div'); |
| 27 | document.body.appendChild(container); |
| 28 | root = ReactDOMClient.createRoot(container); |
| 29 | }); |
| 30 | |
| 31 | afterEach(() => { |
| 32 | document.body.removeChild(container); |
| 33 | container = null; |
| 34 | }); |
| 35 | |
| 36 | it('should normalize properties from the MouseEvent interface', async () => { |
| 37 | const events = []; |
| 38 | const onWheel = event => { |
| 39 | event.persist(); |
| 40 | events.push(event); |
| 41 | }; |
| 42 | await act(async () => { |
| 43 | root.render(<div onWheel={onWheel} />); |
| 44 | }); |
| 45 | |
| 46 | container.firstChild.dispatchEvent( |
| 47 | new MouseEvent('wheel', { |
| 48 | bubbles: true, |
| 49 | button: 1, |
| 50 | }), |
| 51 | ); |
| 52 | |
| 53 | expect(events.length).toBe(1); |
| 54 | expect(events[0].button).toBe(1); |
| 55 | }); |
| 56 | |
| 57 | it('should normalize properties from the WheelEvent interface', async () => { |
| 58 | const events = []; |
| 59 | const onWheel = event => { |
| 60 | event.persist(); |
| 61 | events.push(event); |
| 62 | }; |
| 63 | |
| 64 | await act(async () => { |
| 65 | root.render(<div onWheel={onWheel} />); |
| 66 | }); |
| 67 | |
| 68 | let event = new WheelEvent('wheel', { |
| 69 | bubbles: true, |
| 70 | deltaX: 10, |
| 71 | deltaY: -50, |
| 72 | }); |
| 73 | container.firstChild.dispatchEvent(event); |
| 74 | |
| 75 | event = new MouseEvent('wheel', { |
| 76 | bubbles: true, |
| 77 | }); |
| 78 | // jsdom doesn't support these legacy Webkit properties so we add them manually. |
| 79 | Object.assign(event, { |
| 80 | wheelDeltaX: -10, |
| 81 | wheelDeltaY: 50, |
| 82 | }); |
| 83 | container.firstChild.dispatchEvent(event); |
| 84 | |
| 85 | expect(events.length).toBe(2); |
| 86 | expect(events[0].deltaX).toBe(10); |
| 87 | expect(events[0].deltaY).toBe(-50); |
| 88 | expect(events[1].deltaX).toBe(10); |
| 89 | expect(events[1].deltaY).toBe(-50); |
| 90 | }); |
| 91 | |
| 92 | it('should be able to `preventDefault` and `stopPropagation`', async () => { |
| 93 | const events = []; |
| 94 | const onWheel = event => { |
| 95 | expect(event.isDefaultPrevented()).toBe(false); |
| 96 | event.preventDefault(); |
| 97 | expect(event.isDefaultPrevented()).toBe(true); |
| 98 | event.persist(); |
| 99 | events.push(event); |
| 100 | }; |
| 101 | await act(async () => { |
| 102 | root.render(<div onWheel={onWheel} />); |
| 103 | }); |
| 104 | |
| 105 | container.firstChild.dispatchEvent( |
| 106 | new WheelEvent('wheel', { |
| 107 | bubbles: true, |
| 108 | deltaX: 10, |
| 109 | deltaY: -50, |
| 110 | }), |
| 111 | ); |
| 112 | |
| 113 | container.firstChild.dispatchEvent( |
| 114 | new WheelEvent('wheel', { |
| 115 | bubbles: true, |
| 116 | deltaX: 10, |
| 117 | deltaY: -50, |
| 118 | }), |
| 119 | ); |
| 120 | |
| 121 | expect(events.length).toBe(2); |
| 122 | expect.assertions(5); |
| 123 | }); |
| 124 | }); |