ContactController.test.mjs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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', async function (ctx) {
  81. await new Promise((resolve, reject) => {
  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(
  95. ctx.req,
  96. ctx.res,
  97. ctx.rejectOnError(reject)
  98. )
  99. })
  100. })
  101. it('should fire the getContact module hook', async function (ctx) {
  102. await new Promise((resolve, reject) => {
  103. ctx.res.callback = () => {
  104. expect(ctx.Modules.promises.hooks.fire).to.have.been.calledWith(
  105. 'getContacts',
  106. ctx.user_id
  107. )
  108. resolve()
  109. }
  110. ctx.ContactController.getContacts(
  111. ctx.req,
  112. ctx.res,
  113. ctx.rejectOnError(reject)
  114. )
  115. })
  116. })
  117. it('should return a formatted list of contacts in contact list order, without holding accounts', async function (ctx) {
  118. await new Promise((resolve, reject) => {
  119. ctx.res.callback = () => {
  120. ctx.res.json.args[0][0].contacts.should.deep.equal([
  121. {
  122. id: 'contact-1',
  123. email: 'joe@example.com',
  124. first_name: 'Joe',
  125. last_name: 'Example',
  126. type: 'user',
  127. },
  128. {
  129. id: 'contact-3',
  130. email: 'jim@example.com',
  131. first_name: 'Jim',
  132. last_name: 'Example',
  133. type: 'user',
  134. },
  135. ])
  136. resolve()
  137. }
  138. ctx.ContactController.getContacts(
  139. ctx.req,
  140. ctx.res,
  141. ctx.rejectOnError(reject)
  142. )
  143. })
  144. })
  145. })
  146. })