2019-05-29 05:21:06 -04:00
|
|
|
const SandboxedModule = require('sandboxed-module')
|
|
|
|
const sinon = require('sinon')
|
|
|
|
const modulePath = require('path').join(
|
|
|
|
__dirname,
|
|
|
|
'../../../../app/src/Features/Notifications/NotificationsController.js'
|
|
|
|
)
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('NotificationsController', function () {
|
2021-05-05 09:20:32 -04:00
|
|
|
const userId = '123nd3ijdks'
|
|
|
|
const notificationId = '123njdskj9jlk'
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
beforeEach(function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.handler = {
|
|
|
|
getUserNotifications: sinon.stub().callsArgWith(1),
|
2021-04-27 03:52:58 -04:00
|
|
|
markAsRead: sinon.stub().callsArgWith(2),
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
this.req = {
|
|
|
|
params: {
|
2021-05-05 09:20:32 -04:00
|
|
|
notificationId,
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
session: {
|
|
|
|
user: {
|
2021-05-05 09:20:32 -04:00
|
|
|
_id: userId,
|
2021-04-27 03:52:58 -04:00
|
|
|
},
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
i18n: {
|
2021-04-27 03:52:58 -04:00
|
|
|
translate() {},
|
|
|
|
},
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
this.AuthenticationController = {
|
2021-04-27 03:52:58 -04:00
|
|
|
getLoggedInUserId: sinon.stub().returns(this.req.session.user._id),
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2021-05-05 09:20:32 -04:00
|
|
|
this.controller = SandboxedModule.require(modulePath, {
|
2019-05-29 05:21:06 -04:00
|
|
|
requires: {
|
|
|
|
'./NotificationsHandler': this.handler,
|
|
|
|
'../Authentication/AuthenticationController': this
|
2021-04-27 03:52:58 -04:00
|
|
|
.AuthenticationController,
|
|
|
|
},
|
2021-05-05 09:20:32 -04:00
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should ask the handler for all unread notifications', function (done) {
|
2021-05-05 09:20:32 -04:00
|
|
|
const allNotifications = [{ _id: notificationId, user_id: userId }]
|
2019-05-29 05:21:06 -04:00
|
|
|
this.handler.getUserNotifications = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(1, null, allNotifications)
|
2021-05-05 09:20:32 -04:00
|
|
|
this.controller.getAllUnreadNotifications(this.req, {
|
2019-05-29 05:21:06 -04:00
|
|
|
send: body => {
|
2020-11-27 08:11:12 -05:00
|
|
|
body.should.deep.equal(allNotifications)
|
2021-05-05 09:20:32 -04:00
|
|
|
this.handler.getUserNotifications.calledWith(userId).should.equal(true)
|
|
|
|
done()
|
2021-04-27 03:52:58 -04:00
|
|
|
},
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should send a delete request when a delete has been received to mark a notification', function (done) {
|
2021-05-05 09:20:32 -04:00
|
|
|
this.controller.markNotificationAsRead(this.req, {
|
2020-05-06 06:02:16 -04:00
|
|
|
sendStatus: () => {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.handler.markAsRead
|
2021-05-05 09:20:32 -04:00
|
|
|
.calledWith(userId, notificationId)
|
2019-05-29 05:21:06 -04:00
|
|
|
.should.equal(true)
|
2021-05-05 09:20:32 -04:00
|
|
|
done()
|
2021-04-27 03:52:58 -04:00
|
|
|
},
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|