NotificationsController.test.mjs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import { vi } from 'vitest'
  2. import sinon from 'sinon'
  3. const modulePath = new URL(
  4. '../../../../app/src/Features/Notifications/NotificationsController.mjs',
  5. import.meta.url
  6. ).pathname
  7. describe('NotificationsController', function () {
  8. const userId = '123nd3ijdks'
  9. const notificationId = '123njdskj9jlk'
  10. beforeEach(async function (ctx) {
  11. ctx.handler = {
  12. getUserNotifications: sinon.stub().callsArgWith(1),
  13. markAsRead: sinon.stub().callsArgWith(2),
  14. promises: {
  15. getUserNotifications: sinon.stub().callsArgWith(1),
  16. },
  17. }
  18. ctx.req = {
  19. params: {
  20. notificationId,
  21. },
  22. session: {
  23. user: {
  24. _id: userId,
  25. },
  26. },
  27. i18n: {
  28. translate() {},
  29. },
  30. }
  31. ctx.AuthenticationController = {
  32. getLoggedInUserId: sinon.stub().returns(ctx.req.session.user._id),
  33. }
  34. vi.doMock(
  35. '../../../../app/src/Features/Notifications/NotificationsHandler',
  36. () => ({
  37. default: ctx.handler,
  38. })
  39. )
  40. vi.doMock(
  41. '../../../../app/src/Features/Authentication/AuthenticationController',
  42. () => ({
  43. default: ctx.AuthenticationController,
  44. })
  45. )
  46. ctx.controller = (await import(modulePath)).default
  47. })
  48. it('should ask the handler for all unread notifications', function (ctx) {
  49. return new Promise(resolve => {
  50. const allNotifications = [{ _id: notificationId, user_id: userId }]
  51. ctx.handler.getUserNotifications = sinon
  52. .stub()
  53. .callsArgWith(1, null, allNotifications)
  54. ctx.controller.getAllUnreadNotifications(ctx.req, {
  55. json: body => {
  56. body.should.deep.equal(allNotifications)
  57. ctx.handler.getUserNotifications.calledWith(userId).should.equal(true)
  58. resolve()
  59. },
  60. })
  61. })
  62. })
  63. it('should send a delete request when a delete has been received to mark a notification', function (ctx) {
  64. return new Promise(resolve => {
  65. ctx.controller.markNotificationAsRead(ctx.req, {
  66. sendStatus: () => {
  67. ctx.handler.markAsRead
  68. .calledWith(userId, notificationId)
  69. .should.equal(true)
  70. resolve()
  71. },
  72. })
  73. })
  74. })
  75. it('should get a notification by notification id', function (ctx) {
  76. return new Promise(resolve => {
  77. const notification = { _id: notificationId, user_id: userId }
  78. ctx.handler.getUserNotifications = sinon
  79. .stub()
  80. .callsArgWith(1, null, [notification])
  81. ctx.controller.getNotification(ctx.req, {
  82. json: body => {
  83. body.should.deep.equal(notification)
  84. resolve()
  85. },
  86. status: () => ({
  87. end: () => {},
  88. }),
  89. })
  90. })
  91. })
  92. })