RESTControllerVersioned.test.js 4.5 KB

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