overleaf/services/web/test/unit/src/Contact/ContactManagerTests.js
Miguel Serrano 2e8f66ac6b Merge pull request #3075 from overleaf/msm-codemod-oerror-v3-logging
Replaced logger.warn statements with OError.tag()

GitOrigin-RevId: 4d821ec48a0006abb1fcffe07dbb5511c88f5b9a
2020-08-12 02:07:03 +00:00

168 lines
4.5 KiB
JavaScript

/* eslint-disable
max-len,
no-return-assign,
*/
// 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 chai = require('chai')
chai.should()
const sinon = require('sinon')
const modulePath = '../../../../app/src/Features/Contacts/ContactManager'
const SandboxedModule = require('sandboxed-module')
describe('ContactManager', function() {
beforeEach(function() {
this.ContactManager = SandboxedModule.require(modulePath, {
globals: {
console: console
},
requires: {
request: (this.request = sinon.stub()),
'settings-sharelatex': (this.settings = {
apis: {
contacts: {
url: 'contacts.sharelatex.com'
}
}
})
}
})
this.user_id = 'user-id-123'
this.contact_id = 'contact-id-123'
return (this.callback = sinon.stub())
})
describe('getContacts', function() {
describe('with a successful response code', function() {
beforeEach(function() {
this.request.get = sinon
.stub()
.callsArgWith(
1,
null,
{ statusCode: 204 },
{ contact_ids: (this.contact_ids = ['mock', 'contact_ids']) }
)
return this.ContactManager.getContactIds(
this.user_id,
{ limit: 42 },
this.callback
)
})
it('should get the contacts from the contacts api', function() {
return this.request.get
.calledWith({
url: `${this.settings.apis.contacts.url}/user/${
this.user_id
}/contacts`,
qs: { limit: 42 },
json: true,
jar: false
})
.should.equal(true)
})
it('should call the callback with the contatcs', function() {
return this.callback
.calledWith(null, this.contact_ids)
.should.equal(true)
})
})
describe('with a failed response code', function() {
beforeEach(function() {
this.request.get = sinon
.stub()
.callsArgWith(1, null, { statusCode: 500 }, null)
return this.ContactManager.getContactIds(
this.user_id,
{ limit: 42 },
this.callback
)
})
it('should call the callback with an error', function() {
return this.callback
.calledWith(
sinon.match
.instanceOf(Error)
.and(
sinon.match.has(
'message',
'contacts api responded with non-success code: 500'
)
)
)
.should.equal(true)
})
})
})
describe('addContact', function() {
describe('with a successful response code', function() {
beforeEach(function() {
this.request.post = sinon
.stub()
.callsArgWith(1, null, { statusCode: 200 }, null)
return this.ContactManager.addContact(
this.user_id,
this.contact_id,
this.callback
)
})
it('should add the contacts for the user in the contacts api', function() {
return this.request.post
.calledWith({
url: `${this.settings.apis.contacts.url}/user/${
this.user_id
}/contacts`,
json: {
contact_id: this.contact_id
},
jar: false
})
.should.equal(true)
})
it('should call the callback', function() {
return this.callback.called.should.equal(true)
})
})
describe('with a failed response code', function() {
beforeEach(function() {
this.request.post = sinon
.stub()
.callsArgWith(1, null, { statusCode: 500 }, null)
return this.ContactManager.addContact(
this.user_id,
this.contact_id,
this.callback
)
})
it('should call the callback with an error', function() {
return this.callback
.calledWith(
sinon.match
.instanceOf(Error)
.and(
sinon.match.has(
'message',
'contacts api responded with non-success code: 500'
)
)
)
.should.equal(true)
})
})
})
})