ReferencesSearchHandlerTests.coffee 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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_url = 'http://example.com/some/file'
  11. @handler = SandboxedModule.require modulePath, requires:
  12. 'logger-sharelatex': {
  13. log: ->
  14. err: ->
  15. }
  16. 'settings-sharelatex': @settings = {
  17. apis: {references: {url: 'http://some.url'}}
  18. }
  19. 'request': @request = {
  20. get: sinon.stub()
  21. post: sinon.stub()
  22. }
  23. describe 'indexFile', ->
  24. describe 'when index operation is successful', ->
  25. beforeEach ->
  26. @request.post.callsArgWith(1, null, {statusCode: 201}, {})
  27. it 'should not produce an error', (done) ->
  28. @handler.indexFile @project_id, @file_url, (err) =>
  29. expect(err).to.equal null
  30. done()
  31. describe 'when index operation fails', ->
  32. beforeEach ->
  33. @request.post.callsArgWith(1, null, {statusCode: 500}, {})
  34. it 'should produce an error', (done) ->
  35. @handler.indexFile @project_id, @file_url, (err) =>
  36. expect(err).to.not.equal null
  37. done()
  38. describe 'getKeys', ->
  39. describe 'when request is successful', ->
  40. beforeEach ->
  41. @data =
  42. projectId: @projectId
  43. keys: ['a', 'b', 'c']
  44. @request.get.callsArgWith(1, null, {statusCode: 200}, @data)
  45. it 'should not produce an error', ->
  46. @handler.getKeys @project_id, (err, result) =>
  47. expect(err).to.equal null
  48. it 'should produce a result object', ->
  49. @handler.getKeys @project_id, (err, result) =>
  50. expect(result).to.not.equal null
  51. expect(result).to.deep.equal @data
  52. describe 'when request fails', ->
  53. beforeEach ->
  54. @data =
  55. projectId: @project_Id
  56. keys: ['a', 'b', 'c']
  57. @request.get.callsArgWith(1, null, {statusCode: 500}, null)
  58. it 'should produce an error', ->
  59. @handler.getKeys @project_id, (err, result) =>
  60. expect(err).to.not.equal null
  61. it 'should not produce a result', ->
  62. @handler.getKeys @project_id, (err, result) =>
  63. expect(result).to.not.equal null