/
number_pipe_spec.ts
239 lines (202 loc) · 8.71 KB
/
number_pipe_spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/**
* @license
* Copyright Google LLC 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
*/
import {CurrencyPipe, DecimalPipe, PercentPipe} from '@angular/common';
import localeAr from '@angular/common/locales/ar';
import localeDa from '@angular/common/locales/da';
import localeDeAt from '@angular/common/locales/de-AT';
import localeEn from '@angular/common/locales/en';
import localeEsUS from '@angular/common/locales/es-US';
import localeFr from '@angular/common/locales/fr';
import {Component, ɵregisterLocaleData, ɵunregisterLocaleData} from '@angular/core';
import {TestBed} from '@angular/core/testing';
{
describe('Number pipes', () => {
beforeAll(() => {
ɵregisterLocaleData(localeEn);
ɵregisterLocaleData(localeEsUS);
ɵregisterLocaleData(localeFr);
ɵregisterLocaleData(localeAr);
ɵregisterLocaleData(localeDeAt);
ɵregisterLocaleData(localeDa);
});
afterAll(() => ɵunregisterLocaleData());
describe('DecimalPipe', () => {
describe('transform', () => {
let pipe: DecimalPipe;
beforeEach(() => {
pipe = new DecimalPipe('en-US');
});
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');
});
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');
});
it('should return null for NaN', () => {
expect(pipe.transform(Number.NaN)).toEqual(null);
});
it('should return null for null', () => {
expect(pipe.transform(null)).toEqual(null);
});
it('should return null for undefined', () => {
expect(pipe.transform(undefined)).toEqual(null);
});
it('should not support other objects', () => {
expect(() => pipe.transform({} as any))
.toThrowError(
`NG02100: InvalidPipeArgument: '[object Object] is not a number' for pipe 'DecimalPipe'`);
expect(() => pipe.transform('123abc'))
.toThrowError(
`NG02100: InvalidPipeArgument: '123abc is not a number' for pipe 'DecimalPipe'`);
});
});
describe('transform with custom locales', () => {
it('should return the correct format for es-US', () => {
const pipe = new DecimalPipe('es-US');
expect(pipe.transform('9999999.99', '1.2-2')).toEqual('9,999,999.99');
});
});
it('should be available as a standalone pipe', () => {
@Component({
selector: 'test-component',
imports: [DecimalPipe],
template: '{{ value | number }}',
standalone: true,
})
class TestComponent {
value = 12345;
}
const fixture = TestBed.createComponent(TestComponent);
fixture.detectChanges();
const content = fixture.nativeElement.textContent;
expect(content).toBe('12,345');
});
});
describe('PercentPipe', () => {
let pipe: PercentPipe;
beforeEach(() => {
pipe = new PercentPipe('en-US');
});
describe('transform', () => {
it('should return correct value for numbers', () => {
expect(pipe.transform(1.23)).toEqual('123%');
expect(pipe.transform(1.234)).toEqual('123%');
expect(pipe.transform(1.236)).toEqual('124%');
expect(pipe.transform(12.3456, '0.0-10')).toEqual('1,234.56%');
});
it('should return null for NaN', () => {
expect(pipe.transform(Number.NaN)).toEqual(null);
});
it('should return null for null', () => {
expect(pipe.transform(null)).toEqual(null);
});
it('should return null for undefined', () => {
expect(pipe.transform(undefined)).toEqual(null);
});
it('should not support other objects', () => {
expect(() => pipe.transform({} as any))
.toThrowError(
`NG02100: InvalidPipeArgument: '[object Object] is not a number' for pipe 'PercentPipe'`);
});
});
it('should be available as a standalone pipe', () => {
@Component({
selector: 'test-component',
imports: [PercentPipe],
template: '{{ value | percent }}',
standalone: true,
})
class TestComponent {
value = 15;
}
const fixture = TestBed.createComponent(TestComponent);
fixture.detectChanges();
const content = fixture.nativeElement.textContent;
expect(content).toBe('1,500%');
});
});
describe('CurrencyPipe', () => {
let pipe: CurrencyPipe;
beforeEach(() => {
pipe = new CurrencyPipe('en-US', 'USD');
});
describe('transform', () => {
it('should return correct value for numbers', () => {
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', '')).toEqual('5.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');
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\u202f005,12 $');
expect(pipe.transform(5, 'USD', 'symbol', '', 'fr')).toEqual('5,00 $US');
expect(pipe.transform(123456789, 'EUR', 'symbol', '', 'de-at'))
.toEqual('€ 123.456.789,00');
expect(pipe.transform(5.1234, 'EUR', '', '', 'de-at')).toEqual('5,12');
expect(pipe.transform(5.1234, 'DKK', '', '', 'da')).toEqual('5,12');
});
it('should use the injected default currency code if none is provided', () => {
const clpPipe = new CurrencyPipe('en-US', 'CLP');
expect(clpPipe.transform(1234)).toEqual('CLP1,234');
});
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');
});
it('should return null for NaN', () => {
expect(pipe.transform(Number.NaN)).toEqual(null);
});
it('should return null for null', () => {
expect(pipe.transform(null)).toEqual(null);
});
it('should return null for undefined', () => {
expect(pipe.transform(undefined)).toEqual(null);
});
it('should not support other objects', () => {
expect(() => pipe.transform({} as any))
.toThrowError(
`NG02100: InvalidPipeArgument: '[object Object] is not a number' for pipe 'CurrencyPipe'`);
});
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".`);
});
});
it('should be available as a standalone pipe', () => {
@Component({
selector: 'test-component',
imports: [CurrencyPipe],
template: '{{ value | currency }}',
standalone: true,
})
class TestComponent {
value = 15;
}
const fixture = TestBed.createComponent(TestComponent);
fixture.detectChanges();
const content = fixture.nativeElement.textContent;
expect(content).toBe('$15.00');
});
});
});
}