-
Notifications
You must be signed in to change notification settings - Fork 47k
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
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
184 changes: 0 additions & 184 deletions
184
packages/react-dom/src/__tests__/validateDOMNesting-test.internal.js
This file was deleted.
Oops, something went wrong.
122 changes: 122 additions & 0 deletions
122
packages/react-dom/src/__tests__/validateDOMNesting-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
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>', | ||
], | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.