ContactsManagerTests.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // @ts-check
  2. import sinon from 'sinon'
  3. import { expect } from 'chai'
  4. import { ObjectId } from 'mongodb'
  5. import { cleanupTestDatabase } from '../../../app/js/mongodb.js'
  6. import * as ContactManager from '../../../app/js/ContactManager.js'
  7. import '../../acceptance/js/MongoHelper.js'
  8. describe('ContactManager', function () {
  9. beforeEach(cleanupTestDatabase)
  10. beforeEach(async function () {
  11. this.clock = sinon.useFakeTimers(new Date())
  12. this.user_id = new ObjectId().toString()
  13. this.contact_id = new ObjectId().toString()
  14. })
  15. afterEach(function () {
  16. this.clock.restore()
  17. })
  18. describe('touchContact', function () {
  19. describe('with a valid user_id', function () {
  20. it('should increment the contact count and timestamp', async function () {
  21. const now = new Date()
  22. await ContactManager.touchContact(this.user_id, this.contact_id)
  23. const contacts = await ContactManager.getContacts(this.user_id)
  24. expect(contacts).to.deep.equal({
  25. [this.contact_id]: {
  26. n: 1,
  27. ts: now,
  28. },
  29. })
  30. })
  31. })
  32. describe('with an invalid user id', function () {
  33. it('should be rejected', async function () {
  34. await expect(
  35. ContactManager.touchContact('not-valid-object-id', this.contact_id)
  36. ).to.be.rejectedWith(
  37. 'input must be a 24 character hex string, 12 byte Uint8Array, or an integer'
  38. )
  39. })
  40. })
  41. })
  42. describe('getContacts', function () {
  43. describe('with a valid user_id', function () {
  44. it('should find an empty contact list', async function () {
  45. const contacts = await ContactManager.getContacts(this.user_id)
  46. expect(contacts).to.be.undefined
  47. })
  48. })
  49. describe('with an invalid user id', function () {
  50. it('should be rejected', async function () {
  51. await expect(ContactManager.getContacts('not-valid-object-id')).to.be
  52. .rejected
  53. })
  54. })
  55. })
  56. })