Skip to content

Commit

Permalink
test: Fix unit tests to handle IAP
Browse files Browse the repository at this point in the history
  • Loading branch information
Ldoppea committed Jan 8, 2024
1 parent e0ed08d commit 0504206
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 8 deletions.
41 changes: 41 additions & 0 deletions __mocks__/react-native-iap-mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import RNIAP from 'react-native-iap'

export const mockRNIAP: jest.Mocked<typeof RNIAP> = {
clearTransactionIOS: jest.fn(),
requestSubscription: jest.fn(),
isIosStorekit2: jest.fn(),
setup: jest.fn(),
initConnection: jest.fn(),
endConnection: jest.fn(),
flushFailedPurchasesCachedAsPendingAndroid: jest.fn(),
getProducts: jest.fn(),
getSubscriptions: jest.fn(),
getPurchaseHistory: jest.fn(),
getAvailablePurchases: jest.fn(),
requestPurchase: jest.fn(),
finishTransaction: jest.fn(),
deepLinkToSubscriptions: jest.fn(),
purchaseUpdatedListener: jest.fn(),
purchaseErrorListener: jest.fn(),
promotedProductListener: jest.fn(),
transactionListener: jest.fn(),
useIAP: jest.fn(),
useIAPContext: jest.fn(),
withIAPContext: jest.fn(),
validateReceiptAmazon: jest.fn(),
verifyLicense: jest.fn(),
deepLinkToSubscriptionsAmazon: jest.fn(),
getInstallSourceAndroid: jest.fn(),
deepLinkToSubscriptionsAndroid: jest.fn(),
validateReceiptAndroid: jest.fn(),
acknowledgePurchaseAndroid: jest.fn(),
isFeatureSupported: jest.fn(),
getPendingPurchasesIOS: jest.fn(),
getReceiptIOS: jest.fn(),
presentCodeRedemptionSheetIOS: jest.fn(),
getPromotedProductIOS: jest.fn(),
buyPromotedProductIOS: jest.fn(),
validateReceiptIos: jest.fn(),
clearProductsIOS: jest.fn(),
deepLinkToSubscriptionsIos: jest.fn(),
} as unknown as jest.Mocked<typeof RNIAP>
2 changes: 2 additions & 0 deletions __tests__/jestSetupFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import mockRNPermissions from 'react-native-permissions/mock'

import { mockRNBackgroundGeolocation } from '../__mocks__/react-native-background-geolocation-mock'
import { mockRNFS } from '../__mocks__/react-native-fs-mock'
import { mockRNIAP } from '../__mocks__/react-native-iap-mock'

jest.mock('react-native-device-info', () => mockRNDeviceInfo)

Expand All @@ -32,6 +33,7 @@ jest.mock(
)

jest.mock('react-native-fs', () => mockRNFS)
jest.mock('react-native-iap', () => mockRNIAP)

