-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from kaayru/feature/add-post-source
Feature/add post source
- Loading branch information
Showing
11 changed files
with
226 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 '); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters