This repository has been archived by the owner on May 22, 2021. It is now read-only.
-
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.
- Loading branch information
Showing
8 changed files
with
153 additions
and
5 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
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,47 @@ | ||
const axios = require('axios'); | ||
const chalk = require('chalk'); | ||
|
||
const helpers = require('../helpers'); | ||
|
||
exports.listArticles = function (argv) { | ||
const requestParams = {}; | ||
|
||
if (argv.limit && argv.limit > 1) { | ||
requestParams.limit = argv.limit; | ||
} else { | ||
const now = new Date(); | ||
const todayStart = Date.UTC(now.getFullYear(), now.getMonth(), now.getDate(), 0, 0, 1); | ||
const fullDayMs = 24 * 60 * 60 * 1000; | ||
requestParams.since_added = (todayStart - fullDayMs) / 1000; | ||
} | ||
|
||
axios.get('https://api.spaceflightnewsapi.net/articles/', { | ||
params: requestParams | ||
}).then((response) => { | ||
const articles = response.data; | ||
|
||
const articlesByDate = articles.reduce((byDate, article) => { | ||
const title = chalk`${article.title}`; | ||
|
||
const publishedAt = new Date(article.date_published * 1000).toLocaleDateString(); | ||
const articleDetails = chalk`{yellow ${article.news_site_long}}`; | ||
|
||
const readSource = `Read on ${article.url}`; | ||
|
||
const dayArticles = byDate[publishedAt] = byDate[publishedAt] || []; | ||
dayArticles.push(`${articleDetails} | ${title}\n${readSource} \n`); | ||
|
||
return byDate; | ||
}, {}); | ||
|
||
for (const date in articlesByDate) { | ||
const dayArticles = articlesByDate[date]; | ||
|
||
helpers.printMessage(`${date}\n----------`); | ||
dayArticles.forEach(article => helpers.printMessage(article)); | ||
} | ||
}).catch(error => { | ||
const errorMessage = `${error.code}: ${error.message}`; | ||
helpers.printError(errorMessage); | ||
}); | ||
}; |
File renamed without changes.
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,31 @@ | ||
exports.multiResponse = [ | ||
{ | ||
'tags': [ | ||
'space', | ||
'stars', | ||
'planet', | ||
'orbit' | ||
], | ||
'categories': [], | ||
'_id': '12345abc', | ||
'news_site': 'space', | ||
'news_site_long': 'Space', | ||
'title': 'Space is big', | ||
'url': 'https://example.com/2018/12/30/space-is-big/', | ||
'date_published': 1546197906, | ||
'date_added': 1546197906, | ||
'featured_image': 'https://example.com/2018/12/30/image.jpg' | ||
}, | ||
{ | ||
'tags': [], | ||
'categories': [], | ||
'_id': 'vbnm6789', | ||
'news_site': 'space', | ||
'news_site_long': 'Space', | ||
'title': 'Live coverage: Earth is spinning', | ||
'url': 'https://example.com.com/2018/12/30/earth-is-spinning/', | ||
'date_published': 1546180509, | ||
'date_added': 1546180509, | ||
'featured_image': 'https://example.com/2018/12/image.jpg' | ||
} | ||
]; |
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,57 @@ | ||
const axios = require('axios'); | ||
|
||
const news = require('../src/modules/news'); | ||
const helpers = require('../src/helpers'); | ||
|
||
const newsResponse = require('./mockData/news.js'); | ||
|
||
jest.mock('axios'); | ||
|
||
describe('Space news', function () { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
describe('list articles', function () { | ||
it('should set limit if applied and call printMessage for each news', function (done) { | ||
expect.hasAssertions(); | ||
|
||
const spy = jest.spyOn(helpers, 'printMessage'); | ||
axios.get.mockImplementation((url, query) => { | ||
expect(query.params).not.toHaveProperty('since_added'); | ||
expect(query.params).toHaveProperty('limit'); | ||
|
||
return Promise.resolve({ | ||
data: newsResponse.multiResponse | ||
}); | ||
}); | ||
|
||
const newsCount = 2; | ||
news.listArticles({ | ||
limit: newsCount | ||
}); | ||
|
||
const newsListMessageCount = newsCount + 1; // Adds 1 output for data label | ||
setTimeout(() => { | ||
expect(spy).toHaveBeenCalledTimes(newsListMessageCount); | ||
done(); | ||
}); | ||
}); | ||
it('should set date requirement if no limit applied', function () { | ||
expect.hasAssertions(); | ||
|
||
axios.get.mockImplementation((url, query) => { | ||
expect(query.params).toHaveProperty('since_added'); | ||
expect(query.params).not.toHaveProperty('limit'); | ||
|
||
return Promise.resolve({ | ||
data: newsResponse.multiResponse | ||
}); | ||
}); | ||
|
||
news.listArticles({ | ||
since_added: new Date() | ||
}); | ||
}); | ||
}); | ||
}); |
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