RestControllerVersioned.test.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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/__test_rest-controller_versioned__')
  35. Model.remove({}, err => {
  36. console.log(err)
  37. })
  38. })
  39. afterAll(() => {
  40. Model.remove({}, err => {
  41. console.log(err)
  42. })
  43. })
  44. it('creates an element', () => {
  45. return ModelController.create(createdModel, createdHistory.author, createdHistory.version, createdHistory.comment)
  46. .then(response => {
  47. // Check the copy due to a bug with jest.
  48. const copy = JSON.parse(JSON.stringify(response))
  49. createdId = response._id
  50. expect(copy).toMatchObject({__v: 0, _history: createdHistory, ...createdModel })
  51. expect(spyCreate).toHaveBeenCalledTimes(1)
  52. spyCreate.mockClear()
  53. })
  54. })
  55. it('reads an element', () => {
  56. return ModelController.read(createdModel.tag)
  57. .then(response => {
  58. // Check the copy due to a bug with jest.
  59. const copy = JSON.parse(JSON.stringify(response))
  60. expect(copy).toMatchObject({__v: 0, ...createdModel})
  61. expect(spyFindOne).toHaveBeenCalledWith({ '_history.head': true, tag: createdModel.tag })
  62. expect(spyFindOne).toHaveBeenCalledTimes(1)
  63. spyFindOne.mockClear()
  64. })
  65. })
  66. it('lists elements', () => {
  67. return ModelController.list()
  68. .then(response => {
  69. // Check the copy due to a bug with jest.
  70. const copy = JSON.parse(JSON.stringify(response))
  71. expect(copy[0]).toMatchObject({__v: 0, ...createdModel})
  72. expect(response).toHaveLength(1)
  73. // expect(spyFind).toHaveBeenCalledWith({})
  74. expect(spyFind).toHaveBeenCalledTimes(1)
  75. spyFind.mockClear()
  76. })
  77. })
  78. it('update elements', () => {
  79. return ModelController.update('gisele', updatedModel, updatedHistory.author, updatedHistory.version, updatedHistory.comment)
  80. .then(response => {
  81. // Check the copy due to a bug with jest.
  82. const copy = JSON.parse(JSON.stringify(response))
  83. expect(copy).toMatchObject({ __v: 0, ...updatedModel })
  84. expect(spyFindOne).toHaveBeenCalledWith({ '_history.head': true, tag: 'gisele' })
  85. expect(spyFindOne).toHaveBeenCalledTimes(1)
  86. spyFindOne.mockClear()
  87. })
  88. })
  89. it('delete elements', () => {
  90. return ModelController.delete('gisele')
  91. .then(response => {
  92. // Check the copy due to a bug with jest.
  93. const copy = JSON.parse(JSON.stringify(response))
  94. expect(copy).toMatchObject({ tag: 'gisele', _history: { head: false } })
  95. expect(spyFindOne).toHaveBeenCalledWith({ '_history.head': true, tag: 'gisele' })
  96. expect(spyFindOne).toHaveBeenCalledTimes(1)
  97. spyFindOne.mockClear()
  98. })
  99. })
  100. it('lists elements after all operations', () => {
  101. return ModelController.list()
  102. .then(response => {
  103. // Check the copy due to a bug with jest.
  104. const copy = JSON.parse(JSON.stringify(response))
  105. expect(copy).toEqual([])
  106. expect(spyFind).toHaveBeenCalledWith({'_history.head': true})
  107. expect(spyFind).toHaveBeenCalledTimes(1)
  108. spyFind.mockClear()
  109. })
  110. })
  111. it('lists elements after all operations', () => {
  112. return ModelController.list({})
  113. .then(response => {
  114. // Check the copy due to a bug with jest.
  115. const copy = JSON.parse(JSON.stringify(response))
  116. expect(copy[0]).toMatchObject({ ...createdModel, _history: { head: false } })
  117. expect(copy[1]).toMatchObject({ ...updatedModel, _history: { head: false } })
  118. expect(response).toHaveLength(2)
  119. // expect(spyFind).toHaveBeenCalledWith({})
  120. expect(spyFind).toHaveBeenCalledTimes(1)
  121. spyFind.mockClear()
  122. })
  123. })
  124. })