refactor: using react hooks

This commit is contained in:
alxshelepenok
2019-05-09 16:57:42 +03:00
parent cb4d08f434
commit 4ada925e0f
45 changed files with 1972 additions and 781 deletions

View File

@@ -22,35 +22,35 @@ exports[`Feed renders correctly 1`] = `
<span
className="feed__item-meta-category"
>
<Link
<a
className="feed__item-meta-category-link"
to="/test_0"
href="/test_0"
>
test_0
</Link>
</a>
</span>
</div>
<h2
className="feed__item-title"
>
<Link
<a
className="feed__item-title-link"
to="/test_0"
href="/test_0"
>
test_0
</Link>
</a>
</h2>
<p
className="feed__item-description"
>
test_0
</p>
<Link
<a
className="feed__item-readmore"
to="/test_0"
href="/test_0"
>
Read
</Link>
</a>
</div>
<div
className="feed__item"
@@ -70,35 +70,35 @@ exports[`Feed renders correctly 1`] = `
<span
className="feed__item-meta-category"
>
<Link
<a
className="feed__item-meta-category-link"
to="/test_1"
href="/test_1"
>
test_1
</Link>
</a>
</span>
</div>
<h2
className="feed__item-title"
>
<Link
<a
className="feed__item-title-link"
to="/test_1"
href="/test_1"
>
test_1
</Link>
</a>
</h2>
<p
className="feed__item-description"
>
test_1
</p>
<Link
<a
className="feed__item-readmore"
to="/test_1"
href="/test_1"
>
Read
</Link>
</a>
</div>
</div>
`;

View File

@@ -7,24 +7,24 @@ exports[`Pagination renders correctly 1`] = `
<div
className="pagination__prev"
>
<Link
<a
className="pagination__prev-link"
href="/page/1"
rel="prev"
to="/page/1"
>
← PREV
</Link>
</a>
</div>
<div
className="pagination__next"
>
<Link
<a
className="pagination__next-link"
href="/page/3"
rel="next"
to="/page/3"
>
→ NEXT
</Link>
</a>
</div>
</div>
`;

View File

@@ -1,11 +1,11 @@
// @flow
import React from 'react';
import { graphql, StaticQuery } from 'gatsby';
import { getContactHref } from '../../../utils';
import styles from './Author.module.scss';
import { useSiteMetadata } from '../../../hooks';
export const PureAuthor = ({ data }: Object) => {
const { author } = data.site.siteMetadata;
const Author = () => {
const { author } = useSiteMetadata();
return (
<div className={styles['author']}>
@@ -24,25 +24,4 @@ export const PureAuthor = ({ data }: Object) => {
);
};
export const Author = () => (
<StaticQuery
query={graphql`
query AuthorQuery {
site {
siteMetadata {
author {
name
bio
contacts {
twitter
}
}
}
}
}
`}
render={(data) => <PureAuthor data={data} />}
/>
);
export default Author;

View File

@@ -1,26 +1,21 @@
import React from 'react';
import renderer from 'react-test-renderer';
import { PureAuthor as Author } from './Author';
import { useStaticQuery, StaticQuery } from 'gatsby';
import Author from './Author';
import siteMetadata from '../../../../jest/__fixtures__/site-metadata';
describe('Author', () => {
it('renders correctly', () => {
const props = {
data: {
site: {
siteMetadata: {
author: {
name: 'test',
bio: 'test',
contacts: {
twitter: 'test'
}
}
}
}
}
};
beforeEach(() => {
StaticQuery.mockImplementationOnce(
({ render }) => (
render(siteMetadata)
),
useStaticQuery.mockReturnValue(siteMetadata)
);
});
const tree = renderer.create(<Author {...props} />).toJSON();
it('renders correctly', () => {
const tree = renderer.create(<Author />).toJSON();
expect(tree).toMatchSnapshot();
});
});

View File

@@ -7,15 +7,15 @@ exports[`Author renders correctly 1`] = `
<p
className="author__bio"
>
test
Test bio
<a
className="author__bio-twitter"
href="https://www.twitter.com/test"
href="https://www.twitter.com/#"
rel="noopener noreferrer"
target="_blank"
>
<strong>
test
Test name
</strong>
on Twitter
</a>

