NotificationsControllerTest.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /* eslint-disable
  2. camelcase,
  3. no-return-assign,
  4. no-unused-vars,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. const sinon = require('sinon')
  14. const chai = require('chai')
  15. const should = chai.should()
  16. const modulePath = '../../../app/js/NotificationsController.js'
  17. const SandboxedModule = require('sandboxed-module')
  18. const assert = require('assert')
  19. const user_id = '51dc93e6fb625a261300003b'
  20. const notification_id = 'fb625a26f09d'
  21. const notification_key = 'my-notification-key'
  22. describe('Notifications Controller', function () {
  23. beforeEach(function () {
  24. const self = this
  25. this.notifications = {}
  26. this.controller = SandboxedModule.require(modulePath, {
  27. requires: {
  28. 'logger-sharelatex': { log() {} },
  29. './Notifications': this.notifications,
  30. 'metrics-sharelatex': {
  31. inc: sinon.stub()
  32. }
  33. }
  34. })
  35. return (this.stubbedNotification = [
  36. {
  37. key: notification_key,
  38. messageOpts: 'some info',
  39. templateKey: 'template-key'
  40. }
  41. ])
  42. })
  43. describe('getUserNotifications', function () {
  44. return it('should ask the notifications for the users notifications', function (done) {
  45. this.notifications.getUserNotifications = sinon
  46. .stub()
  47. .callsArgWith(1, null, this.stubbedNotification)
  48. const req = {
  49. params: {
  50. user_id
  51. }
  52. }
  53. return this.controller.getUserNotifications(req, {
  54. json: (result) => {
  55. result.should.equal(this.stubbedNotification)
  56. this.notifications.getUserNotifications
  57. .calledWith(user_id)
  58. .should.equal(true)
  59. return done()
  60. }
  61. })
  62. })
  63. })
  64. describe('addNotification', function () {
  65. return it('should tell the notifications to add the notification for the user', function (done) {
  66. this.notifications.addNotification = sinon.stub().callsArgWith(2)
  67. const req = {
  68. params: {
  69. user_id
  70. },
  71. body: this.stubbedNotification
  72. }
  73. return this.controller.addNotification(req, {
  74. sendStatus: (code) => {
  75. this.notifications.addNotification
  76. .calledWith(user_id, this.stubbedNotification)
  77. .should.equal(true)
  78. code.should.equal(200)
  79. return done()
  80. }
  81. })
  82. })
  83. })
  84. describe('removeNotificationId', function () {
  85. return it('should tell the notifications to mark the notification Id as read', function (done) {
  86. this.notifications.removeNotificationId = sinon.stub().callsArgWith(2)
  87. const req = {
  88. params: {
  89. user_id,
  90. notification_id
  91. }
  92. }
  93. return this.controller.removeNotificationId(req, {
  94. sendStatus: (code) => {
  95. this.notifications.removeNotificationId
  96. .calledWith(user_id, notification_id)
  97. .should.equal(true)
  98. code.should.equal(200)
  99. return done()
  100. }
  101. })
  102. })
  103. })
  104. describe('removeNotificationKey', function () {
  105. return it('should tell the notifications to mark the notification Key as read', function (done) {
  106. this.notifications.removeNotificationKey = sinon.stub().callsArgWith(2)
  107. const req = {
  108. params: {
  109. user_id
  110. },
  111. body: { key: notification_key }
  112. }
  113. return this.controller.removeNotificationKey(req, {
  114. sendStatus: (code) => {
  115. this.notifications.removeNotificationKey
  116. .calledWith(user_id, notification_key)
  117. .should.equal(true)
  118. code.should.equal(200)
  119. return done()
  120. }
  121. })
  122. })
  123. })
  124. return describe('removeNotificationByKeyOnly', function () {
  125. return it('should tell the notifications to mark the notification Key as read', function (done) {
  126. this.notifications.removeNotificationByKeyOnly = sinon
  127. .stub()
  128. .callsArgWith(1)
  129. const req = {
  130. params: {
  131. key: notification_key
  132. }
  133. }
  134. return this.controller.removeNotificationByKeyOnly(req, {
  135. sendStatus: (code) => {
  136. this.notifications.removeNotificationByKeyOnly
  137. .calledWith(notification_key)
  138. .should.equal(true)
  139. code.should.equal(200)
  140. return done()
  141. }
  142. })
  143. })
  144. })
  145. })