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('ReactDOMShorthandCSSPropertyCollision', () => {
    
  13.   let React;
    
  14.   let ReactDOM;
    
  15. 
    
  16.   beforeEach(() => {
    
  17.     jest.resetModules();
    
  18.     React = require('react');
    
  19.     ReactDOM = require('react-dom');
    
  20.   });
    
  21. 
    
  22.   it('should warn for conflicting CSS shorthand updates', () => {
    
  23.     const container = document.createElement('div');
    
  24.     ReactDOM.render(<div style={{font: 'foo', fontStyle: 'bar'}} />, container);
    
  25.     expect(() =>
    
  26.       ReactDOM.render(<div style={{font: 'foo'}} />, container),
    
  27.     ).toErrorDev(
    
  28.       'Warning: Removing a style property during rerender (fontStyle) ' +
    
  29.         'when a conflicting property is set (font) can lead to styling ' +
    
  30.         "bugs. To avoid this, don't mix shorthand and non-shorthand " +
    
  31.         'properties for the same value; instead, replace the shorthand ' +
    
  32.         'with separate values.' +
    
  33.         '\n    in div (at **)',
    
  34.     );
    
  35. 
    
  36.     // These updates are OK and don't warn:
    
  37.     ReactDOM.render(<div style={{font: 'qux', fontStyle: 'bar'}} />, container);
    
  38.     ReactDOM.render(<div style={{font: 'foo', fontStyle: 'baz'}} />, container);
    
  39. 
    
  40.     expect(() =>
    
  41.       ReactDOM.render(
    
  42.         <div style={{font: 'qux', fontStyle: 'baz'}} />,
    
  43.         container,
    
  44.       ),
    
  45.     ).toErrorDev(
    
  46.       'Warning: Updating a style property during rerender (font) when ' +
    
  47.         'a conflicting property is set (fontStyle) can lead to styling ' +
    
  48.         "bugs. To avoid this, don't mix shorthand and non-shorthand " +
    
  49.         'properties for the same value; instead, replace the shorthand ' +
    
  50.         'with separate values.' +
    
  51.         '\n    in div (at **)',
    
  52.     );
    
  53.     expect(() =>
    
  54.       ReactDOM.render(<div style={{fontStyle: 'baz'}} />, container),
    
  55.     ).toErrorDev(
    
  56.       'Warning: Removing a style property during rerender (font) when ' +
    
  57.         'a conflicting property is set (fontStyle) can lead to styling ' +
    
  58.         "bugs. To avoid this, don't mix shorthand and non-shorthand " +
    
  59.         'properties for the same value; instead, replace the shorthand ' +
    
  60.         'with separate values.' +
    
  61.         '\n    in div (at **)',
    
  62.     );
    
  63. 
    
  64.     // A bit of a special case: backgroundPosition isn't technically longhand
    
  65.     // (it expands to backgroundPosition{X,Y} but so does background)
    
  66.     ReactDOM.render(
    
  67.       <div style={{background: 'yellow', backgroundPosition: 'center'}} />,
    
  68.       container,
    
  69.     );
    
  70.     expect(() =>
    
  71.       ReactDOM.render(<div style={{background: 'yellow'}} />, container),
    
  72.     ).toErrorDev(
    
  73.       'Warning: Removing a style property during rerender ' +
    
  74.         '(backgroundPosition) when a conflicting property is set ' +
    
  75.         "(background) can lead to styling bugs. To avoid this, don't mix " +
    
  76.         'shorthand and non-shorthand properties for the same value; ' +
    
  77.         'instead, replace the shorthand with separate values.' +
    
  78.         '\n    in div (at **)',
    
  79.     );
    
  80.     ReactDOM.render(
    
  81.       <div style={{background: 'yellow', backgroundPosition: 'center'}} />,
    
  82.       container,
    
  83.     );
    
  84.     // But setting them  at the same time is OK:
    
  85.     ReactDOM.render(
    
  86.       <div style={{background: 'green', backgroundPosition: 'top'}} />,
    
  87.       container,
    
  88.     );
    
  89.     expect(() =>
    
  90.       ReactDOM.render(<div style={{backgroundPosition: 'top'}} />, container),
    
  91.     ).toErrorDev(
    
  92.       'Warning: Removing a style property during rerender (background) ' +
    
  93.         'when a conflicting property is set (backgroundPosition) can lead ' +
    
  94.         "to styling bugs. To avoid this, don't mix shorthand and " +
    
  95.         'non-shorthand properties for the same value; instead, replace the ' +
    
  96.         'shorthand with separate values.' +
    
  97.         '\n    in div (at **)',
    
  98.     );
    
  99. 
    
  100.     // A bit of an even more special case: borderLeft and borderStyle overlap.
    
  101.     ReactDOM.render(
    
  102.       <div style={{borderStyle: 'dotted', borderLeft: '1px solid red'}} />,
    
  103.       container,
    
  104.     );
    
  105.     expect(() =>
    
  106.       ReactDOM.render(<div style={{borderLeft: '1px solid red'}} />, container),
    
  107.     ).toErrorDev(
    
  108.       'Warning: Removing a style property during rerender (borderStyle) ' +
    
  109.         'when a conflicting property is set (borderLeft) can lead to ' +
    
  110.         "styling bugs. To avoid this, don't mix shorthand and " +
    
  111.         'non-shorthand properties for the same value; instead, replace the ' +
    
  112.         'shorthand with separate values.' +
    
  113.         '\n    in div (at **)',
    
  114.     );
    
  115.     expect(() =>
    
  116.       ReactDOM.render(
    
  117.         <div style={{borderStyle: 'dashed', borderLeft: '1px solid red'}} />,
    
  118.         container,
    
  119.       ),
    
  120.     ).toErrorDev(
    
  121.       'Warning: Updating a style property during rerender (borderStyle) ' +
    
  122.         'when a conflicting property is set (borderLeft) can lead to ' +
    
  123.         "styling bugs. To avoid this, don't mix shorthand and " +
    
  124.         'non-shorthand properties for the same value; instead, replace the ' +
    
  125.         'shorthand with separate values.' +
    
  126.         '\n    in div (at **)',
    
  127.     );
    
  128.     // But setting them  at the same time is OK:
    
  129.     ReactDOM.render(
    
  130.       <div style={{borderStyle: 'dotted', borderLeft: '2px solid red'}} />,
    
  131.       container,
    
  132.     );
    
  133.     expect(() =>
    
  134.       ReactDOM.render(<div style={{borderStyle: 'dotted'}} />, container),
    
  135.     ).toErrorDev(
    
  136.       'Warning: Removing a style property during rerender (borderLeft) ' +
    
  137.         'when a conflicting property is set (borderStyle) can lead to ' +
    
  138.         "styling bugs. To avoid this, don't mix shorthand and " +
    
  139.         'non-shorthand properties for the same value; instead, replace the ' +
    
  140.         'shorthand with separate values.' +
    
  141.         '\n    in div (at **)',
    
  142.     );
    
  143.   });
    
  144. });