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

99 lines
2.6 KiB
JavaScript
Raw Normal View History

import sinon from 'sinon'
import { expect } from 'chai'
import esmock from 'esmock'
import { ObjectId } from 'mongodb'
2020-06-04 03:49:46 -04:00
describe('ContactManager', function () {
beforeEach(async function () {
this.clock = sinon.useFakeTimers(new Date())
this.db = { contacts: {} }
this.ContactManager = await esmock('../../../app/js/ContactManager', {
'../../../app/js/mongodb': {
db: this.db,
ObjectId,
2021-07-13 07:04:47 -04:00
},
})
this.user_id = new ObjectId().toString()
this.contact_id = new ObjectId().toString()
})
2020-06-04 03:49:46 -04:00
afterEach(function () {
this.clock.restore()
})
2020-06-04 03:49:46 -04:00
describe('touchContact', function () {
beforeEach(function () {
this.db.contacts.updateOne = sinon.stub().resolves()
})
2020-06-04 03:49:46 -04:00
describe('with a valid user_id', function () {
it('should increment the contact count and timestamp', async function () {
await expect(
this.ContactManager.touchContact(this.user_id, 'mock_contact')
).not.to.be.rejected
expect(this.db.contacts.updateOne).to.be.calledWith(
{
user_id: sinon.match(o => o.toString() === this.user_id),
},
{
$inc: {
'contacts.mock_contact.n': 1,
},
$set: {
'contacts.mock_contact.ts': new Date(),
},
},
{
upsert: true,
}
)
})
})
describe('with an invalid user id', function () {
it('should be rejected', async function () {
await expect(
this.ContactManager.touchContact(
'not-valid-object-id',
this.contact_id
)
).to.be.rejectedWith(
'input must be a 24 character hex string, 12 byte Uint8Array, or an integer'
)
})
})
})
describe('getContacts', function () {
2020-06-04 03:49:46 -04:00
beforeEach(function () {
this.user = {
2021-07-13 07:04:47 -04:00
contacts: ['mock', 'contacts'],
}
this.db.contacts.findOne = sinon.stub().resolves(this.user)
})
2020-06-04 03:49:46 -04:00
describe('with a valid user_id', function () {
it("should find the user's contacts", async function () {
await expect(
this.ContactManager.getContacts(this.user_id)
).to.eventually.deep.equal(this.user.contacts)
expect(this.db.contacts.findOne).to.be.calledWith({
user_id: sinon.match(o => o.toString() === this.user_id),
})
})
})
describe('with an invalid user id', function () {
it('should be rejected', async function () {
await expect(this.ContactManager.getContacts('not-valid-object-id')).to
.be.rejected
})
})
})
})