main
js 45 lines 1.27 KB
Raw
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 describe('ReactDOM.browser', () => {
13 beforeEach(() => {
14 jest.resetModules();
15 });
16
17 // @gate enableBrowserAPI
18 it('can create browser-only content before the browser renderer is initialized', async () => {
19 const React = require('react');
20 const ReactDOM = require('react-dom');
21 const initializeReason = jest.fn(
22 () => new Error('Only render this content in a browser'),
23 );
24 const browserOnly = ReactDOM.browser(initializeReason);
25 const ReactDOMClient = require('react-dom/client');
26 const {act} = require('internal-test-utils');
27
28 function BrowserOnly() {
29 React.use(browserOnly);
30 return <span>Browser</span>;
31 }
32
33 const container = document.createElement('div');
34 const root = ReactDOMClient.createRoot(container);
35 await act(() => {
36 root.render(
37 <React.Suspense fallback={<span>Fallback</span>}>
38 <BrowserOnly />
39 </React.Suspense>,
40 );
41 });
42 expect(container.innerHTML).toBe('<span>Browser</span>');
43 expect(initializeReason).not.toHaveBeenCalled();
44 });
45 });