Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
Following #9230 (comment) except that `foo={true}` renders an empty string.
See #9230 (comment) for rationale.
  • Loading branch information
eps1lon committed May 11, 2022
1 parent aed6ac4 commit 1a617a4
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
28 changes: 25 additions & 3 deletions packages/react-dom/src/__tests__/DOMPropertyOperations-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
'use strict';

// Set by `yarn test-fire`.
const {disableInputAttributeSyncing} = require('shared/ReactFeatureFlags');
const {
enableCustomElementPropertySupport,
disableInputAttributeSyncing,
} = require('shared/ReactFeatureFlags');

describe('DOMPropertyOperations', () => {
let React;
Expand Down Expand Up @@ -256,8 +259,12 @@ describe('DOMPropertyOperations', () => {
expect(customElement.getAttribute('onstring')).toBe('hello');
expect(customElement.getAttribute('onobj')).toBe('[object Object]');
expect(customElement.getAttribute('onarray')).toBe('one,two');
expect(customElement.getAttribute('ontrue')).toBe('true');
expect(customElement.getAttribute('onfalse')).toBe('false');
expect(customElement.getAttribute('ontrue')).toBe(
enableCustomElementPropertySupport ? '' : 'true',
);
expect(customElement.getAttribute('onfalse')).toBe(
enableCustomElementPropertySupport ? null : 'false',
);

// Dispatch the corresponding event names to make sure that nothing crashes.
customElement.dispatchEvent(new Event('string'));
Expand Down Expand Up @@ -959,6 +966,21 @@ describe('DOMPropertyOperations', () => {
expect(customElement.foo).toBe(null);
});

// @gate enableCustomElementPropertySupport
it('boolean props should not be stringified in attributes', () => {
const container = document.createElement('div');
document.body.appendChild(container);
ReactDOM.render(<my-custom-element foo={true} />, container);
const customElement = container.querySelector('my-custom-element');

expect(customElement.getAttribute('foo')).toBe('');

// true => false
ReactDOM.render(<my-custom-element foo={false} />, container);

expect(customElement.getAttribute('foo')).toBe(null);
});

// @gate enableCustomElementPropertySupport
it('custom element custom event handlers assign multiple types', () => {
const container = document.createElement('div');
Expand Down
8 changes: 6 additions & 2 deletions packages/react-dom/src/__tests__/ReactDOMComponent-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2689,9 +2689,13 @@ describe('ReactDOMComponent', () => {
const container = document.createElement('div');
ReactDOM.render(<some-custom-element foo={true} />, container);
const node = container.firstChild;
expect(node.getAttribute('foo')).toBe('true');
expect(node.getAttribute('foo')).toBe(
ReactFeatureFlags.enableCustomElementPropertySupport ? '' : 'true',
);
ReactDOM.render(<some-custom-element foo={false} />, container);
expect(node.getAttribute('foo')).toBe('false');
expect(node.getAttribute('foo')).toBe(
ReactFeatureFlags.enableCustomElementPropertySupport ? null : 'false',
);
ReactDOM.render(<some-custom-element />, container);
expect(node.hasAttribute('foo')).toBe(false);
ReactDOM.render(<some-custom-element foo={true} />, container);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -696,12 +696,20 @@ describe('ReactDOMServerIntegration', () => {

itRenders('unknown boolean `true` attributes as strings', async render => {
const e = await render(<custom-element foo={true} />);
expect(e.getAttribute('foo')).toBe('true');
if (ReactFeatureFlags.enableCustomElementPropertySupport) {
expect(e.getAttribute('foo')).toBe('');
} else {
expect(e.getAttribute('foo')).toBe('true');
}
});

itRenders('unknown boolean `false` attributes as strings', async render => {
const e = await render(<custom-element foo={false} />);
expect(e.getAttribute('foo')).toBe('false');
if (ReactFeatureFlags.enableCustomElementPropertySupport) {
expect(e.getAttribute('foo')).toBe(null);
} else {
expect(e.getAttribute('foo')).toBe('false');
}
});

itRenders(
Expand Down

0 comments on commit 1a617a4

Please sign in to comment.