Files
Verso/services/web/test/unit/src/Metadata/MetaControllerTests.js
T
Alf Eaton 7e136131c0 Promisify Metadata feature (#19361)
GitOrigin-RevId: 962aa9dbbc41a49c2c3120af9a1254a4db85387b
2024-07-24 08:05:19 +00:00

165 lines
5.0 KiB
JavaScript

const { expect } = require('chai')
const sinon = require('sinon')
const modulePath = '../../../../app/src/Features/Metadata/MetaController'
const SandboxedModule = require('sandboxed-module')
const MockResponse = require('../helpers/MockResponse')
describe('MetaController', function () {
beforeEach(function () {
this.EditorRealTimeController = {
emitToRoom: sinon.stub(),
}
this.MetaHandler = {
promises: {
getAllMetaForProject: sinon.stub(),
getMetaForDoc: sinon.stub(),
},
}
this.MetadataController = SandboxedModule.require(modulePath, {
requires: {
'../Editor/EditorRealTimeController': this.EditorRealTimeController,
'./MetaHandler': this.MetaHandler,
},
})
})
describe('getMetadata', function () {
it('should respond with json', async function () {
const projectMeta = {
'doc-id': {
labels: ['foo'],
packages: { a: { commands: [] } },
packageNames: ['a'],
},
}
this.MetaHandler.promises.getAllMetaForProject = sinon
.stub()
.resolves(projectMeta)
const req = { params: { project_id: 'project-id' } }
const res = new MockResponse()
const next = sinon.stub()
await this.MetadataController.getMetadata(req, res, next)
this.MetaHandler.promises.getAllMetaForProject.should.have.been.calledWith(
'project-id'
)
res.json.should.have.been.calledOnceWith({
projectId: 'project-id',
projectMeta,
})
next.should.not.have.been.called
})
it('should handle an error', async function () {
this.MetaHandler.promises.getAllMetaForProject = sinon
.stub()
.throws(new Error('woops'))
const req = { params: { project_id: 'project-id' } }
const res = new MockResponse()
const next = sinon.stub()
await this.MetadataController.getMetadata(req, res, next)
this.MetaHandler.promises.getAllMetaForProject.should.have.been.calledWith(
'project-id'
)
res.json.should.not.have.been.called
next.should.have.been.calledWithMatch(error => error instanceof Error)
})
})
describe('broadcastMetadataForDoc', function () {
it('should broadcast on broadcast:true ', async function () {
this.MetaHandler.promises.getMetaForDoc = sinon.stub().resolves({
labels: ['foo'],
packages: { a: { commands: [] } },
packageNames: ['a'],
})
this.EditorRealTimeController.emitToRoom = sinon.stub()
const req = {
params: { project_id: 'project-id', doc_id: 'doc-id' },
body: { broadcast: true },
}
const res = new MockResponse()
const next = sinon.stub()
await this.MetadataController.broadcastMetadataForDoc(req, res, next)
this.MetaHandler.promises.getMetaForDoc.should.have.been.calledWith(
'project-id'
)
res.json.should.not.have.been.called
res.sendStatus.should.have.been.calledOnceWith(200)
next.should.not.have.been.called
this.EditorRealTimeController.emitToRoom.should.have.been.calledOnce
const { lastCall } = this.EditorRealTimeController.emitToRoom
expect(lastCall.args[0]).to.equal('project-id')
expect(lastCall.args[1]).to.equal('broadcastDocMeta')
expect(lastCall.args[2]).to.have.all.keys(['docId', 'meta'])
})
it('should return json on broadcast:false ', async function () {
const docMeta = {
labels: ['foo'],
packages: { a: [] },
packageNames: ['a'],
}
this.MetaHandler.promises.getMetaForDoc = sinon.stub().resolves(docMeta)
this.EditorRealTimeController.emitToRoom = sinon.stub()
const req = {
params: { project_id: 'project-id', doc_id: 'doc-id' },
body: { broadcast: false },
}
const res = new MockResponse()
const next = sinon.stub()
await this.MetadataController.broadcastMetadataForDoc(req, res, next)
this.MetaHandler.promises.getMetaForDoc.should.have.been.calledWith(
'project-id'
)
this.EditorRealTimeController.emitToRoom.should.not.have.been.called
res.json.should.have.been.calledOnceWith({
docId: 'doc-id',
meta: docMeta,
})
next.should.not.have.been.called
})
it('should handle an error', async function () {
this.MetaHandler.promises.getMetaForDoc = sinon
.stub()
.throws(new Error('woops'))
this.EditorRealTimeController.emitToRoom = sinon.stub()
const req = {
params: { project_id: 'project-id', doc_id: 'doc-id' },
body: { broadcast: true },
}
const res = new MockResponse()
const next = sinon.stub()
await this.MetadataController.broadcastMetadataForDoc(req, res, next)
this.MetaHandler.promises.getMetaForDoc.should.have.been.calledWith(
'project-id'
)
res.json.should.not.have.been.called
next.should.have.been.calledWithMatch(error => error instanceof Error)
})
})
})