Update test files with vitest compat changes

GitOrigin-RevId: 494f906089d250268a5ff8c8a2150ff2692c37e2
This commit is contained in:
Andrew Rumble
2025-05-29 08:05:06 +00:00
committed by Copybot
parent 51dcc88f27
commit 873068a187
42 changed files with 9712 additions and 8194 deletions
@@ -1,5 +1,5 @@
import { vi } from 'vitest'
import sinon from 'sinon'
import esmock from 'esmock'
import MockRequest from '../helpers/MockRequest.js'
import MockResponse from '../helpers/MockResponse.js'
import Errors from '../../../../app/src/Features/Errors/Errors.js'
@@ -8,14 +8,14 @@ const MODULE_PATH =
'../../../../app/src/Features/Documents/DocumentController.mjs'
describe('DocumentController', function () {
beforeEach(async function () {
this.res = new MockResponse()
this.req = new MockRequest()
this.next = sinon.stub()
this.doc = { _id: 'doc-id-123' }
this.doc_lines = ['one', 'two', 'three']
this.version = 42
this.ranges = {
beforeEach(async function (ctx) {
ctx.res = new MockResponse()
ctx.req = new MockRequest()
ctx.next = sinon.stub()
ctx.doc = { _id: 'doc-id-123' }
ctx.doc_lines = ['one', 'two', 'three']
ctx.version = 42
ctx.ranges = {
comments: [
{
id: 'comment1',
@@ -35,11 +35,11 @@ describe('DocumentController', function () {
},
],
}
this.pathname = '/a/b/c/file.tex'
this.lastUpdatedAt = new Date().getTime()
this.lastUpdatedBy = 'fake-last-updater-id'
this.rev = 5
this.project = {
ctx.pathname = '/a/b/c/file.tex'
ctx.lastUpdatedAt = new Date().getTime()
ctx.lastUpdatedBy = 'fake-last-updater-id'
ctx.rev = 5
ctx.project = {
_id: 'project-id-123',
overleaf: {
history: {
@@ -48,81 +48,100 @@ describe('DocumentController', function () {
},
},
}
this.resolvedThreadIds = [
ctx.resolvedThreadIds = [
'comment2',
'comment4', // Comment in project but not in doc
]
this.ProjectGetter = {
ctx.ProjectGetter = {
promises: {
getProject: sinon.stub().resolves(this.project),
getProject: sinon.stub().resolves(ctx.project),
},
}
this.ProjectLocator = {
ctx.ProjectLocator = {
promises: {
findElement: sinon
.stub()
.resolves({ element: this.doc, path: { fileSystem: this.pathname } }),
.resolves({ element: ctx.doc, path: { fileSystem: ctx.pathname } }),
},
}
this.ProjectEntityHandler = {
ctx.ProjectEntityHandler = {
promises: {
getDoc: sinon.stub().resolves({
lines: this.doc_lines,
rev: this.rev,
version: this.version,
ranges: this.ranges,
lines: ctx.doc_lines,
rev: ctx.rev,
version: ctx.version,
ranges: ctx.ranges,
}),
},
}
this.ProjectEntityUpdateHandler = {
ctx.ProjectEntityUpdateHandler = {
promises: {
updateDocLines: sinon.stub().resolves(),
},
}
this.ChatApiHandler = {
ctx.ChatApiHandler = {
promises: {
getResolvedThreadIds: sinon.stub().resolves(this.resolvedThreadIds),
getResolvedThreadIds: sinon.stub().resolves(ctx.resolvedThreadIds),
},
}
this.DocumentController = await esmock.strict(MODULE_PATH, {
'../../../../app/src/Features/Project/ProjectGetter': this.ProjectGetter,
'../../../../app/src/Features/Project/ProjectLocator':
this.ProjectLocator,
'../../../../app/src/Features/Project/ProjectEntityHandler':
this.ProjectEntityHandler,
'../../../../app/src/Features/Project/ProjectEntityUpdateHandler':
this.ProjectEntityUpdateHandler,
'../../../../app/src/Features/Chat/ChatApiHandler': this.ChatApiHandler,
})
vi.doMock('../../../../app/src/Features/Project/ProjectGetter', () => ({
default: ctx.ProjectGetter,
}))
vi.doMock('../../../../app/src/Features/Project/ProjectLocator', () => ({
default: ctx.ProjectLocator,
}))
vi.doMock(
'../../../../app/src/Features/Project/ProjectEntityHandler',
() => ({
default: ctx.ProjectEntityHandler,
})
)
vi.doMock(
'../../../../app/src/Features/Project/ProjectEntityUpdateHandler',
() => ({
default: ctx.ProjectEntityUpdateHandler,
})
)
vi.doMock('../../../../app/src/Features/Chat/ChatApiHandler', () => ({
default: ctx.ChatApiHandler,
}))
ctx.DocumentController = (await import(MODULE_PATH)).default
})
describe('getDocument', function () {
beforeEach(function () {
this.req.params = {
Project_id: this.project._id,
doc_id: this.doc._id,
beforeEach(function (ctx) {
ctx.req.params = {
Project_id: ctx.project._id,
doc_id: ctx.doc._id,
}
})
describe('when project exists with project history enabled', function () {
beforeEach(function (done) {
this.res.callback = err => {
done(err)
}
this.DocumentController.getDocument(this.req, this.res, this.next)
beforeEach(function (ctx) {
return new Promise(resolve => {
ctx.res.callback = err => {
resolve(err)
}
ctx.DocumentController.getDocument(ctx.req, ctx.res, ctx.next)
})
})
it('should return the history id and display setting to the client as JSON', function () {
this.res.type.should.equal('application/json')
JSON.parse(this.res.body).should.deep.equal({
lines: this.doc_lines,
version: this.version,
ranges: this.ranges,
pathname: this.pathname,
projectHistoryId: this.project.overleaf.history.id,
it('should return the history id and display setting to the client as JSON', function (ctx) {
ctx.res.type.should.equal('application/json')
JSON.parse(ctx.res.body).should.deep.equal({
lines: ctx.doc_lines,
version: ctx.version,
ranges: ctx.ranges,
pathname: ctx.pathname,
projectHistoryId: ctx.project.overleaf.history.id,
projectHistoryType: 'project-history',
resolvedCommentIds: ['comment2'],
historyRangesSupport: false,
@@ -132,75 +151,81 @@ describe('DocumentController', function () {
})
describe('when the project does not exist', function () {
beforeEach(function (done) {
this.ProjectGetter.promises.getProject.resolves(null)
this.res.callback = err => {
done(err)
}
this.DocumentController.getDocument(this.req, this.res, this.next)
beforeEach(function (ctx) {
return new Promise(resolve => {
ctx.ProjectGetter.promises.getProject.resolves(null)
ctx.res.callback = err => {
resolve(err)
}
ctx.DocumentController.getDocument(ctx.req, ctx.res, ctx.next)
})
})
it('returns a 404', function () {
this.res.statusCode.should.equal(404)
it('returns a 404', function (ctx) {
ctx.res.statusCode.should.equal(404)
})
})
})
describe('setDocument', function () {
beforeEach(function () {
this.req.params = {
Project_id: this.project._id,
doc_id: this.doc._id,
beforeEach(function (ctx) {
ctx.req.params = {
Project_id: ctx.project._id,
doc_id: ctx.doc._id,
}
})
describe('when the document exists', function () {
beforeEach(function (done) {
this.req.body = {
lines: this.doc_lines,
version: this.version,
ranges: this.ranges,
lastUpdatedAt: this.lastUpdatedAt,
lastUpdatedBy: this.lastUpdatedBy,
}
this.res.callback = err => {
done(err)
}
this.DocumentController.setDocument(this.req, this.res, this.next)
beforeEach(function (ctx) {
return new Promise(resolve => {
ctx.req.body = {
lines: ctx.doc_lines,
version: ctx.version,
ranges: ctx.ranges,
lastUpdatedAt: ctx.lastUpdatedAt,
lastUpdatedBy: ctx.lastUpdatedBy,
}
ctx.res.callback = err => {
resolve(err)
}
ctx.DocumentController.setDocument(ctx.req, ctx.res, ctx.next)
})
})
it('should update the document in Mongo', function () {
it('should update the document in Mongo', function (ctx) {
sinon.assert.calledWith(
this.ProjectEntityUpdateHandler.promises.updateDocLines,
this.project._id,
this.doc._id,
this.doc_lines,
this.version,
this.ranges,
this.lastUpdatedAt,
this.lastUpdatedBy
ctx.ProjectEntityUpdateHandler.promises.updateDocLines,
ctx.project._id,
ctx.doc._id,
ctx.doc_lines,
ctx.version,
ctx.ranges,
ctx.lastUpdatedAt,
ctx.lastUpdatedBy
)
})
it('should return a successful response', function () {
this.res.success.should.equal(true)
it('should return a successful response', function (ctx) {
ctx.res.success.should.equal(true)
})
})
describe("when the document doesn't exist", function () {
beforeEach(function (done) {
this.ProjectEntityUpdateHandler.promises.updateDocLines.rejects(
new Errors.NotFoundError('document does not exist')
)
this.req.body = { lines: this.doc_lines }
this.next.callsFake(() => {
done()
beforeEach(function (ctx) {
return new Promise(resolve => {
ctx.ProjectEntityUpdateHandler.promises.updateDocLines.rejects(
new Errors.NotFoundError('document does not exist')
)
ctx.req.body = { lines: ctx.doc_lines }
ctx.next.callsFake(() => {
resolve()
})
ctx.DocumentController.setDocument(ctx.req, ctx.res, ctx.next)
})
this.DocumentController.setDocument(this.req, this.res, this.next)
})
it('should call next with the NotFoundError', function () {
this.next
it('should call next with the NotFoundError', function (ctx) {
ctx.next
.calledWith(sinon.match.instanceOf(Errors.NotFoundError))
.should.equal(true)
})