NotificationsControllerTests.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. const SandboxedModule = require('sandboxed-module')
  2. const sinon = require('sinon')
  3. const modulePath = require('path').join(
  4. __dirname,
  5. '../../../../app/src/Features/Notifications/NotificationsController.js'
  6. )
  7. describe('NotificationsController', function () {
  8. const userId = '123nd3ijdks'
  9. const notificationId = '123njdskj9jlk'
  10. beforeEach(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 = SandboxedModule.require(modulePath, {
  32. requires: {
  33. './NotificationsHandler': this.handler,
  34. '../Authentication/AuthenticationController':
  35. this.AuthenticationController,
  36. },
  37. })
  38. })
  39. it('should ask the handler for all unread notifications', function (done) {
  40. const allNotifications = [{ _id: notificationId, user_id: userId }]
  41. this.handler.getUserNotifications = sinon
  42. .stub()
  43. .callsArgWith(1, null, allNotifications)
  44. this.controller.getAllUnreadNotifications(this.req, {
  45. json: body => {
  46. body.should.deep.equal(allNotifications)
  47. this.handler.getUserNotifications.calledWith(userId).should.equal(true)
  48. done()
  49. },
  50. })
  51. })
  52. it('should send a delete request when a delete has been received to mark a notification', function (done) {
  53. this.controller.markNotificationAsRead(this.req, {
  54. sendStatus: () => {
  55. this.handler.markAsRead
  56. .calledWith(userId, notificationId)
  57. .should.equal(true)
  58. done()
  59. },
  60. })
  61. })
  62. })