Last active
August 13, 2019 03:48
-
-
Save antklim/f80ad8ec968bb260308f955db045b58e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
jest.mock('request-promise-native') | |
const request = require('request-promise-native') | |
describe('Provider unit test', () => { | |
let provider | |
beforeAll(() => { | |
process.env.DOWNSTREAM_API_URL = 'http://test.com' | |
provider = require('./provider') | |
}) | |
afterAll(() => { | |
delete process.env.DOWNSTREAM_API_URL | |
}) | |
describe('searchByName() tests', () => { | |
test('calls resource API with valid options', async () => { | |
request.get = jest.fn().mockResolvedValueOnce({ statusCode: 200 }) | |
await provider.searchByName('show') | |
expect(request.get).toHaveBeenCalledTimes(1) | |
const [opts] = request.get.mock.calls[0] | |
expect(opts).toEqual({ | |
baseUrl: 'http://test.com', | |
json: true, | |
resolveWithFullResponse: true, | |
timeout: null, | |
uri: 'songs.json', | |
qs: { pattern: 'show' } | |
}) | |
}) | |
test('returns response from the resource API', async () => { | |
const resourceResponse = [ | |
{ | |
id: 51127, | |
type: 'Song', | |
title: 'The Show Must Go On', | |
artist: { | |
id: 55, | |
type: 'Artist', | |
nameWithoutThePrefix: 'Queen', | |
useThePrefix: false, | |
name: 'Queen' | |
}, | |
chordsPresent: true, | |
tabTypes: ['PLAYER', 'TEXT_GUITAR_TAB', 'CHORDS', 'TEXT_BASS_TAB'] | |
} | |
] | |
request.get = jest.fn().mockResolvedValueOnce({ | |
statusCode: 200, | |
body: resourceResponse | |
}) | |
const songsList = await provider.searchByName('show') | |
expect(songsList).toEqual(resourceResponse) | |
}) | |
test('throws exception when request fails', async () => { | |
request.get = jest.fn().mockRejectedValueOnce(new Error('Internal service error')) | |
await expect(provider.searchByName('show')).rejects.toThrow('Internal service error') | |
}) | |
}) | |
... | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment