2018-06-07 16:05:22 +00:00
|
|
|
/** @format */
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2019-11-21 21:51:52 +00:00
|
|
|
import Currency from '../src';
|
2018-06-07 16:05:22 +00:00
|
|
|
|
2018-06-20 17:43:53 +00:00
|
|
|
describe( 'formatCurrency', () => {
|
2019-09-23 21:47:08 +00:00
|
|
|
it( 'should use defaults (USD) when currency not passed in', () => {
|
2019-11-21 21:51:52 +00:00
|
|
|
const currency = new Currency();
|
|
|
|
expect( currency.formatCurrency( 9.99 ) ).toBe( '$9.99' );
|
|
|
|
expect( currency.formatCurrency( 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', () => {
|
2019-11-21 21:51:52 +00:00
|
|
|
const currency = new Currency( {
|
|
|
|
code: 'JPY',
|
|
|
|
symbol: '¥',
|
|
|
|
precision: 3,
|
|
|
|
priceFormat: '%2$s%1$s',
|
|
|
|
thousandSeparator: '.',
|
|
|
|
decimalSeparator: ',',
|
|
|
|
} );
|
|
|
|
expect( currency.formatCurrency( 9.49258 ) ).toBe( '9,493¥' );
|
|
|
|
expect( currency.formatCurrency( 3000 ) ).toBe( '3.000,000¥' );
|
|
|
|
expect( currency.formatCurrency( 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", () => {
|
2019-11-21 21:51:52 +00:00
|
|
|
const currency = new Currency();
|
|
|
|
expect( currency.formatCurrency( 'abc' ) ).toBe( '' );
|
|
|
|
expect( currency.formatCurrency( false ) ).toBe( '' );
|
|
|
|
expect( currency.formatCurrency( 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', () => {
|
2019-11-21 21:51:52 +00:00
|
|
|
const currency = new Currency();
|
|
|
|
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', () => {
|
2019-11-21 21:51:52 +00:00
|
|
|
const currency = new Currency( { precision: 0 } );
|
|
|
|
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', () => {
|
2019-11-21 21:51:52 +00:00
|
|
|
const currency = new Currency();
|
|
|
|
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", () => {
|
2019-11-21 21:51:52 +00:00
|
|
|
const currency = new Currency();
|
|
|
|
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', () => {
|
2019-11-21 21:51:52 +00:00
|
|
|
const currency = new Currency();
|
|
|
|
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', () => {
|
2019-11-21 21:51:52 +00:00
|
|
|
const currency = new Currency( { precision: 0 } );
|
|
|
|
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', () => {
|
2019-11-21 21:51:52 +00:00
|
|
|
const currency = new Currency();
|
|
|
|
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", () => {
|
2019-11-21 21:51:52 +00:00
|
|
|
const currency = new Currency();
|
|
|
|
expect( currency.formatDecimalString( 'abc' ) ).toBe( '' );
|
|
|
|
expect( currency.formatDecimalString( false ) ).toBe( '' );
|
|
|
|
expect( currency.formatDecimalString( null ) ).toBe( '' );
|
2018-06-07 16:05:22 +00:00
|
|
|
} );
|
|
|
|
} );
|