diff --git a/js/src/components/paid-ads/validateCampaign.js b/js/src/components/paid-ads/validateCampaign.js index a39222f296..b8c4f011a5 100644 --- a/js/src/components/paid-ads/validateCampaign.js +++ b/js/src/components/paid-ads/validateCampaign.js @@ -11,6 +11,7 @@ import { __, sprintf } from '@wordpress/i18n'; * @typedef {Object} ValidateCampaignOptions * @property {number | undefined} dailyBudget Daily budget for the campaign. * @property {Function} formatAmount A function to format the budget amount according to the currency settings. + * @property {number} precision Number of decimal for the amount. */ // Minimum percentage of the recommended daily budget. @@ -33,11 +34,11 @@ const validateCampaign = ( values, opts ) => { const { amount } = values; const { dailyBudget, formatAmount, precision } = opts; - const minAmount = parseFloat( - ( dailyBudget * BUDGET_MIN_PERCENT ).toFixed( precision ) + const minAmount = ( dailyBudget * BUDGET_MIN_PERCENT ).toFixed( + precision ); - if ( amount < minAmount ) { + if ( amount < parseFloat( minAmount ) ) { return { amount: sprintf( /* translators: %1$s: minimum daily budget */ diff --git a/js/src/components/paid-ads/validateCampaign.test.js b/js/src/components/paid-ads/validateCampaign.test.js index a9ca4edb24..103543ff42 100644 --- a/js/src/components/paid-ads/validateCampaign.test.js +++ b/js/src/components/paid-ads/validateCampaign.test.js @@ -12,6 +12,7 @@ describe( 'validateCampaign', () => { const validateCampaignOptions = { dailyBudget: undefined, formatAmount: jest.mock(), + precision: 0, }; beforeEach( () => { @@ -87,6 +88,7 @@ describe( 'validateCampaign', () => { const opts = { dailyBudget: 100, formatAmount: mockFormatAmount, + precision: 0, }; const errors = validateCampaign( values, opts ); @@ -120,4 +122,21 @@ describe( 'validateCampaign', () => { const errors = validateCampaign( values, opts ); expect( errors ).not.toHaveProperty( 'amount' ); } ); + + it( 'When precision is set, the correct amount should be displayed', () => { + const mockFormatAmount = jest + .fn() + .mockImplementation( ( amount ) => `Rs ${ amount }` ); + + values.amount = 10; + + const opts = { + dailyBudget: 100, + formatAmount: mockFormatAmount, + precision: 2, + }; + + const errors = validateCampaign( values, opts ); + expect( errors.amount ).toContain( 'is at least Rs 30.00' ); + } ); } );