Advertisement
Guest User

Untitled

a guest
Aug 24th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. jest.mock('@src/provider')
  2. const provider = require('@src/provider')
  3.  
  4. const request = require('supertest')
  5.  
  6. const App = require('@src/app')
  7. const router = require('@src/router')
  8.  
  9. const app = new App({ router }).init()
  10.  
  11. describe('App integration test', () => {
  12. test('GET /blah 404 - Method not allowed', async () => {
  13. const response = await request(app.app.callback()).get('/blah')
  14. expect(response.status).toBe(404)
  15. })
  16.  
  17. describe('GET /songs/:name', () => {
  18. test('200 - SUCCESS', async () => {
  19. const providerResponse = [
  20. {
  21. id: 51127,
  22. type: 'Song',
  23. title: 'The Show Must Go On',
  24. artist: {
  25. id: 55,
  26. type: 'Artist',
  27. nameWithoutThePrefix: 'Queen',
  28. useThePrefix: false,
  29. name: 'Queen'
  30. },
  31. chordsPresent: true,
  32. tabTypes: ['PLAYER', 'TEXT_GUITAR_TAB', 'CHORDS', 'TEXT_BASS_TAB']
  33. }
  34. ]
  35. provider.searchByName = jest.fn().mockResolvedValueOnce(providerResponse)
  36.  
  37. const response = await request(app.app.callback()).get('/songs/show')
  38. expect(response.status).toBe(200)
  39. expect(response.body).toEqual({ songsList: providerResponse })
  40. expect(provider.searchByName).toHaveBeenCalledTimes(1)
  41. expect(provider.searchByName).toHaveBeenNthCalledWith(1, 'show')
  42. })
  43.  
  44. test('401 - SERVICE ERROR', async () => {
  45. const error = new Error('Unauthorized')
  46. error.name = 'StatusCodeError'
  47. error.statusCode = 401
  48. error.error = 'Unauthorized'
  49. provider.searchByName = jest.fn().mockRejectedValueOnce(error)
  50.  
  51. const response = await request(app.app.callback()).get('/songs/show')
  52. expect(response.status).toBe(401)
  53. expect(response.text).toBe('Unauthorized')
  54. })
  55.  
  56. test('500 - REQUEST TIMEOUT', async () => {
  57. const error = new Error('Socket timeout')
  58. error.name = 'RequestError'
  59. error.cause = { code: 'ESOCKETTIMEDOUT' }
  60. provider.searchByName = jest.fn().mockRejectedValueOnce(error)
  61.  
  62. const response = await request(app.app.callback()).get('/songs/show')
  63. expect(response.status).toBe(500)
  64. expect(response.text).toBe('Service is unavailable')
  65. })
  66. })
  67. ...
  68. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement