Skip to content

Commit

Permalink
fix: features tests
Browse files Browse the repository at this point in the history
  • Loading branch information
moughxyz committed Jul 11, 2023
1 parent d4e2979 commit bc52e05
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 33 deletions.
34 changes: 4 additions & 30 deletions packages/snjs/lib/Services/Features/FeaturesService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import { SettingName } from '@standardnotes/settings'
import { SNFeaturesService } from '@Lib/Services/Features'
import { ContentType } from '@standardnotes/common'
import { RoleName } from '@standardnotes/domain-core'
import { FeatureDescription, FeatureIdentifier, GetFeatures } from '@standardnotes/features'
import { FeatureIdentifier, GetFeatures } from '@standardnotes/features'
import { SNWebSocketsService } from '../Api/WebsocketsService'
import { SNSettingsService } from '../Settings'
import { PureCryptoInterface } from '@standardnotes/sncrypto-common'
import { convertTimestampToMilliseconds } from '@standardnotes/utils'
import {
AlertService,
ApiServiceInterface,
Expand Down Expand Up @@ -44,11 +43,7 @@ describe('FeaturesService', () => {
let sessionManager: SessionsClientInterface
let crypto: PureCryptoInterface
let roles: string[]
let features: FeatureDescription[]
let items: ItemInterface[]
let now: Date
let tomorrow_server: number
let tomorrow_client: number
let internalEventBus: InternalEventBusInterface

const createService = () => {
Expand All @@ -72,21 +67,6 @@ describe('FeaturesService', () => {
beforeEach(() => {
roles = [RoleName.NAMES.CoreUser, RoleName.NAMES.PlusUser]

now = new Date()
tomorrow_client = now.setDate(now.getDate() + 1)
tomorrow_server = convertTimestampToMilliseconds(tomorrow_client * 1_000)

features = [
{
...GetFeatures().find((f) => f.identifier === FeatureIdentifier.MidnightTheme),
expires_at: tomorrow_server,
},
{
...GetFeatures().find((f) => f.identifier === FeatureIdentifier.PlusEditor),
expires_at: tomorrow_server,
},
] as jest.Mocked<FeatureDescription[]>

items = [] as jest.Mocked<ItemInterface[]>

storageService = {} as jest.Mocked<DiskStorageService>
Expand All @@ -95,9 +75,6 @@ describe('FeaturesService', () => {

apiService = {} as jest.Mocked<SNApiService>
apiService.addEventObserver = jest.fn()
apiService.downloadOfflineFeaturesFromRepo = jest.fn().mockReturnValue({
features,
})
apiService.isThirdPartyHostUsed = jest.fn().mockReturnValue(false)

itemManager = {} as jest.Mocked<ItemManager>
Expand Down Expand Up @@ -211,18 +188,17 @@ describe('FeaturesService', () => {
expect(triggeredEvents).not.toContain(FeaturesEvent.DidPurchaseSubscription)
})

it('saves new roles to storage and fetches features if a role has been added', async () => {
const newRoles = [...roles, RoleName.NAMES.PlusUser]

it('saves new roles to storage if a role has been added', async () => {
storageService.getValue = jest.fn().mockReturnValue(roles)
const featuresService = createService()
featuresService.initializeFromDisk()

const newRoles = [...roles, RoleName.NAMES.ProUser]
await featuresService.updateOnlineRolesWithNewValues(newRoles)
expect(storageService.setValue).toHaveBeenCalledWith(StorageKey.UserRoles, newRoles)
})

it('saves new roles to storage and fetches features if a role has been removed', async () => {
it('saves new roles to storage if a role has been removed', async () => {
const newRoles = [RoleName.NAMES.CoreUser]

storageService.getValue = jest.fn().mockReturnValue(roles)
Expand All @@ -236,8 +212,6 @@ describe('FeaturesService', () => {
it('role-based feature status', async () => {
const featuresService = createService()

features = [] as jest.Mocked<FeatureDescription[]>

sessionManager.isSignedIntoFirstPartyServer = jest.fn().mockReturnValue(true)

await featuresService.updateOnlineRolesWithNewValues([RoleName.NAMES.CoreUser, RoleName.NAMES.PlusUser])
Expand Down
5 changes: 2 additions & 3 deletions packages/snjs/lib/Services/Features/FeaturesService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,15 +324,14 @@ export class SNFeaturesService
const userRolesChanged =
roles.some((role) => !this.onlineRoles.includes(role)) || this.onlineRoles.some((role) => !roles.includes(role))

const isInitialLoadRolesChange = previousRoles.length === 0 && userRolesChanged

if (!userRolesChanged) {
return
}

this.setOnlineRoles(roles)

if (userRolesChanged && !isInitialLoadRolesChange) {
const isInitialLoadRolesChange = previousRoles.length === 0
if (!isInitialLoadRolesChange) {
if (this.onlineRolesIncludePaidSubscription()) {
await this.notifyEvent(FeaturesEvent.DidPurchaseSubscription)
}
Expand Down

0 comments on commit bc52e05

Please sign in to comment.