NotificationsController.test.ts 4.2 KB

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