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

ValidateDOMNesting tests(#11299) #11742

Merged
merged 4 commits into from
Dec 7, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
184 changes: 0 additions & 184 deletions packages/react-dom/src/__tests__/validateDOMNesting-test.internal.js

This file was deleted.

122 changes: 122 additions & 0 deletions packages/react-dom/src/__tests__/validateDOMNesting-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/**
* Copyright (c) 2015-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';

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

function normalizeCodeLocInfo(str) {
return str && str.replace(/at .+?:\d+/g, 'at **');
}

function expectInvalidNestingWarning(shouldWarn, tagsList, warningsList = []) {
let element = null;
let tags = tagsList;
Copy link
Collaborator

Choose a reason for hiding this comment

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

This doesn’t copy the array, it only creates a reference to it. Therefore the splice call later mutates it.

It would be clearer IMO if we didn’t mutate the input. Otherwise, for example, declaring an array as a variable in the test and passing it two times wouldn’t work. We don’t do this but it could be potentially confusing in the future.

To solve it we need to copy the array on this line.

if (__DEV__) {
console.error.calls.reset();
}
const container = document.createElement(tags.splice(0, 1));
while (tags.length) {
element = React.createElement(tags.pop(), null, element);
}
ReactDOM.render(element, container);

if (__DEV__) {
let warnings = warningsList;
if (shouldWarn) {
expect(console.error.calls.count()).toEqual(warningsList.length);
while (warnings.length) {
expect(
normalizeCodeLocInfo(
console.error.calls.argsFor(warnings.length - 1)[0],
),
).toContain(warnings.pop());
}
} else {
expect(console.error.calls.count()).toEqual(0);
}
}
}

describe('validateDOMNesting', () => {
it('allows valid nestings', () => {
spyOnDev(console, 'error');
expectInvalidNestingWarning(false, ['table', 'tbody', 'tr', 'td', 'b']);
expectInvalidNestingWarning(false, ['div', 'a', 'object', 'a']);
expectInvalidNestingWarning(false, ['div', 'p', 'button', 'p']);
expectInvalidNestingWarning(false, ['p', 'svg', 'foreignObject', 'p']);
expectInvalidNestingWarning(false, ['html', 'body', 'div']);

// Invalid, but not changed by browser parsing so we allow them
expectInvalidNestingWarning(false, ['div', 'ul', 'ul', 'li']);
expectInvalidNestingWarning(false, ['div', 'label', 'div']);
expectInvalidNestingWarning(false, ['div', 'ul', 'li', 'section', 'li']);
expectInvalidNestingWarning(false, ['div', 'ul', 'li', 'dd', 'li']);
});

it('prevents problematic nestings', () => {
spyOnDev(console, 'error');
expectInvalidNestingWarning(
true,
['body', 'datalist', 'option'],
[
'render(): Rendering components directly into document.body is discouraged',
],
);
expectInvalidNestingWarning(
true,
['table', 'tr'],
['validateDOMNesting(...): <tr> cannot appear as a child of <table>'],
);
expectInvalidNestingWarning(
true,
['p', 'p'],
['validateDOMNesting(...): <p> cannot appear as a descendant of <p>'],
);
expectInvalidNestingWarning(
true,
['div', 'ul', 'li', 'div', 'li'],
['validateDOMNesting(...): <li> cannot appear as a descendant of <li>'],
);
expectInvalidNestingWarning(
true,
['div', 'html'],
['validateDOMNesting(...): <html> cannot appear as a child of <div>'],
);
expectInvalidNestingWarning(
true,
['body', 'body'],
[
'render(): Rendering components directly into document.body is discouraged',
'validateDOMNesting(...): <body> cannot appear as a child of <body>',
],
);
expectInvalidNestingWarning(
true,
['svg', 'foreignObject', 'body', 'p'],
[
'validateDOMNesting(...): <body> cannot appear as a child of <foreignObject>',
'<foreignObject /> is using uppercase HTML',
],
);
expectInvalidNestingWarning(
true,
['a', 'a'],
['validateDOMNesting(...): <a> cannot appear as a descendant of <a>'],
);
expectInvalidNestingWarning(
true,
['form', 'form'],
[
'validateDOMNesting(...): <form> cannot appear as a descendant of <form>',
],
);
});
});
11 changes: 0 additions & 11 deletions packages/react-dom/src/client/validateDOMNesting.js
Original file line number Diff line number Diff line change
Expand Up @@ -482,17 +482,6 @@ if (__DEV__) {

// TODO: turn this into a named export
validateDOMNesting.updatedAncestorInfo = updatedAncestorInfo;

// For testing
validateDOMNesting.isTagValidInContext = function(tag, ancestorInfo) {
ancestorInfo = ancestorInfo || emptyAncestorInfo;
const parentInfo = ancestorInfo.current;
const parentTag = parentInfo && parentInfo.tag;
return (
isTagValidWithParent(tag, parentTag) &&
!findInvalidAncestorForTag(tag, ancestorInfo)
);
};
}

export default validateDOMNesting;