SubscriptionFormattersTests.js 12 KB

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