ContactControllerTests.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* eslint-disable
  2. max-len,
  3. no-dupe-keys,
  4. no-return-assign,
  5. no-unused-vars,
  6. */
  7. // TODO: This file was created by bulk-decaffeinate.
  8. // Fix any style issues and re-enable lint.
  9. /*
  10. * decaffeinate suggestions:
  11. * DS102: Remove unnecessary code created because of implicit returns
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. const sinon = require('sinon')
  15. const { assert, expect } = require('chai')
  16. const modulePath = '../../../../app/src/Features/Contacts/ContactController.js'
  17. const SandboxedModule = require('sandboxed-module')
  18. describe('ContactController', function () {
  19. beforeEach(function () {
  20. this.AuthenticationController = { getLoggedInUserId: sinon.stub() }
  21. this.ContactController = SandboxedModule.require(modulePath, {
  22. requires: {
  23. '../User/UserGetter': (this.UserGetter = {}),
  24. './ContactManager': (this.ContactManager = {}),
  25. '../Authentication/AuthenticationController': (this.AuthenticationController = {}),
  26. '../../infrastructure/Modules': (this.Modules = { hooks: {} }),
  27. '../Authentication/AuthenticationController': this
  28. .AuthenticationController,
  29. },
  30. })
  31. this.next = sinon.stub()
  32. this.req = {}
  33. this.res = {}
  34. this.res.status = sinon.stub().returns(this.req)
  35. return (this.res.send = sinon.stub())
  36. })
  37. describe('getContacts', function () {
  38. beforeEach(function () {
  39. this.user_id = 'mock-user-id'
  40. this.contact_ids = ['contact-1', 'contact-2', 'contact-3']
  41. this.contacts = [
  42. {
  43. _id: 'contact-1',
  44. email: 'joe@example.com',
  45. first_name: 'Joe',
  46. last_name: 'Example',
  47. unsued: 'foo',
  48. },
  49. {
  50. _id: 'contact-2',
  51. email: 'jane@example.com',
  52. first_name: 'Jane',
  53. last_name: 'Example',
  54. unsued: 'foo',
  55. holdingAccount: true,
  56. },
  57. {
  58. _id: 'contact-3',
  59. email: 'jim@example.com',
  60. first_name: 'Jim',
  61. last_name: 'Example',
  62. unsued: 'foo',
  63. },
  64. ]
  65. this.AuthenticationController.getLoggedInUserId = sinon
  66. .stub()
  67. .returns(this.user_id)
  68. this.ContactManager.getContactIds = sinon
  69. .stub()
  70. .callsArgWith(2, null, this.contact_ids)
  71. this.UserGetter.getUsers = sinon
  72. .stub()
  73. .callsArgWith(2, null, this.contacts)
  74. this.Modules.hooks.fire = sinon.stub().callsArg(3)
  75. return this.ContactController.getContacts(this.req, this.res, this.next)
  76. })
  77. it('should look up the logged in user id', function () {
  78. return this.AuthenticationController.getLoggedInUserId
  79. .calledWith(this.req)
  80. .should.equal(true)
  81. })
  82. it('should get the users contact ids', function () {
  83. return this.ContactManager.getContactIds
  84. .calledWith(this.user_id, { limit: 50 })
  85. .should.equal(true)
  86. })
  87. it('should populate the users contacts ids', function () {
  88. return this.UserGetter.getUsers
  89. .calledWith(this.contact_ids, {
  90. email: 1,
  91. first_name: 1,
  92. last_name: 1,
  93. holdingAccount: 1,
  94. })
  95. .should.equal(true)
  96. })
  97. it('should fire the getContact module hook', function () {
  98. return this.Modules.hooks.fire
  99. .calledWith('getContacts', this.user_id)
  100. .should.equal(true)
  101. })
  102. it('should return a formatted list of contacts in contact list order, without holding accounts', function () {
  103. return this.res.send.args[0][0].contacts.should.deep.equal([
  104. {
  105. id: 'contact-1',
  106. email: 'joe@example.com',
  107. first_name: 'Joe',
  108. last_name: 'Example',
  109. type: 'user',
  110. },
  111. {
  112. id: 'contact-3',
  113. email: 'jim@example.com',
  114. first_name: 'Jim',
  115. last_name: 'Example',
  116. type: 'user',
  117. },
  118. ])
  119. })
  120. })
  121. })