ReferencesSearchHandlerTests.coffee 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. SandboxedModule = require('sandboxed-module')
  2. should = require('chai').should()
  3. expect = require('chai').expect
  4. sinon = require 'sinon'
  5. assert = require("chai").assert
  6. modulePath = "../../../../app/js/Features/ReferencesSearch/ReferencesSearchHandler"
  7. describe 'ReferencesSearchHandler', ->
  8. beforeEach ->
  9. @project_id = '222'
  10. @file_id = '111111'
  11. @handler = SandboxedModule.require modulePath, requires:
  12. 'logger-sharelatex': {
  13. log: ->
  14. err: ->
  15. }
  16. 'settings-sharelatex': @settings = {
  17. apis:
  18. references: {url: 'http://some.url'}
  19. web: {url: 'http://some.url'}
  20. }
  21. 'request': @request = {
  22. get: sinon.stub()
  23. post: sinon.stub()
  24. }
  25. describe 'indexFile', ->
  26. describe 'when index operation is successful', ->
  27. beforeEach ->
  28. @request.post.callsArgWith(1, null, {statusCode: 201}, {})
  29. it 'should not produce an error', (done) ->
  30. @handler.indexFile @project_id, @file_id, (err) =>
  31. expect(err).to.equal null
  32. done()
  33. describe 'when index operation fails', ->
  34. beforeEach ->
  35. @request.post.callsArgWith(1, null, {statusCode: 500}, {})
  36. it 'should produce an error', (done) ->
  37. @handler.indexFile @project_id, @file_id, (err) =>
  38. expect(err).to.not.equal null
  39. done()
  40. describe 'getKeys', ->
  41. describe 'when request is successful', ->
  42. beforeEach ->
  43. @data =
  44. projectId: @projectId
  45. keys: ['a', 'b', 'c']
  46. @request.get.callsArgWith(1, null, {statusCode: 200}, @data)
  47. it 'should not produce an error', ->
  48. @handler.getKeys @project_id, (err, result) =>
  49. expect(err).to.equal null
  50. it 'should produce a result object', ->
  51. @handler.getKeys @project_id, (err, result) =>
  52. expect(result).to.not.equal null
  53. expect(result).to.deep.equal @data
  54. describe 'when request fails', ->
  55. beforeEach ->
  56. @data =
  57. projectId: @project_Id
  58. keys: ['a', 'b', 'c']
  59. @request.get.callsArgWith(1, null, {statusCode: 500}, null)
  60. it 'should produce an error', ->
  61. @handler.getKeys @project_id, (err, result) =>
  62. expect(err).to.not.equal null
  63. it 'should not produce a result', ->
  64. @handler.getKeys @project_id, (err, result) =>
  65. expect(result).to.not.equal null