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.  * @jest-environment ./scripts/jest/ReactDOMServerIntegrationEnvironment
    
  9.  */
    
  10. 
    
  11. 'use strict';
    
  12. 
    
  13. const ReactDOMServerIntegrationUtils = require('./utils/ReactDOMServerIntegrationTestUtils');
    
  14. // Set by `yarn test-fire`.
    
  15. const {disableInputAttributeSyncing} = require('shared/ReactFeatureFlags');
    
  16. 
    
  17. let React;
    
  18. let ReactDOM;
    
  19. let ReactDOMServer;
    
  20. let ReactTestUtils;
    
  21. 
    
  22. function initModules() {
    
  23.   // Reset warning cache.
    
  24.   jest.resetModules();
    
  25.   React = require('react');
    
  26.   ReactDOM = require('react-dom');
    
  27.   ReactDOMServer = require('react-dom/server');
    
  28.   ReactTestUtils = require('react-dom/test-utils');
    
  29. 
    
  30.   // Make them available to the helpers.
    
  31.   return {
    
  32.     ReactDOM,
    
  33.     ReactDOMServer,
    
  34.     ReactTestUtils,
    
  35.   };
    
  36. }
    
  37. 
    
  38. const {resetModules, itRenders} = ReactDOMServerIntegrationUtils(initModules);
    
  39. 
    
  40. // TODO: Run this in React Fire mode after we figure out the SSR behavior.
    
  41. const desc = disableInputAttributeSyncing ? xdescribe : describe;
    
  42. desc('ReactDOMServerIntegrationInput', () => {
    
  43.   beforeEach(() => {
    
  44.     resetModules();
    
  45.   });
    
  46. 
    
  47.   itRenders('an input with a value and an onChange', async render => {
    
  48.     const e = await render(<input value="foo" onChange={() => {}} />);
    
  49.     expect(e.value).toBe('foo');
    
  50.   });
    
  51. 
    
  52.   itRenders('an input with a value and readOnly', async render => {
    
  53.     const e = await render(<input value="foo" readOnly={true} />);
    
  54.     expect(e.value).toBe('foo');
    
  55.   });
    
  56. 
    
  57.   itRenders('an input with a value and no onChange/readOnly', async render => {
    
  58.     // this configuration should raise a dev warning that value without
    
  59.     // onChange or readOnly is a mistake.
    
  60.     const e = await render(<input value="foo" />, 1);
    
  61.     expect(e.value).toBe('foo');
    
  62.     expect(e.getAttribute('value')).toBe('foo');
    
  63.   });
    
  64. 
    
  65.   itRenders('an input with a defaultValue', async render => {
    
  66.     const e = await render(<input defaultValue="foo" />);
    
  67.     expect(e.value).toBe('foo');
    
  68.     expect(e.getAttribute('value')).toBe('foo');
    
  69.     expect(e.getAttribute('defaultValue')).toBe(null);
    
  70.   });
    
  71. 
    
  72.   itRenders('an input value overriding defaultValue', async render => {
    
  73.     const e = await render(
    
  74.       <input value="foo" defaultValue="bar" readOnly={true} />,
    
  75.       1,
    
  76.     );
    
  77.     expect(e.value).toBe('foo');
    
  78.     expect(e.getAttribute('value')).toBe('foo');
    
  79.     expect(e.getAttribute('defaultValue')).toBe(null);
    
  80.   });
    
  81. 
    
  82.   itRenders(
    
  83.     'an input value overriding defaultValue no matter the prop order',
    
  84.     async render => {
    
  85.       const e = await render(
    
  86.         <input defaultValue="bar" value="foo" readOnly={true} />,
    
  87.         1,
    
  88.       );
    
  89.       expect(e.value).toBe('foo');
    
  90.       expect(e.getAttribute('value')).toBe('foo');
    
  91.       expect(e.getAttribute('defaultValue')).toBe(null);
    
  92.     },
    
  93.   );
    
  94. });