overleaf/services/notifications/test/unit/js/NotificationsControllerTest.js

151 lines
4.4 KiB
JavaScript
Raw Normal View History

/* eslint-disable
camelcase,
no-return-assign,
no-unused-vars,
*/
// TODO: This file was created by bulk-decaffeinate.
// Fix any style issues and re-enable lint.
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const sinon = require('sinon')
const modulePath = '../../../app/js/NotificationsController.js'
const SandboxedModule = require('sandboxed-module')
const assert = require('assert')
2016-01-14 07:35:16 -05:00
const user_id = '51dc93e6fb625a261300003b'
const notification_id = 'fb625a26f09d'
const notification_key = 'my-notification-key'
2016-01-14 07:35:16 -05:00
2020-06-04 03:50:05 -04:00
describe('Notifications Controller', function () {
beforeEach(function () {
const self = this
this.notifications = {}
this.controller = SandboxedModule.require(modulePath, {
requires: {
'./Notifications': this.notifications,
'@overleaf/metrics': {
2021-07-13 07:04:44 -04:00
inc: sinon.stub(),
},
},
})
2016-01-16 15:28:19 -05:00
return (this.stubbedNotification = [
{
key: notification_key,
messageOpts: 'some info',
2021-07-13 07:04:44 -04:00
templateKey: 'template-key',
},
])
})
2016-01-16 15:28:19 -05:00
2020-06-04 03:50:05 -04:00
describe('getUserNotifications', function () {
return it('should ask the notifications for the users notifications', function (done) {
this.notifications.getUserNotifications = sinon
.stub()
.callsArgWith(1, null, this.stubbedNotification)
const req = {
params: {
2021-07-13 07:04:44 -04:00
user_id,
},
}
return this.controller.getUserNotifications(req, {
2021-07-13 07:04:44 -04:00
json: result => {
result.should.equal(this.stubbedNotification)
this.notifications.getUserNotifications
.calledWith(user_id)
.should.equal(true)
return done()
2021-07-13 07:04:44 -04:00
},
})
2020-02-11 04:08:35 -05:00
})
})
2016-01-16 15:28:19 -05:00
2020-06-04 03:50:05 -04:00
describe('addNotification', function () {
return it('should tell the notifications to add the notification for the user', function (done) {
this.notifications.addNotification = sinon.stub().callsArgWith(2)
const req = {
params: {
2021-07-13 07:04:44 -04:00
user_id,
},
2021-07-13 07:04:44 -04:00
body: this.stubbedNotification,
}
return this.controller.addNotification(req, {
2021-07-13 07:04:44 -04:00
sendStatus: code => {
this.notifications.addNotification
.calledWith(user_id, this.stubbedNotification)
.should.equal(true)
code.should.equal(200)
return done()
2021-07-13 07:04:44 -04:00
},
})
2020-02-11 04:08:35 -05:00
})
})
2016-01-16 15:28:19 -05:00
2020-06-04 03:50:05 -04:00
describe('removeNotificationId', function () {
return it('should tell the notifications to mark the notification Id as read', function (done) {
this.notifications.removeNotificationId = sinon.stub().callsArgWith(2)
const req = {
params: {
user_id,
2021-07-13 07:04:44 -04:00
notification_id,
},
}
return this.controller.removeNotificationId(req, {
2021-07-13 07:04:44 -04:00
sendStatus: code => {
this.notifications.removeNotificationId
.calledWith(user_id, notification_id)
.should.equal(true)
code.should.equal(200)
return done()
2021-07-13 07:04:44 -04:00
},
})
2020-02-11 04:08:35 -05:00
})
})
2016-02-05 04:38:32 -05:00
2020-06-04 03:50:05 -04:00
describe('removeNotificationKey', function () {
return it('should tell the notifications to mark the notification Key as read', function (done) {
this.notifications.removeNotificationKey = sinon.stub().callsArgWith(2)
const req = {
params: {
2021-07-13 07:04:44 -04:00
user_id,
},
2021-07-13 07:04:44 -04:00
body: { key: notification_key },
}
return this.controller.removeNotificationKey(req, {
2021-07-13 07:04:44 -04:00
sendStatus: code => {
this.notifications.removeNotificationKey
.calledWith(user_id, notification_key)
.should.equal(true)
code.should.equal(200)
return done()
2021-07-13 07:04:44 -04:00
},
})
2020-02-11 04:08:35 -05:00
})
})
2020-06-04 03:50:05 -04:00
return describe('removeNotificationByKeyOnly', function () {
return it('should tell the notifications to mark the notification Key as read', function (done) {
this.notifications.removeNotificationByKeyOnly = sinon
.stub()
.callsArgWith(1)
const req = {
params: {
2021-07-13 07:04:44 -04:00
key: notification_key,
},
}
return this.controller.removeNotificationByKeyOnly(req, {
2021-07-13 07:04:44 -04:00
sendStatus: code => {
this.notifications.removeNotificationByKeyOnly
.calledWith(notification_key)
.should.equal(true)
code.should.equal(200)
return done()
2021-07-13 07:04:44 -04:00
},
})
2020-02-11 04:08:35 -05:00
})
})
})