| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316 |
- import { expect } from 'chai'
- import { formatCurrency } from '../../../../frontend/js/shared/utils/currency'
- /*
- Users can select any language we support, regardless of the country where they are located.
- Which mean that any combination of "supported language"-"supported currency" can be displayed
- on the user's screen.
- Users located in the USA visiting https://fr.overleaf.com/user/subscription/plans
- should see amounts in USD (because of their IP address),
- but with French text, number formatting and currency formats (because of language choice).
- (e.g. 1 000,00 $)
- Users located in the France visiting https://www.overleaf.com/user/subscription/plans
- should see amounts in EUR (because of their IP address),
- but with English text, number formatting and currency formats (because of language choice).
- (e.g. €1,000.00)
- */
- describe('formatCurrency', function () {
- describe('en', function () {
- const format = currency => priceInCents =>
- formatCurrency(priceInCents, currency)
- describe('USD', function () {
- const formatUSD = format('USD')
- it('should format basic amounts', function () {
- expect(formatUSD(0)).to.equal('$0.00')
- expect(formatUSD(12.34)).to.equal('$12.34')
- expect(formatUSD(123)).to.equal('$123.00')
- })
- it('should format thousand separators', function () {
- expect(formatUSD(1_000)).to.equal('$1,000.00')
- expect(formatUSD(98_765_432.1)).to.equal('$98,765,432.10')
- })
- it('should format negative amounts', function () {
- expect(formatUSD(-0.01)).to.equal('-$0.01')
- expect(formatUSD(-12.34)).to.equal('-$12.34')
- expect(formatUSD(-123)).to.equal('-$123.00')
- })
- })
- describe('EUR', function () {
- const formatEUR = format('EUR')
- it('should format basic amounts', function () {
- expect(formatEUR(0)).to.equal('€0.00')
- expect(formatEUR(12.34)).to.equal('€12.34')
- expect(formatEUR(123)).to.equal('€123.00')
- })
- it('should format thousand separators', function () {
- expect(formatEUR(1_000)).to.equal('€1,000.00')
- expect(formatEUR(98_765_432.1)).to.equal('€98,765,432.10')
- })
- it('should format negative amounts', function () {
- expect(formatEUR(-0.01)).to.equal('-€0.01')
- expect(formatEUR(-12.34)).to.equal('-€12.34')
- expect(formatEUR(-123)).to.equal('-€123.00')
- })
- })
- describe('HUF', function () {
- const formatHUF = format('HUF')
- it('should format basic amounts', function () {
- expect(formatHUF(0)).to.equal('Ft 0.00')
- expect(formatHUF(12.34)).to.equal('Ft 12.34')
- expect(formatHUF(123)).to.equal('Ft 123.00')
- })
- it('should format thousand separators', function () {
- expect(formatHUF(1_000)).to.equal('Ft 1,000.00')
- expect(formatHUF(98_765_432.1)).to.equal('Ft 98,765,432.10')
- })
- it('should format negative amounts', function () {
- expect(formatHUF(-0.01)).to.equal('-Ft 0.01')
- expect(formatHUF(-12.34)).to.equal('-Ft 12.34')
- expect(formatHUF(-123)).to.equal('-Ft 123.00')
- })
- })
- describe('CLP', function () {
- const formatCLP = format('CLP')
- it('should format basic amounts', function () {
- expect(formatCLP(0)).to.equal('$0')
- expect(formatCLP(12.34)).to.equal('$12')
- expect(formatCLP(123)).to.equal('$123')
- expect(formatCLP(1234)).to.equal('$1,234')
- })
- it('should format thousand separators', function () {
- expect(formatCLP(1_000)).to.equal('$1,000')
- expect(formatCLP(98_765_432.1)).to.equal('$98,765,432')
- })
- it('should format negative amounts', function () {
- expect(formatCLP(-1)).to.equal('-$1')
- expect(formatCLP(-12.34)).to.equal('-$12')
- expect(formatCLP(-1234)).to.equal('-$1,234')
- })
- })
- describe('all currencies', function () {
- it('should format 1 "minimal atomic units"', function () {
- const amount = 1
- // "no cents currencies"
- expect(format('CLP')(amount)).to.equal('$1')
- expect(format('JPY')(amount)).to.equal('¥1')
- expect(format('KRW')(amount)).to.equal('₩1')
- expect(format('VND')(amount)).to.equal('₫1')
- // other currencies
- expect(format('AUD')(amount)).to.equal('$1.00')
- expect(format('BRL')(amount)).to.equal('R$1.00')
- expect(format('CAD')(amount)).to.equal('$1.00')
- expect(format('CHF')(amount)).to.equal('CHF 1.00')
- expect(format('CNY')(amount)).to.equal('¥1.00')
- expect(format('COP')(amount)).to.equal('$1.00')
- expect(format('DKK')(amount)).to.equal('kr 1.00')
- expect(format('EUR')(amount)).to.equal('€1.00')
- expect(format('GBP')(amount)).to.equal('£1.00')
- expect(format('HUF')(amount)).to.equal('Ft 1.00')
- expect(format('IDR')(amount)).to.equal('Rp 1.00')
- expect(format('INR')(amount)).to.equal('₹1.00')
- expect(format('MXN')(amount)).to.equal('$1.00')
- expect(format('MYR')(amount)).to.equal('RM 1.00')
- expect(format('NOK')(amount)).to.equal('kr 1.00')
- expect(format('NZD')(amount)).to.equal('$1.00')
- expect(format('PEN')(amount)).to.equal('PEN 1.00')
- expect(format('PHP')(amount)).to.equal('₱1.00')
- expect(format('SEK')(amount)).to.equal('kr 1.00')
- expect(format('SGD')(amount)).to.equal('$1.00')
- expect(format('THB')(amount)).to.equal('฿1.00')
- expect(format('USD')(amount)).to.equal('$1.00')
- })
- it('should format 1_234_567.897_654 "minimal atomic units"', function () {
- const amount = 1_234_567.897_654
- // "no cents currencies"
- expect(format('CLP')(amount)).to.equal('$1,234,568')
- expect(format('JPY')(amount)).to.equal('¥1,234,568')
- expect(format('KRW')(amount)).to.equal('₩1,234,568')
- expect(format('VND')(amount)).to.equal('₫1,234,568')
- // other currencies
- expect(format('AUD')(amount)).to.equal('$1,234,567.90')
- expect(format('BRL')(amount)).to.equal('R$1,234,567.90')
- expect(format('CAD')(amount)).to.equal('$1,234,567.90')
- expect(format('CHF')(amount)).to.equal('CHF 1,234,567.90')
- expect(format('CNY')(amount)).to.equal('¥1,234,567.90')
- expect(format('COP')(amount)).to.equal('$1,234,567.90')
- expect(format('DKK')(amount)).to.equal('kr 1,234,567.90')
- expect(format('EUR')(amount)).to.equal('€1,234,567.90')
- expect(format('GBP')(amount)).to.equal('£1,234,567.90')
- expect(format('HUF')(amount)).to.equal('Ft 1,234,567.90')
- expect(format('IDR')(amount)).to.equal('Rp 1,234,567.90')
- expect(format('INR')(amount)).to.equal('₹1,234,567.90')
- expect(format('MXN')(amount)).to.equal('$1,234,567.90')
- expect(format('MYR')(amount)).to.equal('RM 1,234,567.90')
- expect(format('NOK')(amount)).to.equal('kr 1,234,567.90')
- expect(format('NZD')(amount)).to.equal('$1,234,567.90')
- expect(format('PEN')(amount)).to.equal('PEN 1,234,567.90')
- expect(format('PHP')(amount)).to.equal('₱1,234,567.90')
- expect(format('SEK')(amount)).to.equal('kr 1,234,567.90')
- expect(format('SGD')(amount)).to.equal('$1,234,567.90')
- expect(format('THB')(amount)).to.equal('฿1,234,567.90')
- expect(format('USD')(amount)).to.equal('$1,234,567.90')
- })
- })
- })
- describe('fr', function () {
- const format = currency => priceInCents =>
- formatCurrency(priceInCents, currency, 'fr')
- describe('USD', function () {
- const formatUSD = format('USD')
- it('should format basic amounts', function () {
- expect(formatUSD(0)).to.equal('0,00 $')
- expect(formatUSD(12.34)).to.equal('12,34 $')
- expect(formatUSD(123)).to.equal('123,00 $')
- })
- it('should format thousand separators', function () {
- expect(formatUSD(1_000)).to.equal('1 000,00 $')
- expect(formatUSD(98_765_432.1)).to.equal('98 765 432,10 $')
- })
- it('should format negative amounts', function () {
- expect(formatUSD(-0.01)).to.equal('-0,01 $')
- expect(formatUSD(-12.34)).to.equal('-12,34 $')
- expect(formatUSD(-123)).to.equal('-123,00 $')
- })
- })
- describe('EUR', function () {
- const formatEUR = format('EUR')
- it('should format basic amounts', function () {
- expect(formatEUR(0)).to.equal('0,00 €')
- expect(formatEUR(12.34)).to.equal('12,34 €')
- expect(formatEUR(123)).to.equal('123,00 €')
- })
- it('should format thousand separators', function () {
- expect(formatEUR(1_000)).to.equal('1 000,00 €')
- expect(formatEUR(98_765_432.1)).to.equal('98 765 432,10 €')
- })
- it('should format negative amounts', function () {
- expect(formatEUR(-0.01)).to.equal('-0,01 €')
- expect(formatEUR(-12.34)).to.equal('-12,34 €')
- expect(formatEUR(-123)).to.equal('-123,00 €')
- })
- })
- describe('HUF', function () {
- const formatHUF = format('HUF')
- it('should format basic amounts', function () {
- expect(formatHUF(0)).to.equal('0,00 Ft')
- expect(formatHUF(12.34)).to.equal('12,34 Ft')
- expect(formatHUF(123)).to.equal('123,00 Ft')
- })
- it('should format thousand separators', function () {
- expect(formatHUF(1_000)).to.equal('1 000,00 Ft')
- expect(formatHUF(98_765_432.1)).to.equal('98 765 432,10 Ft')
- })
- it('should format negative amounts', function () {
- expect(formatHUF(-0.01)).to.equal('-0,01 Ft')
- expect(formatHUF(-12.34)).to.equal('-12,34 Ft')
- expect(formatHUF(-123)).to.equal('-123,00 Ft')
- })
- })
- describe('CLP', function () {
- const formatCLP = format('CLP')
- it('should format basic amounts', function () {
- expect(formatCLP(0)).to.equal('0 $')
- expect(formatCLP(12.34)).to.equal('12 $')
- expect(formatCLP(123)).to.equal('123 $')
- expect(formatCLP(1234)).to.equal('1 234 $')
- })
- it('should format thousand separators', function () {
- expect(formatCLP(100_000)).to.equal('100 000 $')
- expect(formatCLP(9_876_543_210)).to.equal('9 876 543 210 $')
- })
- it('should format negative amounts', function () {
- expect(formatCLP(-1)).to.equal('-1 $')
- expect(formatCLP(-12.34)).to.equal('-12 $')
- expect(formatCLP(-1234)).to.equal('-1 234 $')
- })
- })
- describe('all currencies', function () {
- it('should format 1 "minimal atomic units"', function () {
- const amount = 1
- // "no cents currencies"
- expect(format('CLP')(amount)).to.equal('1 $')
- expect(format('JPY')(amount)).to.equal('1 ¥')
- expect(format('KRW')(amount)).to.equal('1 ₩')
- expect(format('VND')(amount)).to.equal('1 ₫')
- // other currencies
- expect(format('AUD')(amount)).to.equal('1,00 $')
- expect(format('BRL')(amount)).to.equal('1,00 R$')
- expect(format('CAD')(amount)).to.equal('1,00 $')
- expect(format('CHF')(amount)).to.equal('1,00 CHF')
- expect(format('CNY')(amount)).to.equal('1,00 ¥')
- expect(format('COP')(amount)).to.equal('1,00 $')
- expect(format('EUR')(amount)).to.equal('1,00 €')
- expect(format('GBP')(amount)).to.equal('1,00 £')
- expect(format('USD')(amount)).to.equal('1,00 $')
- })
- it('should format 1_234_567.897_654 "minimal atomic units"', function () {
- const amount = 1_234_567.897_654
- // "no cents currencies"
- expect(format('CLP')(amount)).to.equal('1 234 568 $')
- expect(format('JPY')(amount)).to.equal('1 234 568 ¥')
- expect(format('KRW')(amount)).to.equal('1 234 568 ₩')
- expect(format('VND')(amount)).to.equal('1 234 568 ₫')
- // other currencies
- expect(format('AUD')(amount)).to.equal('1 234 567,90 $')
- expect(format('BRL')(amount)).to.equal('1 234 567,90 R$')
- expect(format('CAD')(amount)).to.equal('1 234 567,90 $')
- expect(format('CHF')(amount)).to.equal('1 234 567,90 CHF')
- expect(format('CNY')(amount)).to.equal('1 234 567,90 ¥')
- expect(format('COP')(amount)).to.equal('1 234 567,90 $')
- expect(format('EUR')(amount)).to.equal('1 234 567,90 €')
- expect(format('GBP')(amount)).to.equal('1 234 567,90 £')
- expect(format('USD')(amount)).to.equal('1 234 567,90 $')
- })
- })
- })
- })
|