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

View File

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

View File

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

View File

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

View File

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

View File

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