NotificationsController.test.js 4.5 KB

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