View File

@@ -1,12 +1,9 @@
import React from 'react';
import { graphql, StaticQuery } from 'gatsby';
import ReactDisqusComments from 'react-disqus-comments';
import { useSiteMetadata } from '../../../hooks';
export const PureComments = ({ data, postTitle, postSlug }) => {
const {
url,
disqusShortname
} = data.site.siteMetadata;
const Comments = ({ postTitle, postSlug }) => {
const { url, disqusShortname } = useSiteMetadata();
if (!disqusShortname) {
return null;
@@ -22,20 +19,4 @@ export const PureComments = ({ data, postTitle, postSlug }) => {
);
};
export const Comments = (props) => (
<StaticQuery
query={graphql`
query CommentsQuery {
site {
siteMetadata {
disqusShortname
url
}
}
}
`}
render={(data) => <PureComments {...props} data={data}/>}
/>
);
export default Comments;

View File

@@ -1,22 +1,25 @@
import React from 'react';
import renderer from 'react-test-renderer';
import { PureComments as Comments } from './Comments';
import { useStaticQuery, StaticQuery } from 'gatsby';
import Comments from './Comments';
import siteMetadata from '../../../../jest/__fixtures__/site-metadata';
describe('Comments', () => {
it('renders correctly', () => {
const props = {
data: {
site: {
siteMetadata: {
url: 'http://localhost',
disqusShortname: 'test'
}
}
},
postTitle: 'test',
postSlug: '/test'
};
beforeEach(() => {
StaticQuery.mockImplementationOnce(
({ render }) => (
render(siteMetadata)
),
useStaticQuery.mockReturnValue(siteMetadata)
);
});
const props = {
postTitle: 'test',
postSlug: '/test'
};
it('renders correctly', () => {
const tree = renderer.create(<Comments {...props} />).toJSON();
expect(tree).toMatchSnapshot();
});

View File

@@ -1,9 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`Comments renders correctly 1`] = `
<div>
<div
id="disqus_thread"
/>
</div>
`;
exports[`Comments renders correctly 1`] = `null`;

View File

@@ -8,14 +8,9 @@ import Tags from './Tags';
import styles from './Post.module.scss';
const Post = ({ post }) => {
const {
tags,
title,
date
} = post.frontmatter;
const { html } = post;
const { tagSlugs } = post.fields;
const { tagSlugs, slug } = post.fields;
const { tags, title, date } = post.frontmatter;
return (
<div className={styles['post']}>
@@ -32,7 +27,7 @@ const Post = ({ post }) => {
</div>
<div className={styles['post__comments']}>
<Comments postSlug={post.fields.slug} postTitle={post.frontmatter.title} />
<Comments postSlug={slug} postTitle={post.frontmatter.title} />
</div>
</div>
);

View File

@@ -1,8 +1,19 @@
import React from 'react';
import renderer from 'react-test-renderer';
import { useStaticQuery, StaticQuery } from 'gatsby';
import Post from './Post';
import siteMetadata from '../../../jest/__fixtures__/site-metadata';
describe('Post', () => {
beforeEach(() => {
StaticQuery.mockImplementationOnce(
({ render }) => (
render(siteMetadata)
),
useStaticQuery.mockReturnValue(siteMetadata)
);
});
const props = {
post: {
html: '<p>test</p>',

View File

@@ -10,22 +10,22 @@ exports[`Tags renders correctly 1`] = `
<li
className="tags__list-item"
>
<Link
<a
className="tags__list-item-link"
to="/test_0"
href="/test_0"
>
test_0
</Link>
</a>
</li>
<li
className="tags__list-item"
>
<Link
<a
className="tags__list-item-link"
to="/test_1"
href="/test_1"
>
test_1
</Link>
</a>
</li>
</ul>
</div>

View File

@@ -4,12 +4,12 @@ exports[`Post renders correctly 1`] = `
<div
className="post"
>
<Link
<a
className="post__home-button"
to="/"
href="/"
>
All Articles
</Link>
</a>
<div
className="post__content"
>
@@ -53,35 +53,48 @@ exports[`Post renders correctly 1`] = `
<li
className="tags__list-item"
>
<Link
<a
className="tags__list-item-link"
to="/test_0"
href="/test_0"
>
test_0
</Link>
</a>
</li>
<li
className="tags__list-item"
>
<Link
<a
className="tags__list-item-link"
to="/test_1"
href="/test_1"
>
test_1
</Link>
</a>
</li>
</ul>
</div>
<StaticQuery
render={[Function]}
/>
<div
className="author"
>
<p
className="author__bio"
>
Test bio
<a
className="author__bio-twitter"
href="https://www.twitter.com/#"
rel="noopener noreferrer"
target="_blank"
>
<strong>
Test name
</strong>
on Twitter
</a>
</p>
</div>
</div>
<div
className="post__comments"
>
<StaticQuery
render={[Function]}
/>
</div>
/>
</div>
`;

View File

@@ -4,8 +4,8 @@ exports[`Author renders correctly 1`] = `
<div
className="author"
>
<Link
to="/"
<a
href="/"
>
<img
alt="test"
@@ -14,16 +14,16 @@ exports[`Author renders correctly 1`] = `
src="/photo.jpg"
width="75"
/>
</Link>
</a>
<h2
className="author__title"
>
<Link
<a
className="author__title-link"
to="/"
href="/"
>
test
</Link>
</a>
</h2>
<p
className="author__subtitle"

View File

@@ -10,24 +10,22 @@ exports[`Menu renders correctly 1`] = `
<li
className="menu__list-item"
>
<Link
activeClassName="menu__list-item-link--active"
<a
className="menu__list-item-link"
to="/#0/"
href="/#0/"
>
Item 0
</Link>
</a>
</li>
<li
className="menu__list-item"
>
<Link
activeClassName="menu__list-item-link--active"
<a
className="menu__list-item-link"
to="/#1/"
href="/#1/"
>
Item 1
</Link>
</a>
</li>
</ul>
</nav>

View File

@@ -1,26 +1,18 @@
// @flow
import React from 'react';
import { graphql, StaticQuery } from 'gatsby';
import Author from './Author';
import Contacts from './Contacts';
import Copyright from './Copyright';
import Menu from './Menu';
import styles from './Sidebar.module.scss';
import { useSiteMetadata } from '../../hooks';
type Props = {
+isIndex: ?boolean,
};
type PureProps = Props & {
+data: Object,
};
export const PureSidebar = ({ data, isIndex }: PureProps) => {
const {
author,
copyright,
menu
} = data.site.siteMetadata;
const Sidebar = ({ isIndex }: Props) => {
const { author, copyright, menu } = useSiteMetadata();
return (
<div className={styles['sidebar']}>
@@ -34,38 +26,4 @@ export const PureSidebar = ({ data, isIndex }: PureProps) => {
);
};
export const Sidebar = (props: Props) => (
<StaticQuery
query={graphql`
query SidebarQuery {
site {
siteMetadata {
title
subtitle
copyright
menu {
label
path
}
author {
name
photo
bio
contacts {
twitter
telegram
github
email
rss
vkontakte
}
}
}
}
}
`}
render={(data) => <PureSidebar {...props} data={data}/>}
/>
);
export default Sidebar;

View File

@@ -1,42 +1,24 @@
import React from 'react';
import renderer from 'react-test-renderer';
import { PureSidebar as Sidebar } from './Sidebar';
import { useStaticQuery, StaticQuery } from 'gatsby';
import Sidebar from './Sidebar';
import siteMetadata from '../../../jest/__fixtures__/site-metadata';
describe('Sidebar', () => {
it('renders correctly', () => {
const props = {
data: {
site: {
siteMetadata: {
author: {
name: 'name',
photo: '/photo.jpg',
bio: 'bio',
contacts: {
email: '#',
twitter: '#',
vkontakte: '#',
github: '#',
rss: '#',
telegram: '#'
}
},
copyright: 'copyright',
menu: [
{
label: 'Item 0',
path: '/#0/'
},
{
label: 'Item 1',
path: '/#1/'
}
]
}
}
}
};
beforeEach(() => {
StaticQuery.mockImplementationOnce(
({ render }) => (
render(siteMetadata)
),
useStaticQuery.mockReturnValue(siteMetadata)
);
});
const props = {
isIndex: true
};
it('renders correctly', () => {
const tree = renderer.create(<Sidebar {...props} />).toJSON();
expect(tree).toMatchSnapshot();
});

View File

@@ -10,31 +10,31 @@ exports[`Sidebar renders correctly 1`] = `
<div
className="author"
>
<Link
to="/"
<a
href="/"
>
<img
alt="name"
alt="Test name"
className="author__photo"
height="75"
src="/photo.jpg"
src="/test.jpg"
width="75"
/>
</Link>
<h2
</a>
<h1
className="author__title"
>
<Link
<a
className="author__title-link"
to="/"
href="/"
>
name
</Link>
</h2>
Test name
</a>
</h1>
<p
className="author__subtitle"
>
bio
Test bio
</p>
</div>
<nav
@@ -46,24 +46,32 @@ exports[`Sidebar renders correctly 1`] = `
<li
className="menu__list-item"
>
<Link
activeClassName="menu__list-item-link--active"
<a
className="menu__list-item-link"
to="/#0/"
href="/test/1/"
>
Item 0
</Link>
Test label 1
</a>
</li>
<li
className="menu__list-item"
>
<Link
activeClassName="menu__list-item-link--active"
<a
className="menu__list-item-link"
to="/#1/"
href="/test/2/"
>
Item 1
</Link>
Test label 2
</a>
</li>
<li
className="menu__list-item"
>
<a
className="menu__list-item-link"
href="/test/3/"
>
Test label 3
</a>
</li>
</ul>
</nav>
@@ -92,6 +100,25 @@ exports[`Sidebar renders correctly 1`] = `
</svg>
</a>
</li>
<li
className="contacts__list-item"
>
<a
className="contacts__list-item-link"
href="telegram:#"
rel="noopener noreferrer"
target="_blank"
>
<svg
className="icon"
viewBox="0 0 28 28"
>
<path
d="M27.563 0.172c0.328 0.234 0.484 0.609 0.422 1l-4 24c-0.047 0.297-0.234 0.547-0.5 0.703-0.141 0.078-0.313 0.125-0.484 0.125-0.125 0-0.25-0.031-0.375-0.078l-7.078-2.891-3.781 4.609c-0.187 0.234-0.469 0.359-0.766 0.359-0.109 0-0.234-0.016-0.344-0.063-0.391-0.141-0.656-0.516-0.656-0.938v-5.453l13.5-16.547-16.703 14.453-6.172-2.531c-0.359-0.141-0.594-0.469-0.625-0.859-0.016-0.375 0.172-0.734 0.5-0.922l26-15c0.156-0.094 0.328-0.141 0.5-0.141 0.203 0 0.406 0.063 0.562 0.172z"
/>
</svg>
</a>
</li>
<li
className="contacts__list-item"
>
@@ -111,25 +138,6 @@ exports[`Sidebar renders correctly 1`] = `
</svg>
</a>
</li>
<li
className="contacts__list-item"
>
<a
className="contacts__list-item-link"
href="https://vk.com/#"
rel="noopener noreferrer"
target="_blank"
>
<svg
className="icon"
viewBox="0 0 31 28"
>
<path
d="M29.953 8.125c0.234 0.641-0.5 2.141-2.344 4.594-3.031 4.031-3.359 3.656-0.859 5.984 2.406 2.234 2.906 3.313 2.984 3.453 0 0 1 1.75-1.109 1.766l-4 0.063c-0.859 0.172-2-0.609-2-0.609-1.5-1.031-2.906-3.703-4-3.359 0 0-1.125 0.359-1.094 2.766 0.016 0.516-0.234 0.797-0.234 0.797s-0.281 0.297-0.828 0.344h-1.797c-3.953 0.25-7.438-3.391-7.438-3.391s-3.813-3.938-7.156-11.797c-0.219-0.516 0.016-0.766 0.016-0.766s0.234-0.297 0.891-0.297l4.281-0.031c0.406 0.063 0.688 0.281 0.688 0.281s0.25 0.172 0.375 0.5c0.703 1.75 1.609 3.344 1.609 3.344 1.563 3.219 2.625 3.766 3.234 3.437 0 0 0.797-0.484 0.625-4.375-0.063-1.406-0.453-2.047-0.453-2.047-0.359-0.484-1.031-0.625-1.328-0.672-0.234-0.031 0.156-0.594 0.672-0.844 0.766-0.375 2.125-0.391 3.734-0.375 1.266 0.016 1.625 0.094 2.109 0.203 1.484 0.359 0.984 1.734 0.984 5.047 0 1.062-0.203 2.547 0.562 3.031 0.328 0.219 1.141 0.031 3.141-3.375 0 0 0.938-1.625 1.672-3.516 0.125-0.344 0.391-0.484 0.391-0.484s0.25-0.141 0.594-0.094l4.5-0.031c1.359-0.172 1.578 0.453 1.578 0.453z"
/>
</svg>
</a>
</li>
<li
className="contacts__list-item"
>
@@ -173,16 +181,16 @@ exports[`Sidebar renders correctly 1`] = `
>
<a
className="contacts__list-item-link"
href="telegram:#"
href="https://vk.com/#"
rel="noopener noreferrer"
target="_blank"
>
<svg
className="icon"
viewBox="0 0 28 28"
viewBox="0 0 31 28"
>
<path
d="M27.563 0.172c0.328 0.234 0.484 0.609 0.422 1l-4 24c-0.047 0.297-0.234 0.547-0.5 0.703-0.141 0.078-0.313 0.125-0.484 0.125-0.125 0-0.25-0.031-0.375-0.078l-7.078-2.891-3.781 4.609c-0.187 0.234-0.469 0.359-0.766 0.359-0.109 0-0.234-0.016-0.344-0.063-0.391-0.141-0.656-0.516-0.656-0.938v-5.453l13.5-16.547-16.703 14.453-6.172-2.531c-0.359-0.141-0.594-0.469-0.625-0.859-0.016-0.375 0.172-0.734 0.5-0.922l26-15c0.156-0.094 0.328-0.141 0.5-0.141 0.203 0 0.406 0.063 0.562 0.172z"
d="M29.953 8.125c0.234 0.641-0.5 2.141-2.344 4.594-3.031 4.031-3.359 3.656-0.859 5.984 2.406 2.234 2.906 3.313 2.984 3.453 0 0 1 1.75-1.109 1.766l-4 0.063c-0.859 0.172-2-0.609-2-0.609-1.5-1.031-2.906-3.703-4-3.359 0 0-1.125 0.359-1.094 2.766 0.016 0.516-0.234 0.797-0.234 0.797s-0.281 0.297-0.828 0.344h-1.797c-3.953 0.25-7.438-3.391-7.438-3.391s-3.813-3.938-7.156-11.797c-0.219-0.516 0.016-0.766 0.016-0.766s0.234-0.297 0.891-0.297l4.281-0.031c0.406 0.063 0.688 0.281 0.688 0.281s0.25 0.172 0.375 0.5c0.703 1.75 1.609 3.344 1.609 3.344 1.563 3.219 2.625 3.766 3.234 3.437 0 0 0.797-0.484 0.625-4.375-0.063-1.406-0.453-2.047-0.453-2.047-0.359-0.484-1.031-0.625-1.328-0.672-0.234-0.031 0.156-0.594 0.672-0.844 0.766-0.375 2.125-0.391 3.734-0.375 1.266 0.016 1.625 0.094 2.109 0.203 1.484 0.359 0.984 1.734 0.984 5.047 0 1.062-0.203 2.547 0.562 3.031 0.328 0.219 1.141 0.031 3.141-3.375 0 0 0.938-1.625 1.672-3.516 0.125-0.344 0.391-0.484 0.391-0.484s0.25-0.141 0.594-0.094l4.5-0.031c1.359-0.172 1.578 0.453 1.578 0.453z"
/>
</svg>
</a>
@@ -192,7 +200,7 @@ exports[`Sidebar renders correctly 1`] = `
<div
className="copyright"
>
copyright
Test copyright
</div>
</div>
</div>