forked from osrec/currencyFormatter.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
117 lines (105 loc) · 3.33 KB
/
test.js
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
const chai = require("chai");
const currencyFormatter = require("./currencyFormatter");
chai.should();
const checkCurrency = ([
countryCode,
expected,
expectedNegative = null,
locale = undefined,
]) => {
it(`should format ${countryCode} correctly${
locale ? ` with locale ${locale}` : ""
}`, () => {
currencyFormatter
.format(1234, { currency: countryCode, locale })
.replace(/\s/g, " ")
.replace(/\./g, ".")
.should.equal(expected);
currencyFormatter
.format(-1234, { currency: countryCode, locale })
.replace(/\s/g, " ")
.should.equal(expectedNegative ? expectedNegative : `-${expected}`);
});
};
const checkForThreeDecimals = ([countryCode, locale = undefined]) => {
it(`should format ${countryCode} with three decimals${
locale ? ` with locale ${locale}` : ""
}`, () => {
/.*[.,]000.*/.test(
currencyFormatter.format(1234, { currency: countryCode, locale })
).should.be.true;
});
};
// sanity check
describe("sanity check", () => {
it("should format USD correctly", () => {
currencyFormatter
.format(1000, { currency: "USD" })
.should.equal("$1,000.00");
});
it("should work", () => {
const usdEnt = "$";
const jpyEnt = "¥";
const krwEnt = "₩";
const kronerEnt = "kr";
currencyFormatter
.format(1000, { currency: "JPY", locale: "en_US" })
.should.equal("¥1,000");
currencyFormatter
.format(1000, { currency: "JPY", locale: "ja" })
.should.equal(`${jpyEnt}1,000`);
currencyFormatter
.format(1000, { currency: "USD" })
.should.equal(`${usdEnt}1,000.00`);
currencyFormatter
.format(1000, { currency: "USD", locale: "en_US" })
.should.equal(`${usdEnt}1,000.00`);
currencyFormatter
.format(1000, { currency: "USD", locale: "ja" })
.should.equal(`${usdEnt}1,000.00`);
});
});
// ignored CVE because its decimal status is confusing (https://en.wikipedia.org/wiki/Cape_Verdean_escudo) ,
// ignored KMF because it uses default arabic locale
// ignored XAF because it uses default french locale
describe("currencies with 0 digits after decimal separator", () => {
[
["BIF", "1.234 FBu"],
["CLP", "$1.234", "$-1.234"],
["DJF", "Fdj 1 234", "Fdj-1 234"],
["GNF", "FG 1 234", "FG-1 234"],
["ISK", "1.234 kr"],
["JPY", "¥1,234"],
["KRW", "₩1,234"],
["PYG", "₲ 1.234", "₲ -1.234"],
["RWF", "RF1.234"],
["UGX", "USh1,234"],
["VND", "₫ 1.234"],
["VUV", "VT1,234"],
["XPF", "1 234 CFP"],
].map(checkCurrency);
});
// string matching with dinars was weird so I am just asserting the number of decimal places
describe("currencies with 3 digits after decimal separator", () => {
[["BHD"], ["IQD"], ["JOD"], ["KWD"], ["LYD"], ["OMR"], ["TND"]].map(
checkForThreeDecimals
);
});
describe("mantissa should respect currency code, overriding the locale", () => {
[
["USD", "$1,234.00", null, "ja"],
["USD", "$1,234.00", null, "en_US"],
["USD", "$ 1,234.00", null, "ar_BH"],
["USD", "1.234,00 $", null, "eu_ES"],
["JPY", "¥1,234", null, "ja"],
["JPY", "¥1,234", null, "en_US"],
["JPY", "¥ 1,234", null, "ar_BH"],
["JPY", "1.234 ¥", null, "eu_ES"],
].map(checkCurrency);
[
["BHD", "ja"],
["BHD", "en_US"],
["BHD", "ar_BH"],
["BHD", "eu_ES"],
].map(checkForThreeDecimals);
});