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