Skip to content
This repository has been archived by the owner on Aug 13, 2023. It is now read-only.

add function to detect and render helmet for use in snapshots #2104

Merged
merged 20 commits into from
Sep 13, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions packages/utilities/psammead-test-helpers/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<!-- prettier-ignore -->
| Version | Description |
|---------|-------------|
| 3.0.0 | [PR#2104](https://github.com/bbc/psammead/pull/2104) add function to detect and render helmet for use in snapshots |
| 2.0.2 | [PR#1994](https://github.com/bbc/psammead/pull/1994) Add comment to clarify new shouldMatchSnapshot logic |
| 2.0.1 | [PR#1958](https://github.com/bbc/psammead/pull/1958) Update `shouldMatchSnapshot` to add firstChild logic |
| 2.0.0 | [PR#1917](https://github.com/bbc/psammead/pull/1917) Remove react-test-renderer and shallow rendering |
Expand Down
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>
Copy link
Contributor

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?

Copy link
Contributor Author

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

Copy link
Contributor Author

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 :(

<div />
</body>
</html>
`;
128 changes: 128 additions & 0 deletions packages/utilities/psammead-test-helpers/index.test.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import React from 'react';
import Helmet from 'react-helmet';
import * as testHelpers from '.';
import * as testHelpersFromSrc from './src/index';
import renderWithHelmet from './src/renderWithHelmet';

const testHelpersExpectedExports = {
shouldMatchSnapshot: 'function',
Expand All @@ -16,6 +19,13 @@ const actualExports = { testHelpers };

const actualExportsFromSrc = { testHelpers: testHelpersFromSrc };

const serializeDomString = domString =>
domString
.split('\n')
.map(line => line.trim())
.join('')
.trim();

const ensureErrorWhenMissingExport = testHelperMethod => {
// missing export in the expected
const actualWithAll = { utility: { foo: {}, bar: {} } };
Expand Down Expand Up @@ -104,4 +114,122 @@ describe('Psammead test helpers', () => {
testHelpersFromSrc.testUtilityPackages,
);
});

const NoHelmet = () => (
<main>
<h1>Hello I am a test component</h1>
</main>
);

it('should return correct HTML for components not using helmet', async () => {
const actual = await renderWithHelmet(<NoHelmet />);
const expected = serializeDomString(`
<div>
<main>
<h1>Hello I am a test component</h1>
</main>
</div>
`);

expect(actual.container.outerHTML).toEqual(expected);
});

testHelpers.shouldMatchSnapshot(
'should match the snapshot for the test component',
<NoHelmet />,
);

const NoHelmetWithFragment = () => (
<>
<h1>Hello I am a test component</h1>
<p>I am some test text.</p>
</>
);

it('should return correct HTML for components not using helmet and wrapped with fragment', async () => {
const actual = await renderWithHelmet(<NoHelmetWithFragment />);
const expected = serializeDomString(`
<div>
<h1>Hello I am a test component</h1>
<p>I am some test text.</p>
</div>
`);

expect(actual.container.outerHTML).toEqual(expected);
});

testHelpers.shouldMatchSnapshot(
'should match the snapshot for the test component',
<NoHelmetWithFragment />,
);

const HelmetOnly = () => (
<Helmet htmlAttributes={{ dir: 'rtl', lang: 'fa' }}>
<title>Snapshot with helmet only</title>
<meta name="test name" content="test content" />
<script src="test.js" />
</Helmet>
);

it('should return correct HTML for components using helmet only', async () => {
const actual = await renderWithHelmet(<HelmetOnly />);
const expected = serializeDomString(`
<html dir="rtl" lang="fa">
<head>
<title>Snapshot with helmet only</title>
<meta name="test name" content="test content"><script src="test.js"></script>
</head>
<body>
<div></div>
</body>
</html>
`);

expect(actual.container.outerHTML).toEqual(expected);
});

testHelpers.shouldMatchSnapshot(
'should match the snapshot for the test component with helmet only',
<HelmetOnly />,
);

const HelmetWithContent = () => (
<>
<Helmet htmlAttributes={{ dir: 'rtl', lang: 'fa' }}>
<title>Snapshot with helmet and and other content</title>
<meta name="test name" content="test content" />
<script src="test.js" />
</Helmet>
<main>
<h1>Hello I am a test component with React Helmet</h1>
</main>
</>
);

it('should return correct HTML for components using helmet only with other content', async () => {
const actual = await renderWithHelmet(<HelmetWithContent />);
const expected = serializeDomString(`
<html dir="rtl" lang="fa">
<head>
<title>Snapshot with helmet and and other content</title>
<meta name="test name" content="test content">
<script src="test.js"></script>
</head>
<body>
<div>
<main>
<h1>Hello I am a test component with React Helmet</h1>
</main>
</div>
</body>
</html>
`);

expect(actual.container.outerHTML).toEqual(expected);
});

testHelpers.shouldMatchSnapshot(
'should match the snapshot for the test component with helmet and other content',
<HelmetWithContent />,
);
});
73 changes: 72 additions & 1 deletion packages/utilities/psammead-test-helpers/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion packages/utilities/psammead-test-helpers/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bbc/psammead-test-helpers",
"version": "2.0.2",
"version": "3.0.0",
"main": "dist/index.js",
"module": "esm/index.js",
"sideEffects": false,
Expand Down Expand Up @@ -28,5 +28,8 @@
],
"dependencies": {
"jest-styled-components": "^6.3.3"
},
"devDependencies": {
"react-helmet": "^5.2.1"
}
}
5 changes: 3 additions & 2 deletions packages/utilities/psammead-test-helpers/src/index.js
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
Copy link
Contributor

Choose a reason for hiding this comment

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

This happens now :(

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Are you worried about the async it callback? It won't affect the current test helper API.

Copy link
Contributor

@dr3 dr3 Sep 13, 2019

Choose a reason for hiding this comment

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

Soz i meant select the first child to remove the pointless wrapping div from snapshots happens now. We no longer remove the pointless wrapping div

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
Expand Down
47 changes: 47 additions & 0 deletions packages/utilities/psammead-test-helpers/src/renderWithHelmet.js
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 };
});
};