ContactControllerTests.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. const sinon = require('sinon')
  2. const { expect } = require('chai')
  3. const modulePath = '../../../../app/src/Features/Contacts/ContactController.js'
  4. const SandboxedModule = require('sandboxed-module')
  5. const MockResponse = require('../helpers/MockResponse')
  6. describe('ContactController', function () {
  7. beforeEach(function () {
  8. this.SessionManager = { getLoggedInUserId: sinon.stub() }
  9. this.ContactController = SandboxedModule.require(modulePath, {
  10. requires: {
  11. '../User/UserGetter': (this.UserGetter = { promises: {} }),
  12. './ContactManager': (this.ContactManager = { promises: {} }),
  13. '../Authentication/SessionManager': (this.SessionManager = {}),
  14. '../../infrastructure/Modules': (this.Modules = {
  15. promises: { hooks: {} },
  16. }),
  17. },
  18. })
  19. this.req = {}
  20. this.res = new MockResponse()
  21. })
  22. describe('getContacts', function () {
  23. beforeEach(function () {
  24. this.user_id = 'mock-user-id'
  25. this.contact_ids = ['contact-1', 'contact-2', 'contact-3']
  26. this.contacts = [
  27. {
  28. _id: 'contact-1',
  29. email: 'joe@example.com',
  30. first_name: 'Joe',
  31. last_name: 'Example',
  32. unsued: 'foo',
  33. },
  34. {
  35. _id: 'contact-2',
  36. email: 'jane@example.com',
  37. first_name: 'Jane',
  38. last_name: 'Example',
  39. unsued: 'foo',
  40. holdingAccount: true,
  41. },
  42. {
  43. _id: 'contact-3',
  44. email: 'jim@example.com',
  45. first_name: 'Jim',
  46. last_name: 'Example',
  47. unsued: 'foo',
  48. },
  49. ]
  50. this.SessionManager.getLoggedInUserId = sinon.stub().returns(this.user_id)
  51. this.ContactManager.promises.getContactIds = sinon
  52. .stub()
  53. .resolves(this.contact_ids)
  54. this.UserGetter.promises.getUsers = sinon.stub().resolves(this.contacts)
  55. this.Modules.promises.hooks.fire = sinon.stub()
  56. })
  57. it('should look up the logged in user id', async function () {
  58. this.ContactController.getContacts(this.req, this.res)
  59. this.SessionManager.getLoggedInUserId
  60. .calledWith(this.req.session)
  61. .should.equal(true)
  62. })
  63. it('should get the users contact ids', async function () {
  64. this.res.callback = () => {
  65. expect(
  66. this.ContactManager.promises.getContactIds
  67. ).to.have.been.calledWith(this.user_id, { limit: 50 })
  68. }
  69. this.ContactController.getContacts(this.req, this.res)
  70. })
  71. it('should populate the users contacts ids', function (done) {
  72. this.res.callback = () => {
  73. expect(this.UserGetter.promises.getUsers).to.have.been.calledWith(
  74. this.contact_ids,
  75. {
  76. email: 1,
  77. first_name: 1,
  78. last_name: 1,
  79. holdingAccount: 1,
  80. }
  81. )
  82. done()
  83. }
  84. this.ContactController.getContacts(this.req, this.res, done)
  85. })
  86. it('should fire the getContact module hook', function (done) {
  87. this.res.callback = () => {
  88. expect(this.Modules.promises.hooks.fire).to.have.been.calledWith(
  89. 'getContacts',
  90. this.user_id
  91. )
  92. done()
  93. }
  94. this.ContactController.getContacts(this.req, this.res, done)
  95. })
  96. it('should return a formatted list of contacts in contact list order, without holding accounts', function (done) {
  97. this.res.callback = () => {
  98. 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. done()
  115. }
  116. this.ContactController.getContacts(this.req, this.res, done)
  117. })
  118. })
  119. })