| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- import { vi, expect } from 'vitest'
- import sinon from 'sinon'
- const MODULE_PATH =
- '../../../../app/src/Features/User/UserPostRegistrationAnalyticsManager'
- describe('UserPostRegistrationAnalyticsManager', function () {
- beforeEach(async function (ctx) {
- ctx.fakeUserId = '123abc'
- ctx.Queues = {
- createScheduledJob: sinon.stub().resolves(),
- }
- ctx.UserGetter = {
- promises: {
- getUser: sinon.stub().resolves(),
- },
- }
- ctx.fakeUser = { _id: ctx.fakeUserId, analyticsId: 'uuid' }
- ctx.UserGetter.promises.getUser
- .withArgs({ _id: ctx.fakeUserId })
- .resolves(ctx.fakeUser)
- ctx.InstitutionsAPI = {
- promises: {
- getUserAffiliations: sinon.stub().resolves([]),
- },
- }
- ctx.AnalyticsManager = {
- setUserPropertyForMongoUser: sinon.stub().resolves(),
- }
- vi.doMock('../../../../app/src/infrastructure/Queues', () => ({
- default: ctx.Queues,
- }))
- vi.doMock('../../../../app/src/Features/User/UserGetter', () => ({
- default: ctx.UserGetter,
- }))
- vi.doMock(
- '../../../../app/src/Features/Institutions/InstitutionsAPI',
- () => ({
- default: ctx.InstitutionsAPI,
- })
- )
- vi.doMock(
- '../../../../app/src/Features/Analytics/AnalyticsManager',
- () => ({
- default: ctx.AnalyticsManager,
- })
- )
- ctx.UserPostRegistrationAnalyticsManager = (
- await import(MODULE_PATH)
- ).default
- })
- describe('schedulePostRegistrationAnalytics', function () {
- it('should schedule delayed job on queue', async function (ctx) {
- await ctx.UserPostRegistrationAnalyticsManager.schedulePostRegistrationAnalytics(
- {
- _id: ctx.fakeUserId,
- }
- )
- sinon.assert.calledWith(
- ctx.Queues.createScheduledJob,
- 'post-registration-analytics',
- { data: { userId: ctx.fakeUserId } },
- 24 * 60 * 60 * 1000
- )
- })
- })
- describe('postRegistrationAnalytics', function () {
- it('stops without errors if user is not found', async function (ctx) {
- await ctx.UserPostRegistrationAnalyticsManager.postRegistrationAnalytics(
- 'not-a-user'
- )
- expect(ctx.InstitutionsAPI.promises.getUserAffiliations).not.to.have.been
- .called
- expect(ctx.AnalyticsManager.setUserPropertyForMongoUser).not.to.have.been
- .called
- })
- it('sets user property if user has commons account affiliationd', async function (ctx) {
- ctx.InstitutionsAPI.promises.getUserAffiliations.resolves([
- {},
- {
- institution: {
- commonsAccount: true,
- },
- },
- {
- institution: {
- commonsAccount: false,
- },
- },
- ])
- await ctx.UserPostRegistrationAnalyticsManager.postRegistrationAnalytics(
- ctx.fakeUserId
- )
- expect(
- ctx.AnalyticsManager.setUserPropertyForMongoUser
- ).to.have.been.calledWith(
- ctx.fakeUser,
- 'registered-from-commons-account',
- true
- )
- })
- it('does not set user property if user has no commons account affiliation', async function (ctx) {
- ctx.InstitutionsAPI.promises.getUserAffiliations.resolves([
- {
- institution: {
- commonsAccount: false,
- },
- },
- ])
- await ctx.UserPostRegistrationAnalyticsManager.postRegistrationAnalytics(
- ctx.fakeUserId
- )
- expect(ctx.AnalyticsManager.setUserPropertyForMongoUser).not.to.have.been
- .called
- })
- })
- })
|