ContactsManagerTests.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /* eslint-disable
  2. no-return-assign,
  3. no-unused-vars,
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Fix any style issues and re-enable lint.
  7. /*
  8. * decaffeinate suggestions:
  9. * DS102: Remove unnecessary code created because of implicit returns
  10. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  11. */
  12. const sinon = require('sinon')
  13. const chai = require('chai')
  14. const should = chai.should()
  15. const { expect } = chai
  16. const modulePath = '../../../app/js/ContactManager.js'
  17. const SandboxedModule = require('sandboxed-module')
  18. const { ObjectId } = require('mongodb')
  19. const tk = require('timekeeper')
  20. describe('ContactManager', function () {
  21. beforeEach(function () {
  22. tk.freeze(Date.now())
  23. this.ContactManager = SandboxedModule.require(modulePath, {
  24. requires: {
  25. './mongodb': {
  26. db: (this.db = { contacts: {} }),
  27. ObjectId,
  28. },
  29. },
  30. })
  31. this.user_id = ObjectId().toString()
  32. this.contact_id = ObjectId().toString()
  33. return (this.callback = sinon.stub())
  34. })
  35. afterEach(function () {
  36. return tk.reset()
  37. })
  38. describe('touchContact', function () {
  39. beforeEach(function () {
  40. this.db.contacts.updateOne = sinon.stub().callsArg(3)
  41. })
  42. describe('with a valid user_id', function () {
  43. beforeEach(function () {
  44. return this.ContactManager.touchContact(
  45. this.user_id,
  46. (this.contact_id = 'mock_contact'),
  47. this.callback
  48. )
  49. })
  50. it('should increment the contact count and timestamp', function () {
  51. this.db.contacts.updateOne
  52. .calledWith(
  53. {
  54. user_id: sinon.match(
  55. o => o.toString() === this.user_id.toString()
  56. ),
  57. },
  58. {
  59. $inc: {
  60. 'contacts.mock_contact.n': 1,
  61. },
  62. $set: {
  63. 'contacts.mock_contact.ts': new Date(),
  64. },
  65. },
  66. {
  67. upsert: true,
  68. }
  69. )
  70. .should.equal(true)
  71. })
  72. return it('should call the callback', function () {
  73. return this.callback.called.should.equal(true)
  74. })
  75. })
  76. return describe('with an invalid user id', function () {
  77. beforeEach(function () {
  78. return this.ContactManager.touchContact(
  79. 'not-valid-object-id',
  80. this.contact_id,
  81. this.callback
  82. )
  83. })
  84. return it('should call the callback with an error', function () {
  85. return this.callback.calledWith(sinon.match(Error)).should.equal(true)
  86. })
  87. })
  88. })
  89. return describe('getContacts', function () {
  90. beforeEach(function () {
  91. this.user = {
  92. contacts: ['mock', 'contacts'],
  93. }
  94. return (this.db.contacts.findOne = sinon
  95. .stub()
  96. .callsArgWith(1, null, this.user))
  97. })
  98. describe('with a valid user_id', function () {
  99. beforeEach(function () {
  100. return this.ContactManager.getContacts(this.user_id, this.callback)
  101. })
  102. it("should find the user's contacts", function () {
  103. return this.db.contacts.findOne
  104. .calledWith({
  105. user_id: sinon.match(o => o.toString() === this.user_id.toString()),
  106. })
  107. .should.equal(true)
  108. })
  109. return it('should call the callback with the contacts', function () {
  110. return this.callback
  111. .calledWith(null, this.user.contacts)
  112. .should.equal(true)
  113. })
  114. })
  115. return describe('with an invalid user id', function () {
  116. beforeEach(function () {
  117. return this.ContactManager.getContacts(
  118. 'not-valid-object-id',
  119. this.callback
  120. )
  121. })
  122. return it('should call the callback with an error', function () {
  123. return this.callback.calledWith(sinon.match(Error)).should.equal(true)
  124. })
  125. })
  126. })
  127. })