From 1c5cdc83f7452878837b4a6cd755d4200f550307 Mon Sep 17 00:00:00 2001 From: cballevre Date: Mon, 18 Dec 2023 16:11:33 +0100 Subject: [PATCH] feat(PushBanner): Check if IAP is available to show premium action --- src/components/PushBanner/QuotaBanner.jsx | 21 ++++++++-- .../PushBanner/QuotaBanner.spec.jsx | 39 ++++++++++++++++--- 2 files changed, 51 insertions(+), 9 deletions(-) diff --git a/src/components/PushBanner/QuotaBanner.jsx b/src/components/PushBanner/QuotaBanner.jsx index ffda48c0c..063311bb8 100644 --- a/src/components/PushBanner/QuotaBanner.jsx +++ b/src/components/PushBanner/QuotaBanner.jsx @@ -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' @@ -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' @@ -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) @@ -34,8 +50,7 @@ const QuotaBanner = () => { } const canOpenPremiumLink = - arePremiumLinksEnabled(instanceInfo) && - (!isFlagshipApp() || (isFlagshipApp() && !!flag('flagship.iap.enabled'))) + arePremiumLinksEnabled(instanceInfo) && (!isFlagshipApp() || hasIAP) return (
diff --git a/src/components/PushBanner/QuotaBanner.spec.jsx b/src/components/PushBanner/QuotaBanner.spec.jsx index 501447621..eba3b9092 100644 --- a/src/components/PushBanner/QuotaBanner.spec.jsx +++ b/src/components/PushBanner/QuotaBanner.spec.jsx @@ -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' @@ -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() @@ -37,7 +42,8 @@ describe('QuotaBanner', () => { enablePremiumLinks = false, hasUuid = false, isFlagshipApp: isFlagshipAppReturnValue = false, - isIapEnabled = null + isIapEnabled = null, + isIapAvailable = null } = {}) => { usePushBannerContext.mockReturnValue({ dismissPushBanner: dismissSpy @@ -61,6 +67,10 @@ describe('QuotaBanner', () => { isFlagshipApp.mockReturnValue(isFlagshipAppReturnValue) flag.mockReturnValue(isIapEnabled) + const mockCall = jest.fn().mockResolvedValue(isIapAvailable) + useWebviewIntent.mockReturnValue({ + call: mockCall + }) render( en}> @@ -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'