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. let React;
    
  13. let ReactDOM;
    
  14. let ReactDOMFizzServer;
    
  15. 
    
  16. describe('react-dom-server-rendering-stub', () => {
    
  17.   beforeEach(() => {
    
  18.     jest.mock('react-dom', () => require('react-dom/server-rendering-stub'));
    
  19. 
    
  20.     React = require('react');
    
  21.     ReactDOM = require('react-dom');
    
  22.     ReactDOMFizzServer = require('react-dom/server');
    
  23.   });
    
  24. 
    
  25.   it('exports a version', () => {
    
  26.     expect(ReactDOM.version).toBeTruthy();
    
  27.   });
    
  28. 
    
  29.   it('exports that are expected to be client only in the future are not exported', () => {
    
  30.     expect(ReactDOM.createRoot).toBe(undefined);
    
  31.     expect(ReactDOM.hydrateRoot).toBe(undefined);
    
  32.     expect(ReactDOM.findDOMNode).toBe(undefined);
    
  33.     expect(ReactDOM.hydrate).toBe(undefined);
    
  34.     expect(ReactDOM.render).toBe(undefined);
    
  35.     expect(ReactDOM.unmountComponentAtNode).toBe(undefined);
    
  36.     expect(ReactDOM.unstable_createEventHandle).toBe(undefined);
    
  37.     expect(ReactDOM.unstable_renderSubtreeIntoContainer).toBe(undefined);
    
  38.     expect(ReactDOM.unstable_runWithPriority).toBe(undefined);
    
  39.   });
    
  40. 
    
  41.   // @gate enableFloat
    
  42.   it('provides preload, preloadModule, preinit, and preinitModule exports', async () => {
    
  43.     function App() {
    
  44.       ReactDOM.preload('foo', {as: 'style'});
    
  45.       ReactDOM.preloadModule('foomodule');
    
  46.       ReactDOM.preinit('bar', {as: 'style'});
    
  47.       ReactDOM.preinitModule('barmodule');
    
  48.       return <div>foo</div>;
    
  49.     }
    
  50.     const html = ReactDOMFizzServer.renderToString(<App />);
    
  51.     expect(html).toEqual(
    
  52.       '<link rel="stylesheet" href="bar" data-precedence="default"/><script src="barmodule" type="module" async=""></script><link rel="preload" href="foo" as="style"/><link rel="modulepreload" href="foomodule"/><div>foo</div>',
    
  53.     );
    
  54.   });
    
  55. 
    
  56.   it('provides preconnect and prefetchDNS exports', async () => {
    
  57.     function App() {
    
  58.       ReactDOM.preconnect('foo', {crossOrigin: 'use-credentials'});
    
  59.       ReactDOM.prefetchDNS('bar');
    
  60.       return <div>foo</div>;
    
  61.     }
    
  62.     const html = ReactDOMFizzServer.renderToString(<App />);
    
  63.     expect(html).toEqual(
    
  64.       '<link rel="preconnect" href="foo" crossorigin="use-credentials"/><link href="bar" rel="dns-prefetch"/><div>foo</div>',
    
  65.     );
    
  66.   });
    
  67. 
    
  68.   it('provides a stub for createPortal', async () => {
    
  69.     expect(() => {
    
  70.       ReactDOM.createPortal();
    
  71.     }).toThrow(
    
  72.       'createPortal was called on the server. Portals are not currently supported on the server. Update your program to conditionally call createPortal on the client only.',
    
  73.     );
    
  74.   });
    
  75. 
    
  76.   it('provides a stub for flushSync', async () => {
    
  77.     let x = false;
    
  78.     expect(() => {
    
  79.       ReactDOM.flushSync(() => (x = true));
    
  80.     }).toThrow(
    
  81.       'flushSync was called on the server. This is likely caused by a function being called during render or in module scope that was intended to be called from an effect or event handler. Update your to not call flushSync no the server.',
    
  82.     );
    
  83.     expect(x).toBe(false);
    
  84.   });
    
  85. 
    
  86.   // @gate enableFormActions
    
  87.   // @gate enableAsyncActions
    
  88.   it('exports useFormStatus', async () => {
    
  89.     function App() {
    
  90.       const {pending} = ReactDOM.useFormStatus();
    
  91.       return 'Pending: ' + pending;
    
  92.     }
    
  93. 
    
  94.     const result = await ReactDOMFizzServer.renderToStaticMarkup(<App />);
    
  95.     expect(result).toEqual('Pending: false');
    
  96.   });
    
  97. });