overleaf/services/contacts/test/unit/js/ContactsManagerTests.js

142 lines
3.8 KiB
JavaScript
Raw Normal View History

/* eslint-disable
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 chai = require('chai')
const should = chai.should()
const { expect } = chai
const modulePath = '../../../app/js/ContactManager.js'
const SandboxedModule = require('sandboxed-module')
const { ObjectId } = require('mongodb')
const tk = require('timekeeper')
2020-06-04 03:49:46 -04:00
describe('ContactManager', function () {
beforeEach(function () {
tk.freeze(Date.now())
this.ContactManager = SandboxedModule.require(modulePath, {
requires: {
'./mongodb': {
db: (this.db = { contacts: {} }),
2021-07-13 07:04:47 -04:00
ObjectId,
},
},
})
this.user_id = ObjectId().toString()
this.contact_id = ObjectId().toString()
return (this.callback = sinon.stub())
})
2020-06-04 03:49:46 -04:00
afterEach(function () {
return tk.reset()
})
2020-06-04 03:49:46 -04:00
describe('touchContact', function () {
beforeEach(function () {
this.db.contacts.updateOne = sinon.stub().callsArg(3)
})
2020-06-04 03:49:46 -04:00
describe('with a valid user_id', function () {
beforeEach(function () {
return this.ContactManager.touchContact(
this.user_id,
(this.contact_id = 'mock_contact'),
this.callback
)
})
2020-06-04 03:49:46 -04:00
it('should increment the contact count and timestamp', function () {
this.db.contacts.updateOne
.calledWith(
{
user_id: sinon.match(
2021-07-13 07:04:47 -04:00
o => o.toString() === this.user_id.toString()
),
},
{
$inc: {
2021-07-13 07:04:47 -04:00
'contacts.mock_contact.n': 1,
},
$set: {
2021-07-13 07:04:47 -04:00
'contacts.mock_contact.ts': new Date(),
},
},
{
2021-07-13 07:04:47 -04:00
upsert: true,
}
)
.should.equal(true)
})
2020-06-04 03:49:46 -04:00
return it('should call the callback', function () {
return this.callback.called.should.equal(true)
})
})
2020-06-04 03:49:46 -04:00
return describe('with an invalid user id', function () {
beforeEach(function () {
return this.ContactManager.touchContact(
'not-valid-object-id',
this.contact_id,
this.callback
)
})
2020-06-04 03:49:46 -04:00
return it('should call the callback with an error', function () {
2020-03-23 07:57:02 -04:00
return this.callback.calledWith(sinon.match(Error)).should.equal(true)
})
})
})
2020-06-04 03:49:46 -04:00
return describe('getContacts', function () {
beforeEach(function () {
this.user = {
2021-07-13 07:04:47 -04:00
contacts: ['mock', 'contacts'],
}
return (this.db.contacts.findOne = sinon
.stub()
.callsArgWith(1, null, this.user))
})
2020-06-04 03:49:46 -04:00
describe('with a valid user_id', function () {
beforeEach(function () {
return this.ContactManager.getContacts(this.user_id, this.callback)
})
2020-06-04 03:49:46 -04:00
it("should find the user's contacts", function () {
return this.db.contacts.findOne
.calledWith({
2021-07-13 07:04:47 -04:00
user_id: sinon.match(o => o.toString() === this.user_id.toString()),
})
.should.equal(true)
})
2020-06-04 03:49:46 -04:00
return it('should call the callback with the contacts', function () {
return this.callback
.calledWith(null, this.user.contacts)
.should.equal(true)
})
})
2020-06-04 03:49:46 -04:00
return describe('with an invalid user id', function () {
beforeEach(function () {
return this.ContactManager.getContacts(
'not-valid-object-id',
this.callback
)
})
2020-06-04 03:49:46 -04:00
return it('should call the callback with an error', function () {
2020-03-23 07:57:02 -04:00
return this.callback.calledWith(sinon.match(Error)).should.equal(true)
})
})
})
})