Updating Metadata to use hooks

This commit is contained in:
Jeff Avallone 2019-03-26 18:44:17 -04:00
parent 8186e1cf87
commit 94d7f3d3ce
6 changed files with 25 additions and 40 deletions

View File

@ -5,9 +5,7 @@ exports[`Metadata rendering 1`] = `
<span <span
data-component="HelmetWrapper" data-component="HelmetWrapper"
data-props="{ data-props="{
\\"htmlAttributes\\": { \\"htmlAttributes\\": {}
\\"lang\\": \\"test-lang\\"
}
}" }"
> >
<title> <title>
@ -22,9 +20,7 @@ exports[`Metadata rendering with a title and description 1`] = `
<span <span
data-component="HelmetWrapper" data-component="HelmetWrapper"
data-props="{ data-props="{
\\"htmlAttributes\\": { \\"htmlAttributes\\": {}
\\"lang\\": \\"test-lang\\"
}
}" }"
> >
<title> <title>

View File

@ -1,19 +1,10 @@
import React from 'react'; import React from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import { withTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
import { Helmet } from 'react-helmet'; import { Helmet } from 'react-helmet';
class Metadata extends React.PureComponent { const Metadata = ({ title, description }) => {
static propTypes = { const { i18n } = useTranslation();
title: PropTypes.string,
description: PropTypes.string,
i18n: PropTypes.shape({
language: PropTypes.string.isRequired
}).isRequired
}
render() {
const { title, description, i18n } = this.props;
const htmlAttributes = { const htmlAttributes = {
lang: i18n.language lang: i18n.language
}; };
@ -22,8 +13,11 @@ class Metadata extends React.PureComponent {
<title>{ title ? `Regexper - ${ title }` : 'Regexper' }</title> <title>{ title ? `Regexper - ${ title }` : 'Regexper' }</title>
{ description && <meta name="description" content={ description } /> } { description && <meta name="description" content={ description } /> }
</Helmet>; </Helmet>;
} };
}
export { Metadata }; Metadata.propTypes = {
export default withTranslation()(Metadata); title: PropTypes.string,
description: PropTypes.string
};
export default Metadata;

View File

@ -9,16 +9,12 @@ jest.mock('react-helmet', () => {
import React from 'react'; import React from 'react';
import { render } from 'react-testing-library'; import { render } from 'react-testing-library';
import { Metadata } from 'components/Metadata'; import Metadata from 'components/Metadata';
const commonProps = {
i18n: { language: 'test-lang' }
};
describe('Metadata', () => { describe('Metadata', () => {
test('rendering', () => { test('rendering', () => {
const { asFragment } = render( const { asFragment } = render(
<Metadata { ...commonProps } /> <Metadata/>
); );
expect(asFragment()).toMatchSnapshot(); expect(asFragment()).toMatchSnapshot();
}); });
@ -27,8 +23,7 @@ describe('Metadata', () => {
const { asFragment } = render( const { asFragment } = render(
<Metadata <Metadata
title="Testing" title="Testing"
description="Test description" description="Test description" />
{ ...commonProps } />
); );
expect(asFragment()).toMatchSnapshot(); expect(asFragment()).toMatchSnapshot();
}); });

View File

@ -3,7 +3,7 @@
exports[`Error Page rendering 1`] = ` exports[`Error Page rendering 1`] = `
<DocumentFragment> <DocumentFragment>
<span <span
data-component="withI18nextTranslation(Metadata)" data-component="Metadata"
data-props="{ data-props="{
\\"title\\": \\"TRANSLATE(Page Not Found)\\" \\"title\\": \\"TRANSLATE(Page Not Found)\\"
}" }"

View File

@ -3,7 +3,7 @@
exports[`Index Page rendering 1`] = ` exports[`Index Page rendering 1`] = `
<DocumentFragment> <DocumentFragment>
<span <span
data-component="withI18nextTranslation(Metadata)" data-component="Metadata"
data-props="{ data-props="{
\\"description\\": \\"Test description\\" \\"description\\": \\"Test description\\"
}" }"
@ -33,7 +33,7 @@ exports[`Index Page rendering 1`] = `
exports[`Index Page rendering with an expression on the URL 1`] = ` exports[`Index Page rendering with an expression on the URL 1`] = `
<DocumentFragment> <DocumentFragment>
<span <span
data-component="withI18nextTranslation(Metadata)" data-component="Metadata"
data-props="{ data-props="{
\\"description\\": \\"Test description\\" \\"description\\": \\"Test description\\"
}" }"

View File

@ -3,7 +3,7 @@
exports[`Privacy Page rendering 1`] = ` exports[`Privacy Page rendering 1`] = `
<DocumentFragment> <DocumentFragment>
<span <span
data-component="withI18nextTranslation(Metadata)" data-component="Metadata"
data-props="{ data-props="{
\\"title\\": \\"TRANSLATE(Privacy Policy)\\" \\"title\\": \\"TRANSLATE(Privacy Policy)\\"
}" }"