ReferencesSearchControllerTests.coffee 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. SandboxedModule = require('sandboxed-module')
  2. should = require('chai').should()
  3. sinon = require 'sinon'
  4. assert = require("chai").assert
  5. modulePath = "../../../../app/js/Features/ReferencesSearch/ReferencesSearchController"
  6. MockRequest = require "../helpers/MockRequest"
  7. MockResponse = require "../helpers/MockResponse"
  8. describe "ReferencesSearchController", ->
  9. beforeEach ->
  10. @project_id = '2222'
  11. @doc_id = '3333'
  12. @controller = SandboxedModule.require modulePath, requires:
  13. 'logger-sharelatex': {log: ->}
  14. 'settings-sharelatex': @settings = {
  15. apis: {web: {url: 'http://some.url'}}
  16. }
  17. '../Project/ProjectLocator': @ProjectLocator = {findElement: sinon.stub()}
  18. './ReferencesSearchHandler': @ReferencesSearchHandler = {indexFile: sinon.stub(), getKeys: sinon.stub()}
  19. describe 'indexFile', ->
  20. beforeEach ->
  21. @req = new MockRequest()
  22. @req.params.Project_id = @project_id
  23. @res = new MockResponse()
  24. @ProjectLocator.findElement.callsArgWith(1, null, {})
  25. @ReferencesSearchHandler.indexFile.callsArgWith(2, null)
  26. describe 'with a valid doc_id', ->
  27. beforeEach ->
  28. @req.body = {docId: @doc_id}
  29. it 'should produce a 200 response', (done) ->
  30. @res.send = (status) =>
  31. status.should.equal 200
  32. done()
  33. @controller.indexFile(@req, @res)
  34. it 'should call ProjectLocator.findElement', (done) ->
  35. @res.send = (status) =>
  36. @ProjectLocator.findElement.calledOnce.should.equal true
  37. arg =
  38. project_id: @project_id
  39. element_id: @doc_id,
  40. type: 'doc'
  41. @ProjectLocator.findElement.calledWith(arg).should.equal true
  42. done()
  43. @controller.indexFile(@req, @res)
  44. it 'should call ReferencesSearchHandler.indexFile', (done) ->
  45. @res.send = (status) =>
  46. @ReferencesSearchHandler.indexFile.calledOnce.should.equal true
  47. expected_url = "http://some.url/project/2222/doc/3333"
  48. @ReferencesSearchHandler.indexFile.calledWith(@project_id, expected_url).should.equal true
  49. done()
  50. @controller.indexFile(@req, @res)