route.test.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import router, { getHandler, postHandler, putHandler, deleteHandler } from './route'
  2. import samples from './_sampleData'
  3. import sinon from 'sinon'
  4. import mongoose from 'mongoose'
  5. jest.mock('./model')
  6. function ResClass () {
  7. this._status = null
  8. this._data = null
  9. this.status = (status) => { this._status = status }
  10. this.send = (data) => { this._data = data }
  11. this.json = (data) => {
  12. this._data = data
  13. this._status = 200
  14. }
  15. }
  16. describe('Route handlers', () => {
  17. it('handles the GET request for lists.', () => {
  18. // if no _id is passed, return a list of objects.
  19. // apply the limits correctly
  20. const reqList = { params: [], query: {limit: 2.4, offset: 1.2} }
  21. const res = new ResClass()
  22. const returnValue = getHandler(reqList, res)
  23. expect(returnValue).toBeUndefined()
  24. expect(res._data).toBe(samples)
  25. expect(res._status).toBe(200)
  26. expect(ProjectFind.called).toBe(true)
  27. expect(ProjectFind.firstCall.args[0]).toEqual({})
  28. expect(ProjectFind.firstCall.args[1]).toEqual({limit: 2, skip: 1})
  29. })
  30. it('handles the GET request for single documents.', () => {
  31. const reqOne = { params: ['/58d3d6883d34293254afae42'], query: undefined }
  32. const res = new ResClass()
  33. const returnValue = getHandler(reqOne, res)
  34. expect(returnValue).toBeUndefined()
  35. expect(res._data).toBe(samples[0])
  36. expect(res._status).toBe(200)
  37. expect(ProjectFindOne.called).toBe(true)
  38. expect(ProjectFindOne.firstCall.args[0]).toEqual({_id: mongoose.Types.ObjectId('58d3d6883d34293254afae42')})
  39. })
  40. it('handles the POST request.', () => {
  41. const reqOne = { params: ['/58d3d6883d34293254afae42'], query: undefined }
  42. const res = new ResClass()
  43. const returnValue = postHandler(reqOne, res)
  44. expect(returnValue).toBeUndefined()
  45. expect(res._data).toBe(samples[0])
  46. expect(res._status).toBe(200)
  47. expect(ProjectFindOne.called).toBe(true)
  48. expect(ProjectFindOne.firstCall.args[0]).toEqual({_id: mongoose.Types.ObjectId('58d3d6883d34293254afae42')})
  49. })
  50. })