Skip to content

Commit

Permalink
Convert to float.
Browse files Browse the repository at this point in the history
  • Loading branch information
asvinb committed Sep 27, 2024
1 parent 7432760 commit f23a52c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
7 changes: 4 additions & 3 deletions js/src/components/paid-ads/validateCampaign.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 */
Expand Down
19 changes: 19 additions & 0 deletions js/src/components/paid-ads/validateCampaign.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ describe( 'validateCampaign', () => {
const validateCampaignOptions = {
dailyBudget: undefined,
formatAmount: jest.mock(),
precision: 0,
};

beforeEach( () => {
Expand Down Expand Up @@ -87,6 +88,7 @@ describe( 'validateCampaign', () => {
const opts = {
dailyBudget: 100,
formatAmount: mockFormatAmount,
precision: 0,
};

const errors = validateCampaign( values, opts );
Expand Down Expand Up @@ -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' );
} );
} );

0 comments on commit f23a52c

Please sign in to comment.