/** * 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 * @jest-environment ./scripts/jest/ReactDOMServerIntegrationEnvironment */ 'use strict'; const ReactDOMServerIntegrationUtils = require('./utils/ReactDOMServerIntegrationTestUtils'); let React; let ReactDOMClient; let ReactDOMServer; function initModules() { // Reset warning cache. jest.resetModules(); React = require('react'); ReactDOMClient = require('react-dom/client'); ReactDOMServer = require('react-dom/server'); // Make them available to the helpers. return { ReactDOMClient, ReactDOMServer, }; } const {resetModules, itRenders, itThrowsWhenRendering} = ReactDOMServerIntegrationUtils(initModules); describe('ReactDOMServerIntegrationSelect', () => { let options; beforeEach(() => { resetModules(); options = [ , , , ]; }); // a helper function to test the selected value of a DOM element (element) and a value or array of // values that should be selected (selected). const expectSelectValue = (element, selected) => { if (!Array.isArray(selected)) { selected = [selected]; } // the select DOM element shouldn't ever have a value or defaultValue // attribute; that is not how select values are expressed in the DOM. expect(element.getAttribute('value')).toBe(null); expect(element.getAttribute('defaultValue')).toBe(null); ['foo', 'bar', 'baz'].forEach(value => { const expectedValue = selected.indexOf(value) !== -1; const option = element.querySelector(`#${value}`); expect(option.selected).toBe(expectedValue); }); }; itRenders('a select with a value and an onChange', async render => { const e = await render( , ); expectSelectValue(e, 'bar'); }); itRenders('a select with a value and readOnly', async render => { const e = await render( , ); expectSelectValue(e, 'bar'); }); itRenders('a select with a multiple values and an onChange', async render => { const e = await render( , ); expectSelectValue(e, ['bar', 'baz']); }); itRenders('a select with a multiple values and readOnly', async render => { const e = await render( , ); expectSelectValue(e, ['bar', 'baz']); }); itRenders('a select with a value and no onChange/readOnly', async render => { // this configuration should raise a dev warning that value without // onChange or readOnly is a mistake. const e = await render(, 1); expectSelectValue(e, 'bar'); }); itRenders('a select with a defaultValue', async render => { const e = await render(); expectSelectValue(e, 'bar'); }); itRenders('a select value overriding defaultValue', async render => { const e = await render( , 1, ); expectSelectValue(e, 'bar'); }); itRenders( 'a select with options that use dangerouslySetInnerHTML', async render => { const e = await render( , 2, ); expectSelectValue(e, 'bar'); }, ); itThrowsWhenRendering( 'a select with option that uses dangerouslySetInnerHTML and 0 as child', async render => { await render( , 1, ); }, 'Can only set one of `children` or `props.dangerouslySetInnerHTML`.', ); itThrowsWhenRendering( 'a select with option that uses dangerouslySetInnerHTML and empty string as child', async render => { await render( , 1, ); }, 'Can only set one of `children` or `props.dangerouslySetInnerHTML`.', ); itRenders( 'a select value overriding defaultValue no matter the prop order', async render => { const e = await render( , 1, ); expectSelectValue(e, 'bar'); }, ); itRenders('a select option with flattened children', async render => { const e = await render( , ); const option = e.options[0]; expect(option.textContent).toBe('A B 5'); expect(option.value).toBe('bar'); expect(option.selected).toBe(true); }); itRenders( 'a select option with flattened children no value', async render => { const e = await render( , ); const option = e.options[0]; expect(option.textContent).toBe('A B'); expect(option.value).toBe('A B'); expect(option.selected).toBe(true); }, ); itRenders( 'a boolean true select value match the string "true"', async render => { const e = await render( , ); expect(e.firstChild.selected).toBe(false); expect(e.lastChild.selected).toBe(true); }, ); itRenders( 'a missing select value does not match the string "undefined"', async render => { const e = await render( , ); expect(e.firstChild.selected).toBe(true); expect(e.lastChild.selected).toBe(false); }, ); });