NotificationsControllerTests.mjs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import esmock from 'esmock'
  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 () {
  11. this.handler = {
  12. getUserNotifications: sinon.stub().callsArgWith(1),
  13. markAsRead: sinon.stub().callsArgWith(2),
  14. }
  15. this.req = {
  16. params: {
  17. notificationId,
  18. },
  19. session: {
  20. user: {
  21. _id: userId,
  22. },
  23. },
  24. i18n: {
  25. translate() {},
  26. },
  27. }
  28. this.AuthenticationController = {
  29. getLoggedInUserId: sinon.stub().returns(this.req.session.user._id),
  30. }
  31. this.controller = await esmock.strict(modulePath, {
  32. '../../../../app/src/Features/Notifications/NotificationsHandler':
  33. this.handler,
  34. '../../../../app/src/Features/Authentication/AuthenticationController':
  35. this.AuthenticationController,
  36. })
  37. })
  38. it('should ask the handler for all unread notifications', function (done) {
  39. const allNotifications = [{ _id: notificationId, user_id: userId }]
  40. this.handler.getUserNotifications = sinon
  41. .stub()
  42. .callsArgWith(1, null, allNotifications)
  43. this.controller.getAllUnreadNotifications(this.req, {
  44. json: body => {
  45. body.should.deep.equal(allNotifications)
  46. this.handler.getUserNotifications.calledWith(userId).should.equal(true)
  47. done()
  48. },
  49. })
  50. })
  51. it('should send a delete request when a delete has been received to mark a notification', function (done) {
  52. this.controller.markNotificationAsRead(this.req, {
  53. sendStatus: () => {
  54. this.handler.markAsRead
  55. .calledWith(userId, notificationId)
  56. .should.equal(true)
  57. done()
  58. },
  59. })
  60. })
  61. })