-
Notifications
You must be signed in to change notification settings - Fork 54
add function to detect and render helmet for use in snapshots #2104
Changes from all commits
d46ae43
db0c836
140b90c
733aaa2
bf4cebe
28aed67
5e717f0
58e374d
9b3cf45
98d5bac
e86c3e7
0234a49
da16246
49d8606
b72bab5
fa62362
43b9a57
90ca65f
7b2bad9
393bfce
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Psammead test helpers should match the snapshot for the test component 1`] = ` | ||
<main> | ||
<h1> | ||
Hello I am a test component | ||
</h1> | ||
</main> | ||
`; | ||
|
||
exports[`Psammead test helpers should match the snapshot for the test component 2`] = ` | ||
<div> | ||
<h1> | ||
Hello I am a test component | ||
</h1> | ||
<p> | ||
I am some test text. | ||
</p> | ||
</div> | ||
`; | ||
|
||
exports[`Psammead test helpers should match the snapshot for the test component with helmet and other content 1`] = ` | ||
<html | ||
dir="rtl" | ||
lang="fa" | ||
> | ||
<head> | ||
<title> | ||
Snapshot with helmet and and other content | ||
</title> | ||
<meta | ||
content="test content" | ||
name="test name" | ||
/> | ||
<script | ||
src="test.js" | ||
/> | ||
</head> | ||
<body> | ||
<div> | ||
<main> | ||
<h1> | ||
Hello I am a test component with React Helmet | ||
</h1> | ||
</main> | ||
</div> | ||
</body> | ||
</html> | ||
`; | ||
|
||
exports[`Psammead test helpers should match the snapshot for the test component with helmet only 1`] = ` | ||
<html | ||
dir="rtl" | ||
lang="fa" | ||
> | ||
<head> | ||
<title> | ||
Snapshot with helmet only | ||
</title> | ||
<meta | ||
content="test content" | ||
name="test name" | ||
/> | ||
<script | ||
src="test.js" | ||
/> | ||
</head> | ||
<body> | ||
<div /> | ||
</body> | ||
</html> | ||
`; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,13 @@ | ||
import { render } from '@testing-library/react'; | ||
import 'jest-styled-components'; | ||
import deepClone from 'ramda/src/clone'; | ||
import renderWithHelmet from './renderWithHelmet'; | ||
|
||
export const shouldMatchSnapshot = (title, component) => { | ||
it(title, () => { | ||
it(title, async () => { | ||
// select the first child to remove the pointless wrapping div from snapshots | ||
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 happens now :( 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. Are you worried about the async 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. Soz i meant 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. We still do when rendering components without helmet. I'll change the comments and var names. |
||
const removeWrappingDiv = container => container.firstChild; | ||
const { container } = render(component); | ||
const { container } = await renderWithHelmet(component); | ||
const hasOneChild = container.children.length === 1; | ||
/* | ||
* if the container has more than one child then it's a component that uses a | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { render, waitForDomChange } from '@testing-library/react'; | ||
|
||
export default async component => { | ||
/* | ||
* Similar to this problem https://github.com/testing-library/react-testing-library/issues/402 | ||
* This helper was created to solve the problem of rendering helmet/head contents in snapshots. | ||
* Pass in a component that uses helmet somewhere in the component tree. | ||
* The full html tree is returned which you can then use to snapshot helmet/head contents. | ||
*/ | ||
|
||
const { container } = render(component); | ||
const noop = () => {}; | ||
const ARBITRARY_TIMEOUT = 100; // seems long enough for any dom mutations to occur | ||
const headElement = document.querySelector('head'); | ||
|
||
headElement.innerHTML = ''; // clear out head mutations from previous tests | ||
|
||
return waitForDomChange({ | ||
container: headElement, | ||
timeout: ARBITRARY_TIMEOUT, | ||
}) | ||
.catch(noop) // handle a waitForDomChange timeout | ||
.then(mutationsList => { | ||
const headMutationDetected = mutationsList && mutationsList.length; | ||
|
||
if (headMutationDetected) { | ||
// helmet was probably used so we should get the full html | ||
|
||
const htmlElement = document.querySelector('html'); | ||
|
||
const helmetElements = document.querySelectorAll( | ||
'[data-react-helmet="true"]', | ||
); | ||
|
||
const removeHelmetAttributes = el => | ||
el.removeAttribute('data-react-helmet'); // remove react-helmet attribute noise from elements | ||
|
||
removeHelmetAttributes(htmlElement); // remove react-helmet attribute noise from elements | ||
|
||
Array.from(helmetElements).forEach(removeHelmetAttributes); | ||
|
||
return { container: document.querySelector('html') }; | ||
} | ||
|
||
return { container }; | ||
}); | ||
}; |
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.
Any easy way to remove the body when not needed?
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.
I'll look into it. Was worried about adding more complexity to an already complex looking function for not much value but it should be straightforward
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.
I tried removing the body but that stopped other tests from rendering things in the body. I guess it breaks the renderer because it can't find the body element's div to put stuff in. I also tried this with removing just the div and it had the same effect :(