2018-06-07 16:05:22 +00:00
|
|
|
/** @format */
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2019-09-23 21:47:08 +00:00
|
|
|
import {
|
|
|
|
formatCurrency,
|
|
|
|
getCurrencyFormatDecimal,
|
|
|
|
getCurrencyFormatString,
|
|
|
|
} from '../src';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* WooCommerce dependencies
|
|
|
|
* Note: setCurrencyProp doesn't exist on the module alias, it's used for mocking
|
|
|
|
* values.
|
|
|
|
*/
|
|
|
|
import { setCurrencyProp, resetMock } from '@woocommerce/wc-admin-settings';
|
|
|
|
|
|
|
|
beforeEach( () => {
|
|
|
|
resetMock();
|
|
|
|
} );
|
|
|
|
|
|
|
|
jest.mock( '@woocommerce/wc-admin-settings', () => {
|
|
|
|
let mockedCurrency = jest.requireActual( '@woocommerce/wc-admin-settings' ).CURRENCY;
|
|
|
|
const originalCurrency = {
|
|
|
|
...mockedCurrency,
|
|
|
|
};
|
|
|
|
const reset = () => {
|
|
|
|
mockedCurrency = Object.assign( mockedCurrency, originalCurrency );
|
|
|
|
};
|
|
|
|
return {
|
|
|
|
setCurrencyProp: ( prop, value ) => {
|
|
|
|
mockedCurrency[ prop ] = value;
|
|
|
|
},
|
|
|
|
resetMock: reset,
|
|
|
|
CURRENCY: mockedCurrency,
|
|
|
|
};
|
|
|
|
} );
|
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', () => {
|
2018-06-26 21:39:29 +00:00
|
|
|
expect( formatCurrency( 9.99 ) ).toBe( '$9.99' );
|
|
|
|
expect( formatCurrency( 30 ) ).toBe( '$30.00' );
|
|
|
|
} );
|
|
|
|
|
2019-01-17 07:04:57 +00:00
|
|
|
it( 'should uses store currency settings, not locale-based', () => {
|
2019-09-23 21:47:08 +00:00
|
|
|
setCurrencyProp( 'code', 'JPY' );
|
|
|
|
setCurrencyProp( 'precision', 3 );
|
|
|
|
setCurrencyProp( 'priceFormat', '%2$s%1$s' );
|
|
|
|
setCurrencyProp( 'thousandSeparator', '.' );
|
|
|
|
setCurrencyProp( 'decimalSeparator', ',' );
|
2019-01-17 07:04:57 +00:00
|
|
|
expect( formatCurrency( 9.49258, '¥' ) ).toBe( '9,493¥' );
|
|
|
|
expect( formatCurrency( 3000, '¥' ) ).toBe( '3.000,000¥' );
|
|
|
|
expect( 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-01-17 07:04:57 +00:00
|
|
|
expect( formatCurrency( 'abc' ) ).toBe( '' );
|
|
|
|
expect( formatCurrency( false ) ).toBe( '' );
|
|
|
|
expect( 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
|
|
|
|
2018-06-20 17:43:53 +00:00
|
|
|
describe( 'getCurrencyFormatDecimal', () => {
|
|
|
|
it( 'should round a number to 2 decimal places in USD', () => {
|
|
|
|
expect( getCurrencyFormatDecimal( 9.49258 ) ).toBe( 9.49 );
|
|
|
|
expect( getCurrencyFormatDecimal( 30 ) ).toBe( 30 );
|
|
|
|
expect( getCurrencyFormatDecimal( 3.0002 ) ).toBe( 3 );
|
|
|
|
} );
|
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-09-23 21:47:08 +00:00
|
|
|
setCurrencyProp( 'precision', 0 );
|
2018-06-20 17:43:53 +00:00
|
|
|
expect( getCurrencyFormatDecimal( 1239.88 ) ).toBe( 1240 );
|
|
|
|
expect( getCurrencyFormatDecimal( 1500 ) ).toBe( 1500 );
|
|
|
|
expect( getCurrencyFormatDecimal( 33715.02 ) ).toBe( 33715 );
|
|
|
|
} );
|
2018-06-07 16:05:22 +00:00
|
|
|
|
|
|
|
it( 'should correctly convert and round a string', () => {
|
2018-06-20 17:43:53 +00:00
|
|
|
expect( getCurrencyFormatDecimal( '19.80' ) ).toBe( 19.8 );
|
|
|
|
} );
|
|
|
|
|
2018-06-07 16:05:22 +00:00
|
|
|
it( "should return 0 when given an input that isn't a number", () => {
|
2018-06-20 17:43:53 +00:00
|
|
|
expect( getCurrencyFormatDecimal( 'abc' ) ).toBe( 0 );
|
|
|
|
expect( getCurrencyFormatDecimal( false ) ).toBe( 0 );
|
|
|
|
expect( getCurrencyFormatDecimal( null ) ).toBe( 0 );
|
2018-06-07 16:05:22 +00:00
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
|
|
|
describe( 'getCurrencyFormatString', () => {
|
|
|
|
it( 'should round a number to 2 decimal places in USD', () => {
|
2018-06-20 17:43:53 +00:00
|
|
|
expect( getCurrencyFormatString( 9.49258 ) ).toBe( '9.49' );
|
|
|
|
expect( getCurrencyFormatString( 30 ) ).toBe( '30.00' );
|
|
|
|
expect( getCurrencyFormatString( 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-09-23 21:47:08 +00:00
|
|
|
setCurrencyProp( 'precision', 0 );
|
2018-06-20 17:43:53 +00:00
|
|
|
expect( getCurrencyFormatString( 1239.88 ) ).toBe( '1240' );
|
|
|
|
expect( getCurrencyFormatString( 1500 ) ).toBe( '1500' );
|
|
|
|
expect( getCurrencyFormatString( 33715.02 ) ).toBe( '33715' );
|
|
|
|
} );
|
2018-06-07 16:05:22 +00:00
|
|
|
|
|
|
|
it( 'should correctly convert and round a string', () => {
|
2018-06-20 17:43:53 +00:00
|
|
|
expect( getCurrencyFormatString( '19.80' ) ).toBe( '19.80' );
|
|
|
|
} );
|
|
|
|
|
2018-06-07 16:05:22 +00:00
|
|
|
it( "should return empty string when given an input that isn't a number", () => {
|
2018-06-20 17:43:53 +00:00
|
|
|
expect( getCurrencyFormatString( 'abc' ) ).toBe( '' );
|
|
|
|
expect( getCurrencyFormatString( false ) ).toBe( '' );
|
|
|
|
expect( getCurrencyFormatString( null ) ).toBe( '' );
|
2018-06-07 16:05:22 +00:00
|
|
|
} );
|
|
|
|
} );
|