Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(PushBanner): Check if IAP is available to show premium action #13

Merged
merged 1 commit into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions src/components/PushBanner/QuotaBanner.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react'
import React, { useEffect, useState } from 'react'

import CloudSyncIcon from 'cozy-ui/transpiled/react/Icons/CloudSync'
import Banner from 'cozy-ui/transpiled/react/Banner'
Expand All @@ -12,6 +12,7 @@ import {
} from 'cozy-client/dist/models/instance'
import flag from 'cozy-flags'
import { useInstanceInfo } from 'cozy-client'
import { useWebviewIntent } from 'cozy-intent'

import styles from '../pushClient/pushClient.styl'
import { usePushBannerContext } from './PushBannerProvider'
Expand All @@ -23,6 +24,21 @@ const QuotaBanner = () => {
const { t } = useI18n()
const { dismissPushBanner } = usePushBannerContext()
const instanceInfo = useInstanceInfo()
const webviewIntent = useWebviewIntent()
const [hasIAP, setIAP] = useState(false)

useEffect(() => {
const fetchIapAvailability = async () => {
const isAvailable =
(await webviewIntent?.call('isAvailable', 'iap')) ?? false
const isEnabled = !!flag('flagship.iap.enabled')
setIAP(isAvailable && isEnabled)
}

if (isFlagshipApp()) {
fetchIapAvailability()
}
}, [webviewIntent])

const onAction = () => {
const link = buildPremiumLink(instanceInfo)
Expand All @@ -34,8 +50,7 @@ const QuotaBanner = () => {
}

const canOpenPremiumLink =
arePremiumLinksEnabled(instanceInfo) &&
(!isFlagshipApp() || (isFlagshipApp() && !!flag('flagship.iap.enabled')))
arePremiumLinksEnabled(instanceInfo) && (!isFlagshipApp() || hasIAP)

return (
<div className={styles['coz-banner-client']}>
Expand Down
39 changes: 33 additions & 6 deletions src/components/PushBanner/QuotaBanner.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { isFlagshipApp } from 'cozy-device-helper'
import I18n from 'cozy-ui/transpiled/react/providers/I18n'
import flag from 'cozy-flags'
import { useInstanceInfo } from 'cozy-client'
import { useWebviewIntent } from 'cozy-intent'

import QuotaBanner from './QuotaBanner'
import { usePushBannerContext } from './PushBannerProvider'
Expand All @@ -24,6 +25,10 @@ jest.mock('cozy-client', () => ({
isLoaded: true
}))
}))
jest.mock('cozy-intent', () => ({
...jest.requireActual('cozy-intent'),
useWebviewIntent: jest.fn()
}))

describe('QuotaBanner', () => {
const dismissSpy = jest.fn()
Expand All @@ -37,7 +42,8 @@ describe('QuotaBanner', () => {
enablePremiumLinks = false,
hasUuid = false,
isFlagshipApp: isFlagshipAppReturnValue = false,
isIapEnabled = null
isIapEnabled = null,
isIapAvailable = null
} = {}) => {
usePushBannerContext.mockReturnValue({
dismissPushBanner: dismissSpy
Expand All @@ -61,6 +67,10 @@ describe('QuotaBanner', () => {

isFlagshipApp.mockReturnValue(isFlagshipAppReturnValue)
flag.mockReturnValue(isIapEnabled)
const mockCall = jest.fn().mockResolvedValue(isIapAvailable)
useWebviewIntent.mockReturnValue({
call: mockCall
})

render(
<I18n lang="en" dictRequire={() => en}>
Expand Down Expand Up @@ -112,27 +122,44 @@ describe('QuotaBanner', () => {
expect(premiumButton).toBeNull()
})

it('should hide premium link when is on flagship application and flag flagship.iap.enabled is false', () => {
it('should hide premium link when the flagship app has not IAP available with the flag flagship.iap.enabled is false', () => {
setup({
hasUuid: true,
enablePremiumLinks: true,
isFlagshipApp: true,
isIapEnabled: false
isIapEnabled: false,
isIapAvailable: false
})

const premiumButton = screen.queryByText('Check our plans')
expect(premiumButton).toBeNull()
})

it('should display premium link when is on flagship application and flag flagship.iap.enabled is true', () => {
it('should hide premium link when the flagship app has not IAP available with the flag flagship.iap.enabled is true', () => {
setup({
hasUuid: true,
enablePremiumLinks: true,
isFlagshipApp: true,
isIapEnabled: true
isIapEnabled: true,
isIapAvailable: false
})

fireEvent.click(screen.getByText('Check our plans'))
const premiumButton = screen.queryByText('Check our plans')
expect(premiumButton).toBeNull()
})

it('should display premium link when the flagship app has IAP available with the flag flagship.iap.enabled is true', async () => {
setup({
hasUuid: true,
enablePremiumLinks: true,
isFlagshipApp: true,
isIapEnabled: true,
isIapAvailable: true
})

const actionButton = await screen.findByText('Check our plans')

fireEvent.click(actionButton)
expect(openSpy).toBeCalledWith(
'http://mycozy.cloud/cozy/instances/123/premium',
'_self'
Expand Down
Loading