Upgrade to ExtJS 4.0.0 - Released 04/26/2011
[extjs.git] / src / core / test / unit / spec / util / Format.js
1 describe("Ext.util.Format", function() {
2     var num = Ext.util.Format.number,
3         usMoney = Ext.util.Format.usMoney,
4         currency = Ext.util.Format.currency,
5         savedFormatLocale = {
6             thousandSeparator: Ext.util.Format.thousandSeparator,
7             decimalSeparator: Ext.util.Format.decimalSeparator,
8             currencySign: Ext.util.Format.currencySign
9         };
10
11     describe("usMoney", function(){
12         it("should format with 2 decimals, prefixed by a dollar sign", function() {
13             expect(usMoney(1234.567)).toEqual("$1,234.57");
14         });
15         it("should format with 2 decimals, prefixed by a negative sign, and a dollar sign", function() {
16             expect(usMoney(-1234.567)).toEqual("-$1,234.57");
17         });
18         it("should format with a comma as a thousand separator", function() {
19             expect(usMoney(1234567.89)).toEqual("$1,234,567.89");
20         });
21     });
22
23     describe("currency in FR locale", function(){
24         beforeEach(function() {
25             Ext.apply(Ext.util.Format, {
26                 thousandSeparator: '.',
27                 decimalSeparator: ',',
28                 currencySign: '\u20ac',
29                 dateFormat: 'd/m/Y'
30             });
31         });
32         afterEach(function() {
33             Ext.apply(Ext.util.Format, savedFormatLocale);
34         });
35
36         it("should format with 2 decimals, prefixed by a euro sign", function() {
37             expect(currency(1234.567)).toEqual("\u20ac1.234,57");
38         });
39         it("should format with 2 decimals, prefixed by a negative sign, and a euro sign", function() {
40             expect(currency(-1234.567)).toEqual("-\u20ac1.234,57");
41         });
42     });
43
44     describe("number in default (US) locale", function() {
45         it("should format with no decimals", function() {
46             expect(num(1, "0")).toEqual("1");
47         });
48         it("should format with two decimals", function() {
49             expect(num(1, "0.00")).toEqual("1.00");
50         });
51         it("should format+round with two decimals, and no thousand separators", function() {
52             expect(num(1234.567, "0.00")).toEqual("1234.57");
53         });
54         it("should format+round with two decimals, and ',' as the thousand separator", function() {
55             expect(num(1234.567, ",0.00")).toEqual("1,234.57");
56         });
57         it("should format+round with no decimals, and ',' as the thousand separator", function() {
58             expect(num(1234.567, ",0")).toEqual("1,235");
59         });
60     });
61
62     describe("number using FR locale", function() {
63         var savedFormatLocale = {
64             thousandSeparator: Ext.util.Format.thousandSeparator,
65             decimalSeparator: Ext.util.Format.decimalSeparator,
66             currencySign: Ext.util.Format.currencySign,
67             dateFormat: Ext.util.Format.dateFormat
68         };
69
70         beforeEach(function() {
71             Ext.apply(Ext.util.Format, {
72                 thousandSeparator: '.',
73                 decimalSeparator: ',',
74                 currencySign: '\u20ac',
75                 dateFormat: 'd/m/Y'
76             });
77         });
78         afterEach(function() {
79             Ext.apply(Ext.util.Format, savedFormatLocale);
80         });
81
82         it("should format with no decimals", function() {
83             expect(num(1, "0")).toEqual("1");
84         });
85         it("should format with two decimals", function() {
86             expect(num(1, "0.00")).toEqual("1,00");
87         });
88         it("should format+round with two decimals, and no thousand separators", function() {
89             expect(num(1234.567, "0.00")).toEqual("1234,57");
90         });
91         it("should format+round with two decimals after a ',', and '.' as the thousand separator", function() {
92             expect(num(1234.567, ",0.00")).toEqual("1.234,57");
93         });
94         it("should format+round with no decimals, and '.' as the thousand separator", function() {
95             expect(num(1234.567, ",0")).toEqual("1.235");
96         });
97     });
98
99     // In Ext4, the "/i" suffix allows you to use locale-specific separators in the format string, as opposed
100     // to US/UK conventions. Output however ALWAYS follows the local settings in the Format singleton which may
101     // be overridden by locale files.
102     describe("number using FR locale with /i", function() {
103         var savedFormatLocale = {
104             thousandSeparator: Ext.util.Format.thousandSeparator,
105             decimalSeparator: Ext.util.Format.decimalSeparator,
106             currencySign: Ext.util.Format.currencySign,
107             dateFormat: Ext.util.Format.dateFormat
108         };
109
110         // set up the FR formatting locale
111         beforeEach(function() {
112             Ext.apply(Ext.util.Format, {
113                 thousandSeparator: '.',
114                 decimalSeparator: ',',
115                 currencySign: '\u20ac',
116                 dateFormat: 'd/m/Y'
117             });
118         });
119         afterEach(function() {
120             Ext.apply(Ext.util.Format, savedFormatLocale);
121         });
122
123         // Demonstrate "Incorrect" use with "/i". '.' means thousand separator and ',' means decimal in FR locale.
124         // Read carefully. In the formatting strings below, '.' is taken to mean thousand separator, and
125         // ',' is taken to mean decimal separator
126         it("should format with no decimals", function() {
127             expect(num(1, "0.00/i")).toEqual("1");
128         });
129         it("should format+round with no decimals, and '.' as thousand separator", function() {
130             expect(num(1234.567, "0.00/i")).toEqual("1.235");
131         });
132         it("should format+round with three decimals after a ',', and '.' as the thousand separator", function() {
133             expect(num(1234.567, ",0.00/i")).toEqual("1.234,567");
134         });
135         it("should format+round with one decimal, and no thousand separator", function() {
136             expect(num(1234.567, ",0/i")).toEqual("1234,6");
137         });
138
139         // Correct usage
140         it("should format with two decimals", function() {
141             expect(num(1, "0,00/i")).toEqual("1,00");
142         });
143         it("should format+round with two decimals, and no thousand separators", function() {
144             expect(num(1234.567, "0,00/i")).toEqual("1234,57");
145         });
146         it("should format+round with two decimals after a ',', and '.' as the thousand separator", function() {
147             expect(num(1234.567, ".0,00/i")).toEqual("1.234,57");
148         });
149         it("should format+round with no decimals, and '.' as the thousand separator", function() {
150             expect(num(1234.567, ".0/i")).toEqual("1.235");
151         });
152
153     });
154 });