|
|
import { getCachedFormatter } from '../src/get-cached-formatter'; |
|
|
|
|
|
describe( 'getFormatter', () => { |
|
|
it( 'should return a cached formatter when one exists', () => { |
|
|
const locale = 'en-US'; |
|
|
const options: Intl.NumberFormatOptions = { style: 'currency', currency: 'USD' }; |
|
|
|
|
|
|
|
|
const formatter1 = getCachedFormatter( { locale, options } ); |
|
|
|
|
|
|
|
|
const formatter2 = getCachedFormatter( { locale, options } ); |
|
|
|
|
|
|
|
|
expect( formatter1 ).toBe( formatter2 ); |
|
|
|
|
|
|
|
|
expect( formatter1.format( 1234.56 ) ).toBe( '$1,234.56' ); |
|
|
} ); |
|
|
|
|
|
it( 'should create a new formatter when options are different', () => { |
|
|
const locale = 'en-US'; |
|
|
const options1: Intl.NumberFormatOptions = { style: 'currency', currency: 'USD' }; |
|
|
const options2: Intl.NumberFormatOptions = { style: 'currency', currency: 'EUR' }; |
|
|
|
|
|
|
|
|
const formatter1 = getCachedFormatter( { locale, options: options1 } ); |
|
|
|
|
|
|
|
|
const formatter2 = getCachedFormatter( { locale, options: options2 } ); |
|
|
|
|
|
|
|
|
expect( formatter1 ).not.toBe( formatter2 ); |
|
|
|
|
|
|
|
|
expect( formatter1.format( 1234.56 ) ).toBe( '$1,234.56' ); |
|
|
expect( formatter2.format( 1234.56 ) ).toBe( '€1,234.56' ); |
|
|
} ); |
|
|
} ); |
|
|
|