| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- /* eslint-disable
- max-len,
- 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
- * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
- */
- const SandboxedModule = require('sandboxed-module')
- const sinon = require('sinon')
- const { assert } = require('chai')
- const modulePath =
- '../../../../app/src/Features/References/ReferencesController'
- const MockRequest = require('../helpers/MockRequest')
- const MockResponse = require('../helpers/MockResponse')
- describe('ReferencesController', function () {
- beforeEach(function () {
- this.projectId = '2222'
- this.controller = SandboxedModule.require(modulePath, {
- requires: {
- '@overleaf/settings': (this.settings = {
- apis: { web: { url: 'http://some.url' } },
- }),
- './ReferencesHandler': (this.ReferencesHandler = {
- index: sinon.stub(),
- indexAll: sinon.stub(),
- }),
- '../Editor/EditorRealTimeController': (this.EditorRealTimeController = {
- emitToRoom: sinon.stub(),
- }),
- },
- })
- this.req = new MockRequest()
- this.req.params.Project_id = this.projectId
- this.req.body = {
- docIds: (this.docIds = ['aaa', 'bbb']),
- shouldBroadcast: false,
- }
- this.res = new MockResponse()
- this.res.json = sinon.stub()
- this.res.sendStatus = sinon.stub()
- this.next = sinon.stub()
- return (this.fakeResponseData = {
- projectId: this.projectId,
- keys: ['one', 'two', 'three'],
- })
- })
- describe('indexAll', function () {
- beforeEach(function () {
- this.req.body = { shouldBroadcast: false }
- this.ReferencesHandler.indexAll.callsArgWith(
- 1,
- null,
- this.fakeResponseData
- )
- return (this.call = callback => {
- this.controller.indexAll(this.req, this.res, this.next)
- return callback()
- })
- })
- it('should not produce an error', function (done) {
- return this.call(() => {
- this.res.sendStatus.callCount.should.equal(0)
- this.res.sendStatus.calledWith(500).should.equal(false)
- this.res.sendStatus.calledWith(400).should.equal(false)
- return done()
- })
- })
- it('should return data', function (done) {
- return this.call(() => {
- this.res.json.callCount.should.equal(1)
- this.res.json.calledWith(this.fakeResponseData).should.equal(true)
- return done()
- })
- })
- it('should call ReferencesHandler.indexAll', function (done) {
- return this.call(() => {
- this.ReferencesHandler.indexAll.callCount.should.equal(1)
- this.ReferencesHandler.indexAll
- .calledWith(this.projectId)
- .should.equal(true)
- return done()
- })
- })
- describe('when shouldBroadcast is true', function () {
- beforeEach(function () {
- this.ReferencesHandler.index.callsArgWith(
- 2,
- null,
- this.fakeResponseData
- )
- return (this.req.body.shouldBroadcast = true)
- })
- it('should call EditorRealTimeController.emitToRoom', function (done) {
- return this.call(() => {
- this.EditorRealTimeController.emitToRoom.callCount.should.equal(1)
- return done()
- })
- })
- it('should not produce an error', function (done) {
- return this.call(() => {
- this.res.sendStatus.callCount.should.equal(0)
- this.res.sendStatus.calledWith(500).should.equal(false)
- this.res.sendStatus.calledWith(400).should.equal(false)
- return done()
- })
- })
- it('should still return data', function (done) {
- return this.call(() => {
- this.res.json.callCount.should.equal(1)
- this.res.json.calledWith(this.fakeResponseData).should.equal(true)
- return done()
- })
- })
- })
- describe('when shouldBroadcast is false', function () {
- beforeEach(function () {
- this.ReferencesHandler.index.callsArgWith(
- 2,
- null,
- this.fakeResponseData
- )
- return (this.req.body.shouldBroadcast = false)
- })
- it('should not call EditorRealTimeController.emitToRoom', function (done) {
- return this.call(() => {
- this.EditorRealTimeController.emitToRoom.callCount.should.equal(0)
- return done()
- })
- })
- it('should not produce an error', function (done) {
- return this.call(() => {
- this.res.sendStatus.callCount.should.equal(0)
- this.res.sendStatus.calledWith(500).should.equal(false)
- this.res.sendStatus.calledWith(400).should.equal(false)
- return done()
- })
- })
- it('should still return data', function (done) {
- return this.call(() => {
- this.res.json.callCount.should.equal(1)
- this.res.json.calledWith(this.fakeResponseData).should.equal(true)
- return done()
- })
- })
- })
- })
- describe('there is no data', function () {
- beforeEach(function () {
- this.ReferencesHandler.indexAll.callsArgWith(1)
- return (this.call = callback => {
- this.controller.indexAll(this.req, this.res, this.next)
- return callback()
- })
- })
- it('should not call EditorRealTimeController.emitToRoom', function (done) {
- return this.call(() => {
- this.EditorRealTimeController.emitToRoom.callCount.should.equal(0)
- return done()
- })
- })
- it('should not produce an error', function (done) {
- return this.call(() => {
- this.res.sendStatus.callCount.should.equal(0)
- this.res.sendStatus.calledWith(500).should.equal(false)
- this.res.sendStatus.calledWith(400).should.equal(false)
- return done()
- })
- })
- it('should send a response with an empty keys list', function (done) {
- return this.call(() => {
- this.res.json.called.should.equal(true)
- this.res.json
- .calledWith({ projectId: this.projectId, keys: [] })
- .should.equal(true)
- return done()
- })
- })
- })
- })
|