AccountMappingHelperTests.mjs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. import path from 'node:path'
  2. import esmock from 'esmock'
  3. import { expect } from 'chai'
  4. import mongodb from 'mongodb-legacy'
  5. import { fileURLToPath } from 'node:url'
  6. const { ObjectId } = mongodb
  7. const __dirname = fileURLToPath(new URL('.', import.meta.url))
  8. const MODULE_PATH = path.join(
  9. __dirname,
  10. '../../../../app/src/Features/Analytics/AccountMappingHelper'
  11. )
  12. describe('AccountMappingHelper', function () {
  13. beforeEach(async function () {
  14. this.AccountMappingHelper = await esmock.strict(MODULE_PATH)
  15. })
  16. describe('extractAccountMappingsFromSubscription', function () {
  17. describe('when the v1 id is the same in the updated subscription and the subscription', function () {
  18. describe('when the salesforce id is the same in the updated subscription and the subscription', function () {
  19. beforeEach(function () {
  20. this.subscription = {
  21. id: new ObjectId('abc123abc123abc123abc123'),
  22. salesforce_id: 'def456def456def456',
  23. }
  24. this.updatedSubscription = { salesforce_id: 'def456def456def456' }
  25. this.result =
  26. this.AccountMappingHelper.extractAccountMappingsFromSubscription(
  27. this.subscription,
  28. this.updatedSubscription
  29. )
  30. })
  31. it('returns an empty array', function () {
  32. expect(this.result).to.be.an('array')
  33. expect(this.result).to.have.length(0)
  34. })
  35. })
  36. describe('when the salesforce id has changed between the subscription and the updated subscription', function () {
  37. beforeEach(function () {
  38. this.subscription = {
  39. id: new ObjectId('abc123abc123abc123abc123'),
  40. salesforce_id: 'def456def456def456',
  41. }
  42. this.updatedSubscription = { salesforce_id: 'ghi789ghi789ghi789' }
  43. this.result =
  44. this.AccountMappingHelper.extractAccountMappingsFromSubscription(
  45. this.subscription,
  46. this.updatedSubscription
  47. )
  48. })
  49. it('returns an array with a single item', function () {
  50. expect(this.result).to.be.an('array')
  51. expect(this.result).to.have.length(1)
  52. })
  53. it('uses "account" as sourceEntity', function () {
  54. expect(this.result[0]).to.haveOwnProperty('sourceEntity', 'account')
  55. })
  56. it('uses the salesforceId from the updated subscription as sourceEntityId', function () {
  57. expect(this.result[0]).to.haveOwnProperty(
  58. 'sourceEntityId',
  59. this.updatedSubscription.salesforce_id
  60. )
  61. })
  62. it('uses "subscription" as targetEntity', function () {
  63. expect(this.result[0]).to.haveOwnProperty(
  64. 'targetEntity',
  65. 'subscription'
  66. )
  67. })
  68. it('uses the subscriptionId as targetEntityId', function () {
  69. expect(this.result[0]).to.haveOwnProperty(
  70. 'targetEntityId',
  71. this.subscription.id
  72. )
  73. })
  74. })
  75. describe('when the update subscription has a salesforce id and the subscription has no salesforce_id', function () {
  76. beforeEach(function () {
  77. this.subscription = { id: new ObjectId('abc123abc123abc123abc123') }
  78. this.updatedSubscription = { salesforce_id: 'def456def456def456' }
  79. this.result =
  80. this.AccountMappingHelper.extractAccountMappingsFromSubscription(
  81. this.subscription,
  82. this.updatedSubscription
  83. )
  84. })
  85. it('returns an array with a single item', function () {
  86. expect(this.result).to.be.an('array')
  87. expect(this.result).to.have.length(1)
  88. })
  89. it('uses "account" as sourceEntity', function () {
  90. expect(this.result[0]).to.haveOwnProperty('sourceEntity', 'account')
  91. })
  92. it('uses the salesforceId from the updated subscription as sourceEntityId', function () {
  93. expect(this.result[0]).to.haveOwnProperty(
  94. 'sourceEntityId',
  95. this.updatedSubscription.salesforce_id
  96. )
  97. })
  98. it('uses "subscription" as targetEntity', function () {
  99. expect(this.result[0]).to.haveOwnProperty(
  100. 'targetEntity',
  101. 'subscription'
  102. )
  103. })
  104. it('uses the subscriptionId as targetEntityId', function () {
  105. expect(this.result[0]).to.haveOwnProperty(
  106. 'targetEntityId',
  107. this.subscription.id
  108. )
  109. })
  110. })
  111. })
  112. })
  113. describe('when the v1 id has changed between the subscription and the updated subscription', function () {
  114. describe('when the salesforce id has not changed between the subscription and the updated subscription', function () {
  115. beforeEach(function () {
  116. this.subscription = {
  117. id: new ObjectId('abc123abc123abc123abc123'),
  118. v1_id: '1',
  119. salesforce_id: '',
  120. }
  121. this.updatedSubscription = { v1_id: '2', salesforce_id: '' }
  122. this.result =
  123. this.AccountMappingHelper.extractAccountMappingsFromSubscription(
  124. this.subscription,
  125. this.updatedSubscription
  126. )
  127. })
  128. it('returns an array with a single item', function () {
  129. expect(this.result).to.be.an('array')
  130. expect(this.result).to.have.length(1)
  131. })
  132. it('uses "university" as the sourceEntity', function () {
  133. expect(this.result[0]).to.haveOwnProperty('sourceEntity', 'university')
  134. })
  135. it('uses the v1_id from the updated subscription as the sourceEntityId', function () {
  136. expect(this.result[0]).to.haveOwnProperty(
  137. 'sourceEntityId',
  138. this.updatedSubscription.v1_id
  139. )
  140. })
  141. it('uses "subscription" as the targetEntity', function () {
  142. expect(this.result[0]).to.haveOwnProperty(
  143. 'targetEntity',
  144. 'subscription'
  145. )
  146. })
  147. it('uses the subscription id as the targetEntityId', function () {
  148. expect(this.result[0]).to.haveOwnProperty(
  149. 'targetEntityId',
  150. this.subscription.id
  151. )
  152. })
  153. })
  154. describe('when the salesforce id has changed between the subscription and the updated subscription', function () {
  155. beforeEach(function () {
  156. this.subscription = {
  157. id: new ObjectId('abc123abc123abc123abc123'),
  158. v1_id: '',
  159. salesforce_id: 'def456def456def456',
  160. }
  161. this.updatedSubscription = {
  162. v1_id: '2',
  163. salesforce_id: '',
  164. }
  165. this.result =
  166. this.AccountMappingHelper.extractAccountMappingsFromSubscription(
  167. this.subscription,
  168. this.updatedSubscription
  169. )
  170. })
  171. it('returns an array with two items', function () {
  172. expect(this.result).to.be.an('array')
  173. expect(this.result).to.have.length(2)
  174. })
  175. it('uses the salesforce_id from the updated subscription as the sourceEntityId for the first item', function () {
  176. expect(this.result[0]).to.haveOwnProperty(
  177. 'sourceEntityId',
  178. this.updatedSubscription.salesforce_id
  179. )
  180. })
  181. it('uses the subscription id as the targetEntityId for the first item', function () {
  182. expect(this.result[0]).to.haveOwnProperty(
  183. 'targetEntityId',
  184. this.subscription.id
  185. )
  186. })
  187. it('uses the v1_id from the updated subscription as the sourceEntityId for the second item', function () {
  188. expect(this.result[1]).to.haveOwnProperty(
  189. 'sourceEntityId',
  190. this.updatedSubscription.v1_id
  191. )
  192. })
  193. it('uses the subscription id as the targetEntityId for the second item', function () {
  194. expect(this.result[1]).to.haveOwnProperty(
  195. 'targetEntityId',
  196. this.subscription.id
  197. )
  198. })
  199. })
  200. })
  201. })