RESTController.test.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import mongoose from 'mongoose'
  2. import RESTController from './RESTController'
  3. import { Model } from '../routeGenModel'
  4. const spyFind = jest.spyOn(Model, 'find')
  5. const spyFindOne = jest.spyOn(Model, 'findOne')
  6. const spyCreate = jest.spyOn(Model, 'create')
  7. const spyUpdate = jest.spyOn(Model, 'update')
  8. const spyRemove = jest.spyOn(Model, 'remove')
  9. describe('REST Controller', () => {
  10. const ModelController = new RESTController(Model)
  11. const createdModel = {
  12. name: 'Gisele'
  13. }
  14. const updatedModel = {
  15. name: 'Adriana'
  16. }
  17. let createdId = null
  18. beforeAll(() => {
  19. return mongoose.connect('mongodb://localhost/rest-controller')
  20. })
  21. afterAll(() => {
  22. Model.remove({}, err => {
  23. console.log(err)
  24. })
  25. })
  26. it('creates an element', () => {
  27. return ModelController.create(createdModel)
  28. .then(response => {
  29. // Check the copy due to a bug with jest.
  30. const copy = {}
  31. Object.keys(Model.schema.paths).forEach(key => {
  32. copy[key] = response.model[key]
  33. })
  34. createdId = response.model._id
  35. expect(copy).toMatchObject({__v: 0, ...createdModel})
  36. expect(spyCreate).toHaveBeenCalledWith(createdModel)
  37. expect(spyCreate).toHaveBeenCalledTimes(1)
  38. spyCreate.mockClear()
  39. })
  40. })
  41. it('reads an element', () => {
  42. return ModelController.read(createdId)
  43. .then(response => {
  44. // Check the copy due to a bug with jest.
  45. const copy = {}
  46. Object.keys(Model.schema.paths).forEach(key => {
  47. copy[key] = response.model[key]
  48. })
  49. expect(copy).toMatchObject({__v: 0, ...createdModel})
  50. expect(spyFindOne).toHaveBeenCalledWith({ _id: createdId })
  51. expect(spyFindOne).toHaveBeenCalledTimes(1)
  52. spyFindOne.mockClear()
  53. })
  54. })
  55. it('lists elements', () => {
  56. return ModelController.list()
  57. .then(response => {
  58. // Check the copy due to a bug with jest.
  59. const copy = {}
  60. Object.keys(Model.schema.paths).forEach(key => {
  61. copy[key] = response.model[0][key]
  62. })
  63. expect(copy).toMatchObject({__v: 0, ...createdModel})
  64. expect(response.model).toHaveLength(1)
  65. expect(spyFind).toHaveBeenCalledWith({})
  66. expect(spyFind).toHaveBeenCalledTimes(1)
  67. spyFind.mockClear()
  68. })
  69. })
  70. it('update elements', () => {
  71. return ModelController.update(createdId, { name: 'Adriana' })
  72. .then(response => {
  73. // Check the copy due to a bug with jest.
  74. const copy = {}
  75. Object.keys(Model.schema.paths).forEach(key => {
  76. copy[key] = response.model[key]
  77. })
  78. expect(copy).toMatchObject({ __v: 0, ...updatedModel })
  79. expect(spyFindOne).toHaveBeenCalledWith({ _id: createdId })
  80. expect(spyFindOne).toHaveBeenCalledTimes(1)
  81. spyFindOne.mockClear()
  82. })
  83. })
  84. it('delete elements', () => {
  85. return ModelController.delete(createdId)
  86. .then(response => {
  87. /* / Check the copy due to a bug with jest.
  88. const copy = {}
  89. Object.keys(Model.schema.paths).forEach(key => {
  90. copy[key] = response.model[key]
  91. }) */
  92. expect(response).toMatchObject({})
  93. expect(spyRemove).toHaveBeenCalledWith({ _id: createdId })
  94. expect(spyRemove).toHaveBeenCalledTimes(1)
  95. spyRemove.mockClear()
  96. })
  97. })
  98. })