|
@@ -1,21 +1,16 @@
|
|
|
|
|
+// @ts-check
|
|
|
|
|
+
|
|
|
import sinon from 'sinon'
|
|
import sinon from 'sinon'
|
|
|
import { expect } from 'chai'
|
|
import { expect } from 'chai'
|
|
|
-import esmock from 'esmock'
|
|
|
|
|
import { ObjectId } from 'mongodb'
|
|
import { ObjectId } from 'mongodb'
|
|
|
|
|
+import { cleanupTestDatabase } from '../../../app/js/mongodb.js'
|
|
|
|
|
+import * as ContactManager from '../../../app/js/ContactManager.js'
|
|
|
|
|
|
|
|
describe('ContactManager', function () {
|
|
describe('ContactManager', function () {
|
|
|
|
|
+ beforeEach(cleanupTestDatabase)
|
|
|
|
|
+
|
|
|
beforeEach(async function () {
|
|
beforeEach(async function () {
|
|
|
this.clock = sinon.useFakeTimers(new Date())
|
|
this.clock = sinon.useFakeTimers(new Date())
|
|
|
-
|
|
|
|
|
- this.db = { contacts: {} }
|
|
|
|
|
-
|
|
|
|
|
- this.ContactManager = await esmock('../../../app/js/ContactManager', {
|
|
|
|
|
- '../../../app/js/mongodb': {
|
|
|
|
|
- db: this.db,
|
|
|
|
|
- ObjectId,
|
|
|
|
|
- },
|
|
|
|
|
- })
|
|
|
|
|
-
|
|
|
|
|
this.user_id = new ObjectId().toString()
|
|
this.user_id = new ObjectId().toString()
|
|
|
this.contact_id = new ObjectId().toString()
|
|
this.contact_id = new ObjectId().toString()
|
|
|
})
|
|
})
|
|
@@ -25,42 +20,24 @@ describe('ContactManager', function () {
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
describe('touchContact', function () {
|
|
describe('touchContact', function () {
|
|
|
- beforeEach(function () {
|
|
|
|
|
- this.db.contacts.updateOne = sinon.stub().resolves()
|
|
|
|
|
- })
|
|
|
|
|
-
|
|
|
|
|
describe('with a valid user_id', function () {
|
|
describe('with a valid user_id', function () {
|
|
|
it('should increment the contact count and timestamp', async 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(),
|
|
|
|
|
- },
|
|
|
|
|
|
|
+ const now = new Date()
|
|
|
|
|
+ await ContactManager.touchContact(this.user_id, this.contact_id)
|
|
|
|
|
+ const contacts = await ContactManager.getContacts(this.user_id)
|
|
|
|
|
+ expect(contacts).to.deep.equal({
|
|
|
|
|
+ [this.contact_id]: {
|
|
|
|
|
+ n: 1,
|
|
|
|
|
+ ts: now,
|
|
|
},
|
|
},
|
|
|
- {
|
|
|
|
|
- upsert: true,
|
|
|
|
|
- }
|
|
|
|
|
- )
|
|
|
|
|
|
|
+ })
|
|
|
})
|
|
})
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
describe('with an invalid user id', function () {
|
|
describe('with an invalid user id', function () {
|
|
|
it('should be rejected', async function () {
|
|
it('should be rejected', async function () {
|
|
|
await expect(
|
|
await expect(
|
|
|
- this.ContactManager.touchContact(
|
|
|
|
|
- 'not-valid-object-id',
|
|
|
|
|
- this.contact_id
|
|
|
|
|
- )
|
|
|
|
|
|
|
+ ContactManager.touchContact('not-valid-object-id', this.contact_id)
|
|
|
).to.be.rejectedWith(
|
|
).to.be.rejectedWith(
|
|
|
'input must be a 24 character hex string, 12 byte Uint8Array, or an integer'
|
|
'input must be a 24 character hex string, 12 byte Uint8Array, or an integer'
|
|
|
)
|
|
)
|
|
@@ -69,29 +46,17 @@ describe('ContactManager', function () {
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
describe('getContacts', function () {
|
|
describe('getContacts', function () {
|
|
|
- beforeEach(function () {
|
|
|
|
|
- this.user = {
|
|
|
|
|
- contacts: ['mock', 'contacts'],
|
|
|
|
|
- }
|
|
|
|
|
- this.db.contacts.findOne = sinon.stub().resolves(this.user)
|
|
|
|
|
- })
|
|
|
|
|
-
|
|
|
|
|
describe('with a valid user_id', function () {
|
|
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),
|
|
|
|
|
- })
|
|
|
|
|
|
|
+ it('should find an empty contact list', async function () {
|
|
|
|
|
+ const contacts = await ContactManager.getContacts(this.user_id)
|
|
|
|
|
+ expect(contacts).to.be.undefined
|
|
|
})
|
|
})
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
describe('with an invalid user id', function () {
|
|
describe('with an invalid user id', function () {
|
|
|
it('should be rejected', async function () {
|
|
it('should be rejected', async function () {
|
|
|
- await expect(this.ContactManager.getContacts('not-valid-object-id')).to
|
|
|
|
|
- .be.rejected
|
|
|
|
|
|
|
+ await expect(ContactManager.getContacts('not-valid-object-id')).to.be
|
|
|
|
|
+ .rejected
|
|
|
})
|
|
})
|
|
|
})
|
|
})
|
|
|
})
|
|
})
|