| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- import { vi } from 'vitest'
- import sinon from 'sinon'
- const modulePath = new URL(
- '../../../../app/src/Features/Notifications/NotificationsController.mjs',
- import.meta.url
- ).pathname
- describe('NotificationsController', function () {
- const userId = '123nd3ijdks'
- const notificationId = '123njdskj9jlk'
- beforeEach(async function (ctx) {
- ctx.handler = {
- getUserNotifications: sinon.stub().callsArgWith(1),
- markAsRead: sinon.stub().callsArgWith(2),
- promises: {
- getUserNotifications: sinon.stub().callsArgWith(1),
- },
- }
- ctx.req = {
- params: {
- notificationId,
- },
- session: {
- user: {
- _id: userId,
- },
- },
- i18n: {
- translate() {},
- },
- }
- ctx.AuthenticationController = {
- getLoggedInUserId: sinon.stub().returns(ctx.req.session.user._id),
- }
- vi.doMock(
- '../../../../app/src/Features/Notifications/NotificationsHandler',
- () => ({
- default: ctx.handler,
- })
- )
- vi.doMock(
- '../../../../app/src/Features/Authentication/AuthenticationController',
- () => ({
- default: ctx.AuthenticationController,
- })
- )
- ctx.controller = (await import(modulePath)).default
- })
- it('should ask the handler for all unread notifications', function (ctx) {
- return new Promise(resolve => {
- const allNotifications = [{ _id: notificationId, user_id: userId }]
- ctx.handler.getUserNotifications = sinon
- .stub()
- .callsArgWith(1, null, allNotifications)
- ctx.controller.getAllUnreadNotifications(ctx.req, {
- json: body => {
- body.should.deep.equal(allNotifications)
- ctx.handler.getUserNotifications.calledWith(userId).should.equal(true)
- resolve()
- },
- })
- })
- })
- it('should send a delete request when a delete has been received to mark a notification', function (ctx) {
- return new Promise(resolve => {
- ctx.controller.markNotificationAsRead(ctx.req, {
- sendStatus: () => {
- ctx.handler.markAsRead
- .calledWith(userId, notificationId)
- .should.equal(true)
- resolve()
- },
- })
- })
- })
- it('should get a notification by notification id', function (ctx) {
- return new Promise(resolve => {
- const notification = { _id: notificationId, user_id: userId }
- ctx.handler.getUserNotifications = sinon
- .stub()
- .callsArgWith(1, null, [notification])
- ctx.controller.getNotification(ctx.req, {
- json: body => {
- body.should.deep.equal(notification)
- resolve()
- },
- status: () => ({
- end: () => {},
- }),
- })
- })
- })
- })
|