overleaf/services/web/test/unit/coffee/Referal/ReferalAllocatorTests.coffee

76 lines
2.5 KiB
CoffeeScript
Raw Normal View History

2014-02-12 10:23:40 +00:00
SandboxedModule = require('sandboxed-module')
assert = require('assert')
require('chai').should()
sinon = require('sinon')
modulePath = require('path').join __dirname, '../../../../app/js/Features/Referal/ReferalAllocator.js'
describe 'ReferalAllocator', ->
2014-02-12 10:23:40 +00:00
beforeEach ->
@ReferalAllocator = SandboxedModule.require modulePath, requires:
'../../models/User': User: @User = {}
"../Subscription/FeaturesUpdater": @FeaturesUpdater = {}
2014-02-12 10:23:40 +00:00
"settings-sharelatex": @Settings = {}
'logger-sharelatex':
log:->
err:->
@callback = sinon.stub()
@referal_id = "referal-id-123"
@referal_medium = "twitter"
@user_id = "user-id-123"
@new_user_id = "new-user-id-123"
describe "allocate", ->
describe "when the referal was a bonus referal", ->
beforeEach ->
@referal_source = "bonus"
@User.update = sinon.stub().callsArgWith 3, null
@User.findOne = sinon.stub().callsArgWith 1, null, { _id: @user_id }
@FeaturesUpdater.refreshFeatures = sinon.stub().yields()
2014-02-12 10:23:40 +00:00
@ReferalAllocator.allocate @referal_id, @new_user_id, @referal_source, @referal_medium, @callback
it 'should update the referring user with the refered users id', ->
@User.update.calledWith({
"referal_id":@referal_id
}, {
$push:
refered_users: @new_user_id
$inc:
refered_user_count: 1
}).should.equal true
it "find the referring users id", ->
@User.findOne
.calledWith( referal_id: @referal_id )
.should.equal true
it "should refresh the user's subscription", ->
@FeaturesUpdater.refreshFeatures
2014-02-12 10:23:40 +00:00
.calledWith(@user_id)
.should.equal true
it "should call the callback", ->
@callback.called.should.equal true
describe "when the referal is not a bonus referal", ->
beforeEach ->
@referal_source = "public_share"
@User.update = sinon.stub().callsArgWith 3, null
@User.findOne = sinon.stub().callsArgWith 1, null, { _id: @user_id }
@FeaturesUpdater.refreshFeatures = sinon.stub().yields()
2014-02-12 10:23:40 +00:00
@ReferalAllocator.allocate @referal_id, @new_user_id, @referal_source, @referal_medium, @callback
it 'should not update the referring user with the refered users id', ->
@User.update.called.should.equal false
it "find the referring users id", ->
@User.findOne
.calledWith( referal_id: @referal_id )
.should.equal true
it "should not assign the user a bonus", ->
@FeaturesUpdater.refreshFeatures.called.should.equal false
2014-02-12 10:23:40 +00:00
it "should call the callback", ->
@callback.called.should.equal true