NotificationsControllerTest.js 4.4 KB

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