2019-12-16 16:48:02 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2019-12-18 11:29:20 +00:00
|
|
|
import { formatPrice, getCurrency } from '../price';
|
2019-12-16 16:48:02 +00:00
|
|
|
|
|
|
|
describe( 'formatPrice', () => {
|
|
|
|
test.each`
|
2021-11-19 12:32:37 +00:00
|
|
|
value | prefix | suffix | expected
|
|
|
|
${ 1000 } | ${ '€' } | ${ '' } | ${ '€10' }
|
|
|
|
${ 1000 } | ${ '' } | ${ '€' } | ${ '10€' }
|
|
|
|
${ 1000 } | ${ '' } | ${ '$' } | ${ '10$' }
|
|
|
|
${ '1000' } | ${ '€' } | ${ '' } | ${ '€10' }
|
|
|
|
${ 0 } | ${ '€' } | ${ '' } | ${ '€0' }
|
|
|
|
${ '' } | ${ '€' } | ${ '' } | ${ '' }
|
|
|
|
${ null } | ${ '€' } | ${ '' } | ${ '' }
|
|
|
|
${ undefined } | ${ '€' } | ${ '' } | ${ '' }
|
|
|
|
${ 100000 } | ${ '€' } | ${ '' } | ${ '€1,000' }
|
|
|
|
${ 1000000 } | ${ '€' } | ${ '' } | ${ '€10,000' }
|
|
|
|
${ 1000000000 } | ${ '€' } | ${ '' } | ${ '€10,000,000' }
|
2019-12-16 16:48:02 +00:00
|
|
|
`(
|
2021-11-19 12:32:37 +00:00
|
|
|
'correctly formats price given "$value", "$prefix" prefix, and "$suffix" suffix as "$expected"',
|
2019-12-18 11:29:20 +00:00
|
|
|
( { value, prefix, suffix, expected } ) => {
|
2019-12-16 16:48:02 +00:00
|
|
|
const formattedPrice = formatPrice(
|
|
|
|
value,
|
2019-12-18 11:29:20 +00:00
|
|
|
getCurrency( { prefix, suffix } )
|
2019-12-16 16:48:02 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
expect( formattedPrice ).toEqual( expected );
|
|
|
|
}
|
|
|
|
);
|
2021-10-04 03:54:28 +00:00
|
|
|
|
2021-11-19 12:32:37 +00:00
|
|
|
test.each`
|
|
|
|
value | prefix | decimalSeparator | thousandSeparator | expected
|
|
|
|
${ 1000000099 } | ${ '$' } | ${ '.' } | ${ ',' } | ${ '$10,000,000.99' }
|
|
|
|
${ 1000000099 } | ${ '$' } | ${ ',' } | ${ '.' } | ${ '$10.000.000,99' }
|
|
|
|
`(
|
|
|
|
'correctly formats price given "$value", "$prefix" prefix, "$decimalSeparator" decimal separator, "$thousandSeparator" thousand separator as "$expected"',
|
|
|
|
( {
|
|
|
|
value,
|
|
|
|
prefix,
|
|
|
|
decimalSeparator,
|
|
|
|
thousandSeparator,
|
|
|
|
expected,
|
|
|
|
} ) => {
|
|
|
|
const formattedPrice = formatPrice(
|
|
|
|
value,
|
|
|
|
getCurrency( { prefix, decimalSeparator, thousandSeparator } )
|
|
|
|
);
|
|
|
|
|
|
|
|
expect( formattedPrice ).toEqual( expected );
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2021-10-04 03:54:28 +00:00
|
|
|
test.each`
|
|
|
|
value | expected
|
|
|
|
${ 1000 } | ${ '$10' }
|
|
|
|
${ 0 } | ${ '$0' }
|
|
|
|
${ '' } | ${ '' }
|
|
|
|
${ null } | ${ '' }
|
|
|
|
${ undefined } | ${ '' }
|
|
|
|
`(
|
2021-11-19 12:32:37 +00:00
|
|
|
'correctly formats price given "$value" only as "$expected"',
|
2021-10-04 03:54:28 +00:00
|
|
|
( { value, expected } ) => {
|
|
|
|
const formattedPrice = formatPrice( value );
|
|
|
|
|
|
|
|
expect( formattedPrice ).toEqual( expected );
|
|
|
|
}
|
|
|
|
);
|
2019-12-16 16:48:02 +00:00
|
|
|
} );
|