ContactController.test.mjs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import { expect, vi } from 'vitest'
  2. import sinon from 'sinon'
  3. import MockResponse from '../helpers/MockResponse.js'
  4. const modulePath = '../../../../app/src/Features/Contacts/ContactController.mjs'
  5. describe('ContactController', function () {
  6. beforeEach(async function (ctx) {
  7. ctx.SessionManager = { getLoggedInUserId: sinon.stub() }
  8. vi.doMock('../../../../app/src/Features/User/UserGetter', () => ({
  9. default: (ctx.UserGetter = {
  10. promises: {},
  11. }),
  12. }))
  13. vi.doMock('../../../../app/src/Features/Contacts/ContactManager', () => ({
  14. default: (ctx.ContactManager = { promises: {} }),
  15. }))
  16. vi.doMock(
  17. '../../../../app/src/Features/Authentication/SessionManager',
  18. () => ({
  19. default: (ctx.SessionManager = {}),
  20. })
  21. )
  22. vi.doMock('../../../../app/src/infrastructure/Modules', () => ({
  23. default: (ctx.Modules = {
  24. promises: { hooks: {} },
  25. }),
  26. }))
  27. ctx.ContactController = (await import(modulePath)).default
  28. ctx.req = {}
  29. ctx.res = new MockResponse()
  30. })
  31. describe('getContacts', function () {
  32. beforeEach(function (ctx) {
  33. ctx.user_id = 'mock-user-id'
  34. ctx.contact_ids = ['contact-1', 'contact-2', 'contact-3']
  35. ctx.contacts = [
  36. {
  37. _id: 'contact-1',
  38. email: 'joe@example.com',
  39. first_name: 'Joe',
  40. last_name: 'Example',
  41. unsued: 'foo',
  42. },
  43. {
  44. _id: 'contact-2',
  45. email: 'jane@example.com',
  46. first_name: 'Jane',
  47. last_name: 'Example',
  48. unsued: 'foo',
  49. holdingAccount: true,
  50. },
  51. {
  52. _id: 'contact-3',
  53. email: 'jim@example.com',
  54. first_name: 'Jim',
  55. last_name: 'Example',
  56. unsued: 'foo',
  57. },
  58. ]
  59. ctx.SessionManager.getLoggedInUserId = sinon.stub().returns(ctx.user_id)
  60. ctx.ContactManager.promises.getContactIds = sinon
  61. .stub()
  62. .resolves(ctx.contact_ids)
  63. ctx.UserGetter.promises.getUsers = sinon.stub().resolves(ctx.contacts)
  64. ctx.Modules.promises.hooks.fire = sinon.stub()
  65. })
  66. it('should look up the logged in user id', async function (ctx) {
  67. ctx.ContactController.getContacts(ctx.req, ctx.res)
  68. ctx.SessionManager.getLoggedInUserId
  69. .calledWith(ctx.req.session)
  70. .should.equal(true)
  71. })
  72. it('should get the users contact ids', async function (ctx) {
  73. ctx.res.callback = () => {
  74. expect(
  75. ctx.ContactManager.promises.getContactIds
  76. ).to.have.been.calledWith(ctx.user_id, { limit: 50 })
  77. }
  78. ctx.ContactController.getContacts(ctx.req, ctx.res)
  79. })
  80. it('should populate the users contacts ids', function (ctx) {
  81. return new Promise(resolve => {
  82. ctx.res.callback = () => {
  83. expect(ctx.UserGetter.promises.getUsers).to.have.been.calledWith(
  84. ctx.contact_ids,
  85. {
  86. email: 1,
  87. first_name: 1,
  88. last_name: 1,
  89. holdingAccount: 1,
  90. }
  91. )
  92. resolve()
  93. }
  94. ctx.ContactController.getContacts(ctx.req, ctx.res, resolve)
  95. })
  96. })
  97. it('should fire the getContact module hook', function (ctx) {
  98. return new Promise(resolve => {
  99. ctx.res.callback = () => {
  100. expect(ctx.Modules.promises.hooks.fire).to.have.been.calledWith(
  101. 'getContacts',
  102. ctx.user_id
  103. )
  104. resolve()
  105. }
  106. ctx.ContactController.getContacts(ctx.req, ctx.res, resolve)
  107. })
  108. })
  109. it('should return a formatted list of contacts in contact list order, without holding accounts', function (ctx) {
  110. return new Promise(resolve => {
  111. ctx.res.callback = () => {
  112. ctx.res.json.args[0][0].contacts.should.deep.equal([
  113. {
  114. id: 'contact-1',
  115. email: 'joe@example.com',
  116. first_name: 'Joe',
  117. last_name: 'Example',
  118. type: 'user',
  119. },
  120. {
  121. id: 'contact-3',
  122. email: 'jim@example.com',
  123. first_name: 'Jim',
  124. last_name: 'Example',
  125. type: 'user',
  126. },
  127. ])
  128. resolve()
  129. }
  130. ctx.ContactController.getContacts(ctx.req, ctx.res, resolve)
  131. })
  132. })
  133. })
  134. })