RESTController.test.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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/rest-controller')
  22. })
  23. afterAll(() => {
  24. Model.remove({}, err => {
  25. console.log(err)
  26. })
  27. })
  28. it('creates an element', () => {
  29. return ModelController.create(createdModel)
  30. .then(response => {
  31. // Check the copy due to a bug with jest.
  32. const copy = {}
  33. Object.keys(Model.schema.paths).forEach(key => {
  34. copy[key] = response[key]
  35. })
  36. createdId = response._id
  37. expect(copy).toMatchObject({__v: 0, ...createdModel})
  38. expect(spyCreate).toHaveBeenCalledWith(createdModel)
  39. expect(spyCreate).toHaveBeenCalledTimes(1)
  40. spyCreate.mockClear()
  41. })
  42. })
  43. it('reads an element', () => {
  44. return ModelController.read(createdId)
  45. .then(response => {
  46. // Check the copy due to a bug with jest.
  47. const copy = {}
  48. Object.keys(Model.schema.paths).forEach(key => {
  49. copy[key] = response[key]
  50. })
  51. expect(copy).toMatchObject({__v: 0, ...createdModel})
  52. expect(spyFindOne).toHaveBeenCalledWith({ _id: createdId })
  53. expect(spyFindOne).toHaveBeenCalledTimes(1)
  54. spyFindOne.mockClear()
  55. })
  56. })
  57. it('lists elements', () => {
  58. return ModelController.list()
  59. .then(response => {
  60. // Check the copy due to a bug with jest.
  61. const copy = {}
  62. Object.keys(Model.schema.paths).forEach(key => {
  63. copy[key] = response[0][key]
  64. })
  65. expect(copy).toMatchObject({__v: 0, ...createdModel})
  66. expect(response).toHaveLength(1)
  67. expect(spyFind).toHaveBeenCalledWith({})
  68. expect(spyFind).toHaveBeenCalledTimes(1)
  69. spyFind.mockClear()
  70. })
  71. })
  72. it('update elements', () => {
  73. return ModelController.update(createdId, { name: 'Adriana' })
  74. .then(response => {
  75. // Check the copy due to a bug with jest.
  76. const copy = {}
  77. Object.keys(Model.schema.paths).forEach(key => {
  78. copy[key] = response[key]
  79. })
  80. expect(copy).toMatchObject({ __v: 0, ...updatedModel })
  81. expect(spyFindOne).toHaveBeenCalledWith({ _id: createdId })
  82. expect(spyFindOne).toHaveBeenCalledTimes(1)
  83. spyFindOne.mockClear()
  84. })
  85. })
  86. it('delete elements', () => {
  87. return ModelController.delete(createdId)
  88. .then(response => {
  89. expect(response).toMatchObject({})
  90. expect(spyRemove).toHaveBeenCalledWith({ _id: createdId })
  91. expect(spyRemove).toHaveBeenCalledTimes(1)
  92. spyRemove.mockClear()
  93. })
  94. })
  95. })