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