RESTController.test.js 3.2 KB

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