ContactControllerTests.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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.SessionManager = { getLoggedInUserId: sinon.stub() }
  21. this.ContactController = SandboxedModule.require(modulePath, {
  22. requires: {
  23. '../User/UserGetter': (this.UserGetter = {}),
  24. './ContactManager': (this.ContactManager = {}),
  25. '../Authentication/SessionManager': (this.SessionManager = {}),
  26. '../../infrastructure/Modules': (this.Modules = { hooks: {} }),
  27. },
  28. })
  29. this.next = sinon.stub()
  30. this.req = {}
  31. this.res = {}
  32. this.res.status = sinon.stub().returns(this.req)
  33. return (this.res.send = sinon.stub())
  34. })
  35. describe('getContacts', function () {
  36. beforeEach(function () {
  37. this.user_id = 'mock-user-id'
  38. this.contact_ids = ['contact-1', 'contact-2', 'contact-3']
  39. this.contacts = [
  40. {
  41. _id: 'contact-1',
  42. email: 'joe@example.com',
  43. first_name: 'Joe',
  44. last_name: 'Example',
  45. unsued: 'foo',
  46. },
  47. {
  48. _id: 'contact-2',
  49. email: 'jane@example.com',
  50. first_name: 'Jane',
  51. last_name: 'Example',
  52. unsued: 'foo',
  53. holdingAccount: true,
  54. },
  55. {
  56. _id: 'contact-3',
  57. email: 'jim@example.com',
  58. first_name: 'Jim',
  59. last_name: 'Example',
  60. unsued: 'foo',
  61. },
  62. ]
  63. this.SessionManager.getLoggedInUserId = sinon.stub().returns(this.user_id)
  64. this.ContactManager.getContactIds = sinon
  65. .stub()
  66. .callsArgWith(2, null, this.contact_ids)
  67. this.UserGetter.getUsers = sinon
  68. .stub()
  69. .callsArgWith(2, null, this.contacts)
  70. this.Modules.hooks.fire = sinon.stub().callsArg(3)
  71. return this.ContactController.getContacts(this.req, this.res, this.next)
  72. })
  73. it('should look up the logged in user id', function () {
  74. return this.SessionManager.getLoggedInUserId
  75. .calledWith(this.req.session)
  76. .should.equal(true)
  77. })
  78. it('should get the users contact ids', function () {
  79. return this.ContactManager.getContactIds
  80. .calledWith(this.user_id, { limit: 50 })
  81. .should.equal(true)
  82. })
  83. it('should populate the users contacts ids', function () {
  84. return this.UserGetter.getUsers
  85. .calledWith(this.contact_ids, {
  86. email: 1,
  87. first_name: 1,
  88. last_name: 1,
  89. holdingAccount: 1,
  90. })
  91. .should.equal(true)
  92. })
  93. it('should fire the getContact module hook', function () {
  94. return this.Modules.hooks.fire
  95. .calledWith('getContacts', this.user_id)
  96. .should.equal(true)
  97. })
  98. it('should return a formatted list of contacts in contact list order, without holding accounts', function () {
  99. return this.res.send.args[0][0].contacts.should.deep.equal([
  100. {
  101. id: 'contact-1',
  102. email: 'joe@example.com',
  103. first_name: 'Joe',
  104. last_name: 'Example',
  105. type: 'user',
  106. },
  107. {
  108. id: 'contact-3',
  109. email: 'jim@example.com',
  110. first_name: 'Jim',
  111. last_name: 'Example',
  112. type: 'user',
  113. },
  114. ])
  115. })
  116. })
  117. })