Skip to content

Add more test coverage for custom elements #11811

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions packages/react-dom/src/__tests__/ReactDOMComponent-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2551,4 +2551,34 @@ describe('ReactDOMComponent', () => {
}
});
});

// These tests mostly verify the existing behavior.
// It may not always makes sense but we can't change it in minors.
describe('Custom elements', () => {
it('does not strip unknown boolean attributes', () => {
const container = document.createElement('div');
ReactDOM.render(<some-custom-element foo={true} />, container);
const node = container.firstChild;
expect(node.getAttribute('foo')).toBe('true');
ReactDOM.render(<some-custom-element foo={false} />, container);
expect(node.getAttribute('foo')).toBe('false');
ReactDOM.render(<some-custom-element />, container);
expect(node.hasAttribute('foo')).toBe(false);
ReactDOM.render(<some-custom-element foo={true} />, container);
expect(node.hasAttribute('foo')).toBe(true);
});

it('does not strip the on* attributes', () => {
const container = document.createElement('div');
ReactDOM.render(<some-custom-element onx="bar" />, container);
const node = container.firstChild;
expect(node.getAttribute('onx')).toBe('bar');
ReactDOM.render(<some-custom-element onx="buzz" />, container);
expect(node.getAttribute('onx')).toBe('buzz');
ReactDOM.render(<some-custom-element />, container);
expect(node.hasAttribute('onx')).toBe(false);
ReactDOM.render(<some-custom-element onx="bar" />, container);
expect(node.getAttribute('onx')).toBe('bar');
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -213,16 +213,6 @@ describe('ReactDOMServerIntegration', () => {
expect(e.className).toBe('test');
},
);

itRenders('class for custom elements', async render => {
const e = await render(<div is="custom-element" class="test" />, 0);
expect(e.getAttribute('class')).toBe('test');
});

itRenders('className for custom elements', async render => {
const e = await render(<div is="custom-element" className="test" />, 0);
expect(e.getAttribute('className')).toBe('test');
});
});

describe('htmlFor property', function() {
Expand Down Expand Up @@ -256,16 +246,6 @@ describe('ReactDOMServerIntegration', () => {
const e = await render(<div htmlFor={null} />);
expect(e.hasAttribute('htmlFor')).toBe(false);
});

itRenders('htmlFor attribute on custom elements', async render => {
const e = await render(<div is="custom-element" htmlFor="test" />);
expect(e.getAttribute('htmlFor')).toBe('test');
});

itRenders('for attribute on custom elements', async render => {
const e = await render(<div is="custom-element" for="test" />);
expect(e.getAttribute('for')).toBe('test');
});
});

describe('numeric properties', function() {
Expand Down Expand Up @@ -529,35 +509,6 @@ describe('ReactDOMServerIntegration', () => {
expect(e.getAttribute('foo')).toBe('bar');
});

itRenders('unknown attributes for custom elements', async render => {
const e = await render(<custom-element foo="bar" />);
expect(e.getAttribute('foo')).toBe('bar');
});

itRenders(
'no unknown attributes for custom elements with null value',
async render => {
const e = await render(<custom-element foo={null} />);
expect(e.hasAttribute('foo')).toBe(false);
},
);

itRenders(
'unknown attributes for custom elements using is',
async render => {
const e = await render(<div is="custom-element" foo="bar" />);
expect(e.getAttribute('foo')).toBe('bar');
},
);

itRenders(
'no unknown attributes for custom elements using is with null value',
async render => {
const e = await render(<div is="custom-element" foo={null} />);
expect(e.hasAttribute('foo')).toBe(false);
},
);

itRenders('SVG tags with dashes in them', async render => {
const e = await render(
<svg>
Expand Down Expand Up @@ -594,4 +545,72 @@ describe('ReactDOMServerIntegration', () => {
expect(e.getAttribute('on')).toEqual('tap:do-something');
});
});

// These tests mostly verify the existing behavior.
// It may not always make sense but we can't change it in minors.
describe('custom elements', () => {
itRenders('class for custom elements', async render => {
const e = await render(<div is="custom-element" class="test" />, 0);
expect(e.getAttribute('class')).toBe('test');
});

itRenders('className for custom elements', async render => {
const e = await render(<div is="custom-element" className="test" />, 0);
expect(e.getAttribute('className')).toBe('test');
});

itRenders('htmlFor attribute on custom elements', async render => {
const e = await render(<div is="custom-element" htmlFor="test" />);
expect(e.getAttribute('htmlFor')).toBe('test');
});

itRenders('for attribute on custom elements', async render => {
const e = await render(<div is="custom-element" for="test" />);
expect(e.getAttribute('for')).toBe('test');
});

itRenders('unknown attributes for custom elements', async render => {
const e = await render(<custom-element foo="bar" />);
expect(e.getAttribute('foo')).toBe('bar');
});

itRenders('unknown `on*` attributes for custom elements', async render => {
const e = await render(<custom-element onunknown="bar" />);
expect(e.getAttribute('onunknown')).toBe('bar');
});

itRenders('unknown boolean `true` attributes as strings', async render => {
const e = await render(<custom-element foo={true} />);
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');
});

itRenders(
'no unknown attributes for custom elements with null value',
async render => {
const e = await render(<custom-element foo={null} />);
expect(e.hasAttribute('foo')).toBe(false);
},
);

itRenders(
'unknown attributes for custom elements using is',
async render => {
const e = await render(<div is="custom-element" foo="bar" />);
expect(e.getAttribute('foo')).toBe('bar');
},
);

itRenders(
'no unknown attributes for custom elements using is with null value',
async render => {
const e = await render(<div is="custom-element" foo={null} />);
expect(e.hasAttribute('foo')).toBe(false);
},
);
});
});