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