Skip to content

Commit

Permalink
Merge pull request #7 from kaayru/feature/add-post-source
Browse files Browse the repository at this point in the history
Feature/add post source
  • Loading branch information
kaayru authored Mar 27, 2020
2 parents 6b5e40f + 334dbb3 commit 03d39ab
Show file tree
Hide file tree
Showing 11 changed files with 226 additions and 27 deletions.
2 changes: 2 additions & 0 deletions gatsby-config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require('dotenv').config({
path: `.env.${process.env.NODE_ENV}`,
});
const { normalizer } = require('./normalizer/normalizer.js');

module.exports = {
siteMetadata: {
Expand Down Expand Up @@ -65,6 +66,7 @@ module.exports = {
'**/taxonomies',
'**/users',
],
normalizer,
},
},
{
Expand Down
23 changes: 23 additions & 0 deletions normalizer/normalizer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const getDomainFromLink = link => {
try {
const { hostname } = new URL(link);
return hostname;
} catch (e) {
return '';
}
};

const normalizer = ({ entities }) =>
entities.map(entity => {
if (entity && entity.__type === 'wordpress__POST') {
return {
...entity,
source: getDomainFromLink(entity.link),
};
}
return entity;
});

module.exports = {
normalizer,
};
36 changes: 36 additions & 0 deletions normalizer/normalizer.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { normalizer } from './normalizer';

describe('Normalizer', () => {
it('Should add source field to posts', () => {
const entities = [
{ __type: 'wordpress__POST', link: 'https://www.domain.com/another-article-on-js-fatigue' },
];
const posts = normalizer({
entities,
});
expect(posts[0]).toEqual({ ...entities[0], source: 'www.domain.com' });
});

it('Should add empty source field to posts if link is not valid', () => {
const entities = [
{
__type: 'wordpress__POST',
link: 'www.domain.com/another-article-on-js-fatigue',
},
];
const posts = normalizer({
entities,
});
expect(posts[0]).toEqual({ ...entities[0], source: '' });
});

it('Should not add source field to other post types', () => {
const entities = [
{ __type: 'wordpress__OTHER', link: 'https://www.domain.com/another-article-on-js-fatigue' },
];
const posts = normalizer({
entities,
});
expect(posts[0]).toEqual(entities[0]);
});
});
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"typography-theme-wordpress-2016": "^0.16.19"
},
"devDependencies": {
"@testing-library/react": "^10.0.1",
"babel-jest": "^25.2.1",
"babel-preset-gatsby": "^0.3.1",
"eslint": "^6.8.0",
Expand Down
5 changes: 5 additions & 0 deletions src/components/post/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Post from './post';

export { default as postFixture } from './post.fixture';

export default Post;
20 changes: 20 additions & 0 deletions src/components/post/post.fixture.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const fixture = {
link:
'https://www.freecodecamp.org/news/a-beginners-guide-to-git-how-to-write-a-good-commit-message/',
source: 'www.freecodecamp.org',
title: 'A Beginner’s Guide to Git — How to Write a Good Commit Message',
wordpressId: '1234',
date: 'March 27, 2020',
tags: [
{
name: 'Git',
slug: 'git',
},
{
name: 'Workflow',
slug: 'workflow',
},
],
};

export default fixture;
36 changes: 32 additions & 4 deletions src/components/post.js → src/components/post/post.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { Link } from 'gatsby';
import React, { Fragment } from 'react';
import { graphql, Link } from 'gatsby';
import styled from 'styled-components';

const Li = styled.li`
Expand All @@ -8,7 +8,7 @@ const Li = styled.li`
margin-bottom: 45px;
`;

const Post = ({ node: { date, link, tags, title, wordpressId } }) => (
const Post = ({ node: { date, link, source, tags, title, wordpressId } }) => (
<Li key={wordpressId}>
<div>
{tags && tags.length > 0 && (
Expand Down Expand Up @@ -40,11 +40,39 @@ const Post = ({ node: { date, link, tags, title, wordpressId } }) => (
marginTop: 8,
marginBottom: 10,
}}
data-testid="post__byon"
>
{date}
{source && (
<Fragment>
By{' '}
<a
href={source}
target="_blank"
rel="noopener noreferrer"
title={`To ${source} homepage`}
>
{source}
</a>{' '}
</Fragment>
)}
{date && `on ${date}`}
</p>
</div>
</Li>
);

export default Post;

export const postFragment = graphql`
fragment Post on wordpress__POST {
link
source
title
wordpressId: wordpress_id
date(formatString: "MMMM DD, YYYY")
tags {
name
slug
}
}
`;
27 changes: 27 additions & 0 deletions src/components/post/post.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* @jest-environment jsdom
*/

import React from 'react';
import { render } from '@testing-library/react';

import Post from './post';
import postFixture from './post.fixture';

describe('Post', () => {
it('Should contain source and date', () => {
const post = { ...postFixture, date: '30 March, 2020', source: 'www.source.com' };
const { getByTestId } = render(<Post node={post} />);
expect(getByTestId('post__byon').textContent).toEqual('By www.source.com on 30 March, 2020');
});
it('Should only contain date if source is not set', () => {
const post = { ...postFixture, date: '30 March, 2020', source: null };
const { getByTestId } = render(<Post node={post} />);
expect(getByTestId('post__byon').textContent).toEqual('on 30 March, 2020');
});
it('Should only contain source if date is not set', () => {
const post = { ...postFixture, source: 'www.source.com', date: null };
const { getByTestId } = render(<Post node={post} />);
expect(getByTestId('post__byon').textContent).toEqual('By www.source.com ');
});
});
14 changes: 1 addition & 13 deletions src/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,7 @@ export const query = graphql`
allWordpressPost {
edges {
node {
title
content
link
excerpt
wordpressId: wordpress_id
author {
name
}
date(formatString: "MMMM DD, YYYY")
tags {
name
slug
}
...Post
}
}
}
Expand Down
9 changes: 1 addition & 8 deletions src/templates/tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,7 @@ export const query = graphql`
allWordpressPost(filter: { tags: { elemMatch: { wordpress_id: { eq: $id } } } }) {
edges {
node {
link
title
wordpressId: wordpress_id
date(formatString: "MMMM DD, YYYY")
tags {
name
slug
}
...Post
}
}
}
Expand Down
80 changes: 78 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1538,7 +1538,7 @@
"@babel/plugin-transform-react-jsx-self" "^7.9.0"
"@babel/plugin-transform-react-jsx-source" "^7.9.0"

"@babel/runtime-corejs3@^7.8.3":
"@babel/runtime-corejs3@^7.7.4", "@babel/runtime-corejs3@^7.8.3":
version "7.9.2"
resolved "https://registry.yarnpkg.com/@babel/runtime-corejs3/-/runtime-corejs3-7.9.2.tgz#26fe4aa77e9f1ecef9b776559bbb8e84d34284b7"
integrity sha512-HHxmgxbIzOfFlZ+tdeRKtaxWOMUoCG5Mu3wKeUmOxjYrwb3AAHgnmtCUbPPK11/raIWLIBK250t8E2BPO0p7jA==
Expand All @@ -1553,7 +1553,7 @@
dependencies:
regenerator-runtime "^0.13.2"

"@babel/runtime@^7.7.2", "@babel/runtime@^7.8.4", "@babel/runtime@^7.8.7":
"@babel/runtime@^7.7.2", "@babel/runtime@^7.7.4", "@babel/runtime@^7.8.4", "@babel/runtime@^7.8.7", "@babel/runtime@^7.9.2":
version "7.9.2"
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.9.2.tgz#d90df0583a3a252f09aaa619665367bae518db06"
integrity sha512-NE2DtOdufG7R5vnfQUTehdTfNycfUANEtCa9PssN9O/xmTzP4E08UI797ixaei6hBEVL9BI/PsdJS5x7mWoB9Q==
Expand Down Expand Up @@ -1887,6 +1887,16 @@
"@types/yargs" "^15.0.0"
chalk "^3.0.0"

"@jest/types@^25.2.3":
version "25.2.3"
resolved "https://registry.yarnpkg.com/@jest/types/-/types-25.2.3.tgz#035c4fb94e2da472f359ff9a211915d59987f6b6"
integrity sha512-6oLQwO9mKif3Uph3RX5J1i3S7X7xtDHWBaaaoeKw8hOzV6YUd0qDcYcHZ6QXMHDIzSr7zzrEa51o2Ovlj6AtKQ==
dependencies:
"@types/istanbul-lib-coverage" "^2.0.0"
"@types/istanbul-reports" "^1.1.1"
"@types/yargs" "^15.0.0"
chalk "^3.0.0"

"@jimp/bmp@^0.6.4":
version "0.6.4"
resolved "https://registry.yarnpkg.com/@jimp/bmp/-/bmp-0.6.4.tgz#0f4b94486e979d82c83e1e87cd00b756afb26e49"
Expand Down Expand Up @@ -2185,6 +2195,26 @@
dependencies:
type-detect "4.0.8"

"@testing-library/dom@^7.0.2":
version "7.1.2"
resolved "https://registry.yarnpkg.com/@testing-library/dom/-/dom-7.1.2.tgz#0942e3751beeea9820e14dd4bf685f1f1767353a"
integrity sha512-U0wLMbND1NUMUB65E9VmfuehT1GUSIHnT2zK7rjpDIdFNDbMtjDzbdaZXBYDp5lWzHJwUdPQozMd1GHp3O9gow==
dependencies:
"@babel/runtime" "^7.9.2"
"@types/testing-library__dom" "^7.0.0"
aria-query "^4.0.2"
dom-accessibility-api "^0.4.2"
pretty-format "^25.1.0"

"@testing-library/react@^10.0.1":
version "10.0.1"
resolved "https://registry.yarnpkg.com/@testing-library/react/-/react-10.0.1.tgz#4f5e2a8836257c5bd3df640b21d7bea5a0d83ead"
integrity sha512-sMHWud2dcymOzq2AhEniICSijEwKeTiBX+K0y36FYNY7wH2t0SIP1o732Bf5dDY0jYoMC2hj2UJSVpZC/rDsWg==
dependencies:
"@babel/runtime" "^7.8.7"
"@testing-library/dom" "^7.0.2"
"@types/testing-library__react" "^9.1.3"

"@types/babel__core@^7.1.0":
version "7.1.6"
resolved "https://registry.yarnpkg.com/@types/babel__core/-/babel__core-7.1.6.tgz#16ff42a5ae203c9af1c6e190ed1f30f83207b610"
Expand Down Expand Up @@ -2334,6 +2364,13 @@
"@types/history" "*"
"@types/react" "*"

"@types/react-dom@*":
version "16.9.5"
resolved "https://registry.yarnpkg.com/@types/react-dom/-/react-dom-16.9.5.tgz#5de610b04a35d07ffd8f44edad93a71032d9aaa7"
integrity sha512-BX6RQ8s9D+2/gDhxrj8OW+YD4R+8hj7FEM/OJHGNR0KipE1h1mSsf39YeyC81qafkq+N3rU3h3RFbLSwE5VqUg==
dependencies:
"@types/react" "*"

"@types/react@*", "@types/react@^16.8.12", "@types/react@^16.8.6":
version "16.9.1"
resolved "https://registry.yarnpkg.com/@types/react/-/react-16.9.1.tgz#862c83b4c9d5cd116e42fd9a4f3694843cd2c051"
Expand All @@ -2347,6 +2384,22 @@
resolved "https://registry.yarnpkg.com/@types/stack-utils/-/stack-utils-1.0.1.tgz#0a851d3bd96498fa25c33ab7278ed3bd65f06c3e"
integrity sha512-l42BggppR6zLmpfU6fq9HEa2oGPEI8yrSPL3GITjfRInppYFahObbIQOQK3UGxEnyQpltZLaPe75046NOZQikw==

"@types/testing-library__dom@*", "@types/testing-library__dom@^7.0.0":
version "7.0.0"
resolved "https://registry.yarnpkg.com/@types/testing-library__dom/-/testing-library__dom-7.0.0.tgz#c0fb7d1c2495a3d26f19342102142d47500f0319"
integrity sha512-1TEPWyqQ6IQ7R1hCegZmFSA3KrBQjdzJW7yC9ybpRcFst5XuPOqBGNr0mTAKbxwI/TrTyc1skeyLJrpcvAf93w==
dependencies:
pretty-format "^25.1.0"

"@types/testing-library__react@^9.1.3":
version "9.1.3"
resolved "https://registry.yarnpkg.com/@types/testing-library__react/-/testing-library__react-9.1.3.tgz#35eca61cc6ea923543796f16034882a1603d7302"
integrity sha512-iCdNPKU3IsYwRK9JieSYAiX0+aYDXOGAmrC/3/M7AqqSDKnWWVv07X+Zk1uFSL7cMTUYzv4lQRfohucEocn5/w==
dependencies:
"@types/react-dom" "*"
"@types/testing-library__dom" "*"
pretty-format "^25.1.0"

"@types/tmp@^0.0.32":
version "0.0.32"
resolved "https://registry.yarnpkg.com/@types/tmp/-/tmp-0.0.32.tgz#0d3cb31022f8427ea58c008af32b80da126ca4e3"
Expand Down Expand Up @@ -2820,6 +2873,14 @@ aria-query@^3.0.0:
ast-types-flow "0.0.7"
commander "^2.11.0"

aria-query@^4.0.2:
version "4.0.2"
resolved "https://registry.yarnpkg.com/aria-query/-/aria-query-4.0.2.tgz#250687b4ccde1ab86d127da0432ae3552fc7b145"
integrity sha512-S1G1V790fTaigUSM/Gd0NngzEfiMy9uTUfMyHhKhVyy4cH5O/eTuR01ydhGL0z4Za1PXFTRGH3qL8VhUQuEO5w==
dependencies:
"@babel/runtime" "^7.7.4"
"@babel/runtime-corejs3" "^7.7.4"

arr-diff@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/arr-diff/-/arr-diff-4.0.0.tgz#d6461074febfec71e7e15235761a329a5dc7c520"
Expand Down Expand Up @@ -5174,6 +5235,11 @@ doctrine@^3.0.0:
dependencies:
esutils "^2.0.2"

dom-accessibility-api@^0.4.2:
version "0.4.3"
resolved "https://registry.yarnpkg.com/dom-accessibility-api/-/dom-accessibility-api-0.4.3.tgz#93ca9002eb222fd5a343b6e5e6b9cf5929411c4c"
integrity sha512-JZ8iPuEHDQzq6q0k7PKMGbrIdsgBB7TRrtVOUm4nSMCExlg5qQG4KXWTH2k90yggjM4tTumRGwTKJSldMzKyLA==

dom-converter@^0.2:
version "0.2.0"
resolved "https://registry.yarnpkg.com/dom-converter/-/dom-converter-0.2.0.tgz#6721a9daee2e293682955b6afe416771627bb768"
Expand Down Expand Up @@ -11684,6 +11750,16 @@ pretty-error@^2.1.1:
renderkid "^2.0.1"
utila "~0.4"

pretty-format@^25.1.0:
version "25.2.3"
resolved "https://registry.yarnpkg.com/pretty-format/-/pretty-format-25.2.3.tgz#ba6e9603a0d80fa2e470b1fed55de1f9bfd81421"
integrity sha512-IP4+5UOAVGoyqC/DiomOeHBUKN6q00gfyT2qpAsRH64tgOKB2yF7FHJXC18OCiU0/YFierACup/zdCOWw0F/0w==
dependencies:
"@jest/types" "^25.2.3"
ansi-regex "^5.0.0"
ansi-styles "^4.0.0"
react-is "^16.12.0"

pretty-format@^25.2.1:
version "25.2.1"
resolved "https://registry.yarnpkg.com/pretty-format/-/pretty-format-25.2.1.tgz#3b8f7b9241faa6736cdbc32879bee18454d1318d"
Expand Down

0 comments on commit 03d39ab

Please sign in to comment.