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
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ function WorkspaceSettlementFrequencyPage({route}: WorkspaceSettlementFrequencyP
return;
}
updateSettlementFrequencyUtil(defaultFundID, programKey, currentFrequency, settings?.monthlySettlementDate);
Navigation.goBack();
Navigation.goBack(ROUTES.WORKSPACE_EXPENSIFY_CARD_SETTINGS.getRoute(policyID));

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.

This changes the deep link behavior. I'd keep this change since the new behavior seems more accurate.
Also consistent with back button behavior:

<HeaderWithBackButton
title={translate('workspace.expensifyCard.settlementFrequency')}
onBackButtonPress={() => Navigation.goBack(ROUTES.WORKSPACE_EXPENSIFY_CARD_SETTINGS.getRoute(policyID))}
/>

Before: (dismiss RHP modal)

before.mov

After: (go back to Settings page)

after.mov

};

const confirmButtonOptions = {
Expand All @@ -93,7 +93,7 @@ function WorkspaceSettlementFrequencyPage({route}: WorkspaceSettlementFrequencyP
<AccessOrNotFoundWrapper
accessVariants={[CONST.POLICY.ACCESS_VARIANTS.ADMIN, CONST.POLICY.ACCESS_VARIANTS.PAID]}
policyID={policyID}
shouldBeBlocked={isSettlementFrequencyBlocked}
shouldBeBlocked={isSettlementFrequencyBlocked && !selectedFrequencyDraft}
featureName={CONST.POLICY.MORE_FEATURES.ARE_EXPENSIFY_CARDS_ENABLED}
policyFeature={CONST.POLICY.POLICY_FEATURE.EXPENSIFY_CARD}
policyFeatureAccess={CONST.POLICY.POLICY_FEATURE_ACCESS.WRITE}
Expand Down
191 changes: 191 additions & 0 deletions tests/ui/WorkspaceSettlementFrequencyPageTest.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
import {act, fireEvent, render, screen} from '@testing-library/react-native';

import ComposeProviders from '@components/ComposeProviders';
import {LocaleContextProvider} from '@components/LocaleContextProvider';
import OnyxListItemProvider from '@components/OnyxListItemProvider';

import type {PlatformStackScreenProps} from '@libs/Navigation/PlatformStackNavigation/types';

import type {SettingsNavigatorParamList} from '@navigation/types';

import WorkspaceSettlementFrequencyPage from '@pages/workspace/expensifyCard/WorkspaceSettlementFrequencyPage';

import CONST from '@src/CONST';
import OnyxUpdateManager from '@src/libs/actions/OnyxUpdateManager';
import ONYXKEYS from '@src/ONYXKEYS';
import SCREENS from '@src/SCREENS';

import React from 'react';
import Onyx from 'react-native-onyx';

import * as TestHelper from '../utils/TestHelper';
import waitForBatchedUpdatesWithAct from '../utils/waitForBatchedUpdatesWithAct';

// Test constants - these values MUST match the literals used in jest.mock() below
// because jest.mock() is hoisted before variable declarations are evaluated
const POLICY_ID = 'policy123';
const WORKSPACE_ACCOUNT_ID = 424242;

// The text the mocked access wrapper renders in place of the page when it blocks it, standing in for the not-found page.
const NOT_FOUND_TEXT = 'Mocked not found page';

// jest.mock() factories are hoisted and run before imports/variables are defined, so they cannot
// reference the constants above. Keep the literals in both places in sync.
jest.mock('@react-navigation/native', () => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const actualNav = jest.requireActual('@react-navigation/native');
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return {
...actualNav,
useIsFocused: () => true,
useRoute: () => ({
key: 'test-route',
name: 'Workspace_ExpensifyCard_Settings_Frequency',
params: {policyID: 'policy123'}, // Must match POLICY_ID
}),
usePreventRemove: jest.fn(),
};
});

jest.mock('@src/hooks/useResponsiveLayout');

// The default fund ID falls back to the workspace account ID when there is no last-selected feed.
jest.mock('@hooks/useWorkspaceAccountID', () => ({
__esModule: true,
default: () => 424242, // Must match WORKSPACE_ACCOUNT_ID
}));

jest.mock('@hooks/useScreenWrapperTransitionStatus', () => ({
__esModule: true,
default: () => ({didScreenTransitionEnd: true}),
}));

jest.mock('@libs/Navigation/Navigation', () => ({

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.

❌ CONSISTENCY-15 (docs)

Comments should read as plain, natural sentences. This comment uses a semicolon to join two independent clauses, which the style guide asks you to split into two separate sentences.

Split the semicolon into two sentences:

// The screen does not read navigation. This inert test double only satisfies the navigator-provided prop.
const navigation = {} as PlatformStackScreenProps<SettingsNavigatorParamList, typeof SCREENS.WORKSPACE.EXPENSIFY_CARD_SETTINGS_FREQUENCY>['navigation'];

Reviewed at: f2980cf | Please rate this suggestion with 👍 or 👎 to help us improve! Reactions are used to monitor reviewer efficiency.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done in 2d0de26 — split the semicolon into two sentences, exactly as suggested:

// The screen does not read navigation. This inert test double only satisfies the navigator-provided prop.

It was the only semicolon-joined comment in either file this PR touches.

Checks run
  • npx eslint tests/ui/WorkspaceSettlementFrequencyPageTest.tsx — passed
  • npm test -- tests/ui/WorkspaceSettlementFrequencyPageTest.tsx — 3 passed

lint-changed and spell-changed both abort with git merge-base failed because this checkout is shallow, and prettier can't resolve @trivago/prettier-plugin-sort-imports from the config it picks up here. Those are environment limits, not findings — and none of them apply to a change that only edits text inside a comment. CI covers all three.


view run

__esModule: true,
default: {
goBack: jest.fn(),
navigate: jest.fn(),
getActiveRoute: jest.fn(() => ''),
getActiveRouteWithoutParams: jest.fn(() => ''),
isNavigationReady: jest.fn(() => Promise.resolve()),
isTopmostRouteModalScreen: jest.fn(() => false),
},
}));

// Stand in for the real access gate so the test can drive `shouldBeBlocked` directly: the policy access checks it also runs
// are not what this page decides, but whether it blocks itself is.
jest.mock('@pages/workspace/AccessOrNotFoundWrapper', () => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
const ReactMock = require('react') as typeof React;
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
const {Text} = require('react-native') as {Text: React.ComponentType<{children?: React.ReactNode}>};

return ({children, shouldBeBlocked}: {children: React.ReactNode; shouldBeBlocked?: boolean}) =>
shouldBeBlocked ? ReactMock.createElement(Text, null, 'Mocked not found page') : children; // Must match NOT_FOUND_TEXT
});

