chore: fix and add test for role change event

This commit is contained in:
Aman Harwara
2023-09-06 15:06:02 +05:30
parent 3290f76e97
commit 9ad779c89a
2 changed files with 20 additions and 6 deletions

View File

@@ -259,6 +259,20 @@ describe('FeaturesService', () => {
expect(triggeredEvents).not.toContain(FeaturesEvent.DidPurchaseSubscription) expect(triggeredEvents).not.toContain(FeaturesEvent.DidPurchaseSubscription)
}) })
it('should not notify of subscription purchase if new roles are not paid', async () => {
storageService.getValue = jest.fn().mockReturnValue(roles)
featureService.initializeFromDisk()
const spy = jest.spyOn(featureService, 'notifyEvent' as never)
const newRoles = [...roles, 'TRANSITION_USER']
await featureService.updateOnlineRolesWithNewValues(newRoles)
const triggeredEvents = spy.mock.calls.map((call) => call[0])
expect(triggeredEvents).not.toContain(FeaturesEvent.DidPurchaseSubscription)
})
it('saves new roles to storage if a role has been added', async () => { it('saves new roles to storage if a role has been added', async () => {
storageService.getValue = jest.fn().mockReturnValue(roles) storageService.getValue = jest.fn().mockReturnValue(roles)

View File

@@ -360,8 +360,10 @@ export class FeaturesService
const isInitialLoadRolesChange = previousRoles.length === 0 const isInitialLoadRolesChange = previousRoles.length === 0
if (!isInitialLoadRolesChange) { if (!isInitialLoadRolesChange) {
const newRolesIncludePaidSubscription = this.rolesIncludePaidSubscription(roles) const changedRoles = roles.filter((role) => !previousRoles.includes(role))
if (newRolesIncludePaidSubscription) { const changedRolesIncludePaidSubscription = this.rolesIncludePaidSubscription(changedRoles)
if (changedRolesIncludePaidSubscription) {
await this.notifyEvent(FeaturesEvent.DidPurchaseSubscription) await this.notifyEvent(FeaturesEvent.DidPurchaseSubscription)
} }
} }
@@ -397,11 +399,9 @@ export class FeaturesService
} }
private rolesIncludePaidSubscription(roles: string[]) { private rolesIncludePaidSubscription(roles: string[]) {
{ const paidRoles = [RoleName.NAMES.PlusUser, RoleName.NAMES.ProUser]
const unpaidRoles = [RoleName.NAMES.CoreUser]
return roles.some((role) => !unpaidRoles.includes(role)) return roles.some((role) => paidRoles.includes(role))
}
} }
onlineRolesIncludePaidSubscription(): boolean { onlineRolesIncludePaidSubscription(): boolean {