/** * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * * @emails react-core */ 'use strict'; function emptyFunction() {} describe('ReactDOMTextarea', () => { let React; let ReactDOMClient; let ReactDOMServer; let act; let assertConsoleErrorDev; let renderTextarea; const ReactFeatureFlags = require('shared/ReactFeatureFlags'); beforeEach(() => { jest.resetModules(); React = require('react'); ReactDOMClient = require('react-dom/client'); ReactDOMServer = require('react-dom/server'); act = require('internal-test-utils').act; assertConsoleErrorDev = require('internal-test-utils').assertConsoleErrorDev; renderTextarea = async function (component, container, root) { await act(() => { root.render(component); }); const node = container.firstChild; // Fixing jsdom's quirky behavior -- in reality, the parser should strip // off the leading newline but we need to do it by hand here. node.defaultValue = node.innerHTML.replace(/^\n/, ''); return node; }; }); afterEach(() => { jest.restoreAllMocks(); }); it('should allow setting `defaultValue`', async () => { const container = document.createElement('div'); const root = ReactDOMClient.createRoot(container); const node = await renderTextarea( ); }); expect(node.value).toBe('0'); }); it('should not incur unnecessary DOM mutations', async () => { const container = document.createElement('div'); const root = ReactDOMClient.createRoot(container); await act(() => { root.render(, container, root, ); assertConsoleErrorDev([ 'Use the `defaultValue` or `value` props instead of setting children on ); }); expect(node.value).toEqual(''); }); } if (ReactFeatureFlags.disableTextareaChildren) { it('should receive defaultValue and still ignore children content', async () => { const container = document.createElement('div'); const root = ReactDOMClient.createRoot(container); const node = await renderTextarea( , container, root, ); assertConsoleErrorDev([ 'Use the `defaultValue` or `value` props instead of setting children on , container, root, ); assertConsoleErrorDev([ 'Use the `defaultValue` or `value` props instead of setting children on ); }); expect(node.value).toEqual('giraffe'); }); } it('should keep value when switching to uncontrolled element if not changed', async () => { const container = document.createElement('div'); const root = ReactDOMClient.createRoot(container); const node = await renderTextarea( , container, root, ); assertConsoleErrorDev([ 'Use the `defaultValue` or `value` props instead of setting children on , container, root, ); assertConsoleErrorDev([ 'Use the `defaultValue` or `value` props instead of setting children on , container, root, ); assertConsoleErrorDev([ 'Use the `defaultValue` or `value` props instead of setting children on , container, root, ); assertConsoleErrorDev([ 'Use the `defaultValue` or `value` props instead of setting children on , container, root, ); assertConsoleErrorDev([ 'Use the `defaultValue` or `value` props instead of setting children on , container, root, ); assertConsoleErrorDev([ 'Use the `defaultValue` or `value` props instead of setting children on , ); }); }).rejects.toThrow(', container, root, )))(), ).resolves.not.toThrow(); assertConsoleErrorDev([ 'Use the `defaultValue` or `value` props instead of setting children on ); }); assertConsoleErrorDev([ 'Use the `defaultValue` or `value` props instead of setting children on ); }); assertConsoleErrorDev([ 'Use the `defaultValue` or `value` props instead of setting children on