NotificationsControllerTest.js 4.3 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. import { stub } from 'sinon'
  13. import { require as _require } from 'sandboxed-module'
  14. import assert from 'node:assert'
  15. const modulePath = '../../../app/js/NotificationsController.js'
  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 = _require(modulePath, {
  24. requires: {
  25. './Notifications': this.notifications,
  26. '@overleaf/metrics': {
  27. inc: 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 = stub().callsArgWith(
  42. 1,
  43. null,
  44. this.stubbedNotification
  45. )
  46. const req = {
  47. params: {
  48. user_id: userId,
  49. },
  50. }
  51. return this.controller.getUserNotifications(req, {
  52. json: result => {
  53. result.should.equal(this.stubbedNotification)
  54. this.notifications.getUserNotifications
  55. .calledWith(userId)
  56. .should.equal(true)
  57. return done()
  58. },
  59. })
  60. })
  61. })
  62. describe('addNotification', function () {
  63. return it('should tell the notifications to add the notification for the user', function (done) {
  64. this.notifications.addNotification = stub().callsArgWith(2)
  65. const req = {
  66. params: {
  67. user_id: userId,
  68. },
  69. body: this.stubbedNotification,
  70. }
  71. return this.controller.addNotification(req, {
  72. sendStatus: code => {
  73. this.notifications.addNotification
  74. .calledWith(userId, this.stubbedNotification)
  75. .should.equal(true)
  76. code.should.equal(200)
  77. return done()
  78. },
  79. })
  80. })
  81. })
  82. describe('removeNotificationId', function () {
  83. return it('should tell the notifications to mark the notification Id as read', function (done) {
  84. this.notifications.removeNotificationId = stub().callsArgWith(2)
  85. const req = {
  86. params: {
  87. user_id: userId,
  88. notification_id: notificationId,
  89. },
  90. }
  91. return this.controller.removeNotificationId(req, {
  92. sendStatus: code => {
  93. this.notifications.removeNotificationId
  94. .calledWith(userId, notificationId)
  95. .should.equal(true)
  96. code.should.equal(200)
  97. return done()
  98. },
  99. })
  100. })
  101. })
  102. describe('removeNotificationKey', function () {
  103. return it('should tell the notifications to mark the notification Key as read', function (done) {
  104. this.notifications.removeNotificationKey = stub().callsArgWith(2)
  105. const req = {
  106. params: {
  107. user_id: userId,
  108. },
  109. body: { key: notificationKey },
  110. }
  111. return this.controller.removeNotificationKey(req, {
  112. sendStatus: code => {
  113. this.notifications.removeNotificationKey
  114. .calledWith(userId, notificationKey)
  115. .should.equal(true)
  116. code.should.equal(200)
  117. return done()
  118. },
  119. })
  120. })
  121. })
  122. return describe('removeNotificationByKeyOnly', function () {
  123. return it('should tell the notifications to mark the notification Key as read', function (done) {
  124. this.notifications.removeNotificationByKeyOnly = stub().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. })