Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert dangerouslySetInnerHTML-test to createRoot #28008

Merged
merged 1 commit into from
Jan 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,21 @@
'use strict';

const React = require('react');
const ReactDOM = require('react-dom');
const ReactDOMClient = require('react-dom/client');

const act = require('internal-test-utils').act;

describe('dangerouslySetInnerHTML', () => {
describe('when the node has innerHTML property', () => {
it('sets innerHTML on it', () => {
it('sets innerHTML on it', async () => {
const container = document.createElement('div');
const node = ReactDOM.render(
<div dangerouslySetInnerHTML={{__html: '<h1>Hello</h1>'}} />,
container,
);
expect(node.innerHTML).toBe('<h1>Hello</h1>');
const root = ReactDOMClient.createRoot(container);
await act(() => {
root.render(
<div dangerouslySetInnerHTML={{__html: '<h1>Hello</h1>'}} />,
);
});
expect(container.firstChild.innerHTML).toBe('<h1>Hello</h1>');
});
});

Expand Down Expand Up @@ -56,39 +60,38 @@ describe('dangerouslySetInnerHTML', () => {
});

// @gate !disableIEWorkarounds
it('sets innerHTML on it', () => {
it('sets innerHTML on it', async () => {
const html = '<circle></circle>';
const container = document.createElementNS(
'http://www.w3.org/2000/svg',
'svg',
);
ReactDOM.render(
<g dangerouslySetInnerHTML={{__html: html}} />,
container,
);
const root = ReactDOMClient.createRoot(container);
await act(() => {
root.render(<g dangerouslySetInnerHTML={{__html: html}} />);
});
const circle = container.firstChild.firstChild;
expect(circle.tagName).toBe('circle');
});

// @gate !disableIEWorkarounds
it('clears previous children', () => {
it('clears previous children', async () => {
const firstHtml = '<rect></rect>';
const secondHtml = '<circle></circle>';

const container = document.createElementNS(
'http://www.w3.org/2000/svg',
'svg',
);
ReactDOM.render(
<g dangerouslySetInnerHTML={{__html: firstHtml}} />,
container,
);
const root = ReactDOMClient.createRoot(container);
await act(() => {
root.render(<g dangerouslySetInnerHTML={{__html: firstHtml}} />);
});
const rect = container.firstChild.firstChild;
expect(rect.tagName).toBe('rect');
ReactDOM.render(
<g dangerouslySetInnerHTML={{__html: secondHtml}} />,
container,
);
await act(() => {
root.render(<g dangerouslySetInnerHTML={{__html: secondHtml}} />);
});
const circle = container.firstChild.firstChild;
expect(circle.tagName).toBe('circle');
});
Expand Down