jest.mock('redux-persist', () => {
const real = jest.requireActual('redux-persist')
Expand Down
89 changes: 82 additions & 7 deletions src/app/domain/limits/OauthClientsLimitService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ import type {
} from 'react-native-webview/lib/WebViewTypes'

import CozyClient from 'cozy-client'
import { InstanceInfo } from 'cozy-client/types/types'

import {
clouderyOfferEventHandler,
CLOUDERY_OFFER
} from '/app/domain/iap/services/clouderyOffer'
import {
interceptNavigation,
interceptOpenWindow
Expand Down Expand Up @@ -50,6 +55,11 @@ const mockCurrentRouteName = (currentRouteName: string): void => {
}

describe('interceptNavigation', () => {
let mockClouderyOfferEventHandler: () => void
beforeEach(() => {
mockClouderyOfferEventHandler = jest.fn()
})

it('should do nothing if client is null', () => {
const initialUrl = 'SOME_URL'
const destinationUrl = 'SOME_DESTINATION_URL'
Expand All @@ -58,11 +68,14 @@ describe('interceptNavigation', () => {
const closePopup = jest.fn()
const navigationRequest = mockWebViewNavigationRequest(destinationUrl)

const instanceInfo = getFakeContext()

const allowNavigation = interceptNavigation(
initialUrl,
closePopup,
client,
mockNavigationProp
mockNavigationProp,
instanceInfo
)(navigationRequest)

expect(allowNavigation).toBe(false)
Expand All @@ -79,13 +92,16 @@ describe('interceptNavigation', () => {
const closePopup = jest.fn()
const navigationRequest = mockWebViewNavigationRequest(destinationUrl)

const instanceInfo = getFakeContext()

mockCurrentRouteName(routes.default)

const allowNavigation = interceptNavigation(
initialUrl,
closePopup,
client,
mockNavigationProp
mockNavigationProp,
instanceInfo
)(navigationRequest)

expect(allowNavigation).toBe(false)
Expand All @@ -102,13 +118,16 @@ describe('interceptNavigation', () => {
const closePopup = jest.fn()
const navigationRequest = mockWebViewNavigationRequest(destinationUrl)

const instanceInfo = getFakeContext()

mockCurrentRouteName(routes.default)

const allowNavigation = interceptNavigation(
initialUrl,
closePopup,
client,
mockNavigationProp
mockNavigationProp,
instanceInfo
)(navigationRequest)

expect(allowNavigation).toBe(false)
Expand All @@ -129,13 +148,16 @@ describe('interceptNavigation', () => {
const closePopup = jest.fn()
const navigationRequest = mockWebViewNavigationRequest(destinationUrl)

const instanceInfo = getFakeContext()

mockCurrentRouteName(routes.cozyapp)

const allowNavigation = interceptNavigation(
initialUrl,
closePopup,
client,
mockNavigationProp
mockNavigationProp,
instanceInfo
)(navigationRequest)

expect(allowNavigation).toBe(false)
Expand All @@ -152,13 +174,16 @@ describe('interceptNavigation', () => {
const closePopup = jest.fn()
const navigationRequest = mockWebViewNavigationRequest(destinationUrl)

const instanceInfo = getFakeContext()

mockCurrentRouteName(routes.default)

const allowNavigation = interceptNavigation(
initialUrl,
closePopup,
client,
mockNavigationProp
mockNavigationProp,
instanceInfo
)(navigationRequest)

expect(allowNavigation).toBe(true)
Expand All @@ -175,13 +200,16 @@ describe('interceptNavigation', () => {
const closePopup = jest.fn()
const navigationRequest = mockWebViewNavigationRequest(destinationUrl)

const instanceInfo = getFakeContext()

mockCurrentRouteName(routes.default)

const allowNavigation = interceptNavigation(
initialUrl,
closePopup,
client,
mockNavigationProp
mockNavigationProp,
instanceInfo
)(navigationRequest)

expect(allowNavigation).toBe(true)
Expand All @@ -206,17 +234,49 @@ describe('interceptNavigation', () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any

const instanceInfo = getFakeContext()

const allowNavigation = interceptNavigation(
initialUrl,
closePopup,
client,
mockNavigationProp
mockNavigationProp,
instanceInfo
)(navigationRequest)

expect(allowNavigation).toBe(false)
expect(closePopup).not.toHaveBeenCalled()
expect(navigateToApp).not.toHaveBeenCalled()
})

it('should intercept ClouderyOffer url', () => {
const initialUrl =
'http://claude.mycozy.cloud/settings/clients/limit-exceeded?redirect=http%3A%2F%2Fclaude-drive.mycozy.cloud%2F'
const destinationUrl =
'https://manager.cozycloud.cc/cozy/instances/SOME_UUID/premium'

const client = mockClient()
const closePopup = jest.fn()
const navigationRequest = mockWebViewNavigationRequest(destinationUrl)
clouderyOfferEventHandler.on(CLOUDERY_OFFER, mockClouderyOfferEventHandler)

const instanceInfo = getFakeContext()

const allowNavigation = interceptNavigation(
initialUrl,
closePopup,
client,
mockNavigationProp,
instanceInfo
)(navigationRequest)

expect(allowNavigation).toBe(false)
expect(closePopup).not.toHaveBeenCalled()
expect(navigateToApp).not.toHaveBeenCalled()
expect(mockClouderyOfferEventHandler).toHaveBeenCalledWith(
'https://manager.cozycloud.cc/cozy/instances/SOME_UUID/premium?iap_vendor=ios&iap_url=https%3A%2F%2Fiapflagship'
)
})
})

describe('interceptOpenWindow', () => {
Expand Down Expand Up @@ -262,3 +322,18 @@ describe('interceptOpenWindow', () => {
expect(true).toBe(true)
})
})

const getFakeContext = (): InstanceInfo => {
return {
context: {
data: {
manager_url: 'https://manager.cozycloud.cc'
}
},
instance: {
data: {
uuid: 'SOME_UUID'
}
}
} as unknown as InstanceInfo
}
3 changes: 2 additions & 1 deletion src/components/webviews/CozyWebview.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ jest.mock('cozy-intent', () => ({
}))

jest.mock('cozy-client', () => ({
useClient: jest.fn().mockReturnValue({})
useClient: jest.fn().mockReturnValue({}),
useInstanceInfo: jest.fn().mockReturnValue({})
}))

jest.mock('@react-navigation/native', () => ({
Expand Down

0 comments on commit 0504206

Please sign in to comment.