main
js 104 lines 2.87 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('ReactDOMServerIntegrationCheckbox', () => {
40 beforeEach(() => {
41 resetModules();
42 });
43
44 itRenders('a checkbox that is checked with an onChange', async render => {
45 const e = await render(
46 <input type="checkbox" checked={true} onChange={() => {}} />,
47 );
48 expect(e.checked).toBe(true);
49 });
50
51 itRenders('a checkbox that is checked with readOnly', async render => {
52 const e = await render(
53 <input type="checkbox" checked={true} readOnly={true} />,
54 );
55 expect(e.checked).toBe(true);
56 });
57
58 itRenders(
59 'a checkbox that is checked and no onChange/readOnly',
60 async render => {
61 // this configuration should raise a dev warning that checked without
62 // onChange or readOnly is a mistake.
63 const e = await render(<input type="checkbox" checked={true} />, 1);
64 expect(e.checked).toBe(true);
65 },
66 );
67
68 itRenders('a checkbox with defaultChecked', async render => {
69 const e = await render(<input type="checkbox" defaultChecked={true} />);
70 expect(e.checked).toBe(true);
71 expect(e.getAttribute('defaultChecked')).toBe(null);
72 });
73
74 itRenders('a checkbox checked overriding defaultChecked', async render => {
75 const e = await render(
76 <input
77 type="checkbox"
78 checked={true}
79 defaultChecked={false}
80 readOnly={true}
81 />,
82 1,
83 );
84 expect(e.checked).toBe(true);
85 expect(e.getAttribute('defaultChecked')).toBe(null);
86 });
87
88 itRenders(
89 'a checkbox checked overriding defaultChecked no matter the prop order',
90 async render => {
91 const e = await render(
92 <input
93 type="checkbox"
94 defaultChecked={false}
95 checked={true}
96 readOnly={true}
97 />,
98 1,
99 );
100 expect(e.checked).toBe(true);
101 expect(e.getAttribute('defaultChecked')).toBe(null);
102 },
103 );
104 });