DeletingADocumentTests.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. const sinon = require('sinon')
  2. const MockProjectHistoryApi = require('./helpers/MockProjectHistoryApi')
  3. const MockWebApi = require('./helpers/MockWebApi')
  4. const DocUpdaterClient = require('./helpers/DocUpdaterClient')
  5. const DocUpdaterApp = require('./helpers/DocUpdaterApp')
  6. const { setTimeout } = require('node:timers/promises')
  7. describe('Deleting a document', function () {
  8. before(async function () {
  9. this.lines = ['one', 'two', 'three']
  10. this.version = 42
  11. this.update = {
  12. doc: this.doc_id,
  13. op: [
  14. {
  15. i: 'one and a half\n',
  16. p: 4,
  17. },
  18. ],
  19. v: this.version,
  20. }
  21. this.result = ['one', 'one and a half', 'two', 'three']
  22. sinon.spy(MockProjectHistoryApi, 'flushProject')
  23. await DocUpdaterApp.ensureRunning()
  24. })
  25. after(function () {
  26. MockProjectHistoryApi.flushProject.restore()
  27. })
  28. describe('when the updated doc exists in the doc updater', function () {
  29. before(async function () {
  30. this.project_id = DocUpdaterClient.randomId()
  31. this.doc_id = DocUpdaterClient.randomId()
  32. sinon.spy(MockWebApi, 'setDocument')
  33. sinon.spy(MockWebApi, 'getDocument')
  34. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  35. lines: this.lines,
  36. version: this.version,
  37. })
  38. await DocUpdaterClient.preloadDoc(this.project_id, this.doc_id)
  39. await DocUpdaterClient.sendUpdate(
  40. this.project_id,
  41. this.doc_id,
  42. this.update
  43. )
  44. await setTimeout(200)
  45. const res = await DocUpdaterClient.deleteDoc(this.project_id, this.doc_id)
  46. this.statusCode = res.status
  47. })
  48. after(function () {
  49. MockWebApi.setDocument.restore()
  50. MockWebApi.getDocument.restore()
  51. })
  52. it('should return a 204 status code', function () {
  53. this.statusCode.should.equal(204)
  54. })
  55. it('should send the updated document and version to the web api', function () {
  56. MockWebApi.setDocument
  57. .calledWith(this.project_id, this.doc_id, this.result, this.version + 1)
  58. .should.equal(true)
  59. })
  60. it('should need to reload the doc if read again', async function () {
  61. MockWebApi.getDocument.resetHistory()
  62. MockWebApi.getDocument.called.should.equals(false)
  63. await DocUpdaterClient.getDoc(this.project_id, this.doc_id)
  64. MockWebApi.getDocument
  65. .calledWith(this.project_id, this.doc_id)
  66. .should.equal(true)
  67. })
  68. it('should flush project history', function () {
  69. MockProjectHistoryApi.flushProject
  70. .calledWith(this.project_id)
  71. .should.equal(true)
  72. })
  73. })
  74. describe('when the doc is not in the doc updater', function () {
  75. before(async function () {
  76. this.project_id = DocUpdaterClient.randomId()
  77. this.doc_id = DocUpdaterClient.randomId()
  78. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  79. lines: this.lines,
  80. })
  81. sinon.spy(MockWebApi, 'setDocument')
  82. sinon.spy(MockWebApi, 'getDocument')
  83. const res = await DocUpdaterClient.deleteDoc(this.project_id, this.doc_id)
  84. this.statusCode = res.status
  85. })
  86. after(function () {
  87. MockWebApi.setDocument.restore()
  88. MockWebApi.getDocument.restore()
  89. })
  90. it('should return a 204 status code', function () {
  91. this.statusCode.should.equal(204)
  92. })
  93. it('should not need to send the updated document to the web api', function () {
  94. MockWebApi.setDocument.called.should.equal(false)
  95. })
  96. it('should need to reload the doc if read again', async function () {
  97. MockWebApi.getDocument.called.should.equals(false)
  98. await DocUpdaterClient.getDoc(this.project_id, this.doc_id)
  99. MockWebApi.getDocument
  100. .calledWith(this.project_id, this.doc_id)
  101. .should.equal(true)
  102. })
  103. it('should flush project history', function () {
  104. MockProjectHistoryApi.flushProject
  105. .calledWith(this.project_id)
  106. .should.equal(true)
  107. })
  108. })
  109. })