| 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 | |
| 8 | 'use strict'; |
| 9 | |
| 10 | describe('SyntheticFocusEvent', () => { |
| 11 | let React; |
| 12 | let ReactDOMClient; |
| 13 | let act; |
| 14 | let container; |
| 15 | |
| 16 | beforeEach(() => { |
| 17 | jest.resetModules(); |
| 18 | React = require('react'); |
| 19 | ReactDOMClient = require('react-dom/client'); |
| 20 | act = require('internal-test-utils').act; |
| 21 | |
| 22 | container = document.createElement('div'); |
| 23 | document.body.appendChild(container); |
| 24 | }); |
| 25 | |
| 26 | afterEach(() => { |
| 27 | document.body.removeChild(container); |
| 28 | container = null; |
| 29 | }); |
| 30 | |
| 31 | it('onFocus events have the focus type', async () => { |
| 32 | const log = []; |
| 33 | const root = ReactDOMClient.createRoot(container); |
| 34 | await act(() => { |
| 35 | root.render( |
| 36 | <button |
| 37 | onFocus={event => log.push(`onFocus: ${event.type}`)} |
| 38 | onFocusCapture={event => log.push(`onFocusCapture: ${event.type}`)} |
| 39 | />, |
| 40 | ); |
| 41 | }); |
| 42 | |
| 43 | const button = container.querySelector('button'); |
| 44 | |
| 45 | await act(() => { |
| 46 | button.dispatchEvent( |
| 47 | new FocusEvent('focusin', { |
| 48 | bubbles: true, |
| 49 | cancelable: false, |
| 50 | }), |
| 51 | ); |
| 52 | }); |
| 53 | |
| 54 | expect(log).toEqual(['onFocusCapture: focus', 'onFocus: focus']); |
| 55 | }); |
| 56 | |
| 57 | it('onBlur events have the blur type', async () => { |
| 58 | const log = []; |
| 59 | const root = ReactDOMClient.createRoot(container); |
| 60 | await act(() => { |
| 61 | root.render( |
| 62 | <button |
| 63 | onBlur={event => log.push(`onBlur: ${event.type}`)} |
| 64 | onBlurCapture={event => log.push(`onBlurCapture: ${event.type}`)} |
| 65 | />, |
| 66 | ); |
| 67 | }); |
| 68 | |
| 69 | const button = container.querySelector('button'); |
| 70 | |
| 71 | await act(() => { |
| 72 | button.dispatchEvent( |
| 73 | new FocusEvent('focusout', { |
| 74 | bubbles: true, |
| 75 | cancelable: false, |
| 76 | }), |
| 77 | ); |
| 78 | }); |
| 79 | |
| 80 | expect(log).toEqual(['onBlurCapture: blur', 'onBlur: blur']); |
| 81 | }); |
| 82 | }); |