Revert "Adding a HOC for using StaticQuery"

This reverts commit 3b11fcb0b6.
This commit is contained in:
Jeff Avallone 2019-01-06 16:26:55 -05:00
parent 3b11fcb0b6
commit 8426eaa433
8 changed files with 57 additions and 57 deletions

View File

@ -4,7 +4,13 @@ exports[`Footer rendering 1`] = `
ShallowWrapper { ShallowWrapper {
Symbol(enzyme.__root__): [Circular], Symbol(enzyme.__root__): [Circular],
Symbol(enzyme.__unrendered__): <Footer Symbol(enzyme.__unrendered__): <Footer
buildId="abc-123" site={
Object {
"siteMetadata": Object {
"buildId": "abc-123",
},
}
}
t={[Function]} t={[Function]}
/>, />,
Symbol(enzyme.__renderer__): Object { Symbol(enzyme.__renderer__): Object {

View File

@ -1,10 +1,8 @@
import React from 'react'; import React from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import { graphql } from 'gatsby'; import { StaticQuery, graphql } from 'gatsby';
import { withNamespaces, Trans } from 'react-i18next'; import { withNamespaces, Trans } from 'react-i18next';
import withQuery from 'lib/with-query';
import style from './style.module.css'; import style from './style.module.css';
const query = graphql` const query = graphql`
@ -17,7 +15,7 @@ const query = graphql`
} }
`; `;
export const Footer = ({ t, buildId }) => ( export const Footer = ({ t, site: { siteMetadata } }) => (
<footer className={ style.footer }> <footer className={ style.footer }>
<ul className={ style.list }> <ul className={ style.list }>
<li> <li>
@ -35,19 +33,22 @@ export const Footer = ({ t, buildId }) => (
</li> </li>
</ul> </ul>
<div className={ style.buildId }> <div className={ style.buildId }>
{ buildId } { siteMetadata.buildId }
</div> </div>
</footer> </footer>
); );
Footer.propTypes = { Footer.propTypes = {
t: PropTypes.func.isRequired, t: PropTypes.func.isRequired,
buildId: PropTypes.string.isRequired site: PropTypes.shape({
siteMetadata: PropTypes.shape({
buildId: PropTypes.string.isRequired
}).isRequired
}).isRequired
}; };
export default [ export default withNamespaces()(props => (
withQuery(query, { <StaticQuery query={ query } render={ data => (
toProps: ({ site: { siteMetadata } }) => siteMetadata <Footer { ...props } { ...data } />
}), ) } />
withNamespaces() ));
].reduce((component, fn) => fn(component), Footer);

View File

@ -7,7 +7,7 @@ import { Footer } from 'components/Footer';
describe('Footer', () => { describe('Footer', () => {
test('rendering', () => { test('rendering', () => {
const component = shallow( const component = shallow(
<Footer buildId="abc-123" t={ mockT } /> <Footer site={{ siteMetadata: { buildId: 'abc-123' } }} t={ mockT } />
); );
expect(component).toMatchSnapshot(); expect(component).toMatchSnapshot();
}); });

View File

@ -4,7 +4,13 @@ exports[`Header rendering 1`] = `
ShallowWrapper { ShallowWrapper {
Symbol(enzyme.__root__): [Circular], Symbol(enzyme.__root__): [Circular],
Symbol(enzyme.__unrendered__): <Header Symbol(enzyme.__unrendered__): <Header
banner="testing" site={
Object {
"siteMetadata": Object {
"banner": "testing",
},
}
}
/>, />,
Symbol(enzyme.__renderer__): Object { Symbol(enzyme.__renderer__): Object {
"batchedUpdates": [Function], "batchedUpdates": [Function],
@ -534,7 +540,13 @@ exports[`Header rendering with no banner 1`] = `
ShallowWrapper { ShallowWrapper {
Symbol(enzyme.__root__): [Circular], Symbol(enzyme.__root__): [Circular],
Symbol(enzyme.__unrendered__): <Header Symbol(enzyme.__unrendered__): <Header
banner={false} site={
Object {
"siteMetadata": Object {
"banner": false,
},
}
}
/>, />,
Symbol(enzyme.__renderer__): Object { Symbol(enzyme.__renderer__): Object {
"batchedUpdates": [Function], "batchedUpdates": [Function],

View File

@ -1,11 +1,10 @@
import React from 'react'; import React from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import { Link, graphql } from 'gatsby'; import { Link, StaticQuery, graphql } from 'gatsby';
import { withNamespaces, Trans } from 'react-i18next'; import { withNamespaces, Trans } from 'react-i18next';
import GitlabIcon from 'react-feather/dist/icons/gitlab'; import GitlabIcon from 'react-feather/dist/icons/gitlab';
import withQuery from 'lib/with-query';
import LocaleSwitcher from 'components/LocaleSwitcher'; import LocaleSwitcher from 'components/LocaleSwitcher';
import style from './style.module.css'; import style from './style.module.css';
@ -20,10 +19,10 @@ const query = graphql`
} }
`; `;
export const Header = ({ banner }) => ( export const Header = ({ site: { siteMetadata } }) => (
<header <header
className={ style.header } className={ style.header }
data-banner={ banner || null }> data-banner={ siteMetadata.banner || null }>
<h1> <h1>
<Link to="/">Regexper</Link> <Link to="/">Regexper</Link>
</h1> </h1>
@ -50,15 +49,18 @@ export const Header = ({ banner }) => (
); );
Header.propTypes = { Header.propTypes = {
banner: PropTypes.oneOfType([ site: PropTypes.shape({
PropTypes.bool, siteMetadata: PropTypes.shape({
PropTypes.string banner: PropTypes.oneOfType([
]).isRequired PropTypes.bool,
PropTypes.string
]).isRequired
}).isRequired
}).isRequired
}; };
export default [ export default withNamespaces()(props => (
withQuery(query, { <StaticQuery query={ query } render={ data => (
toProps: ({ site: { siteMetadata } }) => siteMetadata <Header { ...props } { ...data } />
}), ) } />
withNamespaces() ));
].reduce((component, fn) => fn(component), Header);

View File

@ -6,14 +6,14 @@ import { Header } from 'components/Header';
describe('Header', () => { describe('Header', () => {
test('rendering', () => { test('rendering', () => {
const component = shallow( const component = shallow(
<Header banner="testing" /> <Header site={{ siteMetadata: { banner: 'testing' } }} />
); );
expect(component).toMatchSnapshot(); expect(component).toMatchSnapshot();
}); });
test('rendering with no banner', () => { test('rendering with no banner', () => {
const component = shallow( const component = shallow(
<Header banner={ false } /> <Header site={{ siteMetadata: { banner: false } }} />
); );
expect(component).toMatchSnapshot(); expect(component).toMatchSnapshot();
}); });

View File

@ -20,11 +20,11 @@ ShallowWrapper {
"nodeType": "class", "nodeType": "class",
"props": Object { "props": Object {
"children": Array [ "children": Array [
<LoadNamespace(WithQuery(Header)) />, <LoadNamespace(Component) />,
<SentryBoundary> <SentryBoundary>
Example content Example content
</SentryBoundary>, </SentryBoundary>,
<LoadNamespace(WithQuery(Footer)) />, <LoadNamespace(Component) />,
], ],
}, },
"ref": null, "ref": null,
@ -68,11 +68,11 @@ ShallowWrapper {
"nodeType": "class", "nodeType": "class",
"props": Object { "props": Object {
"children": Array [ "children": Array [
<LoadNamespace(WithQuery(Header)) />, <LoadNamespace(Component) />,
<SentryBoundary> <SentryBoundary>
Example content Example content
</SentryBoundary>, </SentryBoundary>,
<LoadNamespace(WithQuery(Footer)) />, <LoadNamespace(Component) />,
], ],
}, },
"ref": null, "ref": null,

View File

@ -1,21 +0,0 @@
import React from 'react';
import { StaticQuery } from 'gatsby';
const withQuery = (query, options = {}) => Component => {
const { toProps } = {
toProps: data => data,
...options
};
const displayName = Component.displayName || Component.name || 'Component';
const wrapped = props => (
<StaticQuery query={ query } render={ data => (
<Component { ...props } { ...toProps(data) } />
) } />
);
wrapped.displayName = `WithQuery(${ displayName })`;
return wrapped;
};
export default withQuery;