NotificationsController.test.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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', () => notifications)
  24. vi.doMock('@overleaf/metrics', () => ({
  25. default: {
  26. inc: vi.fn(),
  27. },
  28. }))
  29. controller = (await import(modulePath)).default
  30. stubbedNotification = [
  31. {
  32. key: notificationKey,
  33. messageOpts: 'some info',
  34. templateKey: 'template-key',
  35. },
  36. ]
  37. })
  38. describe('getUserNotifications', () => {
  39. it('should ask the notifications for the users notifications', async () => {
  40. notifications.getUserNotifications.mockResolvedValue(stubbedNotification)
  41. const req = {
  42. params: {
  43. user_id: userId,
  44. },
  45. }
  46. await new Promise(resolve => {
  47. controller.getUserNotifications(req, {
  48. json: result => {
  49. expect(result).toBe(stubbedNotification)
  50. expect(notifications.getUserNotifications).toHaveBeenCalledWith(
  51. userId
  52. )
  53. resolve()
  54. },
  55. })
  56. })
  57. })
  58. })
  59. describe('addNotification', () => {
  60. it('should tell the notifications to add the notification for the user', async () => {
  61. notifications.addNotification.mockResolvedValue()
  62. const req = {
  63. params: {
  64. user_id: userId,
  65. },
  66. body: stubbedNotification,
  67. }
  68. await new Promise(resolve => {
  69. controller.addNotification(req, {
  70. sendStatus: code => {
  71. expect(notifications.addNotification).toHaveBeenCalledWith(
  72. userId,
  73. stubbedNotification
  74. )
  75. expect(code).toBe(200)
  76. resolve()
  77. },
  78. })
  79. })
  80. })
  81. })
  82. describe('removeNotificationId', () => {
  83. it('should tell the notifications to mark the notification Id as read', async () => {
  84. notifications.removeNotificationId.mockResolvedValue()
  85. const req = {
  86. params: {
  87. user_id: userId,
  88. notification_id: notificationId,
  89. },
  90. }
  91. await new Promise(resolve => {
  92. controller.removeNotificationId(req, {
  93. sendStatus: code => {
  94. expect(notifications.removeNotificationId).toHaveBeenCalledWith(
  95. userId,
  96. notificationId
  97. )
  98. expect(code).toBe(200)
  99. resolve()
  100. },
  101. })
  102. })
  103. })
  104. })
  105. describe('removeNotificationKey', () => {
  106. it('should tell the notifications to mark the notification Key as read', async () => {
  107. notifications.removeNotificationKey.mockResolvedValue()
  108. const req = {
  109. params: {
  110. user_id: userId,
  111. },
  112. body: { key: notificationKey },
  113. }
  114. await new Promise(resolve => {
  115. controller.removeNotificationKey(req, {
  116. sendStatus: code => {
  117. expect(notifications.removeNotificationKey).toHaveBeenCalledWith(
  118. userId,
  119. notificationKey
  120. )
  121. expect(code).toBe(200)
  122. resolve()
  123. },
  124. })
  125. })
  126. })
  127. })
  128. describe('removeNotificationByKeyOnly', () => {
  129. it('should tell the notifications to mark the notification Key as read', async () => {
  130. notifications.removeNotificationByKeyOnly.mockResolvedValue()
  131. const req = {
  132. params: {
  133. key: notificationKey,
  134. },
  135. }
  136. await new Promise(resolve =>
  137. controller.removeNotificationByKeyOnly(req, {
  138. sendStatus: code => {
  139. expect(
  140. notifications.removeNotificationByKeyOnly
  141. ).toHaveBeenCalledWith(notificationKey)
  142. expect(code).toBe(200)
  143. resolve()
  144. },
  145. })
  146. )
  147. })
  148. })
  149. })