Skip to content
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

Rewrite setInnerHTML tests to use Public API. #11385

Merged
merged 8 commits into from
Nov 4, 2017
Merged
Show file tree
Hide file tree
Changes from 6 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
* Copyright (c) 2016-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @emails react-core
*/

'use strict';

const React = require('react');
const ReactDOM = require('react-dom');

describe('dangerouslySetInnerHTML', () => {
describe('when the node has innerHTML property', () => {
it('sets innerHTML on it', () => {
const container = document.createElement('div');
const node = ReactDOM.render(
<div dangerouslySetInnerHTML={{__html: '<h1>Hello</h1>'}} />,
container,
);
expect(node.innerHTML).toBe('<h1>Hello</h1>');
});
});

describe('when the node does not have an innerHTML property', () => {
let container;
beforeEach(() => {
// Create a mock container that looks like a svg in IE (without innerHTML)
container = document.createElementNS('http://www.w3.org/2000/svg', 'svg');

spyOn(container, 'appendChild').and.callThrough();
spyOn(container, 'removeChild').and.callThrough();
});

it('sets innerHTML on it', () => {
const html = '<circle></circle>';

ReactDOM.render(
<g dangerouslySetInnerHTML={{__html: html}} />,
container,
);

expect(container.appendChild.calls.argsFor(0)[0].innerHTML).toBe(
'<circle></circle>',
);
});

it('clears previous children', () => {
const firstHtml = '<rect></rect>';
const secondHtml = '<circle></circle>';

ReactDOM.render(
<g dangerouslySetInnerHTML={{__html: firstHtml}} />,
container,
);
ReactDOM.unmountComponentAtNode(container);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is odd. Why do we need this?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems to defeat the purpose of the test (which is to verify setInnerHTML itself cleans up old nodes).

Copy link
Collaborator

@gaearon gaearon Nov 4, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is mistakingly testing that unmounting removes a child, and mounting into the same container later adds it.

Instead of testing that inside an already mounted container, operations setting dangerous HTML on SVG node, if it doesn't have innerHTML (as in IE11), will use appendChild / removeChild.

ReactDOM.render(
<g dangerouslySetInnerHTML={{__html: secondHtml}} />,
container,
);

expect(container.removeChild.calls.argsFor(0)[0].innerHTML).toBe(
'<rect></rect>',
);
expect(container.appendChild.calls.argsFor(1)[0].innerHTML).toBe(
'<circle></circle>',
);
});
});
});
68 changes: 0 additions & 68 deletions packages/react-dom/src/client/__tests__/setInnerHTML-test.js

This file was deleted.

Loading