currency.test.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. import { expect } from 'chai'
  2. import { formatCurrency } from '../../../../frontend/js/shared/utils/currency'
  3. /*
  4. Users can select any language we support, regardless of the country where they are located.
  5. Which mean that any combination of "supported language"-"supported currency" can be displayed
  6. on the user's screen.
  7. Users located in the USA visiting https://fr.overleaf.com/user/subscription/plans
  8. should see amounts in USD (because of their IP address),
  9. but with French text, number formatting and currency formats (because of language choice).
  10. (e.g. 1 000,00 $)
  11. Users located in the France visiting https://www.overleaf.com/user/subscription/plans
  12. should see amounts in EUR (because of their IP address),
  13. but with English text, number formatting and currency formats (because of language choice).
  14. (e.g. €1,000.00)
  15. */
  16. describe('formatCurrency', function () {
  17. describe('en', function () {
  18. const format = currency => priceInCents =>
  19. formatCurrency(priceInCents, currency)
  20. describe('USD', function () {
  21. const formatUSD = format('USD')
  22. it('should format basic amounts', function () {
  23. expect(formatUSD(0)).to.equal('$0.00')
  24. expect(formatUSD(12.34)).to.equal('$12.34')
  25. expect(formatUSD(123)).to.equal('$123.00')
  26. })
  27. it('should format thousand separators', function () {
  28. expect(formatUSD(1_000)).to.equal('$1,000.00')
  29. expect(formatUSD(98_765_432.1)).to.equal('$98,765,432.10')
  30. })
  31. it('should format negative amounts', function () {
  32. expect(formatUSD(-0.01)).to.equal('-$0.01')
  33. expect(formatUSD(-12.34)).to.equal('-$12.34')
  34. expect(formatUSD(-123)).to.equal('-$123.00')
  35. })
  36. })
  37. describe('EUR', function () {
  38. const formatEUR = format('EUR')
  39. it('should format basic amounts', function () {
  40. expect(formatEUR(0)).to.equal('€0.00')
  41. expect(formatEUR(12.34)).to.equal('€12.34')
  42. expect(formatEUR(123)).to.equal('€123.00')
  43. })
  44. it('should format thousand separators', function () {
  45. expect(formatEUR(1_000)).to.equal('€1,000.00')
  46. expect(formatEUR(98_765_432.1)).to.equal('€98,765,432.10')
  47. })
  48. it('should format negative amounts', function () {
  49. expect(formatEUR(-0.01)).to.equal('-€0.01')
  50. expect(formatEUR(-12.34)).to.equal('-€12.34')
  51. expect(formatEUR(-123)).to.equal('-€123.00')
  52. })
  53. })
  54. describe('HUF', function () {
  55. const formatHUF = format('HUF')
  56. it('should format basic amounts', function () {
  57. expect(formatHUF(0)).to.equal('Ft 0.00')
  58. expect(formatHUF(12.34)).to.equal('Ft 12.34')
  59. expect(formatHUF(123)).to.equal('Ft 123.00')
  60. })
  61. it('should format thousand separators', function () {
  62. expect(formatHUF(1_000)).to.equal('Ft 1,000.00')
  63. expect(formatHUF(98_765_432.1)).to.equal('Ft 98,765,432.10')
  64. })
  65. it('should format negative amounts', function () {
  66. expect(formatHUF(-0.01)).to.equal('-Ft 0.01')
  67. expect(formatHUF(-12.34)).to.equal('-Ft 12.34')
  68. expect(formatHUF(-123)).to.equal('-Ft 123.00')
  69. })
  70. })
  71. describe('CLP', function () {
  72. const formatCLP = format('CLP')
  73. it('should format basic amounts', function () {
  74. expect(formatCLP(0)).to.equal('$0')
  75. expect(formatCLP(12.34)).to.equal('$12')
  76. expect(formatCLP(123)).to.equal('$123')
  77. expect(formatCLP(1234)).to.equal('$1,234')
  78. })
  79. it('should format thousand separators', function () {
  80. expect(formatCLP(1_000)).to.equal('$1,000')
  81. expect(formatCLP(98_765_432.1)).to.equal('$98,765,432')
  82. })
  83. it('should format negative amounts', function () {
  84. expect(formatCLP(-1)).to.equal('-$1')
  85. expect(formatCLP(-12.34)).to.equal('-$12')
  86. expect(formatCLP(-1234)).to.equal('-$1,234')
  87. })
  88. })
  89. describe('all currencies', function () {
  90. it('should format 1 "minimal atomic units"', function () {
  91. const amount = 1
  92. // "no cents currencies"
  93. expect(format('CLP')(amount)).to.equal('$1')
  94. expect(format('JPY')(amount)).to.equal('¥1')
  95. expect(format('KRW')(amount)).to.equal('₩1')
  96. expect(format('VND')(amount)).to.equal('₫1')
  97. // other currencies
  98. expect(format('AUD')(amount)).to.equal('$1.00')
  99. expect(format('BRL')(amount)).to.equal('R$1.00')
  100. expect(format('CAD')(amount)).to.equal('$1.00')
  101. expect(format('CHF')(amount)).to.equal('CHF 1.00')
  102. expect(format('CNY')(amount)).to.equal('¥1.00')
  103. expect(format('COP')(amount)).to.equal('$1.00')
  104. expect(format('DKK')(amount)).to.equal('kr 1.00')
  105. expect(format('EUR')(amount)).to.equal('€1.00')
  106. expect(format('GBP')(amount)).to.equal('£1.00')
  107. expect(format('HUF')(amount)).to.equal('Ft 1.00')
  108. expect(format('IDR')(amount)).to.equal('Rp 1.00')
  109. expect(format('INR')(amount)).to.equal('₹1.00')
  110. expect(format('MXN')(amount)).to.equal('$1.00')
  111. expect(format('MYR')(amount)).to.equal('RM 1.00')
  112. expect(format('NOK')(amount)).to.equal('kr 1.00')
  113. expect(format('NZD')(amount)).to.equal('$1.00')
  114. expect(format('PEN')(amount)).to.equal('PEN 1.00')
  115. expect(format('PHP')(amount)).to.equal('₱1.00')
  116. expect(format('SEK')(amount)).to.equal('kr 1.00')
  117. expect(format('SGD')(amount)).to.equal('$1.00')
  118. expect(format('THB')(amount)).to.equal('฿1.00')
  119. expect(format('USD')(amount)).to.equal('$1.00')
  120. })
  121. it('should format 1_234_567.897_654 "minimal atomic units"', function () {
  122. const amount = 1_234_567.897_654
  123. // "no cents currencies"
  124. expect(format('CLP')(amount)).to.equal('$1,234,568')
  125. expect(format('JPY')(amount)).to.equal('¥1,234,568')
  126. expect(format('KRW')(amount)).to.equal('₩1,234,568')
  127. expect(format('VND')(amount)).to.equal('₫1,234,568')
  128. // other currencies
  129. expect(format('AUD')(amount)).to.equal('$1,234,567.90')
  130. expect(format('BRL')(amount)).to.equal('R$1,234,567.90')
  131. expect(format('CAD')(amount)).to.equal('$1,234,567.90')
  132. expect(format('CHF')(amount)).to.equal('CHF 1,234,567.90')
  133. expect(format('CNY')(amount)).to.equal('¥1,234,567.90')
  134. expect(format('COP')(amount)).to.equal('$1,234,567.90')
  135. expect(format('DKK')(amount)).to.equal('kr 1,234,567.90')
  136. expect(format('EUR')(amount)).to.equal('€1,234,567.90')
  137. expect(format('GBP')(amount)).to.equal('£1,234,567.90')
  138. expect(format('HUF')(amount)).to.equal('Ft 1,234,567.90')
  139. expect(format('IDR')(amount)).to.equal('Rp 1,234,567.90')
  140. expect(format('INR')(amount)).to.equal('₹1,234,567.90')
  141. expect(format('MXN')(amount)).to.equal('$1,234,567.90')
  142. expect(format('MYR')(amount)).to.equal('RM 1,234,567.90')
  143. expect(format('NOK')(amount)).to.equal('kr 1,234,567.90')
  144. expect(format('NZD')(amount)).to.equal('$1,234,567.90')
  145. expect(format('PEN')(amount)).to.equal('PEN 1,234,567.90')
  146. expect(format('PHP')(amount)).to.equal('₱1,234,567.90')
  147. expect(format('SEK')(amount)).to.equal('kr 1,234,567.90')
  148. expect(format('SGD')(amount)).to.equal('$1,234,567.90')
  149. expect(format('THB')(amount)).to.equal('฿1,234,567.90')
  150. expect(format('USD')(amount)).to.equal('$1,234,567.90')
  151. })
  152. })
  153. })
  154. describe('fr', function () {
  155. const format = currency => priceInCents =>
  156. formatCurrency(priceInCents, currency, 'fr')
  157. describe('USD', function () {
  158. const formatUSD = format('USD')
  159. it('should format basic amounts', function () {
  160. expect(formatUSD(0)).to.equal('0,00 $')
  161. expect(formatUSD(12.34)).to.equal('12,34 $')
  162. expect(formatUSD(123)).to.equal('123,00 $')
  163. })
  164. it('should format thousand separators', function () {
  165. expect(formatUSD(1_000)).to.equal('1 000,00 $')
  166. expect(formatUSD(98_765_432.1)).to.equal('98 765 432,10 $')
  167. })
  168. it('should format negative amounts', function () {
  169. expect(formatUSD(-0.01)).to.equal('-0,01 $')
  170. expect(formatUSD(-12.34)).to.equal('-12,34 $')
  171. expect(formatUSD(-123)).to.equal('-123,00 $')
  172. })
  173. })
  174. describe('EUR', function () {
  175. const formatEUR = format('EUR')
  176. it('should format basic amounts', function () {
  177. expect(formatEUR(0)).to.equal('0,00 €')
  178. expect(formatEUR(12.34)).to.equal('12,34 €')
  179. expect(formatEUR(123)).to.equal('123,00 €')
  180. })
  181. it('should format thousand separators', function () {
  182. expect(formatEUR(1_000)).to.equal('1 000,00 €')
  183. expect(formatEUR(98_765_432.1)).to.equal('98 765 432,10 €')
  184. })
  185. it('should format negative amounts', function () {
  186. expect(formatEUR(-0.01)).to.equal('-0,01 €')
  187. expect(formatEUR(-12.34)).to.equal('-12,34 €')
  188. expect(formatEUR(-123)).to.equal('-123,00 €')
  189. })
  190. })
  191. describe('HUF', function () {
  192. const formatHUF = format('HUF')
  193. it('should format basic amounts', function () {
  194. expect(formatHUF(0)).to.equal('0,00 Ft')
  195. expect(formatHUF(12.34)).to.equal('12,34 Ft')
  196. expect(formatHUF(123)).to.equal('123,00 Ft')
  197. })
  198. it('should format thousand separators', function () {
  199. expect(formatHUF(1_000)).to.equal('1 000,00 Ft')
  200. expect(formatHUF(98_765_432.1)).to.equal('98 765 432,10 Ft')
  201. })
  202. it('should format negative amounts', function () {
  203. expect(formatHUF(-0.01)).to.equal('-0,01 Ft')
  204. expect(formatHUF(-12.34)).to.equal('-12,34 Ft')
  205. expect(formatHUF(-123)).to.equal('-123,00 Ft')
  206. })
  207. })
  208. describe('CLP', function () {
  209. const formatCLP = format('CLP')
  210. it('should format basic amounts', function () {
  211. expect(formatCLP(0)).to.equal('0 $')
  212. expect(formatCLP(12.34)).to.equal('12 $')
  213. expect(formatCLP(123)).to.equal('123 $')
  214. expect(formatCLP(1234)).to.equal('1 234 $')
  215. })
  216. it('should format thousand separators', function () {
  217. expect(formatCLP(100_000)).to.equal('100 000 $')
  218. expect(formatCLP(9_876_543_210)).to.equal('9 876 543 210 $')
  219. })
  220. it('should format negative amounts', function () {
  221. expect(formatCLP(-1)).to.equal('-1 $')
  222. expect(formatCLP(-12.34)).to.equal('-12 $')
  223. expect(formatCLP(-1234)).to.equal('-1 234 $')
  224. })
  225. })
  226. describe('all currencies', function () {
  227. it('should format 1 "minimal atomic units"', function () {
  228. const amount = 1
  229. // "no cents currencies"
  230. expect(format('CLP')(amount)).to.equal('1 $')
  231. expect(format('JPY')(amount)).to.equal('1 ¥')
  232. expect(format('KRW')(amount)).to.equal('1 ₩')
  233. expect(format('VND')(amount)).to.equal('1 ₫')
  234. // other currencies
  235. expect(format('AUD')(amount)).to.equal('1,00 $')
  236. expect(format('BRL')(amount)).to.equal('1,00 R$')
  237. expect(format('CAD')(amount)).to.equal('1,00 $')
  238. expect(format('CHF')(amount)).to.equal('1,00 CHF')
  239. expect(format('CNY')(amount)).to.equal('1,00 ¥')
  240. expect(format('COP')(amount)).to.equal('1,00 $')
  241. expect(format('EUR')(amount)).to.equal('1,00 €')
  242. expect(format('GBP')(amount)).to.equal('1,00 £')
  243. expect(format('USD')(amount)).to.equal('1,00 $')
  244. })
  245. it('should format 1_234_567.897_654 "minimal atomic units"', function () {
  246. const amount = 1_234_567.897_654
  247. // "no cents currencies"
  248. expect(format('CLP')(amount)).to.equal('1 234 568 $')
  249. expect(format('JPY')(amount)).to.equal('1 234 568 ¥')
  250. expect(format('KRW')(amount)).to.equal('1 234 568 ₩')
  251. expect(format('VND')(amount)).to.equal('1 234 568 ₫')
  252. // other currencies
  253. expect(format('AUD')(amount)).to.equal('1 234 567,90 $')
  254. expect(format('BRL')(amount)).to.equal('1 234 567,90 R$')
  255. expect(format('CAD')(amount)).to.equal('1 234 567,90 $')
  256. expect(format('CHF')(amount)).to.equal('1 234 567,90 CHF')
  257. expect(format('CNY')(amount)).to.equal('1 234 567,90 ¥')
  258. expect(format('COP')(amount)).to.equal('1 234 567,90 $')
  259. expect(format('EUR')(amount)).to.equal('1 234 567,90 €')
  260. expect(format('GBP')(amount)).to.equal('1 234 567,90 £')
  261. expect(format('USD')(amount)).to.equal('1 234 567,90 $')
  262. })
  263. })
  264. })
  265. })