Files
overleaf-cep/services/web/test/unit/src/Templates/TemplatesControllerTests.js
T
Alf Eaton d1d0e79670 Return the Promise from an expressified async function (#19359)
* Return the Promise from an expressified async function

GitOrigin-RevId: ca4c221a92de888e210e995faad97d0ea241e93f
2024-07-15 09:05:29 +00:00

104 lines
3.0 KiB
JavaScript

const SandboxedModule = require('sandboxed-module')
const { expect } = require('chai')
const sinon = require('sinon')
const ProjectHelper = require('../../../../app/src/Features/Project/ProjectHelper')
const modulePath = '../../../../app/src/Features/Templates/TemplatesController'
describe('TemplatesController', function () {
beforeEach(function () {
this.user_id = 'user-id'
this.TemplatesController = SandboxedModule.require(modulePath, {
requires: {
'../Project/ProjectHelper': ProjectHelper,
'../Authentication/AuthenticationController':
(this.AuthenticationController = {
getLoggedInUserId: sinon.stub().returns(this.user_id),
}),
'./TemplatesManager': (this.TemplatesManager = {
promises: { createProjectFromV1Template: sinon.stub() },
}),
},
})
this.next = sinon.stub()
this.req = {
body: {
brandVariationId: 'brand-variation-id',
compiler: 'compiler',
mainFile: 'main-file',
templateId: 'template-id',
templateName: 'template-name',
templateVersionId: 'template-version-id',
},
session: {
templateData: 'template-data',
user: {
_id: this.user_id,
},
},
}
return (this.res = { redirect: sinon.stub() })
})
describe('createProjectFromV1Template', function () {
describe('on success', function () {
beforeEach(function () {
this.project = { _id: 'project-id' }
this.TemplatesManager.promises.createProjectFromV1Template.resolves(
this.project
)
return this.TemplatesController.createProjectFromV1Template(
this.req,
this.res,
this.next
)
})
it('should call TemplatesManager', function () {
return this.TemplatesManager.promises.createProjectFromV1Template.should.have.been.calledWithMatch(
'brand-variation-id',
'compiler',
'main-file',
'template-id',
'template-name',
'template-version-id',
'user-id'
)
})
it('should redirect to project', function () {
return this.res.redirect.should.have.been.calledWith(
'/project/project-id'
)
})
it('should delete session', function () {
return expect(this.req.session.templateData).to.be.undefined
})
})
describe('on error', function () {
beforeEach(function () {
this.TemplatesManager.promises.createProjectFromV1Template.rejects(
'error'
)
return this.TemplatesController.createProjectFromV1Template(
this.req,
this.res,
this.next
)
})
it('should call next with error', function () {
return this.next.should.have.been.calledWithMatch(
sinon.match.instanceOf(Error)
)
})
it('should not redirect', function () {
return this.res.redirect.called.should.equal(false)
})
})
})
})