ContactControllerTests.js 3.7 KB

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