-
Notifications
You must be signed in to change notification settings - Fork 47.2k
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
[assert helpers] ReactChildren-test #31844
Merged
rickhanlonii
merged 1 commit into
facebook:main
from
rickhanlonii:rh/asserts/reactchildren
Dec 19, 2024
Merged
Changes from all commits
Commits
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
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 |
---|---|---|
|
@@ -13,12 +13,13 @@ describe('ReactChildren', () => { | |
let React; | ||
let ReactDOMClient; | ||
let act; | ||
let assertConsoleErrorDev; | ||
|
||
beforeEach(() => { | ||
jest.resetModules(); | ||
React = require('react'); | ||
ReactDOMClient = require('react-dom/client'); | ||
act = require('internal-test-utils').act; | ||
({act, assertConsoleErrorDev} = require('internal-test-utils')); | ||
}); | ||
|
||
it('should support identity for simple', () => { | ||
|
@@ -331,14 +332,16 @@ describe('ReactChildren', () => { | |
callback.mockClear(); | ||
} | ||
|
||
let instance; | ||
expect(() => { | ||
instance = <div>{threeDivIterable}</div>; | ||
}).toErrorDev( | ||
const instance = <div>{threeDivIterable}</div>; | ||
assertConsoleErrorDev( | ||
// With the flag on this doesn't warn eagerly but only when rendered | ||
gate(flag => flag.enableOwnerStacks) | ||
? [] | ||
: ['Each child in a list should have a unique "key" prop.'], | ||
: [ | ||
'Each child in a list should have a unique "key" prop.\n\n' + | ||
'Check the top-level render call using <div>. See https://react.dev/link/warning-keys for more information.\n' + | ||
' in div (at **)', | ||
], | ||
); | ||
|
||
React.Children.forEach(instance.props.children, callback, context); | ||
|
@@ -359,11 +362,16 @@ describe('ReactChildren', () => { | |
|
||
const container = document.createElement('div'); | ||
const root = ReactDOMClient.createRoot(container); | ||
await expect(async () => { | ||
await act(() => { | ||
root.render(instance); | ||
}); | ||
}).toErrorDev('Each child in a list should have a unique "key" prop.'); | ||
await act(() => { | ||
root.render(instance); | ||
}); | ||
assertConsoleErrorDev([ | ||
'Each child in a list should have a unique "key" prop.\n\n' + | ||
'Check the top-level render call using <div>. It was passed a child from div.' + | ||
' See https://react.dev/link/warning-keys for more information.\n' + | ||
' in div (at **)' + | ||
(gate(flag => flag.enableOwnerStacks) ? '' : '\n in div (at **)'), | ||
]); | ||
}); | ||
|
||
it('should be called for each child in an iterable with keys', () => { | ||
|
@@ -879,15 +887,29 @@ describe('ReactChildren', () => { | |
|
||
const container = document.createElement('div'); | ||
const root = ReactDOMClient.createRoot(container); | ||
await expect(async () => { | ||
await act(() => { | ||
root.render( | ||
<ComponentRenderingMappedChildren> | ||
{[<div />]} | ||
</ComponentRenderingMappedChildren>, | ||
); | ||
}); | ||
}).toErrorDev(['Each child in a list should have a unique "key" prop.']); | ||
await act(() => { | ||
root.render( | ||
<ComponentRenderingMappedChildren> | ||
{[<div />]} | ||
</ComponentRenderingMappedChildren>, | ||
); | ||
}); | ||
assertConsoleErrorDev( | ||
gate(flags => flags.enableOwnerStacks) | ||
? [ | ||
'Each child in a list should have a unique "key" prop.\n\n' + | ||
'Check the render method of `ComponentRenderingMappedChildren`.' + | ||
' See https://react.dev/link/warning-keys for more information.\n' + | ||
' in div (at **)\n' + | ||
' in **/ReactChildren-test.js:**:** (at **)', | ||
] | ||
: [ | ||
'Each child in a list should have a unique "key" prop.\n\n' + | ||
'Check the top-level render call using <ComponentRenderingMappedChildren>.' + | ||
' See https://react.dev/link/warning-keys for more information.\n' + | ||
' in div (at **)', | ||
], | ||
); | ||
}); | ||
|
||
it('does not warn for mapped static children without keys', async () => { | ||
|
@@ -903,16 +925,14 @@ describe('ReactChildren', () => { | |
|
||
const container = document.createElement('div'); | ||
const root = ReactDOMClient.createRoot(container); | ||
await expect(async () => { | ||
await act(() => { | ||
root.render( | ||
<ComponentRenderingMappedChildren> | ||
<div /> | ||
<div /> | ||
</ComponentRenderingMappedChildren>, | ||
); | ||
}); | ||
}).toErrorDev([]); | ||
await act(() => { | ||
root.render( | ||
<ComponentRenderingMappedChildren> | ||
<div /> | ||
<div /> | ||
</ComponentRenderingMappedChildren>, | ||
); | ||
}); | ||
}); | ||
|
||
it('warns for cloned list children without keys', async () => { | ||
|
@@ -926,15 +946,28 @@ describe('ReactChildren', () => { | |
|
||
const container = document.createElement('div'); | ||
const root = ReactDOMClient.createRoot(container); | ||
await expect(async () => { | ||
await act(() => { | ||
root.render( | ||
<ComponentRenderingClonedChildren> | ||
{[<div />]} | ||
</ComponentRenderingClonedChildren>, | ||
); | ||
}); | ||
}).toErrorDev(['Each child in a list should have a unique "key" prop.']); | ||
await act(() => { | ||
root.render( | ||
<ComponentRenderingClonedChildren> | ||
{[<div />]} | ||
</ComponentRenderingClonedChildren>, | ||
); | ||
}); | ||
assertConsoleErrorDev( | ||
gate(flags => flags.enableOwnerStacks) | ||
? [ | ||
'Each child in a list should have a unique "key" prop.\n\n' + | ||
'Check the render method of `ComponentRenderingClonedChildren`.' + | ||
' See https://react.dev/link/warning-keys for more information.\n' + | ||
' in div (at **)', | ||
] | ||
: [ | ||
'Each child in a list should have a unique "key" prop.\n\n' + | ||
'Check the top-level render call using <ComponentRenderingClonedChildren>.' + | ||
' See https://react.dev/link/warning-keys for more information.\n' + | ||
' in div (at **)', | ||
], | ||
); | ||
}); | ||
|
||
it('does not warn for cloned static children without keys', async () => { | ||
|
@@ -948,16 +981,14 @@ describe('ReactChildren', () => { | |
|
||
const container = document.createElement('div'); | ||
const root = ReactDOMClient.createRoot(container); | ||
await expect(async () => { | ||
await act(() => { | ||
root.render( | ||
<ComponentRenderingClonedChildren> | ||
<div /> | ||
<div /> | ||
</ComponentRenderingClonedChildren>, | ||
); | ||
}); | ||
}).toErrorDev([]); | ||
await act(() => { | ||
root.render( | ||
<ComponentRenderingClonedChildren> | ||
<div /> | ||
<div /> | ||
</ComponentRenderingClonedChildren>, | ||
); | ||
}); | ||
Comment on lines
+984
to
+991
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this doesn't error anymore? |
||
}); | ||
|
||
it('warns for flattened list children without keys', async () => { | ||
|
@@ -967,15 +998,28 @@ describe('ReactChildren', () => { | |
|
||
const container = document.createElement('div'); | ||
const root = ReactDOMClient.createRoot(container); | ||
await expect(async () => { | ||
await act(() => { | ||
root.render( | ||
<ComponentRenderingFlattenedChildren> | ||
{[<div />]} | ||
</ComponentRenderingFlattenedChildren>, | ||
); | ||
}); | ||
}).toErrorDev(['Each child in a list should have a unique "key" prop.']); | ||
await act(() => { | ||
root.render( | ||
<ComponentRenderingFlattenedChildren> | ||
{[<div />]} | ||
</ComponentRenderingFlattenedChildren>, | ||
); | ||
}); | ||
assertConsoleErrorDev( | ||
gate(flags => flags.enableOwnerStacks) | ||
? [ | ||
'Each child in a list should have a unique "key" prop.\n\n' + | ||
'Check the render method of `ComponentRenderingFlattenedChildren`.' + | ||
' See https://react.dev/link/warning-keys for more information.\n' + | ||
' in div (at **)', | ||
] | ||
: [ | ||
'Each child in a list should have a unique "key" prop.\n\n' + | ||
'Check the top-level render call using <ComponentRenderingFlattenedChildren>.' + | ||
' See https://react.dev/link/warning-keys for more information.\n' + | ||
' in div (at **)', | ||
], | ||
); | ||
}); | ||
|
||
it('does not warn for flattened static children without keys', async () => { | ||
|
@@ -985,16 +1029,14 @@ describe('ReactChildren', () => { | |
|
||
const container = document.createElement('div'); | ||
const root = ReactDOMClient.createRoot(container); | ||
await expect(async () => { | ||
await act(() => { | ||
root.render( | ||
<ComponentRenderingFlattenedChildren> | ||
<div /> | ||
<div /> | ||
</ComponentRenderingFlattenedChildren>, | ||
); | ||
}); | ||
}).toErrorDev([]); | ||
await act(() => { | ||
root.render( | ||
<ComponentRenderingFlattenedChildren> | ||
<div /> | ||
<div /> | ||
</ComponentRenderingFlattenedChildren>, | ||
); | ||
}); | ||
}); | ||
|
||
it('should escape keys', () => { | ||
|
@@ -1153,18 +1195,16 @@ describe('ReactChildren', () => { | |
|
||
const container = document.createElement('div'); | ||
const root = ReactDOMClient.createRoot(container); | ||
await expect(async () => { | ||
await act(() => { | ||
root.render(<ComponentReturningArray />); | ||
}); | ||
}).toErrorDev( | ||
'' + | ||
'Each child in a list should have a unique "key" prop.' + | ||
await act(() => { | ||
root.render(<ComponentReturningArray />); | ||
}); | ||
assertConsoleErrorDev([ | ||
'Each child in a list should have a unique "key" prop.' + | ||
'\n\nCheck the top-level render call using <ComponentReturningArray>. It was passed a child from ComponentReturningArray. ' + | ||
'See https://react.dev/link/warning-keys for more information.' + | ||
'\n in div (at **)' + | ||
'\n in ComponentReturningArray (at **)', | ||
); | ||
]); | ||
}); | ||
|
||
it('does not warn when there are keys on elements in a fragment', async () => { | ||
|
@@ -1184,17 +1224,15 @@ describe('ReactChildren', () => { | |
it('warns for keys for arrays at the top level', async () => { | ||
const container = document.createElement('div'); | ||
const root = ReactDOMClient.createRoot(container); | ||
await expect(async () => { | ||
await act(() => { | ||
root.render([<div />, <div />]); | ||
}); | ||
}).toErrorDev( | ||
'' + | ||
'Each child in a list should have a unique "key" prop.' + | ||
await act(() => { | ||
root.render([<div />, <div />]); | ||
}); | ||
assertConsoleErrorDev([ | ||
'Each child in a list should have a unique "key" prop.' + | ||
'\n\nCheck the top-level render call using <Root>. ' + | ||
'See https://react.dev/link/warning-keys for more information.' + | ||
'\n in div (at **)', | ||
); | ||
]); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
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.
no error here anymore?
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.
Yeah it's confusing but the
toErrorDev()
helper is a console.error, and since the array was empty it means there were no console errors. The assert helpers throw if a test ends with un-asserted logs, so no assertion is needed.