Update test files with vitest compat changes

GitOrigin-RevId: 494f906089d250268a5ff8c8a2150ff2692c37e2
This commit is contained in:
Andrew Rumble
2025-04-30 09:08:59 +01:00
committed by Copybot
parent 4cced4dcb8
commit 20619a740a
42 changed files with 9712 additions and 8194 deletions

View File

@@ -1,4 +1,4 @@
import esmock from 'esmock'
import { vi } from 'vitest'
import path from 'node:path'
import sinon from 'sinon'
import { expect } from 'chai'
@@ -13,185 +13,228 @@ const modulePath = path.join(
)
describe('BetaProgramController', function () {
beforeEach(async function () {
this.user = {
_id: (this.user_id = 'a_simple_id'),
beforeEach(async function (ctx) {
ctx.user = {
_id: (ctx.user_id = 'a_simple_id'),
email: 'user@example.com',
features: {},
betaProgram: false,
}
this.req = {
ctx.req = {
query: {},
session: {
user: this.user,
user: ctx.user,
},
}
this.SplitTestSessionHandler = {
ctx.SplitTestSessionHandler = {
promises: {
sessionMaintenance: sinon.stub(),
},
}
this.BetaProgramController = await esmock.strict(modulePath, {
'../../../../app/src/Features/SplitTests/SplitTestSessionHandler':
this.SplitTestSessionHandler,
'../../../../app/src/Features/BetaProgram/BetaProgramHandler':
(this.BetaProgramHandler = {
vi.doMock(
'../../../../app/src/Features/SplitTests/SplitTestSessionHandler',
() => ({
default: ctx.SplitTestSessionHandler,
})
)
vi.doMock(
'../../../../app/src/Features/BetaProgram/BetaProgramHandler',
() => ({
default: (ctx.BetaProgramHandler = {
promises: {
optIn: sinon.stub().resolves(),
optOut: sinon.stub().resolves(),
},
}),
'../../../../app/src/Features/User/UserGetter': (this.UserGetter = {
})
)
vi.doMock('../../../../app/src/Features/User/UserGetter', () => ({
default: (ctx.UserGetter = {
promises: {
getUser: sinon.stub().resolves(),
},
}),
'@overleaf/settings': (this.settings = {
}))
vi.doMock('@overleaf/settings', () => ({
default: (ctx.settings = {
languages: {},
}),
'../../../../app/src/Features/Authentication/AuthenticationController':
(this.AuthenticationController = {
getLoggedInUserId: sinon.stub().returns(this.user._id),
}))
vi.doMock(
'../../../../app/src/Features/Authentication/AuthenticationController',
() => ({
default: (ctx.AuthenticationController = {
getLoggedInUserId: sinon.stub().returns(ctx.user._id),
}),
})
this.res = new MockResponse()
this.next = sinon.stub()
})
)
ctx.BetaProgramController = (await import(modulePath)).default
ctx.res = new MockResponse()
ctx.next = sinon.stub()
})
describe('optIn', function () {
it("should redirect to '/beta/participate'", function (done) {
this.res.callback = () => {
this.res.redirectedTo.should.equal('/beta/participate')
done()
}
this.BetaProgramController.optIn(this.req, this.res, done)
it("should redirect to '/beta/participate'", function (ctx) {
return new Promise(resolve => {
ctx.res.callback = () => {
ctx.res.redirectedTo.should.equal('/beta/participate')
resolve()
}
ctx.BetaProgramController.optIn(ctx.req, ctx.res, resolve)
})
})
it('should not call next with an error', function () {
this.BetaProgramController.optIn(this.req, this.res, this.next)
this.next.callCount.should.equal(0)
it('should not call next with an error', function (ctx) {
ctx.BetaProgramController.optIn(ctx.req, ctx.res, ctx.next)
ctx.next.callCount.should.equal(0)
})
it('should call BetaProgramHandler.optIn', function () {
this.BetaProgramController.optIn(this.req, this.res, this.next)
this.BetaProgramHandler.promises.optIn.callCount.should.equal(1)
it('should call BetaProgramHandler.optIn', function (ctx) {
ctx.BetaProgramController.optIn(ctx.req, ctx.res, ctx.next)
ctx.BetaProgramHandler.promises.optIn.callCount.should.equal(1)
})
it('should invoke the session maintenance', function (done) {
this.res.callback = () => {
this.SplitTestSessionHandler.promises.sessionMaintenance.should.have.been.calledWith(
this.req
)
done()
}
this.BetaProgramController.optIn(this.req, this.res, done)
it('should invoke the session maintenance', function (ctx) {
return new Promise(resolve => {
ctx.res.callback = () => {
ctx.SplitTestSessionHandler.promises.sessionMaintenance.should.have.been.calledWith(
ctx.req
)
resolve()
}
ctx.BetaProgramController.optIn(ctx.req, ctx.res, resolve)
})
})
describe('when BetaProgramHandler.opIn produces an error', function () {
beforeEach(function () {
this.BetaProgramHandler.promises.optIn.throws(new Error('woops'))
beforeEach(function (ctx) {
ctx.BetaProgramHandler.promises.optIn.throws(new Error('woops'))
})
it("should not redirect to '/beta/participate'", function () {
this.BetaProgramController.optIn(this.req, this.res, this.next)
this.res.redirect.callCount.should.equal(0)
it("should not redirect to '/beta/participate'", function (ctx) {
ctx.BetaProgramController.optIn(ctx.req, ctx.res, ctx.next)
ctx.res.redirect.callCount.should.equal(0)
})
it('should produce an error', function (done) {
this.BetaProgramController.optIn(this.req, this.res, err => {
expect(err).to.be.instanceof(Error)
done()
it('should produce an error', function (ctx) {
return new Promise(resolve => {
ctx.BetaProgramController.optIn(ctx.req, ctx.res, err => {
expect(err).to.be.instanceof(Error)
resolve()
})
})
})
})
})
describe('optOut', function () {
it("should redirect to '/beta/participate'", function (done) {
this.res.callback = () => {
expect(this.res.redirectedTo).to.equal('/beta/participate')
done()
}
this.BetaProgramController.optOut(this.req, this.res, done)
it("should redirect to '/beta/participate'", function (ctx) {
return new Promise(resolve => {
ctx.res.callback = () => {
expect(ctx.res.redirectedTo).to.equal('/beta/participate')
resolve()
}
ctx.BetaProgramController.optOut(ctx.req, ctx.res, resolve)
})
})
it('should not call next with an error', function (done) {
this.res.callback = () => {
this.next.callCount.should.equal(0)
done()
}
this.BetaProgramController.optOut(this.req, this.res, done)
it('should not call next with an error', function (ctx) {
return new Promise(resolve => {
ctx.res.callback = () => {
ctx.next.callCount.should.equal(0)
resolve()
}
ctx.BetaProgramController.optOut(ctx.req, ctx.res, resolve)
})
})
it('should call BetaProgramHandler.optOut', function (done) {
this.res.callback = () => {
this.BetaProgramHandler.promises.optOut.callCount.should.equal(1)
done()
}
this.BetaProgramController.optOut(this.req, this.res, done)
it('should call BetaProgramHandler.optOut', function (ctx) {
return new Promise(resolve => {
ctx.res.callback = () => {
ctx.BetaProgramHandler.promises.optOut.callCount.should.equal(1)
resolve()
}
ctx.BetaProgramController.optOut(ctx.req, ctx.res, resolve)
})
})
it('should invoke the session maintenance', function (done) {
this.res.callback = () => {
this.SplitTestSessionHandler.promises.sessionMaintenance.should.have.been.calledWith(
this.req,
null
)
done()
}
this.BetaProgramController.optOut(this.req, this.res, done)
it('should invoke the session maintenance', function (ctx) {
return new Promise(resolve => {
ctx.res.callback = () => {
ctx.SplitTestSessionHandler.promises.sessionMaintenance.should.have.been.calledWith(
ctx.req,
null
)
resolve()
}
ctx.BetaProgramController.optOut(ctx.req, ctx.res, resolve)
})
})
describe('when BetaProgramHandler.optOut produces an error', function () {
beforeEach(function () {
this.BetaProgramHandler.promises.optOut.throws(new Error('woops'))
beforeEach(function (ctx) {
ctx.BetaProgramHandler.promises.optOut.throws(new Error('woops'))
})
it("should not redirect to '/beta/participate'", function (done) {
this.BetaProgramController.optOut(this.req, this.res, error => {
expect(error).to.exist
expect(this.res.redirected).to.equal(false)
done()
it("should not redirect to '/beta/participate'", function (ctx) {
return new Promise(resolve => {
ctx.BetaProgramController.optOut(ctx.req, ctx.res, error => {
expect(error).to.exist
expect(ctx.res.redirected).to.equal(false)
resolve()
})
})
})
it('should produce an error', function (done) {
this.BetaProgramController.optOut(this.req, this.res, error => {
expect(error).to.exist
done()
it('should produce an error', function (ctx) {
return new Promise(resolve => {
ctx.BetaProgramController.optOut(ctx.req, ctx.res, error => {
expect(error).to.exist
resolve()
})
})
})
})
})
describe('optInPage', function () {
beforeEach(function () {
this.UserGetter.promises.getUser.resolves(this.user)
beforeEach(function (ctx) {
ctx.UserGetter.promises.getUser.resolves(ctx.user)
})
it('should render the opt-in page', function (done) {
this.res.callback = () => {
expect(this.res.renderedTemplate).to.equal('beta_program/opt_in')
done()
}
this.BetaProgramController.optInPage(this.req, this.res, done)
it('should render the opt-in page', function (ctx) {
return new Promise(resolve => {
ctx.res.callback = () => {
expect(ctx.res.renderedTemplate).to.equal('beta_program/opt_in')
resolve()
}
ctx.BetaProgramController.optInPage(ctx.req, ctx.res, resolve)
})
})
describe('when UserGetter.getUser produces an error', function () {
beforeEach(function () {
this.UserGetter.promises.getUser.throws(new Error('woops'))
beforeEach(function (ctx) {
ctx.UserGetter.promises.getUser.throws(new Error('woops'))
})
it('should not render the opt-in page', function () {
this.BetaProgramController.optInPage(this.req, this.res, this.next)
this.res.render.callCount.should.equal(0)
it('should not render the opt-in page', function (ctx) {
ctx.BetaProgramController.optInPage(ctx.req, ctx.res, ctx.next)
ctx.res.render.callCount.should.equal(0)
})
it('should produce an error', function (done) {
this.BetaProgramController.optInPage(this.req, this.res, error => {
expect(error).to.exist
expect(error).to.be.instanceof(Error)
done()
it('should produce an error', function (ctx) {
return new Promise(resolve => {
ctx.BetaProgramController.optInPage(ctx.req, ctx.res, error => {
expect(error).to.exist
expect(error).to.be.instanceof(Error)
resolve()
})
})
})
})