overleaf/services/web/test/unit/coffee/Institutions/InstitutionsGetterTests.coffee
2018-08-27 15:45:31 +02:00

45 lines
1.8 KiB
CoffeeScript

SandboxedModule = require('sandboxed-module')
require('chai').should()
expect = require('chai').expect
sinon = require('sinon')
modulePath = require('path').join __dirname, '../../../../app/js/Features/Institutions/InstitutionsGetter.js'
describe 'InstitutionsGetter', ->
beforeEach ->
@UserGetter = getUserFullEmails: sinon.stub()
@InstitutionsGetter = SandboxedModule.require modulePath, requires:
'../User/UserGetter': @UserGetter
'logger-sharelatex':
log:-> console.log(arguments)
err:->
@userId = '12345abcde'
describe "getConfirmedInstitutions", ->
it 'filters unconfirmed affiliations', (done) ->
@userEmails = [
{ confirmedAt: null, affiliation: institution: { id: 123, confirmed: true } }
{ confirmedAt: new Date(), affiliation: institution: { id: 456, confirmed: true } }
{ confirmedAt: new Date(), affiliation: null }
{ confirmedAt: new Date(), affiliation: institution: null }
{ confirmedAt: new Date(), affiliation: institution: { id: 789, confirmed: false } }
]
@UserGetter.getUserFullEmails.yields(null, @userEmails)
@InstitutionsGetter.getConfirmedInstitutions @userId, (error, institutions) ->
expect(error).to.not.exist
institutions.length.should.equal 1
institutions[0].id.should.equal 456
done()
it 'should handle empty response', (done) ->
@UserGetter.getUserFullEmails.yields(null, [])
@InstitutionsGetter.getConfirmedInstitutions @userId, (error, institutions) ->
expect(error).to.not.exist
institutions.length.should.equal 0
done()
it 'should handle error', (done) ->
@UserGetter.getUserFullEmails.yields(new Error('Nope'))
@InstitutionsGetter.getConfirmedInstitutions @userId, (error, institutions) ->
expect(error).to.exist
done()