RestControllerVersioned.test.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import * as dotenv from 'dotenv'
  2. dotenv.config()
  3. import mongoose from 'mongoose'
  4. import RESTControllerVersioned from './RESTControllerVersioned'
  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 Versioned', () => {
  12. const ModelController = new RESTControllerVersioned(Model)
  13. const createdModel = {
  14. name: 'Gisele',
  15. tag: 'gisele'
  16. }
  17. const updatedModel = {
  18. name: 'Adriana'
  19. }
  20. const createdHistory = {
  21. author: 'Tomi',
  22. version: '1st shot',
  23. comment: 'Nothing to brag about',
  24. head: true
  25. }
  26. const updatedHistory = {
  27. author: 'Ben',
  28. version: '2nd shot',
  29. comment: 'I\'m a little narcissict',
  30. head: true
  31. }
  32. let createdId = null
  33. beforeAll(() => {
  34. return mongoose.connect('mongodb://localhost/rest-controller')
  35. Model.remove({}, err => {
  36. console.log(err)
  37. })
  38. })
  39. afterAll(() => {
  40. })
  41. it('creates an element', () => {
  42. return ModelController.create(createdModel, createdHistory.author, createdHistory.version, createdHistory.comment)
  43. .then(response => {
  44. // Check the copy due to a bug with jest.
  45. const copy = JSON.parse(JSON.stringify(response))
  46. createdId = response._id
  47. expect(copy).toMatchObject({__v: 0, _history: createdHistory, ...createdModel })
  48. expect(spyCreate).toHaveBeenCalledTimes(1)
  49. spyCreate.mockClear()
  50. })
  51. })
  52. it('reads an element', () => {
  53. return ModelController.read(createdModel.tag)
  54. .then(response => {
  55. // Check the copy due to a bug with jest.
  56. const copy = JSON.parse(JSON.stringify(response))
  57. expect(copy).toMatchObject({__v: 0, ...createdModel})
  58. expect(spyFindOne).toHaveBeenCalledWith({ '_history.head': true, tag: createdModel.tag })
  59. expect(spyFindOne).toHaveBeenCalledTimes(1)
  60. spyFindOne.mockClear()
  61. })
  62. })
  63. it('lists elements', () => {
  64. return ModelController.list()
  65. .then(response => {
  66. // Check the copy due to a bug with jest.
  67. const copy = JSON.parse(JSON.stringify(response))
  68. expect(copy[0]).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('gisele', updatedModel, updatedHistory.author, updatedHistory.version, updatedHistory.comment)
  77. .then(response => {
  78. // Check the copy due to a bug with jest.
  79. const copy = JSON.parse(JSON.stringify(response))
  80. expect(copy).toMatchObject({ __v: 0, ...updatedModel })
  81. expect(spyFindOne).toHaveBeenCalledWith({ '_history.head': true, tag: 'gisele' })
  82. expect(spyFindOne).toHaveBeenCalledTimes(1)
  83. spyFindOne.mockClear()
  84. })
  85. })
  86. it('delete elements', () => {
  87. return ModelController.delete('gisele')
  88. .then(response => {
  89. // Check the copy due to a bug with jest.
  90. const copy = JSON.parse(JSON.stringify(response))
  91. expect(copy).toMatchObject({ tag: 'gisele', _history: { head: false } })
  92. expect(spyFindOne).toHaveBeenCalledWith({ '_history.head': true, tag: 'gisele' })
  93. expect(spyFindOne).toHaveBeenCalledTimes(1)
  94. spyFindOne.mockClear()
  95. })
  96. })
  97. it('lists elements after all operations', () => {
  98. return ModelController.list()
  99. .then(response => {
  100. // Check the copy due to a bug with jest.
  101. const copy = JSON.parse(JSON.stringify(response))
  102. expect(copy).toEqual([])
  103. expect(spyFind).toHaveBeenCalledWith({'_history.head': true})
  104. expect(spyFind).toHaveBeenCalledTimes(1)
  105. spyFind.mockClear()
  106. })
  107. })
  108. it('lists elements after all operations', () => {
  109. return ModelController.list({})
  110. .then(response => {
  111. // Check the copy due to a bug with jest.
  112. const copy = JSON.parse(JSON.stringify(response))
  113. expect(copy[0]).toMatchObject({ ...createdModel, _history: { head: false } })
  114. expect(copy[1]).toMatchObject({ ...updatedModel, _history: { head: false } })
  115. expect(response).toHaveLength(2)
  116. // expect(spyFind).toHaveBeenCalledWith({})
  117. expect(spyFind).toHaveBeenCalledTimes(1)
  118. spyFind.mockClear()
  119. })
  120. })
  121. })