NotificationsControllerTest.coffee 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. modulePath = "../../../app/js/NotificationsController.js"
  5. SandboxedModule = require('sandboxed-module')
  6. assert = require('assert')
  7. user_id = "51dc93e6fb625a261300003b"
  8. notification_id = "fb625a26f09d"
  9. notification_key = "my-notification-key"
  10. describe 'Notifications Controller', ->
  11. beforeEach ->
  12. self = @
  13. @notifications = {}
  14. @controller = SandboxedModule.require modulePath, requires:
  15. 'logger-sharelatex': log:->
  16. './Notifications':@notifications
  17. 'metrics-sharelatex':
  18. inc: sinon.stub()
  19. @stubbedNotification = [{key: notification_key, messageOpts:"some info", templateKey:"template-key"}]
  20. describe "getUserNotifications", ->
  21. it 'should ask the notifications for the users notifications', (done)->
  22. @notifications.getUserNotifications = sinon.stub().callsArgWith(1, null, @stubbedNotification)
  23. req =
  24. params:
  25. user_id: user_id
  26. @controller.getUserNotifications req, json:(result)=>
  27. result.should.equal @stubbedNotification
  28. @notifications.getUserNotifications.calledWith(user_id).should.equal true
  29. done()
  30. describe "addNotification", ->
  31. it "should tell the notifications to add the notification for the user", (done)->
  32. @notifications.addNotification = sinon.stub().callsArgWith(2)
  33. req =
  34. params:
  35. user_id: user_id
  36. body: @stubbedNotification
  37. @controller.addNotification req, send:(result)=>
  38. @notifications.addNotification.calledWith(user_id, @stubbedNotification).should.equal true
  39. done()
  40. describe "removeNotificationId", ->
  41. it "should tell the notifications to mark the notification Id as read", (done)->
  42. @notifications.removeNotificationId = sinon.stub().callsArgWith(2)
  43. req =
  44. params:
  45. user_id: user_id
  46. notification_id: notification_id
  47. @controller.removeNotificationId req, send:(result)=>
  48. @notifications.removeNotificationId.calledWith(user_id, notification_id).should.equal true
  49. done()
  50. describe "removeNotificationKey", ->
  51. it "should tell the notifications to mark the notification Key as read", (done)->
  52. @notifications.removeNotificationKey = sinon.stub().callsArgWith(2)
  53. req =
  54. params:
  55. user_id: user_id
  56. body: {key: notification_key}
  57. @controller.removeNotificationKey req, send:(result)=>
  58. @notifications.removeNotificationKey.calledWith(user_id, notification_key).should.equal true
  59. done()
  60. describe "removeNotificationByKeyOnly", ->
  61. it "should tell the notifications to mark the notification Key as read", (done)->
  62. @notifications.removeNotificationByKeyOnly = sinon.stub().callsArgWith(1)
  63. req =
  64. params:
  65. key: notification_key
  66. @controller.removeNotificationByKeyOnly req, send:(result)=>
  67. @notifications.removeNotificationByKeyOnly.calledWith(notification_key).should.equal true
  68. done()