-
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
1 changed file
with
157 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
import axios from 'axios'; | ||
import * as querystring from 'querystring'; | ||
import NewsAPI from '../src'; | ||
|
||
jest.mock('axios'); | ||
// @ts-ignore | ||
axios.get.mockImplementation(() => Promise.resolve({ data: { } })); | ||
|
||
describe('NewsAPI tests', () => { | ||
const TEST_API_KEY = 'test_api_key'; | ||
|
||
const newsApi = new NewsAPI(TEST_API_KEY); | ||
|
||
describe('Sources', () => { | ||
|
||
test('should fetch data successfully without params', async () => { | ||
await expect( newsApi.getSources() ).resolves.toEqual({ }); | ||
expect(axios.get).toHaveBeenCalledWith( | ||
`http://newsapi.org/v2/sources?${ querystring.stringify({ apiKey: TEST_API_KEY }) }` | ||
); | ||
}); | ||
|
||
test('should fetch data successfully', async () => { | ||
await expect( | ||
newsApi.getSources({ | ||
category: 'business', | ||
country: 'ua', | ||
language: 'en', | ||
}) | ||
).resolves.toEqual({ }); | ||
|
||
expect(axios.get).toHaveBeenCalledWith( | ||
`http://newsapi.org/v2/sources?` + [ | ||
querystring.stringify({ apiKey: TEST_API_KEY }), | ||
querystring.stringify({ category: 'business' }), | ||
querystring.stringify({ language: 'en' }), | ||
querystring.stringify({ country: 'ua' }), | ||
].join('&') | ||
); | ||
}) | ||
|
||
}); | ||
|
||
describe('Top Headlines', () => { | ||
|
||
test('should fetch data successfully without params', async () => { | ||
await expect( newsApi.getTopHeadlines() ).resolves.toEqual({ }); | ||
expect(axios.get).toHaveBeenCalledWith( | ||
`http://newsapi.org/v2/top-headlines?${ querystring.stringify({ apiKey: TEST_API_KEY }) }` | ||
); | ||
}); | ||
|
||
test('should fetch data successfully with sources', async () => { | ||
await expect( | ||
newsApi.getTopHeadlines({ | ||
// country: 'ua', | ||
// category: 'business', | ||
sources: [ 'test-source-1', 'test-source-2' ], | ||
q: 'query', | ||
pageSize: 40, | ||
page: 2, | ||
}) | ||
).resolves.toEqual({ }); | ||
|
||
expect(axios.get).toHaveBeenCalledWith( | ||
`http://newsapi.org/v2/top-headlines?` + [ | ||
querystring.stringify({ apiKey: TEST_API_KEY }), | ||
querystring.stringify({ q: 'query' }), | ||
querystring.stringify({ sources: [ 'test-source-1', 'test-source-2' ].join(',') }), | ||
querystring.stringify({ pageSize: 40 }), | ||
querystring.stringify({ page: 2 }), | ||
].join('&') | ||
); | ||
}) | ||
|
||
test('should fetch data successfully without sources', async () => { | ||
await expect( | ||
newsApi.getTopHeadlines({ | ||
country: 'ua', | ||
category: 'business', | ||
q: 'query', | ||
pageSize: 40, | ||
page: 2, | ||
}) | ||
).resolves.toEqual({ }); | ||
|
||
expect(axios.get).toHaveBeenCalledWith( | ||
`http://newsapi.org/v2/top-headlines?` + [ | ||
querystring.stringify({ apiKey: TEST_API_KEY }), | ||
querystring.stringify({ q: 'query' }), | ||
querystring.stringify({ country: 'ua' }), | ||
querystring.stringify({ category: 'business' }), | ||
querystring.stringify({ pageSize: 40 }), | ||
querystring.stringify({ page: 2 }), | ||
].join('&') | ||
); | ||
}) | ||
|
||
test('should throw error if source and country params are mixed', async() => { | ||
await expect(newsApi.getTopHeadlines({ country: 'ua', sources: [ 'test-source-1' ] })) | ||
.rejects.toThrow(); | ||
}); | ||
|
||
test('should throw error if source and category params are mixed', async() => { | ||
await expect(newsApi.getTopHeadlines({ category: 'business', sources: [ 'test-source-1' ] })) | ||
.rejects.toThrow(); | ||
}); | ||
|
||
}); | ||
|
||
describe('Everything', () => { | ||
|
||
test('should fetch data successfully without params', async () => { | ||
await expect( newsApi.getEverything() ).resolves.toEqual({ }); | ||
expect(axios.get).toHaveBeenCalledWith( | ||
`http://newsapi.org/v2/everything?${ querystring.stringify({ apiKey: TEST_API_KEY }) }` | ||
); | ||
}); | ||
|
||
test('should fetch data successfully', async () => { | ||
await expect( | ||
newsApi.getEverything({ | ||
q: 'query', | ||
qInTitle: 'titleQuery', | ||
sources: [ 'test-source-1', 'test-source-2' ], | ||
domains: [ 'test-domain-1', 'test-domain-2' ], | ||
excludeDomains: [ 'test-excl-domain-1', 'test-excl-domain-2' ], | ||
from: '1990-01-01', | ||
to: '2000-01-01', | ||
language: 'en', | ||
sortBy: 'relevancy', | ||
pageSize: 40, | ||
page: 2, | ||
}) | ||
).resolves.toEqual({ }); | ||
|
||
expect(axios.get).toHaveBeenCalledWith( | ||
`http://newsapi.org/v2/everything?` + [ | ||
querystring.stringify({ apiKey: TEST_API_KEY }), | ||
querystring.stringify({ q: 'query' }), | ||
querystring.stringify({ qInTitle: 'titleQuery' }), | ||
querystring.stringify({ sources: [ 'test-source-1', 'test-source-2' ].join(',') }), | ||
querystring.stringify({ domains: [ 'test-domain-1', 'test-domain-2' ].join(',') }), | ||
querystring.stringify({ excludeDomains: [ 'test-excl-domain-1', 'test-excl-domain-2' ].join(',') }), | ||
querystring.stringify({ from: '1990-01-01' }), | ||
querystring.stringify({ to: '2000-01-01' }), | ||
querystring.stringify({ language: 'en' }), | ||
querystring.stringify({ sortBy: 'relevancy' }), | ||
querystring.stringify({ pageSize: 40 }), | ||
querystring.stringify({ page: 2 }), | ||
].join('&') | ||
); | ||
}) | ||
|
||
}); | ||
|
||
}) |