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 @@ -65,9 +65,11 @@ function CategoryField({
const isAutoFillFromReceipt = categoryState?.isAutoFillFromReceipt ?? false;
const decodedCategoryName = getDecodedLeafCategoryName(iouCategory);

// The workspace setting defaults to on. Turning it off means nothing will pick a category, so the row must not promise one.
const isAutoCategorizationEnabled = policy?.autoCategorizeNewExpenses !== false;
const shouldPromiseAutomaticCategory = isAutoCategorizationEnabled && willAutoFill && (isAutoFillFromReceipt || !isCategoryRequired);
// Categorization comes from the workspace, so there is nothing to promise without one. The setting itself defaults to on.
const isAutoCategorizationEnabled = !!policy && policy.autoCategorizeNewExpenses !== false;
// Invoices are never auto-categorized, so the row must not promise a category it will never get.
const isInvoice = iouType === CONST.IOU.TYPE.INVOICE;
const shouldPromiseAutomaticCategory = isAutoCategorizationEnabled && !isInvoice && willAutoFill && (isAutoFillFromReceipt || !isCategoryRequired);
Comment thread
thelullabyy marked this conversation as resolved.
Resolved

const getCategoryRightLabelIcon = () => (shouldPromiseAutomaticCategory ? icons.Sparkles : undefined);
const getCategoryRightLabel = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import ConfirmationFieldsProvider from '@components/MoneyRequestConfirmationFiel
import CategoryField from '@components/MoneyRequestConfirmationList/sections/CategoryField';

import CONST from '@src/CONST';
import type {IOUType} from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {Policy} from '@src/types/onyx';

Expand Down Expand Up @@ -33,21 +34,23 @@ const REPORT_ID = 'reportID';
const disabledPolicy: Policy = {...createRandomPolicy(1), autoCategorizeNewExpenses: false};
const enabledPolicy: Policy = {...createRandomPolicy(1), autoCategorizeNewExpenses: true};

const renderCategoryField = (isCategoryRequired: boolean, policy: Policy | undefined) =>
type ConfirmationIOUType = Exclude<IOUType, typeof CONST.IOU.TYPE.REQUEST | typeof CONST.IOU.TYPE.SEND>;

const renderCategoryField = (isCategoryRequired: boolean, policy: Policy | undefined, iouType: ConfirmationIOUType = CONST.IOU.TYPE.SUBMIT) =>
render(
<ConfirmationFieldsProvider
transactionID={TRANSACTION_ID}
reportID={REPORT_ID}
action={CONST.IOU.ACTION.CREATE}
iouType={CONST.IOU.TYPE.SUBMIT}
iouType={iouType}
>
<CategoryField
isCategoryRequired={isCategoryRequired}
didConfirm={false}
isReadOnly={false}
transactionID={TRANSACTION_ID}
action={CONST.IOU.ACTION.CREATE}
iouType={CONST.IOU.TYPE.SUBMIT}
iouType={iouType}
reportID={REPORT_ID}
reportActionID={undefined}
policy={policy}
Expand Down Expand Up @@ -82,6 +85,21 @@ describe('CategoryField', () => {
await waitForBatchedUpdates();
}

async function givenScanExpense() {
await Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${TRANSACTION_ID}`, {
transactionID: TRANSACTION_ID,
reportID: REPORT_ID,
amount: 0,
currency: 'USD',
merchant: CONST.TRANSACTION.PARTIAL_TRANSACTION_MERCHANT,
created: '2026-01-15',
category: 'Travel',
iouRequestType: CONST.IOU.REQUEST_TYPE.SCAN,
receipt: {receiptID: 1, source: 'source', state: CONST.IOU.RECEIPT_STATE.SCAN_READY},
});
await waitForBatchedUpdates();
}

it('does not promise an automatic category when the workspace turned auto-categorization off', async () => {
// Given a manual expense on a workspace that will not categorize it
await givenManualExpense();
Expand Down Expand Up @@ -124,18 +142,7 @@ describe('CategoryField', () => {

it('does not promise an automatic category on a scan when auto-categorization is off', async () => {
// Given a scan whose workspace will not categorize the receipt
await Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${TRANSACTION_ID}`, {
transactionID: TRANSACTION_ID,
reportID: REPORT_ID,
amount: 0,
currency: 'USD',
merchant: CONST.TRANSACTION.PARTIAL_TRANSACTION_MERCHANT,
created: '2026-01-15',
category: 'Travel',
iouRequestType: CONST.IOU.REQUEST_TYPE.SCAN,
receipt: {receiptID: 1, source: 'source', state: CONST.IOU.RECEIPT_STATE.SCAN_READY},
});
await waitForBatchedUpdates();
await givenScanExpense();

// When the confirmation form renders the category row
renderCategoryField(false, disabledPolicy);
Expand All @@ -147,6 +154,37 @@ describe('CategoryField', () => {
expect(screen.queryByText('common.automatic')).toBeNull();
});

it('does not promise an automatic category on an invoice', async () => {
// Given a manual invoice on a workspace that categorizes new expenses
await givenManualExpense();

// When the confirmation form renders an optional category row for an invoice
renderCategoryField(false, enabledPolicy, CONST.IOU.TYPE.INVOICE);

// Then the row must not say Automatic, because invoices are never auto-categorized
await waitFor(() => {
expect(screen.getByText('Meals')).toBeOnTheScreen();
});
expect(screen.queryByText('common.automatic')).toBeNull();
});

it.each([
['manual', givenManualExpense, 'Meals'],
['scan', givenScanExpense, 'Travel'],
])('does not promise an automatic category on a %s track expense without a workspace', async (_, givenExpense, category) => {
// Given a track expense that isn't tied to any workspace
await givenExpense();

// When the confirmation form renders the category row with no policy
renderCategoryField(false, undefined, CONST.IOU.TYPE.TRACK);

// Then the row must not say Automatic, because categorization only runs on a workspace
await waitFor(() => {
expect(screen.getByText(category)).toBeOnTheScreen();
});
expect(screen.queryByText('common.automatic')).toBeNull();
});

it('shows Required instead of Automatic when the category is required and auto-categorization is off', async () => {
// Given a manual expense whose workspace both requires a category and will not pick one
await givenManualExpense();
Expand Down
Loading