Merge pull request #2097 from overleaf/em-chai-spies

Remove chai-spies

GitOrigin-RevId: 7fa41fe32c1be985d761b7327950995d6afe7c4e
This commit is contained in:
Chrystal Maria Griffiths
2019-08-27 11:30:15 +01:00
committed by sharelatex
parent 03460ba229
commit 1335d21136
5 changed files with 412 additions and 419 deletions
@@ -1,68 +1,39 @@
/* eslint-disable
camelcase,
handle-callback-err,
max-len,
no-path-concat,
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
* DS206: Consider reworking classes to avoid initClass
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const spies = require('chai-spies')
const chai = require('chai').use(spies)
const sinon = require('sinon')
const should = chai.should()
const { expect } = chai
const { expect } = require('chai')
const modulePath =
'../../../../app/src/Features/Project/ProjectCreationHandler.js'
const SandboxedModule = require('sandboxed-module')
const Settings = require('settings-sharelatex')
const Path = require('path')
const _ = require('underscore')
describe('ProjectCreationHandler', function() {
const ownerId = '4eecb1c1bffa66588e0000a1'
const projectName = 'project name goes here'
const project_id = '4eecaffcbffa66588e000008'
const projectId = '4eecaffcbffa66588e000008'
const docId = '4eecb17ebffa66588e00003f'
const rootFolderId = '234adfa3r2afe'
beforeEach(function() {
let Folder, Project
this.ProjectModel = Project = (function() {
Project = class Project {
static initClass() {
this.prototype.save = sinon.stub().callsArg(0)
this.prototype.rootFolder = [
{
_id: rootFolderId,
docs: []
}
]
}
constructor(options) {
if (options == null) {
options = {}
}
this._id = project_id
this.owner_ref = options.owner_ref
this.name = options.name
this.overleaf = { history: {} }
}
}
Project.initClass()
return Project
})()
this.FolderModel = Folder = class Folder {
this.ProjectModel = class Project {
constructor(options) {
;({ name: this.name } = options)
if (options == null) {
options = {}
}
this._id = projectId
this.owner_ref = options.owner_ref
this.name = options.name
this.overleaf = { history: {} }
}
}
this.ProjectModel.prototype.save = sinon.stub().callsArg(0)
this.ProjectModel.prototype.rootFolder = [
{
_id: rootFolderId,
docs: []
}
]
this.FolderModel = class Folder {
constructor(options) {
this.name = options.name
}
}
this.ProjectEntityUpdateHandler = {
@@ -88,7 +59,7 @@ describe('ProjectCreationHandler', function() {
this.AnalyticsManager = { recordEvent: sinon.stub() }
return (this.handler = SandboxedModule.require(modulePath, {
this.handler = SandboxedModule.require(modulePath, {
globals: {
console: console
},
@@ -109,107 +80,123 @@ describe('ProjectCreationHandler', function() {
timeAsyncMethod() {}
}
}
}))
})
})
describe('Creating a Blank project', function() {
beforeEach(function() {
this.overleaf_id = 1234
this.overleafId = 1234
this.HistoryManager.initializeProject = sinon
.stub()
.callsArgWith(0, null, { overleaf_id: this.overleaf_id })
return (this.ProjectModel.prototype.save = sinon.stub().callsArg(0))
.callsArgWith(0, null, { overleaf_id: this.overleafId })
this.ProjectModel.prototype.save = sinon.stub().callsArg(0)
})
describe('successfully', function() {
it('should save the project', function(done) {
return this.handler.createBlankProject(ownerId, projectName, () => {
this.handler.createBlankProject(ownerId, projectName, () => {
this.ProjectModel.prototype.save.called.should.equal(true)
return done()
done()
})
})
it('should return the project in the callback', function(done) {
return this.handler.createBlankProject(
this.handler.createBlankProject(
ownerId,
projectName,
(err, project) => {
if (err != null) {
return done(err)
}
project.name.should.equal(projectName)
;(project.owner_ref + '').should.equal(ownerId)
return done()
expect(project.owner_ref + '').to.equal(ownerId)
done()
}
)
})
it('should initialize the project overleaf if history id not provided', function(done) {
this.handler.createBlankProject(ownerId, projectName, done)
return this.HistoryManager.initializeProject
.calledWith()
.should.equal(true)
this.HistoryManager.initializeProject.calledWith().should.equal(true)
})
it('should set the overleaf id if overleaf id not provided', function(done) {
return this.handler.createBlankProject(
this.handler.createBlankProject(
ownerId,
projectName,
(err, project) => {
project.overleaf.history.id.should.equal(this.overleaf_id)
return done()
if (err != null) {
return done(err)
}
project.overleaf.history.id.should.equal(this.overleafId)
done()
}
)
})
it('should set the overleaf id if overleaf id provided', function(done) {
const overleaf_id = 2345
const overleafId = 2345
const attributes = {
overleaf: {
history: {
id: overleaf_id
id: overleafId
}
}
}
return this.handler.createBlankProject(
this.handler.createBlankProject(
ownerId,
projectName,
attributes,
(err, project) => {
project.overleaf.history.id.should.equal(overleaf_id)
return done()
if (err != null) {
return done(err)
}
project.overleaf.history.id.should.equal(overleafId)
done()
}
)
})
it('should set the language from the user', function(done) {
return this.handler.createBlankProject(
this.handler.createBlankProject(
ownerId,
projectName,
(err, project) => {
if (err != null) {
return done(err)
}
project.spellCheckLanguage.should.equal('de')
return done()
done()
}
)
})
it('should set the imageName to currentImageName if set and no imageName attribute', function(done) {
this.Settings.currentImageName = 'mock-image-name'
return this.handler.createBlankProject(
this.handler.createBlankProject(
ownerId,
projectName,
(err, project) => {
if (err != null) {
return done(err)
}
project.imageName.should.equal(this.Settings.currentImageName)
return done()
done()
}
)
})
it('should not set the imageName if no currentImageName', function(done) {
this.Settings.currentImageName = null
return this.handler.createBlankProject(
this.handler.createBlankProject(
ownerId,
projectName,
(err, project) => {
if (err != null) {
return done(err)
}
expect(project.imageName).to.not.exist
return done()
done()
}
)
})
@@ -217,46 +204,58 @@ describe('ProjectCreationHandler', function() {
it('should set the imageName to the attribute value if set and not overwrite it with the currentImageName', function(done) {
this.Settings.currentImageName = 'mock-image-name'
const attributes = { imageName: 'attribute-image-name' }
return this.handler.createBlankProject(
this.handler.createBlankProject(
ownerId,
projectName,
attributes,
(err, project) => {
if (err != null) {
return done(err)
}
project.imageName.should.equal(attributes.imageName)
return done()
done()
}
)
})
it('should not set the overleaf.history.display if not configured in settings', function(done) {
this.Settings.apis.project_history.displayHistoryForNewProjects = false
return this.handler.createBlankProject(
this.handler.createBlankProject(
ownerId,
projectName,
(err, project) => {
if (err != null) {
return done(err)
}
expect(project.overleaf.history.display).to.not.exist
return done()
done()
}
)
})
it('should set the overleaf.history.display if configured in settings', function(done) {
this.Settings.apis.project_history.displayHistoryForNewProjects = true
return this.handler.createBlankProject(
this.handler.createBlankProject(
ownerId,
projectName,
(err, project) => {
if (err != null) {
return done(err)
}
expect(project.overleaf.history.display).to.equal(true)
return done()
done()
}
)
})
it('should send a project-created event to analytics', function(done) {
return this.handler.createBlankProject(
this.handler.createBlankProject(
ownerId,
projectName,
(err, project) => {
if (err != null) {
return done(err)
}
expect(this.AnalyticsManager.recordEvent.callCount).to.equal(1)
expect(
this.AnalyticsManager.recordEvent.calledWith(
@@ -264,7 +263,7 @@ describe('ProjectCreationHandler', function() {
'project-created'
)
).to.equal(true)
return done()
done()
}
)
})
@@ -273,11 +272,14 @@ describe('ProjectCreationHandler', function() {
const attributes = {
fromV1TemplateId: 100
}
return this.handler.createBlankProject(
this.handler.createBlankProject(
ownerId,
projectName,
attributes,
(err, project) => {
if (err != null) {
return done(err)
}
expect(this.AnalyticsManager.recordEvent.callCount).to.equal(1)
expect(
this.AnalyticsManager.recordEvent.calledWith(
@@ -286,7 +288,7 @@ describe('ProjectCreationHandler', function() {
{ projectId: project._id, attributes }
)
).to.equal(true)
return done()
done()
}
)
})
@@ -299,11 +301,14 @@ describe('ProjectCreationHandler', function() {
}
}
}
return this.handler.createBlankProject(
this.handler.createBlankProject(
ownerId,
projectName,
attributes,
(err, project) => {
if (err != null) {
return done(err)
}
expect(this.AnalyticsManager.recordEvent.callCount).to.equal(1)
expect(
this.AnalyticsManager.recordEvent.calledWith(
@@ -311,7 +316,7 @@ describe('ProjectCreationHandler', function() {
'project-imported'
)
).to.equal(true)
return done()
done()
}
)
})
@@ -322,15 +327,11 @@ describe('ProjectCreationHandler', function() {
this.ProjectModel.prototype.save = sinon
.stub()
.callsArgWith(0, new Error('something went wrong'))
return this.handler.createBlankProject(
ownerId,
projectName,
this.callback
)
this.handler.createBlankProject(ownerId, projectName, this.callback)
})
it('should return the error to the callback', function() {
return should.exist(this.callback.args[0][0])
expect(this.callback.args[0][0]).to.exist
})
})
@@ -339,19 +340,15 @@ describe('ProjectCreationHandler', function() {
this.ProjectDetailsHandler.validateProjectName = sinon
.stub()
.yields(new Error('bad name'))
return this.handler.createBlankProject(
ownerId,
projectName,
this.callback
)
this.handler.createBlankProject(ownerId, projectName, this.callback)
})
it('should return the error to the callback', function() {
return should.exist(this.callback.args[0][0])
expect(this.callback.args[0][0]).to.exist
})
it('should not try to create the project', function() {
return this.ProjectModel.prototype.save.called.should.equal(false)
this.ProjectModel.prototype.save.called.should.equal(false)
})
})
})
@@ -360,15 +357,15 @@ describe('ProjectCreationHandler', function() {
beforeEach(function() {
this.project = new this.ProjectModel()
this.handler._buildTemplate = function(
template_name,
templateName,
user,
project_name,
projectName,
callback
) {
if (template_name === 'mainbasic.tex') {
if (templateName === 'mainbasic.tex') {
return callback(null, ['mainbasic.tex', 'lines'])
}
throw new Error(`unknown template: ${template_name}`)
throw new Error(`unknown template: ${templateName}`)
}
sinon.spy(this.handler, '_buildTemplate')
this.handler.createBlankProject = sinon
@@ -377,27 +374,23 @@ describe('ProjectCreationHandler', function() {
this.handler._createRootDoc = sinon
.stub()
.callsArgWith(3, null, this.project)
return this.handler.createBasicProject(
ownerId,
projectName,
this.callback
)
this.handler.createBasicProject(ownerId, projectName, this.callback)
})
it('should create a blank project first', function() {
return this.handler.createBlankProject
this.handler.createBlankProject
.calledWith(ownerId, projectName)
.should.equal(true)
})
it('should create the root document', function() {
return this.handler._createRootDoc
this.handler._createRootDoc
.calledWith(this.project, ownerId, ['mainbasic.tex', 'lines'])
.should.equal(true)
})
it('should build the mainbasic.tex template', function() {
return this.handler._buildTemplate
this.handler._buildTemplate
.calledWith('mainbasic.tex', ownerId, projectName)
.should.equal(true)
})
@@ -412,7 +405,7 @@ describe('ProjectCreationHandler', function() {
this.handler._createRootDoc = sinon
.stub()
.callsArgWith(3, null, this.project)
return this.handler.createProjectFromSnippet(
this.handler.createProjectFromSnippet(
ownerId,
projectName,
['snippet line 1', 'snippet line 2'],
@@ -421,13 +414,13 @@ describe('ProjectCreationHandler', function() {
})
it('should create a blank project first', function() {
return this.handler.createBlankProject
this.handler.createBlankProject
.calledWith(ownerId, projectName)
.should.equal(true)
})
it('should create the root document', function() {
return this.handler._createRootDoc
this.handler._createRootDoc
.calledWith(this.project, ownerId, ['snippet line 1', 'snippet line 2'])
.should.equal(true)
})
@@ -437,18 +430,18 @@ describe('ProjectCreationHandler', function() {
beforeEach(function() {
this.project = new this.ProjectModel()
this.handler._buildTemplate = function(
template_name,
templateName,
user,
project_name,
projectName,
callback
) {
if (template_name === 'main.tex') {
if (templateName === 'main.tex') {
return callback(null, ['main.tex', 'lines'])
}
if (template_name === 'references.bib') {
if (templateName === 'references.bib') {
return callback(null, ['references.bib', 'lines'])
}
throw new Error(`unknown template: ${template_name}`)
throw new Error(`unknown template: ${templateName}`)
}
sinon.spy(this.handler, '_buildTemplate')
this.handler.createBlankProject = sinon
@@ -457,29 +450,25 @@ describe('ProjectCreationHandler', function() {
this.handler._createRootDoc = sinon
.stub()
.callsArgWith(3, null, this.project)
return this.handler.createExampleProject(
ownerId,
projectName,
this.callback
)
this.handler.createExampleProject(ownerId, projectName, this.callback)
})
it('should create a blank project first', function() {
return this.handler.createBlankProject
this.handler.createBlankProject
.calledWith(ownerId, projectName)
.should.equal(true)
})
it('should create the root document', function() {
return this.handler._createRootDoc
this.handler._createRootDoc
.calledWith(this.project, ownerId, ['main.tex', 'lines'])
.should.equal(true)
})
it('should insert references.bib', function() {
return this.ProjectEntityUpdateHandler.addDoc
this.ProjectEntityUpdateHandler.addDoc
.calledWith(
project_id,
projectId,
rootFolderId,
'references.bib',
['references.bib', 'lines'],
@@ -489,13 +478,16 @@ describe('ProjectCreationHandler', function() {
})
it('should insert universe.jpg', function() {
return this.ProjectEntityUpdateHandler.addFile
this.ProjectEntityUpdateHandler.addFile
.calledWith(
project_id,
projectId,
rootFolderId,
'universe.jpg',
Path.resolve(
__dirname + '/../../../../app/templates/project_files/universe.jpg'
Path.join(
__dirname,
'../../../../app/templates/project_files/universe.jpg'
)
),
null,
ownerId
@@ -504,13 +496,13 @@ describe('ProjectCreationHandler', function() {
})
it('should build the main.tex template', function() {
return this.handler._buildTemplate
this.handler._buildTemplate
.calledWith('main.tex', ownerId, projectName)
.should.equal(true)
})
it('should build the references.bib template', function() {
return this.handler._buildTemplate
this.handler._buildTemplate
.calledWith('references.bib', ownerId, projectName)
.should.equal(true)
})
@@ -518,44 +510,47 @@ describe('ProjectCreationHandler', function() {
describe('_buildTemplate', function() {
beforeEach(function(done) {
return this.handler._buildTemplate(
this.handler._buildTemplate(
'main.tex',
this.user_id,
projectName,
(err, templateLines) => {
if (err != null) {
return done(err)
}
this.template = templateLines.reduce(
(singleLine, line) => `${singleLine}\n${line}`
)
return done()
done()
}
)
})
it('should insert the project name into the template', function(done) {
this.template.indexOf(projectName).should.not.equal(-1)
return done()
done()
})
it('should insert the users name into the template', function(done) {
this.template.indexOf(this.user.first_name).should.not.equal(-1)
this.template.indexOf(this.user.last_name).should.not.equal(-1)
return done()
done()
})
it('should not have undefined in the template', function(done) {
this.template.indexOf('undefined').should.equal(-1)
return done()
done()
})
it('should not have any underscore brackets in the output', function(done) {
this.template.indexOf('{{').should.equal(-1)
this.template.indexOf('<%=').should.equal(-1)
return done()
done()
})
it('should put the year in', function(done) {
this.template.indexOf(new Date().getUTCFullYear()).should.not.equal(-1)
return done()
done()
})
})
@@ -563,7 +558,7 @@ describe('ProjectCreationHandler', function() {
beforeEach(function(done) {
this.project = new this.ProjectModel()
return this.handler._createRootDoc(
this.handler._createRootDoc(
this.project,
ownerId,
['line 1', 'line 2'],
@@ -572,9 +567,9 @@ describe('ProjectCreationHandler', function() {
})
it('should insert main.tex', function() {
return this.ProjectEntityUpdateHandler.addDoc
this.ProjectEntityUpdateHandler.addDoc
.calledWith(
project_id,
projectId,
rootFolderId,
'main.tex',
['line 1', 'line 2'],
@@ -584,8 +579,8 @@ describe('ProjectCreationHandler', function() {
})
it('should set the main doc id', function() {
return this.ProjectEntityUpdateHandler.setRootDoc
.calledWith(project_id, docId)
this.ProjectEntityUpdateHandler.setRootDoc
.calledWith(projectId, docId)
.should.equal(true)
})
})