overleaf/services/track-changes/test/unit/js/UpdateTrimmer/UpdateTrimmerTests.js

183 lines
5.7 KiB
JavaScript
Raw Normal View History

/* eslint-disable
no-return-assign,
no-unused-vars,
*/
// TODO: This file was created by bulk-decaffeinate.
// Fix any style issues and re-enable lint.
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const sinon = require('sinon')
const { expect } = require('chai')
const modulePath = '../../../../app/js/UpdateTrimmer.js'
const SandboxedModule = require('sandboxed-module')
const tk = require('timekeeper')
2020-06-04 04:24:21 -04:00
describe('UpdateTrimmer', function () {
beforeEach(function () {
this.now = new Date()
tk.freeze(this.now)
this.UpdateTrimmer = SandboxedModule.require(modulePath, {
requires: {
'./WebApiManager': (this.WebApiManager = {}),
'./MongoManager': (this.MongoManager = {})
}
})
this.callback = sinon.stub()
return (this.project_id = 'mock-project-id')
})
2020-06-04 04:24:21 -04:00
afterEach(function () {
return tk.reset()
})
2020-06-04 04:24:21 -04:00
return describe('shouldTrimUpdates', function () {
beforeEach(function () {
this.metadata = {}
this.details = { features: {} }
this.MongoManager.getProjectMetaData = sinon
.stub()
.callsArgWith(1, null, this.metadata)
this.MongoManager.setProjectMetaData = sinon.stub().callsArgWith(2)
this.MongoManager.upgradeHistory = sinon.stub().callsArgWith(1)
return (this.WebApiManager.getProjectDetails = sinon
.stub()
.callsArgWith(1, null, this.details))
})
2020-06-04 04:24:21 -04:00
describe('with preserveHistory set in the project meta data', function () {
beforeEach(function () {
this.metadata.preserveHistory = true
return this.UpdateTrimmer.shouldTrimUpdates(
this.project_id,
this.callback
)
})
2020-06-04 04:24:21 -04:00
it('should look up the meta data', function () {
return this.MongoManager.getProjectMetaData
.calledWith(this.project_id)
.should.equal(true)
})
2020-06-04 04:24:21 -04:00
it('should not look up the project details', function () {
return this.WebApiManager.getProjectDetails.called.should.equal(false)
})
2020-06-04 04:24:21 -04:00
return it('should return false', function () {
return this.callback.calledWith(null, false).should.equal(true)
})
})
2020-06-04 04:24:21 -04:00
describe('without preserveHistory set in the project meta data', function () {
beforeEach(function () {
return (this.metadata.preserveHistory = false)
})
2020-06-04 04:24:21 -04:00
describe('when the project has the versioning feature', function () {
beforeEach(function () {
this.details.features.versioning = true
return this.UpdateTrimmer.shouldTrimUpdates(
this.project_id,
this.callback
)
})
2020-06-04 04:24:21 -04:00
it('should look up the meta data', function () {
return this.MongoManager.getProjectMetaData
.calledWith(this.project_id)
.should.equal(true)
})
2020-06-04 04:24:21 -04:00
it('should look up the project details', function () {
return this.WebApiManager.getProjectDetails
.calledWith(this.project_id)
.should.equal(true)
})
2020-06-04 04:24:21 -04:00
it('should insert preserveHistory into the metadata', function () {
return this.MongoManager.setProjectMetaData
.calledWith(this.project_id, { preserveHistory: true })
.should.equal(true)
})
2020-06-04 04:24:21 -04:00
it('should upgrade any existing history', function () {
return this.MongoManager.upgradeHistory
.calledWith(this.project_id)
.should.equal(true)
})
2020-06-04 04:24:21 -04:00
return it('should return false', function () {
return this.callback.calledWith(null, false).should.equal(true)
})
})
2020-06-04 04:24:21 -04:00
return describe('when the project does not have the versioning feature', function () {
beforeEach(function () {
this.details.features.versioning = false
return this.UpdateTrimmer.shouldTrimUpdates(
this.project_id,
this.callback
)
})
2020-06-04 04:24:21 -04:00
return it('should return true', function () {
return this.callback.calledWith(null, true).should.equal(true)
})
})
})
2020-06-04 04:24:21 -04:00
return describe('without any meta data', function () {
beforeEach(function () {
return (this.MongoManager.getProjectMetaData = sinon
.stub()
.callsArgWith(1, null, null))
})
2020-06-04 04:24:21 -04:00
describe('when the project has the versioning feature', function () {
beforeEach(function () {
this.details.features.versioning = true
return this.UpdateTrimmer.shouldTrimUpdates(
this.project_id,
this.callback
)
})
2020-06-04 04:24:21 -04:00
it('should insert preserveHistory into the metadata', function () {
return this.MongoManager.setProjectMetaData
.calledWith(this.project_id, { preserveHistory: true })
.should.equal(true)
})
2020-06-04 04:24:21 -04:00
it('should upgrade any existing history', function () {
return this.MongoManager.upgradeHistory
.calledWith(this.project_id)
.should.equal(true)
})
2020-06-04 04:24:21 -04:00
return it('should return false', function () {
return this.callback.calledWith(null, false).should.equal(true)
})
})
2020-06-04 04:24:21 -04:00
return describe('when the project does not have the versioning feature', function () {
beforeEach(function () {
this.details.features.versioning = false
return this.UpdateTrimmer.shouldTrimUpdates(
this.project_id,
this.callback
)
})
2020-06-04 04:24:21 -04:00
return it('should return true', function () {
return this.callback.calledWith(null, true).should.equal(true)
})
})
})
})
})