Files
overleaf-cep/services/web/test/unit/src/User/UserPostRegistrationAnalyticsManager.test.mjs
Antoine Clausse 3a1f3af6a4 [web] Migrate some User and UserMembership files to ESM (#29181)
* Rename files to mjs

* Migrate files to ESM

* Fix imports

* Misc. fixes: Fixup InsititutionsAPI import, ObjectId import, ...

* Rename test files to mjs

* Convert test files to ESM

* Fix tests

* Update UserMembershipErrors imports

* Convert some tests: sinon -> vitest

* Fixup UserMembershipHandler.test.mjs

* Convert UserMembershipErrors to ESM

GitOrigin-RevId: 05d34c7e112a567f9c59398740ae0830ef93d32f
2025-10-21 08:06:30 +00:00

126 lines
3.4 KiB
JavaScript

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.UserGetter.promises.getUser
.withArgs({ _id: ctx.fakeUserId })
.resolves({ _id: ctx.fakeUserId })
ctx.InstitutionsAPI = {
promises: {
getUserAffiliations: sinon.stub().resolves([]),
},
}
ctx.AnalyticsManager = {
setUserPropertyForUser: 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.setUserPropertyForUser).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.setUserPropertyForUser
).to.have.been.calledWith(
ctx.fakeUserId,
'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.setUserPropertyForUser).not.to.have.been
.called
})
})
})