ContactsManagerTests.js 1.9 KB

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