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 @@ -161,7 +161,7 @@ function MoneyRequestViewReportFields({report, policy, pendingAction, style, sho
// time it was edited, so the field would stop following the field it refers to.
const shouldUseRawValue = isEditable && field.type !== CONST.REPORT_FIELD_TYPES.FORMULA;
const fieldValue = shouldUseRawValue ? (field.value ?? field.defaultValue ?? '') : resolveReportFieldValue(field, report, policy, fieldValues, fieldsByName, getCurrencyDecimals);
const violation = isFieldDisabled ? undefined : getFieldViolation(field);
const violation = isFieldDisabled ? undefined : getFieldViolation(field, report);
const violationTranslation = getFieldViolationTranslation(field, violation);
return {
...field,
Expand Down
6 changes: 3 additions & 3 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11200,7 +11200,7 @@ function hasVisibleReportFieldViolations(
if (isReportFieldDisabledForUser(report, field, policy, currentUserAccountID, rules, reportOwnerLogin)) {
return false;
}
return !!getFieldViolation(field);
return !!getFieldViolation(field, report);
});
}

Expand Down Expand Up @@ -14478,8 +14478,8 @@ function getChatUsedForOnboarding(onboardingValue: OnyxEntry<Onboarding>, concie
/**
* Checks if given field has any violations and returns name of the first encountered one
*/
function getFieldViolation(reportField: PolicyReportField): ReportViolationName | undefined {
if (!reportField) {
function getFieldViolation(reportField: PolicyReportField, report: OnyxEntry<Report>): ReportViolationName | undefined {
if (!reportField || isInvoiceReport(report)) {
return undefined;
}

Expand Down
36 changes: 34 additions & 2 deletions tests/ui/MoneyRequestViewReportFieldsTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ const buildPolicy = (fieldCount: number, extraFields: OnyxTypes.PolicyReportFiel
role: CONST.POLICY.ROLE.ADMIN,
outputCurrency: CONST.CURRENCY.USD,
areReportFieldsEnabled: true,
areInvoiceFieldsEnabled: true,
fieldList,
};
};
Expand All @@ -174,13 +175,21 @@ const buildReport = (reportOnlyFields: OnyxTypes.PolicyReportField[] = []): Onyx
total: 0,
});

const renderReportFields = async (fieldCount: number, extraFields: OnyxTypes.PolicyReportField[] = [], reportOnlyFields: OnyxTypes.PolicyReportField[] = []) => {
const renderReportFields = async (
fieldCount: number,
extraFields: OnyxTypes.PolicyReportField[] = [],
reportOnlyFields: OnyxTypes.PolicyReportField[] = [],
options: {report?: Partial<OnyxTypes.Report>; reportNameValuePairs?: OnyxTypes.ReportNameValuePairs} = {},
) => {
const policy = buildPolicy(fieldCount, extraFields);
const report = buildReport(reportOnlyFields);
const report = {...buildReport(reportOnlyFields), ...options.report};

await act(async () => {
await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, policy);
await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`, report);
if (options.reportNameValuePairs) {
await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${reportID}`, options.reportNameValuePairs);
}
});
await waitForBatchedUpdatesWithAct();

Expand Down Expand Up @@ -406,6 +415,29 @@ describe('MoneyRequestViewReportFields', () => {
expect(screen.getByText('EmptyField is required')).toBeOnTheScreen();
});

it.each([false, true])('shows empty invoice fields without required errors when stored in reportNameValuePairs: %s', async (isStoredInNameValuePairs) => {
// Given empty invoice fields, including invoices whose saved fields live in name-value pairs
const invoiceFields = ['Customer Address', 'PO Number'].map((name, index) => ({
...buildTextField(index),
name,
value: '',
target: CONST.REPORT_FIELD_TARGETS.INVOICE,
}));
const reportNameValuePairs = isStoredInNameValuePairs ? Object.fromEntries(invoiceFields.map((field) => [`expensify_${field.fieldID}`, field])) : undefined;

// When an unpaid invoice is opened
await renderReportFields(0, invoiceFields, [], {
report: {type: CONST.REPORT.TYPE.INVOICE, stateNum: CONST.REPORT.STATE_NUM.SUBMITTED, statusNum: CONST.REPORT.STATUS_NUM.SUBMITTED},
reportNameValuePairs,
});

// Then both fields remain visible and empty without the expense-report required errors
for (const field of invoiceFields) {
expect(screen.getByLabelText(field.name)).toHaveProp('value', '');
expect(screen.queryByText(`${field.name} is required`)).not.toBeOnTheScreen();
}
});

it('renders a list field as a collapsed combobox rather than a row that opens a page', async () => {
// Given a report holding a list field set to Option1
await renderReportFields(1, [buildListField()]);
Expand Down
29 changes: 27 additions & 2 deletions tests/unit/ReportUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ import {
getDisplayNameForParticipant,
getDisplayNamesWithTooltips,
getEffectiveReportErrors,
getFieldViolation,
getHarvestOriginalReportID,
getIcons,
getIconsForParticipants,
Expand Down Expand Up @@ -27918,6 +27919,27 @@ describe('ReportUtils', () => {
await waitForBatchedUpdates();
});

describe.each([CONST.REPORT.TYPE.INVOICE, CONST.REPORT.TYPE.EXPENSE])('getFieldViolation for %s reports', (type) => {
it.each([
{value: null, defaultValue: '', isEmpty: true},
{value: undefined, defaultValue: '', isEmpty: true},
{value: '', defaultValue: '', isEmpty: true},
{value: 'Saved value', defaultValue: '', isEmpty: false},
{value: null, defaultValue: 'Default value', isEmpty: false},
{value: '', defaultValue: 'Default value', isEmpty: true},
])('validates value $value with default $defaultValue', ({value, defaultValue, isEmpty}) => {
// Given saved and default values, where an explicit empty value must not fall back to the default
const field = {...baseField, value, defaultValue};
const report = {...expenseReport, type};

// When the field is validated in its report context
const violation = getFieldViolation(field, report);

// Then only empty expense fields are required; invoices are exempt regardless of the stored value
expect(violation).toBe(type === CONST.REPORT.TYPE.EXPENSE && isEmpty ? CONST.REPORT_VIOLATIONS.FIELD_REQUIRED : undefined);
});
});

it('should return false when policy does not have areReportFieldsEnabled enabled', () => {
const policyWithFieldsDisabled = {...basePolicy, areReportFieldsEnabled: false};

Expand Down Expand Up @@ -27952,7 +27974,8 @@ describe('ReportUtils', () => {
expect(hasVisibleReportFieldViolations(expenseReport, policyWithEmptyField, currentUserAccountID, undefined, undefined, undefined)).toBe(true);
});

it('should return true when an unpaid invoice report has a required field with no value', async () => {
it('should return false when an unpaid invoice report has an empty field', async () => {
// Given an unpaid invoice with an empty field, which is exempt from required-field validation
const fieldWithNoValue: PolicyReportField = {
...baseField,
target: CONST.REPORT.TYPE.INVOICE,
Expand All @@ -27976,10 +27999,12 @@ describe('ReportUtils', () => {
statusNum: CONST.REPORT.STATUS_NUM.SUBMITTED,
};

// When the invoice policy is loaded
await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, policyWithEmptyInvoiceField);
await waitForBatchedUpdates();

expect(hasVisibleReportFieldViolations(unpaidInvoiceReport, policyWithEmptyInvoiceField, currentUserAccountID, undefined, undefined, undefined)).toBe(true);
// Then no report-field warning is shown for the empty invoice field
expect(hasVisibleReportFieldViolations(unpaidInvoiceReport, policyWithEmptyInvoiceField, currentUserAccountID, undefined, undefined, undefined)).toBe(false);
});

it('should return false when a paid invoice report has a required field with no value', async () => {
Expand Down
Loading