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.  */
    
  9. 
    
  10. 'use strict';
    
  11. 
    
  12. describe('ReactDOM unknown attribute', () => {
    
  13.   let React;
    
  14.   let ReactDOM;
    
  15. 
    
  16.   beforeEach(() => {
    
  17.     jest.resetModules();
    
  18.     React = require('react');
    
  19.     ReactDOM = require('react-dom');
    
  20.   });
    
  21. 
    
  22.   function testUnknownAttributeRemoval(givenValue) {
    
  23.     const el = document.createElement('div');
    
  24.     ReactDOM.render(<div unknown="something" />, el);
    
  25.     expect(el.firstChild.getAttribute('unknown')).toBe('something');
    
  26.     ReactDOM.render(<div unknown={givenValue} />, el);
    
  27.     expect(el.firstChild.hasAttribute('unknown')).toBe(false);
    
  28.   }
    
  29. 
    
  30.   function testUnknownAttributeAssignment(givenValue, expectedDOMValue) {
    
  31.     const el = document.createElement('div');
    
  32.     ReactDOM.render(<div unknown="something" />, el);
    
  33.     expect(el.firstChild.getAttribute('unknown')).toBe('something');
    
  34.     ReactDOM.render(<div unknown={givenValue} />, el);
    
  35.     expect(el.firstChild.getAttribute('unknown')).toBe(expectedDOMValue);
    
  36.   }
    
  37. 
    
  38.   describe('unknown attributes', () => {
    
  39.     it('removes values null and undefined', () => {
    
  40.       testUnknownAttributeRemoval(null);
    
  41.       testUnknownAttributeRemoval(undefined);
    
  42.     });
    
  43. 
    
  44.     it('changes values true, false to null, and also warns once', () => {
    
  45.       expect(() => testUnknownAttributeAssignment(true, null)).toErrorDev(
    
  46.         'Received `true` for a non-boolean attribute `unknown`.\n\n' +
    
  47.           'If you want to write it to the DOM, pass a string instead: ' +
    
  48.           'unknown="true" or unknown={value.toString()}.\n' +
    
  49.           '    in div (at **)',
    
  50.       );
    
  51.       testUnknownAttributeAssignment(false, null);
    
  52.     });
    
  53. 
    
  54.     it('removes unknown attributes that were rendered but are now missing', () => {
    
  55.       const el = document.createElement('div');
    
  56.       ReactDOM.render(<div unknown="something" />, el);
    
  57.       expect(el.firstChild.getAttribute('unknown')).toBe('something');
    
  58.       ReactDOM.render(<div />, el);
    
  59.       expect(el.firstChild.hasAttribute('unknown')).toBe(false);
    
  60.     });
    
  61. 
    
  62.     it('passes through strings', () => {
    
  63.       testUnknownAttributeAssignment('a string', 'a string');
    
  64.     });
    
  65. 
    
  66.     it('coerces numbers to strings', () => {
    
  67.       testUnknownAttributeAssignment(0, '0');
    
  68.       testUnknownAttributeAssignment(-1, '-1');
    
  69.       testUnknownAttributeAssignment(42, '42');
    
  70.       testUnknownAttributeAssignment(9000.99, '9000.99');
    
  71.     });
    
  72. 
    
  73.     it('coerces NaN to strings and warns', () => {
    
  74.       expect(() => testUnknownAttributeAssignment(NaN, 'NaN')).toErrorDev(
    
  75.         'Warning: Received NaN for the `unknown` attribute. ' +
    
  76.           'If this is expected, cast the value to a string.\n' +
    
  77.           '    in div (at **)',
    
  78.       );
    
  79.     });
    
  80. 
    
  81.     it('coerces objects to strings and warns', () => {
    
  82.       const lol = {
    
  83.         toString() {
    
  84.           return 'lol';
    
  85.         },
    
  86.       };
    
  87. 
    
  88.       testUnknownAttributeAssignment({hello: 'world'}, '[object Object]');
    
  89.       testUnknownAttributeAssignment(lol, 'lol');
    
  90.     });
    
  91. 
    
  92.     it('throws with Temporal-like objects', () => {
    
  93.       class TemporalLike {
    
  94.         valueOf() {
    
  95.           // Throwing here is the behavior of ECMAScript "Temporal" date/time API.
    
  96.           // See https://tc39.es/proposal-temporal/docs/plaindate.html#valueOf
    
  97.           throw new TypeError('prod message');
    
  98.         }
    
  99.         toString() {
    
  100.           return '2020-01-01';
    
  101.         }
    
  102.       }
    
  103.       const test = () =>
    
  104.         testUnknownAttributeAssignment(new TemporalLike(), null);
    
  105.       expect(() =>
    
  106.         expect(test).toThrowError(new TypeError('prod message')),
    
  107.       ).toErrorDev(
    
  108.         'Warning: The provided `unknown` attribute is an unsupported type TemporalLike.' +
    
  109.           ' This value must be coerced to a string before using it here.',
    
  110.       );
    
  111.     });
    
  112. 
    
  113.     it('removes symbols and warns', () => {
    
  114.       expect(() => testUnknownAttributeRemoval(Symbol('foo'))).toErrorDev(
    
  115.         'Warning: Invalid value for prop `unknown` on <div> tag. Either remove it ' +
    
  116.           'from the element, or pass a string or number value to keep it ' +
    
  117.           'in the DOM. For details, see https://reactjs.org/link/attribute-behavior \n' +
    
  118.           '    in div (at **)',
    
  119.       );
    
  120.     });
    
  121. 
    
  122.     it('removes functions and warns', () => {
    
  123.       expect(() =>
    
  124.         testUnknownAttributeRemoval(function someFunction() {}),
    
  125.       ).toErrorDev(
    
  126.         'Warning: Invalid value for prop `unknown` on <div> tag. Either remove ' +
    
  127.           'it from the element, or pass a string or number value to ' +
    
  128.           'keep it in the DOM. For details, see ' +
    
  129.           'https://reactjs.org/link/attribute-behavior \n' +
    
  130.           '    in div (at **)',
    
  131.       );
    
  132.     });
    
  133. 
    
  134.     it('allows camelCase unknown attributes and warns', () => {
    
  135.       const el = document.createElement('div');
    
  136. 
    
  137.       expect(() =>
    
  138.         ReactDOM.render(<div helloWorld="something" />, el),
    
  139.       ).toErrorDev(
    
  140.         'React does not recognize the `helloWorld` prop on a DOM element. ' +
    
  141.           'If you intentionally want it to appear in the DOM as a custom ' +
    
  142.           'attribute, spell it as lowercase `helloworld` instead. ' +
    
  143.           'If you accidentally passed it from a parent component, remove ' +
    
  144.           'it from the DOM element.\n' +
    
  145.           '    in div (at **)',
    
  146.       );
    
  147. 
    
  148.       expect(el.firstChild.getAttribute('helloworld')).toBe('something');
    
  149.     });
    
  150.   });
    
  151. });