UserPostRegistrationAnalyticsManager.test.mjs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import { vi, expect } from 'vitest'
  2. import sinon from 'sinon'
  3. const MODULE_PATH =
  4. '../../../../app/src/Features/User/UserPostRegistrationAnalyticsManager'
  5. describe('UserPostRegistrationAnalyticsManager', function () {
  6. beforeEach(async function (ctx) {
  7. ctx.fakeUserId = '123abc'
  8. ctx.Queues = {
  9. createScheduledJob: sinon.stub().resolves(),
  10. }
  11. ctx.UserGetter = {
  12. promises: {
  13. getUser: sinon.stub().resolves(),
  14. },
  15. }
  16. ctx.fakeUser = { _id: ctx.fakeUserId, analyticsId: 'uuid' }
  17. ctx.UserGetter.promises.getUser
  18. .withArgs({ _id: ctx.fakeUserId })
  19. .resolves(ctx.fakeUser)
  20. ctx.InstitutionsAPI = {
  21. promises: {
  22. getUserAffiliations: sinon.stub().resolves([]),
  23. },
  24. }
  25. ctx.AnalyticsManager = {
  26. setUserPropertyForMongoUser: sinon.stub().resolves(),
  27. }
  28. vi.doMock('../../../../app/src/infrastructure/Queues', () => ({
  29. default: ctx.Queues,
  30. }))
  31. vi.doMock('../../../../app/src/Features/User/UserGetter', () => ({
  32. default: ctx.UserGetter,
  33. }))
  34. vi.doMock(
  35. '../../../../app/src/Features/Institutions/InstitutionsAPI',
  36. () => ({
  37. default: ctx.InstitutionsAPI,
  38. })
  39. )
  40. vi.doMock(
  41. '../../../../app/src/Features/Analytics/AnalyticsManager',
  42. () => ({
  43. default: ctx.AnalyticsManager,
  44. })
  45. )
  46. ctx.UserPostRegistrationAnalyticsManager = (
  47. await import(MODULE_PATH)
  48. ).default
  49. })
  50. describe('schedulePostRegistrationAnalytics', function () {
  51. it('should schedule delayed job on queue', async function (ctx) {
  52. await ctx.UserPostRegistrationAnalyticsManager.schedulePostRegistrationAnalytics(
  53. {
  54. _id: ctx.fakeUserId,
  55. }
  56. )
  57. sinon.assert.calledWith(
  58. ctx.Queues.createScheduledJob,
  59. 'post-registration-analytics',
  60. { data: { userId: ctx.fakeUserId } },
  61. 24 * 60 * 60 * 1000
  62. )
  63. })
  64. })
  65. describe('postRegistrationAnalytics', function () {
  66. it('stops without errors if user is not found', async function (ctx) {
  67. await ctx.UserPostRegistrationAnalyticsManager.postRegistrationAnalytics(
  68. 'not-a-user'
  69. )
  70. expect(ctx.InstitutionsAPI.promises.getUserAffiliations).not.to.have.been
  71. .called
  72. expect(ctx.AnalyticsManager.setUserPropertyForMongoUser).not.to.have.been
  73. .called
  74. })
  75. it('sets user property if user has commons account affiliationd', async function (ctx) {
  76. ctx.InstitutionsAPI.promises.getUserAffiliations.resolves([
  77. {},
  78. {
  79. institution: {
  80. commonsAccount: true,
  81. },
  82. },
  83. {
  84. institution: {
  85. commonsAccount: false,
  86. },
  87. },
  88. ])
  89. await ctx.UserPostRegistrationAnalyticsManager.postRegistrationAnalytics(
  90. ctx.fakeUserId
  91. )
  92. expect(
  93. ctx.AnalyticsManager.setUserPropertyForMongoUser
  94. ).to.have.been.calledWith(
  95. ctx.fakeUser,
  96. 'registered-from-commons-account',
  97. true
  98. )
  99. })
  100. it('does not set user property if user has no commons account affiliation', async function (ctx) {
  101. ctx.InstitutionsAPI.promises.getUserAffiliations.resolves([
  102. {
  103. institution: {
  104. commonsAccount: false,
  105. },
  106. },
  107. ])
  108. await ctx.UserPostRegistrationAnalyticsManager.postRegistrationAnalytics(
  109. ctx.fakeUserId
  110. )
  111. expect(ctx.AnalyticsManager.setUserPropertyForMongoUser).not.to.have.been
  112. .called
  113. })
  114. })
  115. })