2016-06-23 12:47:54 -04: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
|
|
|
|
*/
|
|
|
|
|
2016-08-02 18:53:34 -04:00
|
|
|
import {CurrencyPipe, DecimalPipe, PercentPipe} from '@angular/common';
|
|
|
|
import {afterEach, beforeEach, ddescribe, describe, expect, iit, it, xit} from '@angular/core/testing/testing_internal';
|
2016-06-23 19:42:25 -04:00
|
|
|
import {browserDetection} from '@angular/platform-browser/testing/browser_util';
|
2015-07-04 13:55:43 -04:00
|
|
|
|
|
|
|
export function main() {
|
2016-02-10 19:54:32 -05:00
|
|
|
describe('Number pipes', () => {
|
2016-08-31 18:55:13 -04:00
|
|
|
describe('DecimalPipe', () => {
|
|
|
|
var pipe: DecimalPipe;
|
2015-07-04 13:55:43 -04:00
|
|
|
|
2016-08-31 18:55:13 -04:00
|
|
|
beforeEach(() => { pipe = new DecimalPipe('en-US'); });
|
2015-07-04 13:55:43 -04:00
|
|
|
|
2016-08-31 18:55:13 -04:00
|
|
|
describe('transform', () => {
|
|
|
|
it('should return correct value for numbers', () => {
|
|
|
|
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');
|
|
|
|
});
|
2015-08-06 13:39:02 -04:00
|
|
|
|
2016-08-31 18:55:13 -04: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 14:27:06 -04:00
|
|
|
|
2016-08-31 18:55:13 -04:00
|
|
|
it('should not support other objects', () => {
|
|
|
|
expect(() => pipe.transform(new Object())).toThrowError();
|
|
|
|
expect(() => pipe.transform('123abc')).toThrowError();
|
2016-02-10 19:54:32 -05:00
|
|
|
});
|
2015-08-18 10:16:30 -04:00
|
|
|
});
|
2016-08-31 18:55:13 -04:00
|
|
|
});
|
2015-07-04 13:55:43 -04:00
|
|
|
|
2016-08-31 18:55:13 -04:00
|
|
|
describe('PercentPipe', () => {
|
|
|
|
var pipe: PercentPipe;
|
2015-07-04 13:55:43 -04:00
|
|
|
|
2016-08-31 18:55:13 -04:00
|
|
|
beforeEach(() => { pipe = new PercentPipe('en-US'); });
|
2015-07-04 13:55:43 -04:00
|
|
|
|
2016-08-31 18:55:13 -04:00
|
|
|
describe('transform', () => {
|
|
|
|
it('should return correct value for numbers', () => {
|
|
|
|
expect(normalize(pipe.transform(1.23))).toEqual('123%');
|
|
|
|
expect(normalize(pipe.transform(1.2, '.2'))).toEqual('120.00%');
|
2016-02-10 19:54:32 -05:00
|
|
|
});
|
2015-07-04 13:55:43 -04:00
|
|
|
|
2016-08-31 18:55:13 -04:00
|
|
|
it('should not support other objects',
|
|
|
|
() => { expect(() => pipe.transform(new Object())).toThrowError(); });
|
|
|
|
});
|
|
|
|
});
|
2015-07-04 13:55:43 -04:00
|
|
|
|
2016-08-31 18:55:13 -04:00
|
|
|
describe('CurrencyPipe', () => {
|
|
|
|
var pipe: CurrencyPipe;
|
2015-07-04 13:55:43 -04:00
|
|
|
|
2016-08-31 18:55:13 -04:00
|
|
|
beforeEach(() => { pipe = new CurrencyPipe('en-US'); });
|
2015-08-06 13:39:02 -04:00
|
|
|
|
2016-08-31 18:55:13 -04:00
|
|
|
describe('transform', () => {
|
|
|
|
it('should return correct value for numbers', () => {
|
|
|
|
// In old Chrome, default formatiing for USD is different
|
|
|
|
if (browserDetection.isOldChrome) {
|
|
|
|
expect(normalize(pipe.transform(123))).toEqual('USD123');
|
|
|
|
} else {
|
|
|
|
expect(normalize(pipe.transform(123))).toEqual('USD123.00');
|
|
|
|
}
|
|
|
|
expect(normalize(pipe.transform(12, 'EUR', false, '.1'))).toEqual('EUR12.0');
|
|
|
|
expect(normalize(pipe.transform(5.1234, 'USD', false, '.0-3'))).toEqual('USD5.123');
|
2016-02-10 19:54:32 -05:00
|
|
|
});
|
2016-08-31 18:55:13 -04:00
|
|
|
|
|
|
|
it('should not support other objects',
|
|
|
|
() => { expect(() => pipe.transform(new Object())).toThrowError(); });
|
2015-08-18 10:16:30 -04:00
|
|
|
});
|
2016-08-31 18:55:13 -04:00
|
|
|
});
|
2016-02-10 19:54:32 -05:00
|
|
|
});
|
2015-07-04 13:55:43 -04:00
|
|
|
}
|
2016-08-31 18:55:13 -04:00
|
|
|
|
|
|
|
// Between the symbol and the number, Edge adds a no breaking space and IE11 adds a standard space
|
|
|
|
function normalize(s: string): string {
|
|
|
|
return s.replace(/\u00A0| /g, '');
|
|
|
|
}
|