Skip to content

Commit 8cc58f3

Browse files
Hide FreeTrialSection if there is no workspace
1 parent 096735a commit 8cc58f3

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

src/pages/home/FreeTrialSection/useFreeTrial.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import {useEffect, useState} from 'react';
2+
import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails';
23
import useHasTeam2025Pricing from '@hooks/useHasTeam2025Pricing';
34
import useOnyx from '@hooks/useOnyx';
45
import useSubscriptionPlan from '@hooks/useSubscriptionPlan';
6+
import {getOwnedPaidPolicies} from '@libs/PolicyUtils';
57
import type {DiscountInfo} from '@libs/SubscriptionUtils';
68
import {calculateRemainingFreeTrialDays, doesUserHavePaymentCardAdded, getEarlyDiscountInfo, isUserOnFreeTrial, shouldShowDiscountBanner} from '@libs/SubscriptionUtils';
79
import CONST from '@src/CONST';
@@ -32,12 +34,14 @@ function useFreeTrial(): FreeTrialState {
3234
const [firstDayFreeTrial] = useOnyx(ONYXKEYS.NVP_FIRST_DAY_FREE_TRIAL);
3335
const [lastDayFreeTrial] = useOnyx(ONYXKEYS.NVP_LAST_DAY_FREE_TRIAL);
3436
const [userBillingFundID] = useOnyx(ONYXKEYS.NVP_BILLING_FUND_ID);
37+
const {accountID} = useCurrentUserPersonalDetails();
3538
const hasTeam2025Pricing = useHasTeam2025Pricing();
3639
const subscriptionPlan = useSubscriptionPlan();
3740
const [allPolicies] = useOnyx(ONYXKEYS.COLLECTION.POLICY);
3841

3942
const onFreeTrial = isUserOnFreeTrial(firstDayFreeTrial, lastDayFreeTrial);
4043
const hasPaymentCard = doesUserHavePaymentCardAdded(userBillingFundID);
44+
const hasOwnedPaidPolicies = getOwnedPaidPolicies(allPolicies, accountID).length > 0;
4145
const showDiscount = shouldShowDiscountBanner(hasTeam2025Pricing, subscriptionPlan, firstDayFreeTrial, lastDayFreeTrial, userBillingFundID, allPolicies);
4246
const daysLeft = calculateRemainingFreeTrialDays(lastDayFreeTrial);
4347

@@ -58,7 +62,7 @@ function useFreeTrial(): FreeTrialState {
5862
};
5963
}, [firstDayFreeTrial, showDiscount]);
6064

61-
if (!onFreeTrial || hasPaymentCard) {
65+
if (!onFreeTrial || hasPaymentCard || !hasOwnedPaidPolicies) {
6266
return {shouldShowFreeTrialSection: false, discountType: null, daysLeft: 0, discountInfo: null};
6367
}
6468

tests/unit/hooks/useFreeTrial.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {renderHook} from '@testing-library/react-native';
33
import Onyx from 'react-native-onyx';
44
import useHasTeam2025Pricing from '@hooks/useHasTeam2025Pricing';
55
import useSubscriptionPlan from '@hooks/useSubscriptionPlan';
6+
import {getOwnedPaidPolicies} from '@libs/PolicyUtils';
67
import {calculateRemainingFreeTrialDays, doesUserHavePaymentCardAdded, getEarlyDiscountInfo, isUserOnFreeTrial, shouldShowDiscountBanner} from '@libs/SubscriptionUtils';
78
import useFreeTrial from '@pages/home/FreeTrialSection/useFreeTrial';
89
import ONYXKEYS from '@src/ONYXKEYS';
@@ -18,6 +19,10 @@ jest.mock('@hooks/useSubscriptionPlan', () => ({
1819
default: jest.fn(() => 'corporate'),
1920
}));
2021

22+
jest.mock('@libs/PolicyUtils', () => ({
23+
getOwnedPaidPolicies: jest.fn(() => [{id: 'policyID'}]),
24+
}));
25+
2126
jest.mock('@libs/SubscriptionUtils', () => ({
2227
shouldShowDiscountBanner: jest.fn(() => false),
2328
getEarlyDiscountInfo: jest.fn(() => null),
@@ -28,6 +33,7 @@ jest.mock('@libs/SubscriptionUtils', () => ({
2833

2934
const mockedUseHasTeam2025Pricing = useHasTeam2025Pricing as jest.Mock;
3035
const mockedUseSubscriptionPlan = useSubscriptionPlan as jest.Mock;
36+
const mockedGetOwnedPaidPolicies = getOwnedPaidPolicies as jest.Mock;
3137
const mockedShouldShowDiscountBanner = shouldShowDiscountBanner as jest.Mock;
3238
const mockedGetEarlyDiscountInfo = getEarlyDiscountInfo as jest.Mock;
3339
const mockedIsUserOnFreeTrial = isUserOnFreeTrial as jest.Mock;
@@ -43,6 +49,7 @@ describe('useFreeTrial', () => {
4349
await Onyx.clear();
4450
await waitForBatchedUpdates();
4551
jest.clearAllMocks();
52+
mockedGetOwnedPaidPolicies.mockReturnValue([{id: 'policyID'}]);
4653
});
4754

4855
afterEach(async () => {
@@ -77,6 +84,16 @@ describe('useFreeTrial', () => {
7784

7885
expect(result.current.shouldShowFreeTrialSection).toBe(true);
7986
});
87+
88+
it("should not show section when user doesn't own any paid workspaces", () => {
89+
mockedIsUserOnFreeTrial.mockReturnValue(true);
90+
mockedDoesUserHavePaymentCardAdded.mockReturnValue(false);
91+
mockedGetOwnedPaidPolicies.mockReturnValue([]);
92+
93+
const {result} = renderHook(() => useFreeTrial());
94+
95+
expect(result.current.shouldShowFreeTrialSection).toBe(false);
96+
});
8097
});
8198

8299
describe('discount state - 50% off (first 24 hours)', () => {

0 commit comments

Comments
 (0)