diff --git a/src/components/MoneyRequestReportView/MoneyRequestViewReportFields.tsx b/src/components/MoneyRequestReportView/MoneyRequestViewReportFields.tsx index 63e6b3acab88..c498fef120d2 100644 --- a/src/components/MoneyRequestReportView/MoneyRequestViewReportFields.tsx +++ b/src/components/MoneyRequestReportView/MoneyRequestViewReportFields.tsx @@ -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, diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 367e840f1de0..1028eedbcb49 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -11200,7 +11200,7 @@ function hasVisibleReportFieldViolations( if (isReportFieldDisabledForUser(report, field, policy, currentUserAccountID, rules, reportOwnerLogin)) { return false; } - return !!getFieldViolation(field); + return !!getFieldViolation(field, report); }); } @@ -14478,8 +14478,8 @@ function getChatUsedForOnboarding(onboardingValue: OnyxEntry, 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): ReportViolationName | undefined { + if (!reportField || isInvoiceReport(report)) { return undefined; } diff --git a/tests/ui/MoneyRequestViewReportFieldsTest.tsx b/tests/ui/MoneyRequestViewReportFieldsTest.tsx index 27d512563a15..25177a9ad2e4 100644 --- a/tests/ui/MoneyRequestViewReportFieldsTest.tsx +++ b/tests/ui/MoneyRequestViewReportFieldsTest.tsx @@ -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, }; }; @@ -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; 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(); @@ -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()]); diff --git a/tests/unit/ReportUtilsTest.ts b/tests/unit/ReportUtilsTest.ts index a88ffe4a5aa2..e5f2e22ab864 100644 --- a/tests/unit/ReportUtilsTest.ts +++ b/tests/unit/ReportUtilsTest.ts @@ -113,6 +113,7 @@ import { getDisplayNameForParticipant, getDisplayNamesWithTooltips, getEffectiveReportErrors, + getFieldViolation, getHarvestOriginalReportID, getIcons, getIconsForParticipants, @@ -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}; @@ -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, @@ -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 () => {