routeGen.test.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import mongoose from 'mongoose'
  2. const table = '__routeGenTest__'
  3. const db = `mongodb://localhost/${table}`
  4. mongoose.connect(db)
  5. import routeGen from './routeGen'
  6. import Model from './routeGenModel'
  7. import { model, agency, newModel } from './_samples'
  8. // jest.mock('./projects/model')
  9. // import Project from './projects/model'
  10. const spyFindOne = jest.spyOn(Model, 'findOne')
  11. const spyFind = jest.spyOn(Model, 'find')
  12. // const spySave = jest.spyOn(Model, 'save')
  13. const spyRemove = jest.spyOn(Model, 'remove')
  14. class Result {
  15. constructor () {
  16. this._body = null
  17. this._status = null
  18. }
  19. status (value) {
  20. this._status = value
  21. }
  22. send (data) {
  23. console.log('SEND', data)
  24. this._body = data
  25. }
  26. json (data) {
  27. console.log('JSON', data)
  28. this._status = 200
  29. this._body = data
  30. }
  31. }
  32. describe('routeGen for versioned models', () => {
  33. const route = routeGen(Model)
  34. const res = new Result()
  35. it('generates the handlers', () => {
  36. // Generate a version controlled model
  37. expect(route.get).not.toBeUndefined()
  38. expect(route.post).not.toBeUndefined()
  39. expect(route.put).not.toBeUndefined()
  40. expect(route.del).not.toBeUndefined()
  41. })
  42. describe('get handler', () => {
  43. it('get without arguments returns a list of items', () => {
  44. // call the GET handler
  45. const reqList = { params: {}, query: { limit: 2.1, offset: 1.1 } }
  46. route.get(reqList, res)
  47. expect(res._body).toEqual(model.slice(1, 3))
  48. expect(res._status).toBe(200)
  49. expect(spyFind).toHaveBeenCalledWith({'history.head': true}, {limit: 2, skip: 1}, expect.anything())
  50. })
  51. it('return empty lists.', () => {
  52. // call the GET handler
  53. const reqList = { params: {}, query: { limit: 1, offset: 100 } }
  54. route.get(reqList, res)
  55. expect(res._body).toEqual([])
  56. expect(res._status).toBe(200)
  57. })
  58. it('get with arguments returns queried item by id', () => {
  59. const reqElem = { params: { id: '18a0d6883d34293254afae42' }, query: {} }
  60. route.get(reqElem, res)
  61. expect(res._body).toEqual(projects[0])
  62. expect(res._status).toBe(200)
  63. })
  64. it('get with arguments return queried item by tag (only HEAD in history)', () => {
  65. const reqElem = { params: { id: 'JU_CSD' }, query: {} }
  66. route.get(reqElem, res)
  67. expect(res._body).toEqual(projects[1])
  68. expect(res._status).toBe(200)
  69. })
  70. it('get returns error, when item is not found', () => {
  71. const reqElem = { params: { id: '99a0d6883d34293254afae42' }, query: {} }
  72. route.get(reqElem, res)
  73. expect(res._body).toEqual(Error('item not found'))
  74. expect(res._status).toBe(404)
  75. console.log(spyFind.mock, spyFindOne.mock)
  76. expect(spyFindOne.mock).toBe(true)
  77. })
  78. })
  79. describe('post handler', () => {
  80. it('post creates a new history object', () => {
  81. console.log(newModel)
  82. const reqElem = { body: newModel }
  83. route.post(reqElem, res)
  84. console.log(res)
  85. expect(res._body).toEqual({_id: expect.anything()})
  86. expect(res._status).toBe(200)
  87. console.log(res)
  88. route.get({params: {id: res._body}, query: {}}, res)
  89. // expect(res._body).toEqual(true)
  90. console.log(res)
  91. })
  92. })
  93. })