Skip to content
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
31 changes: 18 additions & 13 deletions src/pages/settings/Wallet/ExpensifyCardPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ function ExpensifyCardPage({

const primaryLogin = account?.primaryLogin ?? '';
const loginData = loginList?.[primaryLogin];
const isSignedInAsdelegate = !!account?.delegatedAccess?.delegate || false;

if (isNotFound) {
return <NotFoundPage onBackButtonPress={() => Navigation.goBack(ROUTES.SETTINGS_WALLET)} />;
Expand Down Expand Up @@ -212,12 +213,14 @@ function ExpensifyCardPage({
titleStyle={styles.walletCardNumber}
shouldShowRightComponent
rightComponent={
<Button
text={translate('cardPage.cardDetails.revealDetails')}
onPress={() => openValidateCodeModal(card.cardID)}
isDisabled={isCardDetailsLoading[card.cardID] || isOffline}
isLoading={isCardDetailsLoading[card.cardID]}
/>
!isSignedInAsdelegate ? (
<Button
text={translate('cardPage.cardDetails.revealDetails')}
onPress={() => openValidateCodeModal(card.cardID)}
isDisabled={isCardDetailsLoading[card.cardID] || isOffline}
isLoading={isCardDetailsLoading[card.cardID]}
/>
) : undefined
}
/>
<DotIndicatorMessage
Expand All @@ -227,13 +230,15 @@ function ExpensifyCardPage({
/>
</>
)}
<MenuItemWithTopDescription
title={translate('cardPage.reportFraud')}
titleStyle={styles.walletCardMenuItem}
icon={Expensicons.Flag}
shouldShowRightIcon
onPress={() => Navigation.navigate(ROUTES.SETTINGS_REPORT_FRAUD.getRoute(String(card.cardID)))}
/>
{!isSignedInAsdelegate && (
<MenuItemWithTopDescription
title={translate('cardPage.reportFraud')}
titleStyle={styles.walletCardMenuItem}
icon={Expensicons.Flag}
shouldShowRightIcon
onPress={() => Navigation.navigate(ROUTES.SETTINGS_REPORT_FRAUD.getRoute(String(card.cardID)))}
/>
)}
</>
))}
{physicalCards.map((card) => {
Expand Down
64 changes: 34 additions & 30 deletions src/pages/settings/Wallet/ReportVirtualCardFraudPage.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, {useCallback, useEffect, useState} from 'react';
import {View} from 'react-native';
import {useOnyx} from 'react-native-onyx';
import DelegateNoAccessWrapper from '@components/DelegateNoAccessWrapper';
import FormAlertWithSubmitButton from '@components/FormAlertWithSubmitButton';
import HeaderWithBackButton from '@components/HeaderWithBackButton';
import ScreenWrapper from '@components/ScreenWrapper';
Expand All @@ -17,6 +18,7 @@ import type {PlatformStackScreenProps} from '@libs/Navigation/PlatformStackNavig
import type {SettingsNavigatorParamList} from '@libs/Navigation/types';
import NotFoundPage from '@pages/ErrorPage/NotFoundPage';
import {clearCardListErrors, clearReportVirtualCardFraudForm, reportVirtualExpensifyCardFraud} from '@userActions/Card';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import SCREENS from '@src/SCREENS';
Expand Down Expand Up @@ -97,37 +99,39 @@ function ReportVirtualCardFraudPage({

return (
<ScreenWrapper testID={ReportVirtualCardFraudPage.displayName}>
<HeaderWithBackButton
title={translate('reportFraudPage.title')}
onBackButtonPress={() => Navigation.goBack(ROUTES.SETTINGS_WALLET_DOMAINCARD.getRoute(cardID))}
/>
<View style={[styles.flex1, styles.justifyContentBetween]}>
<Text style={[styles.webViewStyles.baseFontStyle, styles.mh5]}>{translate('reportFraudPage.description')}</Text>
<FormAlertWithSubmitButton
isAlertVisible={!!virtualCardError}
onSubmit={handleSubmit}
message={virtualCardError}
buttonText={translate('reportFraudPage.deactivateCard')}
containerStyles={[styles.m5]}
<DelegateNoAccessWrapper accessDeniedVariants={[CONST.DELEGATE.DENIED_ACCESS_VARIANTS.DELEGATE]}>
<HeaderWithBackButton
title={translate('reportFraudPage.title')}
onBackButtonPress={() => Navigation.goBack(ROUTES.SETTINGS_WALLET_DOMAINCARD.getRoute(cardID))}
/>
<ValidateCodeActionModal
handleSubmitForm={handleValidateCodeEntered}
sendValidateCode={sendValidateCode}
validateError={validateError}
clearError={() => {
if (!virtualCard?.cardID) {
return;
}
clearCardListErrors(virtualCard.cardID);
}}
onClose={() => setIsValidateCodeActionModalVisible(false)}
isVisible={isValidateCodeActionModalVisible}
title={translate('cardPage.validateCardTitle')}
descriptionPrimary={translate('cardPage.enterMagicCode', {contactMethod: primaryLogin})}
hasMagicCodeBeenSent={!!loginData?.validateCodeSent}
isLoading={formData?.isLoading}
/>
</View>
<View style={[styles.flex1, styles.justifyContentBetween]}>
<Text style={[styles.webViewStyles.baseFontStyle, styles.mh5]}>{translate('reportFraudPage.description')}</Text>
<FormAlertWithSubmitButton
isAlertVisible={!!virtualCardError}
onSubmit={handleSubmit}
message={virtualCardError}
buttonText={translate('reportFraudPage.deactivateCard')}
containerStyles={[styles.m5]}
/>
<ValidateCodeActionModal
handleSubmitForm={handleValidateCodeEntered}
sendValidateCode={sendValidateCode}
validateError={validateError}
clearError={() => {
if (!virtualCard?.cardID) {
return;
}
clearCardListErrors(virtualCard.cardID);
}}
onClose={() => setIsValidateCodeActionModalVisible(false)}
isVisible={isValidateCodeActionModalVisible}
title={translate('cardPage.validateCardTitle')}
descriptionPrimary={translate('cardPage.enterMagicCode', {contactMethod: primaryLogin})}
hasMagicCodeBeenSent={!!loginData?.validateCodeSent}
isLoading={formData?.isLoading}
/>
</View>
</DelegateNoAccessWrapper>
</ScreenWrapper>
);
}
Expand Down
161 changes: 161 additions & 0 deletions tests/ui/WalletExpensifyCardPageTest.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
import {PortalProvider} from '@gorhom/portal';
import {NavigationContainer} from '@react-navigation/native';
import {act, render, screen, waitFor} from '@testing-library/react-native';
import React from 'react';
import Onyx from 'react-native-onyx';
import ComposeProviders from '@components/ComposeProviders';
import {LocaleContextProvider} from '@components/LocaleContextProvider';
import OnyxProvider from '@components/OnyxProvider';
import {CurrentReportIDContextProvider} from '@hooks/useCurrentReportID';
import * as useResponsiveLayoutModule from '@hooks/useResponsiveLayout';
import type ResponsiveLayoutResult from '@hooks/useResponsiveLayout/types';
import {translateLocal} from '@libs/Localize';
import createPlatformStackNavigator from '@libs/Navigation/PlatformStackNavigation/createPlatformStackNavigator';
import type {SettingsNavigatorParamList} from '@navigation/types';
import ExpensifyCardPage from '@pages/settings/Wallet/ExpensifyCardPage';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import SCREENS from '@src/SCREENS';
import * as TestHelper from '../utils/TestHelper';
import waitForBatchedUpdatesWithAct from '../utils/waitForBatchedUpdatesWithAct';

// Set up a global fetch mock for API requests in tests.
TestHelper.setupGlobalFetchMock();

// Create a stack navigator for the settings pages.
const Stack = createPlatformStackNavigator<SettingsNavigatorParamList>();

const userCardID = '1234';

// Renders the ExpensifyCardPage inside a navigation container with necessary providers.
const renderPage = (initialRouteName: typeof SCREENS.SETTINGS.WALLET.DOMAIN_CARD, initialParams: SettingsNavigatorParamList[typeof SCREENS.SETTINGS.WALLET.DOMAIN_CARD]) => {
return render(
<ComposeProviders components={[OnyxProvider, LocaleContextProvider, CurrentReportIDContextProvider]}>
<PortalProvider>
<NavigationContainer>
<Stack.Navigator initialRouteName={initialRouteName}>
<Stack.Screen
name={SCREENS.SETTINGS.WALLET.DOMAIN_CARD}
component={ExpensifyCardPage}
initialParams={initialParams}
/>
</Stack.Navigator>
</NavigationContainer>
</PortalProvider>
</ComposeProviders>,
);
};

describe('ExpensifyCardPage', () => {
beforeAll(() => {
// Initialize Onyx with required keys before running any test.
Onyx.init({
keys: ONYXKEYS,
});
});

beforeEach(() => {
// Mock the useResponsiveLayout hook to control layout behavior in tests.
jest.spyOn(useResponsiveLayoutModule, 'default').mockReturnValue({
isSmallScreenWidth: false,
shouldUseNarrowLayout: false,
} as ResponsiveLayoutResult);
});

afterEach(async () => {
// Clear Onyx data and reset all mocks after each test to ensure a clean state.
await act(async () => {
await Onyx.clear();
});
jest.clearAllMocks();
});

it('should show the Report Fraud and Reveal details options on screen', async () => {
// Sign in as a test user before running the test.
await TestHelper.signInWithTestUser();

// Add a mock card to Onyx storage to simulate a valid card being loaded.
await act(async () => {
await Onyx.merge(ONYXKEYS.CARD_LIST, {
[userCardID]: {
cardID: 1234,
state: CONST.EXPENSIFY_CARD.STATE.OPEN,
domainName: 'xyz',
nameValuePairs: {
isVirtual: true,
cardTitle: 'Test Virtual Card',
},
availableSpend: 50000,
fraud: null,
},
});
});

// Render the page with the specified card ID.
const {unmount} = renderPage(SCREENS.SETTINGS.WALLET.DOMAIN_CARD, {cardID: '1234'});

await waitForBatchedUpdatesWithAct();

// Verify that the "Report Fraud" option is displayed on the screen.
await waitFor(() => {
expect(screen.getByText(translateLocal('cardPage.reportFraud'))).toBeOnTheScreen();
});

// Verify that the "Reveal Details" option is displayed on the screen.
await waitFor(() => {
expect(screen.getByText(translateLocal('cardPage.cardDetails.revealDetails'))).toBeOnTheScreen();
});

// Unmount the component after assertions to clean up.
unmount();
await waitForBatchedUpdatesWithAct();
});

it('should not show the Report Fraud and Reveal details options on screen', async () => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
it('should not show the Report Fraud and Reveal details options on screen', async () => {
it('should not show the Report Fraud and Reveal details options on screen when connected as delegate', async () => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NAB

// Sign in as a test user before running the test.
await TestHelper.signInWithTestUser();

// Add a mock card to Onyx storage with additional delegated access data.
await act(async () => {
await Onyx.merge(ONYXKEYS.CARD_LIST, {
[userCardID]: {
cardID: 1234,
state: CONST.EXPENSIFY_CARD.STATE.OPEN,
domainName: 'xyz',
nameValuePairs: {
isVirtual: true,
cardTitle: 'Test Virtual Card',
},
availableSpend: 50000,
fraud: null,
},
});

// Add delegated access data to simulate a user without fraud reporting permissions.
await Onyx.merge(ONYXKEYS.ACCOUNT, {
delegatedAccess: {
delegate: 'test@test.com',
},
});
});

// Render the page with the specified card ID.
const {unmount} = renderPage(SCREENS.SETTINGS.WALLET.DOMAIN_CARD, {cardID: '1234'});

await waitForBatchedUpdatesWithAct();

// Verify that the "Report Fraud" option is NOT displayed on the screen.
await waitFor(() => {
expect(screen.queryByText(translateLocal('cardPage.reportFraud'))).not.toBeOnTheScreen();
});

// Verify that the "Reveal Details" option is NOT displayed on the screen.
await waitFor(() => {
expect(screen.queryByText(translateLocal('cardPage.cardDetails.revealDetails'))).not.toBeOnTheScreen();
});

// Unmount the component after assertions to clean up.
unmount();
await waitForBatchedUpdatesWithAct();
});
});