2016-06-23 09:47:54 -07:00
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
2017-12-16 20:08:49 -08:00
import localeEn from '@angular/common/locales/en' ;
import localeEsUS from '@angular/common/locales/es-US' ;
import localeFr from '@angular/common/locales/fr' ;
2018-01-18 14:04:41 +01:00
import localeAr from '@angular/common/locales/ar' ;
2018-06-25 16:49:46 +02:00
import localeDeAt from '@angular/common/locales/de-AT' ;
2018-02-23 22:28:07 +01:00
import { registerLocaleData , CurrencyPipe , DecimalPipe , PercentPipe , formatNumber } from '@angular/common' ;
2017-03-02 12:12:46 -08:00
import { beforeEach , describe , expect , it } from '@angular/core/testing/src/testing_internal' ;
2015-07-04 22:25:43 +04:30
2017-12-16 14:42:55 -08:00
{
2016-02-10 16:54:32 -08:00
describe ( 'Number pipes' , ( ) = > {
2017-08-22 20:30:59 +02:00
beforeAll ( ( ) = > {
registerLocaleData ( localeEn ) ;
registerLocaleData ( localeEsUS ) ;
2017-08-29 18:54:17 +02:00
registerLocaleData ( localeFr ) ;
2018-01-18 14:04:41 +01:00
registerLocaleData ( localeAr ) ;
2018-06-25 16:49:46 +02:00
registerLocaleData ( localeDeAt ) ;
2017-08-22 20:30:59 +02:00
} ) ;
2015-07-04 22:25:43 +04:30
2017-08-22 20:30:59 +02:00
describe ( 'DecimalPipe' , ( ) = > {
2016-09-01 00:55:13 +02:00
describe ( 'transform' , ( ) = > {
2017-08-22 20:30:59 +02:00
let pipe : DecimalPipe ;
beforeEach ( ( ) = > { pipe = new DecimalPipe ( 'en-US' ) ; } ) ;
2016-09-01 00:55:13 +02:00
it ( 'should return correct value for numbers' , ( ) = > {
expect ( pipe . transform ( 12345 ) ) . toEqual ( '12,345' ) ;
expect ( pipe . transform ( 1.123456 , '3.4-5' ) ) . toEqual ( '001.12346' ) ;
} ) ;
2015-08-06 10:39:02 -07:00
2016-09-01 00:55:13 +02:00
it ( 'should support strings' , ( ) = > {
expect ( pipe . transform ( '12345' ) ) . toEqual ( '12,345' ) ;
expect ( pipe . transform ( '123' , '.2' ) ) . toEqual ( '123.00' ) ;
expect ( pipe . transform ( '1' , '3.' ) ) . toEqual ( '001' ) ;
expect ( pipe . transform ( '1.1' , '3.4-5' ) ) . toEqual ( '001.1000' ) ;
expect ( pipe . transform ( '1.123456' , '3.4-5' ) ) . toEqual ( '001.12346' ) ;
expect ( pipe . transform ( '1.1234' ) ) . toEqual ( '1.123' ) ;
} ) ;
2016-07-19 11:27:06 -07:00
2016-09-01 00:55:13 +02:00
it ( 'should not support other objects' , ( ) = > {
2017-09-26 19:18:26 +02:00
expect ( ( ) = > pipe . transform ( { } ) )
. toThrowError (
` InvalidPipeArgument: '[object Object] is not a number' for pipe 'DecimalPipe' ` ) ;
expect ( ( ) = > pipe . transform ( '123abc' ) )
. toThrowError ( ` InvalidPipeArgument: '123abc is not a number' for pipe 'DecimalPipe' ` ) ;
2016-02-10 16:54:32 -08:00
} ) ;
2017-08-22 20:30:59 +02:00
} ) ;
describe ( 'transform with custom locales' , ( ) = > {
2017-08-23 17:10:27 -07:00
it ( 'should return the correct format for es-US' , ( ) = > {
2017-08-22 20:30:59 +02:00
const pipe = new DecimalPipe ( 'es-US' ) ;
expect ( pipe . transform ( '9999999.99' , '1.2-2' ) ) . toEqual ( '9,999,999.99' ) ;
} ) ;
2015-08-18 16:16:30 +02:00
} ) ;
2016-09-01 00:55:13 +02:00
} ) ;
2015-07-04 22:25:43 +04:30
2016-09-01 00:55:13 +02:00
describe ( 'PercentPipe' , ( ) = > {
2016-11-12 14:08:58 +01:00
let pipe : PercentPipe ;
2015-07-04 22:25:43 +04:30
2016-09-01 00:55:13 +02:00
beforeEach ( ( ) = > { pipe = new PercentPipe ( 'en-US' ) ; } ) ;
2015-07-04 22:25:43 +04:30
2016-09-01 00:55:13 +02:00
describe ( 'transform' , ( ) = > {
it ( 'should return correct value for numbers' , ( ) = > {
2017-08-22 20:30:59 +02:00
expect ( pipe . transform ( 1.23 ) ) . toEqual ( '123%' ) ;
2018-11-29 22:47:40 -05:00
expect ( pipe . transform ( 1.234 ) ) . toEqual ( '123%' ) ;
expect ( pipe . transform ( 1.236 ) ) . toEqual ( '124%' ) ;
2017-11-10 10:41:15 +01:00
expect ( pipe . transform ( 12.3456 , '0.0-10' ) ) . toEqual ( '1,234.56%' ) ;
2016-02-10 16:54:32 -08:00
} ) ;
2015-07-04 22:25:43 +04:30
2017-09-26 19:18:26 +02:00
it ( 'should not support other objects' , ( ) = > {
expect ( ( ) = > pipe . transform ( { } ) )
. toThrowError (
` InvalidPipeArgument: '[object Object] is not a number' for pipe 'PercentPipe' ` ) ;
} ) ;
2016-09-01 00:55:13 +02:00
} ) ;
} ) ;
2015-07-04 22:25:43 +04:30
2016-09-01 00:55:13 +02:00
describe ( 'CurrencyPipe' , ( ) = > {
2016-11-12 14:08:58 +01:00
let pipe : CurrencyPipe ;
2015-07-04 22:25:43 +04:30
2016-09-01 00:55:13 +02:00
beforeEach ( ( ) = > { pipe = new CurrencyPipe ( 'en-US' ) ; } ) ;
2015-08-06 10:39:02 -07:00
2016-09-01 00:55:13 +02:00
describe ( 'transform' , ( ) = > {
it ( 'should return correct value for numbers' , ( ) = > {
2017-08-22 20:30:59 +02:00
expect ( pipe . transform ( 123 ) ) . toEqual ( '$123.00' ) ;
expect ( pipe . transform ( 12 , 'EUR' , 'code' , '.1' ) ) . toEqual ( 'EUR12.0' ) ;
expect ( pipe . transform ( 5.1234 , 'USD' , 'code' , '.0-3' ) ) . toEqual ( 'USD5.123' ) ;
expect ( pipe . transform ( 5.1234 , 'USD' , 'code' ) ) . toEqual ( 'USD5.12' ) ;
expect ( pipe . transform ( 5.1234 , 'USD' , 'symbol' ) ) . toEqual ( '$5.12' ) ;
expect ( pipe . transform ( 5.1234 , 'CAD' , 'symbol' ) ) . toEqual ( 'CA$5.12' ) ;
expect ( pipe . transform ( 5.1234 , 'CAD' , 'symbol-narrow' ) ) . toEqual ( '$5.12' ) ;
2017-08-29 18:54:17 +02:00
expect ( pipe . transform ( 5.1234 , 'CAD' , 'symbol-narrow' , '5.2-2' ) ) . toEqual ( '$00,005.12' ) ;
expect ( pipe . transform ( 5.1234 , 'CAD' , 'symbol-narrow' , '5.2-2' , 'fr' ) )
. toEqual ( '00 005,12 $' ) ;
2018-01-26 11:06:13 +01:00
expect ( pipe . transform ( 5 , 'USD' , 'symbol' , '' , 'fr' ) ) . toEqual ( '5,00 $US' ) ;
2018-06-25 16:49:46 +02:00
expect ( pipe . transform ( 123456789 , 'EUR' , 'symbol' , '' , 'de-at' ) )
. toEqual ( '€ 123.456.789,00' ) ;
2018-01-26 11:06:13 +01:00
} ) ;
it ( 'should support any currency code name' , ( ) = > {
// currency code is unknown, default formatting options will be used
expect ( pipe . transform ( 5.1234 , 'unexisting_ISO_code' , 'symbol' ) )
. toEqual ( 'unexisting_ISO_code5.12' ) ;
// currency code is USD, the pipe will format based on USD but will display "Custom name"
expect ( pipe . transform ( 5.1234 , 'USD' , 'Custom name' ) ) . toEqual ( 'Custom name5.12' ) ;
2016-02-10 16:54:32 -08:00
} ) ;
2016-09-01 00:55:13 +02:00
2017-09-26 19:18:26 +02:00
it ( 'should not support other objects' , ( ) = > {
expect ( ( ) = > pipe . transform ( { } ) )
. toThrowError (
` InvalidPipeArgument: '[object Object] is not a number' for pipe 'CurrencyPipe' ` ) ;
} ) ;
2017-08-22 20:30:59 +02:00
it ( 'should warn if you are using the v4 signature' , ( ) = > {
const warnSpy = spyOn ( console , 'warn' ) ;
pipe . transform ( 123 , 'USD' , true ) ;
expect ( warnSpy ) . toHaveBeenCalledWith (
` Warning: the currency pipe has been changed in Angular v5. The symbolDisplay option (third parameter) is now a string instead of a boolean. The accepted values are "code", "symbol" or "symbol-narrow". ` ) ;
} ) ;
2015-08-18 16:16:30 +02:00
} ) ;
2016-09-01 00:55:13 +02:00
} ) ;
2016-02-10 16:54:32 -08:00
} ) ;
2015-07-04 22:25:43 +04:30
}