ProjectEntityRestoreHandlerTests.js 2.2 KB

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