Last active
August 13, 2019 03:48
-
-
Save antklim/beb5382f218beb5756c265b65de4b80b 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('@src/provider') | |
const provider = require('@src/provider') | |
const request = require('supertest') | |
const App = require('@src/app') | |
const router = require('@src/router') | |
const app = new App({ router }).init() | |
describe('App integration test', () => { | |
test('GET /blah 404 - Method not allowed', async () => { | |
const response = await request(app.app.callback()).get('/blah') | |
expect(response.status).toBe(404) | |
}) | |
describe('GET /songs/:name', () => { | |
test('200 - SUCCESS', async () => { | |
const providerResponse = [ | |
{ | |
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'] | |
} | |
] | |
provider.searchByName = jest.fn().mockResolvedValueOnce(providerResponse) | |
const response = await request(app.app.callback()).get('/songs/show') | |
expect(response.status).toBe(200) | |
expect(response.body).toEqual({ songsList: providerResponse }) | |
expect(provider.searchByName).toHaveBeenCalledTimes(1) | |
expect(provider.searchByName).toHaveBeenNthCalledWith(1, 'show') | |
}) | |
test('401 - SERVICE ERROR', async () => { | |
const error = new Error('Unauthorized') | |
error.name = 'StatusCodeError' | |
error.statusCode = 401 | |
error.error = 'Unauthorized' | |
provider.searchByName = jest.fn().mockRejectedValueOnce(error) | |
const response = await request(app.app.callback()).get('/songs/show') | |
expect(response.status).toBe(401) | |
expect(response.text).toBe('Unauthorized') | |
}) | |
test('500 - REQUEST TIMEOUT', async () => { | |
const error = new Error('Socket timeout') | |
error.name = 'RequestError' | |
error.cause = { code: 'ESOCKETTIMEDOUT' } | |
provider.searchByName = jest.fn().mockRejectedValueOnce(error) | |
const response = await request(app.app.callback()).get('/songs/show') | |
expect(response.status).toBe(500) | |
expect(response.text).toBe('Service is unavailable') | |
}) | |
}) | |
... | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment