|
| 1 | +/** |
| 2 | + * Copyright (c) Facebook, Inc. and its 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 | + */ |
| 9 | + |
| 10 | +'use strict'; |
| 11 | + |
| 12 | +const ReactDOMServerIntegrationUtils = require('./utils/ReactDOMServerIntegrationTestUtils'); |
| 13 | + |
| 14 | +let React; |
| 15 | +let ReactDOM; |
| 16 | +let ReactDOMServer; |
| 17 | + |
| 18 | +function initModules() { |
| 19 | + // Reset warning cache. |
| 20 | + jest.resetModuleRegistry(); |
| 21 | + React = require('react'); |
| 22 | + ReactDOM = require('react-dom'); |
| 23 | + ReactDOMServer = require('react-dom/server'); |
| 24 | + |
| 25 | + // Make them available to the helpers. |
| 26 | + return { |
| 27 | + ReactDOM, |
| 28 | + ReactDOMServer, |
| 29 | + }; |
| 30 | +} |
| 31 | + |
| 32 | +const {resetModules, itRenders} = ReactDOMServerIntegrationUtils(initModules); |
| 33 | + |
| 34 | +describe('ReactDOMServerIntegrationCheckbox', () => { |
| 35 | + beforeEach(() => { |
| 36 | + resetModules(); |
| 37 | + }); |
| 38 | + |
| 39 | + itRenders('a checkbox that is checked with an onChange', async render => { |
| 40 | + const e = await render( |
| 41 | + <input type="checkbox" checked={true} onChange={() => {}} />, |
| 42 | + ); |
| 43 | + expect(e.checked).toBe(true); |
| 44 | + }); |
| 45 | + |
| 46 | + itRenders('a checkbox that is checked with readOnly', async render => { |
| 47 | + const e = await render( |
| 48 | + <input type="checkbox" checked={true} readOnly={true} />, |
| 49 | + ); |
| 50 | + expect(e.checked).toBe(true); |
| 51 | + }); |
| 52 | + |
| 53 | + itRenders( |
| 54 | + 'a checkbox that is checked and no onChange/readOnly', |
| 55 | + async render => { |
| 56 | + // this configuration should raise a dev warning that checked without |
| 57 | + // onChange or readOnly is a mistake. |
| 58 | + const e = await render(<input type="checkbox" checked={true} />, 1); |
| 59 | + expect(e.checked).toBe(true); |
| 60 | + }, |
| 61 | + ); |
| 62 | + |
| 63 | + itRenders('a checkbox with defaultChecked', async render => { |
| 64 | + const e = await render(<input type="checkbox" defaultChecked={true} />); |
| 65 | + expect(e.checked).toBe(true); |
| 66 | + expect(e.getAttribute('defaultChecked')).toBe(null); |
| 67 | + }); |
| 68 | + |
| 69 | + itRenders('a checkbox checked overriding defaultChecked', async render => { |
| 70 | + const e = await render( |
| 71 | + <input |
| 72 | + type="checkbox" |
| 73 | + checked={true} |
| 74 | + defaultChecked={false} |
| 75 | + readOnly={true} |
| 76 | + />, |
| 77 | + 1, |
| 78 | + ); |
| 79 | + expect(e.checked).toBe(true); |
| 80 | + expect(e.getAttribute('defaultChecked')).toBe(null); |
| 81 | + }); |
| 82 | + |
| 83 | + itRenders( |
| 84 | + 'a checkbox checked overriding defaultChecked no matter the prop order', |
| 85 | + async render => { |
| 86 | + const e = await render( |
| 87 | + <input |
| 88 | + type="checkbox" |
| 89 | + defaultChecked={false} |
| 90 | + checked={true} |
| 91 | + readOnly={true} |
| 92 | + />, |
| 93 | + 1, |
| 94 | + ); |
| 95 | + expect(e.checked).toBe(true); |
| 96 | + expect(e.getAttribute('defaultChecked')).toBe(null); |
| 97 | + }, |
| 98 | + ); |
| 99 | +}); |
0 commit comments