ContactsManagerTests.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. 'logger-sharelatex': { log: sinon.stub() },
  30. 'metrics-sharelatex': { timeAsyncMethod: sinon.stub() }
  31. }
  32. })
  33. this.user_id = ObjectId().toString()
  34. this.contact_id = ObjectId().toString()
  35. return (this.callback = sinon.stub())
  36. })
  37. afterEach(function () {
  38. return tk.reset()
  39. })
  40. describe('touchContact', function () {
  41. beforeEach(function () {
  42. this.db.contacts.updateOne = sinon.stub().callsArg(3)
  43. })
  44. describe('with a valid user_id', function () {
  45. beforeEach(function () {
  46. return this.ContactManager.touchContact(
  47. this.user_id,
  48. (this.contact_id = 'mock_contact'),
  49. this.callback
  50. )
  51. })
  52. it('should increment the contact count and timestamp', function () {
  53. this.db.contacts.updateOne
  54. .calledWith(
  55. {
  56. user_id: sinon.match(
  57. (o) => o.toString() === this.user_id.toString()
  58. )
  59. },
  60. {
  61. $inc: {
  62. 'contacts.mock_contact.n': 1
  63. },
  64. $set: {
  65. 'contacts.mock_contact.ts': new Date()
  66. }
  67. },
  68. {
  69. upsert: true
  70. }
  71. )
  72. .should.equal(true)
  73. })
  74. return it('should call the callback', function () {
  75. return this.callback.called.should.equal(true)
  76. })
  77. })
  78. return describe('with an invalid user id', function () {
  79. beforeEach(function () {
  80. return this.ContactManager.touchContact(
  81. 'not-valid-object-id',
  82. this.contact_id,
  83. this.callback
  84. )
  85. })
  86. return it('should call the callback with an error', function () {
  87. return this.callback.calledWith(sinon.match(Error)).should.equal(true)
  88. })
  89. })
  90. })
  91. return describe('getContacts', function () {
  92. beforeEach(function () {
  93. this.user = {
  94. contacts: ['mock', 'contacts']
  95. }
  96. return (this.db.contacts.findOne = sinon
  97. .stub()
  98. .callsArgWith(1, null, this.user))
  99. })
  100. describe('with a valid user_id', function () {
  101. beforeEach(function () {
  102. return this.ContactManager.getContacts(this.user_id, this.callback)
  103. })
  104. it("should find the user's contacts", function () {
  105. return this.db.contacts.findOne
  106. .calledWith({
  107. user_id: sinon.match(
  108. (o) => o.toString() === this.user_id.toString()
  109. )
  110. })
  111. .should.equal(true)
  112. })
  113. return it('should call the callback with the contacts', function () {
  114. return this.callback
  115. .calledWith(null, this.user.contacts)
  116. .should.equal(true)
  117. })
  118. })
  119. return describe('with an invalid user id', function () {
  120. beforeEach(function () {
  121. return this.ContactManager.getContacts(
  122. 'not-valid-object-id',
  123. this.callback
  124. )
  125. })
  126. return it('should call the callback with an error', function () {
  127. return this.callback.calledWith(sinon.match(Error)).should.equal(true)
  128. })
  129. })
  130. })
  131. })