From 7c79dafb71cab8808749e3284eab3c623e9c2360 Mon Sep 17 00:00:00 2001 From: Matt Carroll <7158882+mattcarrollcode@users.noreply.github.com> Date: Fri, 26 Jan 2024 12:18:14 -0800 Subject: [PATCH] Convert ReactDOMServerIntegrationUntrustedURL-test.js to createRoot (#28105) --- ...ctDOMServerIntegrationUntrustedURL-test.js | 45 ++++++++++++++----- 1 file changed, 34 insertions(+), 11 deletions(-) diff --git a/packages/react-dom/src/__tests__/ReactDOMServerIntegrationUntrustedURL-test.js b/packages/react-dom/src/__tests__/ReactDOMServerIntegrationUntrustedURL-test.js index ef232d76610cd..121ffe93ad25a 100644 --- a/packages/react-dom/src/__tests__/ReactDOMServerIntegrationUntrustedURL-test.js +++ b/packages/react-dom/src/__tests__/ReactDOMServerIntegrationUntrustedURL-test.js @@ -16,8 +16,10 @@ const ReactDOMServerIntegrationUtils = require('./utils/ReactDOMServerIntegratio let React; let ReactDOM; +let ReactDOMClient; let ReactDOMServer; let ReactTestUtils; +let act; const EXPECTED_SAFE_URL = "javascript:throw new Error('React has blocked a javascript: URL as a security precaution.')"; @@ -34,8 +36,10 @@ describe('ReactDOMServerIntegration - Untrusted URLs', () => { jest.resetModules(); React = require('react'); ReactDOM = require('react-dom'); + ReactDOMClient = require('react-dom/client'); ReactDOMServer = require('react-dom/server'); ReactTestUtils = require('react-dom/test-utils'); + act = require('internal-test-utils').act; // Make them available to the helpers. return { @@ -167,11 +171,16 @@ describe('ReactDOMServerIntegration - Untrusted URLs', () => { }, ); - it('rejects a javascript protocol href if it is added during an update', () => { + it('rejects a javascript protocol href if it is added during an update', async () => { const container = document.createElement('div'); - ReactDOM.render(click me, container); - expect(() => { - ReactDOM.render(click me, container); + const root = ReactDOMClient.createRoot(container); + await act(async () => { + root.render(click me); + }); + await expect(async () => { + await act(() => { + root.render(click me); + }); }).toErrorDev( 'Warning: A future version of React will block javascript: URLs as a security precaution. ' + 'Use event handlers instead if you can. If you need to generate unsafe HTML try using ' + @@ -196,8 +205,10 @@ describe('ReactDOMServerIntegration - Untrusted URLs - disableJavaScriptURLs', ( React = require('react'); ReactDOM = require('react-dom'); + ReactDOMClient = require('react-dom/client'); ReactDOMServer = require('react-dom/server'); ReactTestUtils = require('react-dom/test-utils'); + act = require('internal-test-utils').act; // Make them available to the helpers. return { @@ -325,11 +336,16 @@ describe('ReactDOMServerIntegration - Untrusted URLs - disableJavaScriptURLs', ( }, ); - it('rejects a javascript protocol href if it is added during an update', () => { + it('rejects a javascript protocol href if it is added during an update', async () => { const container = document.createElement('div'); - ReactDOM.render(click me, container); + const root = ReactDOMClient.createRoot(container); + await act(() => { + root.render(click me); + }); expect(container.firstChild.href).toBe('http://thisisfine/'); - ReactDOM.render(click me, container); + await act(() => { + root.render(click me); + }); expect(container.firstChild.href).toBe(EXPECTED_SAFE_URL); }); @@ -369,15 +385,22 @@ describe('ReactDOMServerIntegration - Untrusted URLs - disableJavaScriptURLs', ( expect(e.href).toBe('https://reactjs.org/'); }); - it('rejects a javascript protocol href if it is added during an update twice', () => { + it('rejects a javascript protocol href if it is added during an update twice', async () => { const container = document.createElement('div'); - ReactDOM.render(click me, container); + const root = ReactDOMClient.createRoot(container); + await act(async () => { + root.render(click me); + }); expect(container.firstChild.href).toBe('http://thisisfine/'); - ReactDOM.render(click me, container); + await act(async () => { + root.render(click me); + }); expect(container.firstChild.href).toBe(EXPECTED_SAFE_URL); // The second update ensures that a global flag hasn't been added to the regex // which would fail to match the second time it is called. - ReactDOM.render(click me, container); + await act(async () => { + root.render(click me); + }); expect(container.firstChild.href).toBe(EXPECTED_SAFE_URL); }); });