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,29 +1,23 @@
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, const htmlAttributes = {
description: PropTypes.string, lang: i18n.language
i18n: PropTypes.shape({ };
language: PropTypes.string.isRequired
}).isRequired
}
render() { return <Helmet htmlAttributes={ htmlAttributes }>
const { title, description, i18n } = this.props; <title>{ title ? `Regexper - ${ title }` : 'Regexper' }</title>
const htmlAttributes = { { description && <meta name="description" content={ description } /> }
lang: i18n.language </Helmet>;
}; };
return <Helmet htmlAttributes={ htmlAttributes }> Metadata.propTypes = {
<title>{ title ? `Regexper - ${ title }` : 'Regexper' }</title> title: PropTypes.string,
{ description && <meta name="description" content={ description } /> } description: PropTypes.string
</Helmet>; };
}
}
export { Metadata }; export default Metadata;
export default withTranslation()(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)\\"
}" }"