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. 
    
  15. let React;
    
  16. let ReactDOM;
    
  17. let ReactDOMServer;
    
  18. let ReactTestUtils;
    
  19. 
    
  20. function initModules() {
    
  21.   // Reset warning cache.
    
  22.   jest.resetModules();
    
  23.   React = require('react');
    
  24.   ReactDOM = require('react-dom');
    
  25.   ReactDOMServer = require('react-dom/server');
    
  26.   ReactTestUtils = require('react-dom/test-utils');
    
  27. 
    
  28.   // Make them available to the helpers.
    
  29.   return {
    
  30.     ReactDOM,
    
  31.     ReactDOMServer,
    
  32.     ReactTestUtils,
    
  33.   };
    
  34. }
    
  35. 
    
  36. const {resetModules, itRenders} = ReactDOMServerIntegrationUtils(initModules);
    
  37. 
    
  38. describe('ReactDOMServerIntegrationTextarea', () => {
    
  39.   beforeEach(() => {
    
  40.     resetModules();
    
  41.   });
    
  42. 
    
  43.   // textareas
    
  44.   // ---------
    
  45.   itRenders('a textarea with a value and an onChange', async render => {
    
  46.     const e = await render(<textarea value="foo" onChange={() => {}} />);
    
  47.     // textarea DOM elements don't have a value **attribute**, the text is
    
  48.     // a child of the element and accessible via the .value **property**.
    
  49.     expect(e.getAttribute('value')).toBe(null);
    
  50.     expect(e.value).toBe('foo');
    
  51.   });
    
  52. 
    
  53.   itRenders('a textarea with a value of undefined', async render => {
    
  54.     const e = await render(<textarea value={undefined} />);
    
  55.     expect(e.getAttribute('value')).toBe(null);
    
  56.     expect(e.value).toBe('');
    
  57.   });
    
  58. 
    
  59.   itRenders('a textarea with a value and readOnly', async render => {
    
  60.     const e = await render(<textarea value="foo" readOnly={true} />);
    
  61.     // textarea DOM elements don't have a value **attribute**, the text is
    
  62.     // a child of the element and accessible via the .value **property**.
    
  63.     expect(e.getAttribute('value')).toBe(null);
    
  64.     expect(e.value).toBe('foo');
    
  65.   });
    
  66. 
    
  67.   itRenders(
    
  68.     'a textarea with a value and no onChange/readOnly',
    
  69.     async render => {
    
  70.       // this configuration should raise a dev warning that value without
    
  71.       // onChange or readOnly is a mistake.
    
  72.       const e = await render(<textarea value="foo" />, 1);
    
  73.       expect(e.getAttribute('value')).toBe(null);
    
  74.       expect(e.value).toBe('foo');
    
  75.     },
    
  76.   );
    
  77. 
    
  78.   itRenders('a textarea with a defaultValue', async render => {
    
  79.     const e = await render(<textarea defaultValue="foo" />);
    
  80.     expect(e.getAttribute('value')).toBe(null);
    
  81.     expect(e.getAttribute('defaultValue')).toBe(null);
    
  82.     expect(e.value).toBe('foo');
    
  83.   });
    
  84. 
    
  85.   itRenders('a textarea value overriding defaultValue', async render => {
    
  86.     const e = await render(
    
  87.       <textarea value="foo" defaultValue="bar" readOnly={true} />,
    
  88.       1,
    
  89.     );
    
  90.     expect(e.getAttribute('value')).toBe(null);
    
  91.     expect(e.getAttribute('defaultValue')).toBe(null);
    
  92.     expect(e.value).toBe('foo');
    
  93.   });
    
  94. 
    
  95.   itRenders(
    
  96.     'a textarea value overriding defaultValue no matter the prop order',
    
  97.     async render => {
    
  98.       const e = await render(
    
  99.         <textarea defaultValue="bar" value="foo" readOnly={true} />,
    
  100.         1,
    
  101.       );
    
  102.       expect(e.getAttribute('value')).toBe(null);
    
  103.       expect(e.getAttribute('defaultValue')).toBe(null);
    
  104.       expect(e.value).toBe('foo');
    
  105.     },
    
  106.   );
    
  107. });