UserPostRegistrationAnalyticsManager.test.mjs 3.4 KB

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