2018-06-07 16:05:22 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2019-11-21 21:51:52 +00:00
|
|
|
import Currency from '../src';
|
2018-06-07 16:05:22 +00:00
|
|
|
|
2020-06-17 23:33:40 +00:00
|
|
|
describe( 'formatAmount', () => {
|
2019-09-23 21:47:08 +00:00
|
|
|
it( 'should use defaults (USD) when currency not passed in', () => {
|
2020-05-19 02:38:02 +00:00
|
|
|
const currency = Currency();
|
2020-06-17 23:33:40 +00:00
|
|
|
expect( currency.formatAmount( 9.99 ) ).toBe( '$9.99' );
|
|
|
|
expect( currency.formatAmount( 30 ) ).toBe( '$30.00' );
|
2018-06-26 21:39:29 +00:00
|
|
|
} );
|
|
|
|
|
2019-01-17 07:04:57 +00:00
|
|
|
it( 'should uses store currency settings, not locale-based', () => {
|
2020-05-19 02:38:02 +00:00
|
|
|
const currency = Currency( {
|
2019-11-21 21:51:52 +00:00
|
|
|
code: 'JPY',
|
|
|
|
symbol: '¥',
|
|
|
|
precision: 3,
|
|
|
|
priceFormat: '%2$s%1$s',
|
|
|
|
thousandSeparator: '.',
|
|
|
|
decimalSeparator: ',',
|
|
|
|
} );
|
2020-06-17 23:33:40 +00:00
|
|
|
expect( currency.formatAmount( 9.49258 ) ).toBe( '9,493¥' );
|
|
|
|
expect( currency.formatAmount( 3000 ) ).toBe( '3.000,000¥' );
|
|
|
|
expect( currency.formatAmount( 3.0002 ) ).toBe( '3,000¥' );
|
2018-06-20 17:43:53 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
it( "should return empty string when given an input that isn't a number", () => {
|
2020-05-19 02:38:02 +00:00
|
|
|
const currency = Currency();
|
2020-06-17 23:33:40 +00:00
|
|
|
expect( currency.formatAmount( 'abc' ) ).toBe( '' );
|
|
|
|
expect( currency.formatAmount( false ) ).toBe( '' );
|
|
|
|
expect( currency.formatAmount( null ) ).toBe( '' );
|
2018-06-07 16:05:22 +00:00
|
|
|
} );
|
2018-06-20 17:43:53 +00:00
|
|
|
} );
|
2018-06-07 16:05:22 +00:00
|
|
|
|
2019-11-21 21:51:52 +00:00
|
|
|
describe( 'currency.formatDecimal', () => {
|
2018-06-20 17:43:53 +00:00
|
|
|
it( 'should round a number to 2 decimal places in USD', () => {
|
2020-05-19 02:38:02 +00:00
|
|
|
const currency = Currency();
|
2019-11-21 21:51:52 +00:00
|
|
|
expect( currency.formatDecimal( 9.49258 ) ).toBe( 9.49 );
|
|
|
|
expect( currency.formatDecimal( 30 ) ).toBe( 30 );
|
|
|
|
expect( currency.formatDecimal( 3.0002 ) ).toBe( 3 );
|
2018-06-20 17:43:53 +00:00
|
|
|
} );
|
2018-06-07 16:05:22 +00:00
|
|
|
|
2018-06-20 17:43:53 +00:00
|
|
|
it( 'should round a number to 0 decimal places in JPY', () => {
|
2020-05-19 02:38:02 +00:00
|
|
|
const currency = Currency( { precision: 0 } );
|
2019-11-21 21:51:52 +00:00
|
|
|
expect( currency.formatDecimal( 1239.88 ) ).toBe( 1240 );
|
|
|
|
expect( currency.formatDecimal( 1500 ) ).toBe( 1500 );
|
|
|
|
expect( currency.formatDecimal( 33715.02 ) ).toBe( 33715 );
|
2018-06-20 17:43:53 +00:00
|
|
|
} );
|
2018-06-07 16:05:22 +00:00
|
|
|
|
|
|
|
it( 'should correctly convert and round a string', () => {
|
2020-05-19 02:38:02 +00:00
|
|
|
const currency = Currency();
|
2019-11-21 21:51:52 +00:00
|
|
|
expect( currency.formatDecimal( '19.80' ) ).toBe( 19.8 );
|
2018-06-20 17:43:53 +00:00
|
|
|
} );
|
|
|
|
|
2018-06-07 16:05:22 +00:00
|
|
|
it( "should return 0 when given an input that isn't a number", () => {
|
2020-05-19 02:38:02 +00:00
|
|
|
const currency = Currency();
|
2019-11-21 21:51:52 +00:00
|
|
|
expect( currency.formatDecimal( 'abc' ) ).toBe( 0 );
|
|
|
|
expect( currency.formatDecimal( false ) ).toBe( 0 );
|
|
|
|
expect( currency.formatDecimal( null ) ).toBe( 0 );
|
2018-06-07 16:05:22 +00:00
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
2019-11-21 21:51:52 +00:00
|
|
|
describe( 'currency.formatDecimalString', () => {
|
2018-06-07 16:05:22 +00:00
|
|
|
it( 'should round a number to 2 decimal places in USD', () => {
|
2020-05-19 02:38:02 +00:00
|
|
|
const currency = Currency();
|
2019-11-21 21:51:52 +00:00
|
|
|
expect( currency.formatDecimalString( 9.49258 ) ).toBe( '9.49' );
|
|
|
|
expect( currency.formatDecimalString( 30 ) ).toBe( '30.00' );
|
|
|
|
expect( currency.formatDecimalString( 3.0002 ) ).toBe( '3.00' );
|
2018-06-07 16:05:22 +00:00
|
|
|
} );
|
|
|
|
|
2018-06-20 17:43:53 +00:00
|
|
|
it( 'should round a number to 0 decimal places in JPY', () => {
|
2020-05-19 02:38:02 +00:00
|
|
|
const currency = Currency( { precision: 0 } );
|
2019-11-21 21:51:52 +00:00
|
|
|
expect( currency.formatDecimalString( 1239.88 ) ).toBe( '1240' );
|
|
|
|
expect( currency.formatDecimalString( 1500 ) ).toBe( '1500' );
|
|
|
|
expect( currency.formatDecimalString( 33715.02 ) ).toBe( '33715' );
|
2018-06-20 17:43:53 +00:00
|
|
|
} );
|
2018-06-07 16:05:22 +00:00
|
|
|
|
|
|
|
it( 'should correctly convert and round a string', () => {
|
2020-05-19 02:38:02 +00:00
|
|
|
const currency = Currency();
|
2019-11-21 21:51:52 +00:00
|
|
|
expect( currency.formatDecimalString( '19.80' ) ).toBe( '19.80' );
|
2018-06-20 17:43:53 +00:00
|
|
|
} );
|
|
|
|
|
2018-06-07 16:05:22 +00:00
|
|
|
it( "should return empty string when given an input that isn't a number", () => {
|
2020-05-19 02:38:02 +00:00
|
|
|
const currency = Currency();
|
2019-11-21 21:51:52 +00:00
|
|
|
expect( currency.formatDecimalString( 'abc' ) ).toBe( '' );
|
|
|
|
expect( currency.formatDecimalString( false ) ).toBe( '' );
|
|
|
|
expect( currency.formatDecimalString( null ) ).toBe( '' );
|
2018-06-07 16:05:22 +00:00
|
|
|
} );
|
|
|
|
} );
|