diff --git a/packages/react-dom/src/__tests__/ReactDOMComponent-test.js b/packages/react-dom/src/__tests__/ReactDOMComponent-test.js index a25d8f9eff4c..5df5d8b294a0 100644 --- a/packages/react-dom/src/__tests__/ReactDOMComponent-test.js +++ b/packages/react-dom/src/__tests__/ReactDOMComponent-test.js @@ -445,6 +445,111 @@ describe('ReactDOMComponent', () => { expect(node.hasAttribute('data-foo')).toBe(false); }); + if (ReactFeatureFlags.enableFilterEmptyStringAttributesDOM) { + it('should not add an empty src attribute', () => { + const container = document.createElement('div'); + expect(() => ReactDOM.render(, container)).toErrorDev( + 'An empty string ("") was passed to the src attribute. ' + + 'This may cause the browser to download the whole page again over the network. ' + + 'To fix this, either do not render the element at all ' + + 'or pass null to src instead of an empty string.', + ); + const node = container.firstChild; + expect(node.hasAttribute('src')).toBe(false); + + ReactDOM.render(, container); + expect(node.hasAttribute('src')).toBe(true); + + expect(() => ReactDOM.render(, container)).toErrorDev( + 'An empty string ("") was passed to the src attribute. ' + + 'This may cause the browser to download the whole page again over the network. ' + + 'To fix this, either do not render the element at all ' + + 'or pass null to src instead of an empty string.', + ); + expect(node.hasAttribute('src')).toBe(false); + }); + + it('should not add an empty href attribute', () => { + const container = document.createElement('div'); + expect(() => ReactDOM.render(, container)).toErrorDev( + 'An empty string ("") was passed to the href attribute. ' + + 'To fix this, either do not render the element at all ' + + 'or pass null to href instead of an empty string.', + ); + const node = container.firstChild; + expect(node.hasAttribute('href')).toBe(false); + + ReactDOM.render(, container); + expect(node.hasAttribute('href')).toBe(true); + + expect(() => ReactDOM.render(, container)).toErrorDev( + 'An empty string ("") was passed to the href attribute. ' + + 'To fix this, either do not render the element at all ' + + 'or pass null to href instead of an empty string.', + ); + expect(node.hasAttribute('href')).toBe(false); + }); + + it('should not add an empty action attribute', () => { + const container = document.createElement('div'); + expect(() => ReactDOM.render(
, container)).toErrorDev( + 'An empty string ("") was passed to the action attribute. ' + + 'To fix this, either do not render the element at all ' + + 'or pass null to action instead of an empty string.', + ); + const node = container.firstChild; + expect(node.hasAttribute('action')).toBe(false); + + ReactDOM.render(, container); + expect(node.hasAttribute('action')).toBe(true); + + expect(() => ReactDOM.render(, container)).toErrorDev( + 'An empty string ("") was passed to the action attribute. ' + + 'To fix this, either do not render the element at all ' + + 'or pass null to action instead of an empty string.', + ); + expect(node.hasAttribute('action')).toBe(false); + }); + + it('should not add an empty formAction attribute', () => { + const container = document.createElement('div'); + expect(() => + ReactDOM.render(