Skip to content

Commit

Permalink
Convert SyntheticFocusEvent to createRoot
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Silbermann committed Jan 31, 2024
1 parent e7aac1f commit 49aa976
Showing 1 changed file with 42 additions and 30 deletions.
72 changes: 42 additions & 30 deletions packages/react-dom/src/events/__tests__/SyntheticFocusEvent-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@

describe('SyntheticFocusEvent', () => {
let React;
let ReactDOM;
let ReactDOMClient;
let act;
let container;

beforeEach(() => {
jest.resetModules();
React = require('react');
ReactDOM = require('react-dom');
ReactDOMClient = require('react-dom/client');
act = require('internal-test-utils').act;

container = document.createElement('div');
document.body.appendChild(container);
Expand All @@ -26,44 +28,54 @@ describe('SyntheticFocusEvent', () => {
container = null;
});

test('onFocus events have the focus type', () => {
test('onFocus events have the focus type', async () => {
const log = [];
ReactDOM.render(
<button
onFocus={event => log.push(`onFocus: ${event.type}`)}
onFocusCapture={event => log.push(`onFocusCapture: ${event.type}`)}
/>,
container,
);
const root = ReactDOMClient.createRoot(container);
await act(() => {
root.render(
<button
onFocus={event => log.push(`onFocus: ${event.type}`)}
onFocusCapture={event => log.push(`onFocusCapture: ${event.type}`)}
/>,
);
});

const button = container.querySelector('button');

button.dispatchEvent(
new FocusEvent('focusin', {
bubbles: true,
cancelable: false,
}),
);
await act(() => {
button.dispatchEvent(
new FocusEvent('focusin', {
bubbles: true,
cancelable: false,
}),
);
});

expect(log).toEqual(['onFocusCapture: focus', 'onFocus: focus']);
});

test('onBlur events have the blur type', () => {
test('onBlur events have the blur type', async () => {
const log = [];
ReactDOM.render(
<button
onBlur={event => log.push(`onBlur: ${event.type}`)}
onBlurCapture={event => log.push(`onBlurCapture: ${event.type}`)}
/>,
container,
);
const root = ReactDOMClient.createRoot(container);
await act(() => {
root.render(
<button
onBlur={event => log.push(`onBlur: ${event.type}`)}
onBlurCapture={event => log.push(`onBlurCapture: ${event.type}`)}
/>,
);
});

const button = container.querySelector('button');

button.dispatchEvent(
new FocusEvent('focusout', {
bubbles: true,
cancelable: false,
}),
);
await act(() => {
button.dispatchEvent(
new FocusEvent('focusout', {
bubbles: true,
cancelable: false,
}),
);
});

expect(log).toEqual(['onBlurCapture: blur', 'onBlur: blur']);
});
Expand Down

0 comments on commit 49aa976

Please sign in to comment.