const cardSettingsKey = `${ONYXKEYS.COLLECTION.PRIVATE_EXPENSIFY_CARD_SETTINGS}${WORKSPACE_ACCOUNT_ID}` as const;

// The page resolves its settings through the card program they are nested under, so the test data has to be nested the same way.
const programKey = CONST.EXPENSIFY_CARD.CARD_PROGRAM.CURRENT;

// The day of the month the workspace already settles on, standing in for a frequency set in OldDot.
const existingSettlementDay = 10;

const route: PlatformStackScreenProps<SettingsNavigatorParamList, typeof SCREENS.WORKSPACE.EXPENSIFY_CARD_SETTINGS_FREQUENCY>['route'] = {
key: 'workspace-expensify-card-settings-frequency',
name: SCREENS.WORKSPACE.EXPENSIFY_CARD_SETTINGS_FREQUENCY,
params: {policyID: POLICY_ID},
};
// The screen does not read navigation. This inert test double only satisfies the navigator-provided prop.
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
const navigation = {} as PlatformStackScreenProps<SettingsNavigatorParamList, typeof SCREENS.WORKSPACE.EXPENSIFY_CARD_SETTINGS_FREQUENCY>['navigation'];

const renderWorkspaceSettlementFrequencyPage = () =>
render(
<ComposeProviders components={[OnyxListItemProvider, LocaleContextProvider]}>
<WorkspaceSettlementFrequencyPage
route={route}
navigation={navigation}
/>
</ComposeProviders>,
);

OnyxUpdateManager();
describe('WorkspaceSettlementFrequencyPage', () => {
beforeAll(() => {
Onyx.init({keys: ONYXKEYS});
});

const mockFetch = TestHelper.setupGlobalFetchMock();

beforeEach(() => {
mockFetch.succeed();
});

afterEach(async () => {
jest.clearAllMocks();
await act(async () => {
await Onyx.clear();
await waitForBatchedUpdatesWithAct();
});
});

it('does not block the page when the workspace settles monthly but is no longer eligible for monthly settlement', async () => {
// Given a workspace put on monthly settlement in OldDot, which bypasses the eligibility gate NewDot applies
await act(async () => {
await Onyx.merge(cardSettingsKey, {
[programKey]: {isMonthlySettlementAllowed: false, monthlySettlementDate: existingSettlementDay},
});
await waitForBatchedUpdatesWithAct();
});

// When the settlement frequency page is opened
renderWorkspaceSettlementFrequencyPage();
await waitForBatchedUpdatesWithAct();

// Then the page opens on the Daily option rather than the not-found page, because there is still a change to make
expect(screen.queryByText(NOT_FOUND_TEXT)).toBeNull();
expect(screen.getByText('Daily')).toBeTruthy();
});

it('keeps showing the page after saving Daily, when the optimistic update makes the workspace settle daily', async () => {
// Given the same workspace: settling monthly, with monthly no longer selectable
await act(async () => {
await Onyx.merge(cardSettingsKey, {
[programKey]: {isMonthlySettlementAllowed: false, monthlySettlementDate: existingSettlementDay},
});
await waitForBatchedUpdatesWithAct();
});

renderWorkspaceSettlementFrequencyPage();
await waitForBatchedUpdatesWithAct();

// When Daily is picked and saved, which optimistically clears `monthlySettlementDate` while the page is still mounted
fireEvent.press(screen.getByText('Daily'));
await waitForBatchedUpdatesWithAct();
fireEvent.press(screen.getByText('Save'));
await waitForBatchedUpdatesWithAct();

// Then the page does not block itself over the user's own successful change. Blocking here is what dropped the
// not-found page on top of the selection and sent the back button to the workspace overview.
expect(screen.queryByText(NOT_FOUND_TEXT)).toBeNull();
});

it('blocks the page when the workspace already settles daily and monthly is not selectable', async () => {
// Given a workspace that settles daily and cannot switch to monthly, so the page has nothing to offer
await act(async () => {
await Onyx.merge(cardSettingsKey, {
[programKey]: {isMonthlySettlementAllowed: false},
});
await waitForBatchedUpdatesWithAct();
});

// When the settlement frequency page is opened
renderWorkspaceSettlementFrequencyPage();
await waitForBatchedUpdatesWithAct();

// Then it is still blocked - the fix must not open a page with a single, already-selected option
expect(screen.getByText(NOT_FOUND_TEXT)).toBeTruthy();
});
});
Loading