add basic unit tests

This commit is contained in:
Henrique Santos
2016-01-16 18:28:19 -02:00
parent 2c81b978cf
commit 797353de15
3 changed files with 94 additions and 9 deletions

View File

@@ -17,6 +17,6 @@ module.exports =
res.send()
removeNotification: (req, res)->
logger.log user_id: req.params.user_id, notification_id: req.params.notification_id, "removing notification"
logger.log user_id: req.params.user_id, notification_id: req.params.notification_id, "mark notification as read"
Notifications.removeNotification req.params.user_id, req.params.notification_id, (err, notifications)->
res.send()

View File

@@ -6,6 +6,47 @@ SandboxedModule = require('sandboxed-module')
assert = require('assert')
user_id = "51dc93e6fb625a261300003b"
notification_key = '123434'
notification_id = "fb625a26f09d"
describe 'Notifications controller', ->
beforeEach ->
self = @
@notifications = {}
@controller = SandboxedModule.require modulePath, requires:
'logger-sharelatex': log:->
'./Notifications':@notifications
@stubbedNotification = [{key:"notification-key", messageOpts:"some info", templateKey:"template-key"}]
describe "getUserNotifications", ->
it 'should ask the notifications for the users notifications', (done)->
@notifications.getUserNotifications = sinon.stub().callsArgWith(1, null, @stubbedNotification)
req =
params:
user_id: user_id
@controller.getUserNotifications req, json:(result)=>
result.should.equal @stubbedNotification
@notifications.getUserNotifications.calledWith(user_id).should.equal true
done()
describe "addNotification", ->
it "should tell the notifications to add the notification for the user", (done)->
@notifications.addNotification = sinon.stub().callsArgWith(2)
req =
params:
user_id: user_id
body: @stubbedNotification
@controller.addNotification req, send:(result)=>
@notifications.addNotification.calledWith(user_id, @stubbedNotification).should.equal true
done()
describe "removeNotification", ->
it "should tell the notifications to mark the notification as read", (done)->
@notifications.removeNotification = sinon.stub().callsArgWith(2)
req =
params:
user_id: user_id
notification_id: notification_id
@controller.removeNotification req, send:(result)=>
@notifications.removeNotification.calledWith(user_id, notification_id).should.equal true
done()

View File

@@ -4,27 +4,71 @@ should = chai.should()
modulePath = "../../../app/js/Notifications.js"
SandboxedModule = require('sandboxed-module')
assert = require('assert')
ObjectId = require("mongojs").ObjectId
user_id = "51dc93e6fb625a261300003b"
notification_key = '123434'
notification_id = "fb625a26f09d"
notification_key = "notification-key"
describe 'creating a user', ->
beforeEach ->
self = @
@findOneStub = sinon.stub()
@findStub = sinon.stub()
@saveStub = sinon.stub()
@insertStub = sinon.stub()
@countStub = sinon.stub()
@updateStub = sinon.stub()
@mongojs = =>
notifications:
update: self.mongojsUpdate
update: self.mongojsUpdate
find: @findStub
findOne: @findOneStub
save: @saveStub
insert: @insertStub
count: @countStub
update: @updateStub
@mongojs.ObjectId = ObjectId
@repository = SandboxedModule.require modulePath, requires:
@notifications = SandboxedModule.require modulePath, requires:
'logger-sharelatex': log:->
'settings-sharelatex': {}
'mongojs':@mongojs
@stubbedNotification = {user_id: user_id, key:"notification-key", messageOpts:"some info", templateKey:"template-key"}
@stubbedNotificationArray = [@stubbedNotification]
describe 'getUserNotifications', ->
it "should find all notifications and return it", (done)->
@findStub.callsArgWith(1, null, @stubbedNotificationArray)
@notifications.getUserNotifications user_id, (err, notifications)=>
notifications.should.equal @stubbedNotificationArray
@findStub.calledWith({"user_id" : user_id, "templateKey": {"$exists":true}}).should.equal true
done()
describe 'addNotification', ->
it 'should insert the notification into the collection', (done)->
@insertStub.callsArgWith(1, null)
@countStub.callsArgWith(1, null, 0)
@notifications.addNotification user_id, @stubbedNotification, (err)=>
@insertStub.calledWith(@stubbedNotification).should.equal true
done()
it 'should fail insert of existing notification key', (done)->
@insertStub.callsArgWith(1, null)
@countStub.callsArgWith(1, null, 1)
@notifications.addNotification user_id, @stubbedNotification, (err)=>
@insertStub.calledWith(@stubbedNotification).should.equal false
done()
describe 'removeNotification', ->
it 'should mark the notification id as read', (done)->
@updateStub.callsArgWith(2, null)
@notifications.removeNotification user_id, notification_id, (err)=>
searchOps =
user_id:user_id
_id:ObjectId(notification_id)
updateOperation =
"$unset": {templateKey:true}
@updateStub.calledWith(searchOps, updateOperation).should.equal true
done()