ProjectEntityRestoreHandler.test.mjs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import { vi, expect } from 'vitest'
  2. import sinon from 'sinon'
  3. const MODULE_PATH =
  4. '../../../../app/src/Features/Project/ProjectEntityRestoreHandler.mjs'
  5. describe('ProjectEntityRestoreHandler', function () {
  6. beforeEach(async function (ctx) {
  7. ctx.project = {
  8. _id: '123213jlkj9kdlsaj',
  9. }
  10. ctx.user = {
  11. _id: '588f3ddae8ebc1bac07c9fa4',
  12. first_name: 'bjkdsjfk',
  13. features: {},
  14. }
  15. ctx.docId = '4eecb1c1bffa66588e0000a2'
  16. ctx.DocModel = class Doc {
  17. constructor(options) {
  18. this.name = options.name
  19. this.lines = options.lines
  20. this._id = this.docId
  21. this.rev = 0
  22. }
  23. }
  24. ctx.ProjectEntityHandler = {
  25. promises: {
  26. getDoc: sinon.stub(),
  27. },
  28. }
  29. ctx.EditorController = {
  30. promises: {
  31. addDocWithRanges: sinon.stub(),
  32. },
  33. }
  34. vi.doMock(
  35. '../../../../app/src/Features/Project/ProjectEntityHandler.js',
  36. () => ({
  37. default: ctx.ProjectEntityHandler,
  38. })
  39. )
  40. vi.doMock(
  41. '../../../../app/src/Features/Editor/EditorController.js',
  42. () => ({
  43. default: ctx.EditorController,
  44. })
  45. )
  46. ctx.ProjectEntityRestoreHandler = (await import(MODULE_PATH)).default
  47. })
  48. it('should add a new doc with timestamp name and old content', async function (ctx) {
  49. const docName = 'deletedDoc'
  50. ctx.docLines = ['line one', 'line two']
  51. ctx.rev = 3
  52. ctx.ranges = { comments: [{ id: 123 }] }
  53. ctx.newDoc = new ctx.DocModel({
  54. name: ctx.docName,
  55. lines: undefined,
  56. _id: ctx.docId,
  57. rev: 0,
  58. })
  59. ctx.ProjectEntityHandler.promises.getDoc.resolves({
  60. lines: ctx.docLines,
  61. rev: ctx.rev,
  62. version: 'version',
  63. ranges: ctx.ranges,
  64. })
  65. ctx.EditorController.promises.addDocWithRanges = sinon
  66. .stub()
  67. .resolves(ctx.newDoc)
  68. await ctx.ProjectEntityRestoreHandler.promises.restoreDeletedDoc(
  69. ctx.project._id,
  70. ctx.docId,
  71. docName,
  72. ctx.user._id
  73. )
  74. const docNameMatcher = new RegExp(docName + '-\\d{4}-\\d{2}-\\d{2}-\\d+')
  75. expect(
  76. ctx.EditorController.promises.addDocWithRanges
  77. ).to.have.been.calledWith(
  78. ctx.project._id,
  79. null,
  80. sinon.match(docNameMatcher),
  81. ctx.docLines,
  82. ctx.ranges,
  83. null,
  84. ctx.user._id
  85. )
  86. })
  87. })