|
| 1 | +import test from 'ava'; |
| 2 | +import Big from 'big.js'; |
| 3 | +import {Cashify, convert, parse} from './src/index.js'; |
| 4 | + |
| 5 | +const rates = { |
| 6 | + GBP: 0.92, |
| 7 | + EUR: 1, |
| 8 | + USD: 1.12, |
| 9 | +}; |
| 10 | + |
| 11 | +test('exports a constructor', t => { |
| 12 | + const cashify = new Cashify({base: 'EUR', rates}); |
| 13 | + |
| 14 | + t.is(cashify.convert(12, {from: 'USD', to: 'GBP'}), 9.857_142_857_142_856); |
| 15 | +}); |
| 16 | + |
| 17 | +test('exports a `parse` function', t => { |
| 18 | + t.deepEqual(parse('10 eur to pln'), { |
| 19 | + amount: 10, |
| 20 | + from: 'EUR', |
| 21 | + to: 'PLN', |
| 22 | + }); |
| 23 | +}); |
| 24 | + |
| 25 | +test('basic conversion', t => { |
| 26 | + t.is(convert(12, {from: 'USD', to: 'GBP', base: 'EUR', rates}), 9.857_142_857_142_856); |
| 27 | +}); |
| 28 | + |
| 29 | +test('`from` equals `base`', t => { |
| 30 | + t.is(convert(10, {from: 'EUR', to: 'GBP', base: 'EUR', rates}), 9.2); |
| 31 | +}); |
| 32 | + |
| 33 | +test('`to` equals `base`', t => { |
| 34 | + t.is(convert(10, {from: 'GBP', to: 'EUR', base: 'EUR', rates}), 10.869_565_217_391_303); |
| 35 | +}); |
| 36 | + |
| 37 | +test('`from` equals `to`', t => { |
| 38 | + t.is(convert(10, {from: 'USD', to: 'USD', base: 'EUR', rates}), 10); |
| 39 | +}); |
| 40 | + |
| 41 | +test('`from` equals `to`, but `base` is different', t => { |
| 42 | + t.is(convert(10, {from: 'EUR', to: 'EUR', base: 'USD', rates}), 10); |
| 43 | +}); |
| 44 | + |
| 45 | +test('accepts `amount` of type `string`', t => { |
| 46 | + t.is(convert('12', {from: 'USD', to: 'GBP', base: 'EUR', rates}), 9.857_142_857_142_856); |
| 47 | +}); |
| 48 | + |
| 49 | +test('edge case: accepts `amount` of type `string`, equal to 0', t => { |
| 50 | + t.is(convert('0', {from: 'USD', to: 'GBP', base: 'EUR', rates}), 0); |
| 51 | +}); |
| 52 | + |
| 53 | +test('`amount` equals 0', t => { |
| 54 | + t.is(convert(0, {from: 'USD', to: 'GBP', base: 'EUR', rates}), 0); |
| 55 | +}); |
| 56 | + |
| 57 | +test('basic parsing (integer)', t => { |
| 58 | + t.is(convert('$12 USD', {to: 'GBP', base: 'EUR', rates}), 9.857_142_857_142_856); |
| 59 | +}); |
| 60 | + |
| 61 | +test('basic parsing (float)', t => { |
| 62 | + t.is(convert('1.23 GBP', {to: 'EUR', base: 'USD', rates}), 1.336_956_521_739_130_4); |
| 63 | +}); |
| 64 | + |
| 65 | +test('parsing without the `from` currency (integer)', t => { |
| 66 | + t.is(convert('12 to GBP', {from: 'USD', base: 'EUR', rates}), 9.857_142_857_142_856); |
| 67 | +}); |
| 68 | + |
| 69 | +test('full parsing (integer)', t => { |
| 70 | + t.is(convert('$12 USD TO GBP', {base: 'EUR', rates}), 9.857_142_857_142_856); |
| 71 | + t.is(convert('$12 USD IN GBP', {base: 'EUR', rates}), 9.857_142_857_142_856); |
| 72 | + t.is(convert('$12 USD AS GBP', {base: 'EUR', rates}), 9.857_142_857_142_856); |
| 73 | +}); |
| 74 | + |
| 75 | +test('full parsing (float)', t => { |
| 76 | + t.is(convert('1.23 gbp to eur', {base: 'USD', rates}), 1.336_956_521_739_130_4); |
| 77 | + t.is(convert('1.23 gbp in eur', {base: 'USD', rates}), 1.336_956_521_739_130_4); |
| 78 | + t.is(convert('1.23 gbp as eur', {base: 'USD', rates}), 1.336_956_521_739_130_4); |
| 79 | +}); |
| 80 | + |
| 81 | +test('`from` is not defined', t => { |
| 82 | + const error = t.throws(() => { |
| 83 | + convert(10, {to: 'EUR', base: 'USD', rates}); |
| 84 | + }, {instanceOf: Error}); |
| 85 | + |
| 86 | + t.is(error.message, 'Please specify the `from` and/or `to` currency, or use parsing.'); |
| 87 | +}); |
| 88 | + |
| 89 | +test('`rates` without the `base` currency', t => { |
| 90 | + const rates = { |
| 91 | + GBP: 0.92, |
| 92 | + USD: 1.12, |
| 93 | + }; |
| 94 | + |
| 95 | + t.is(convert(10, {from: 'EUR', to: 'GBP', base: 'EUR', rates}), 9.2); |
| 96 | +}); |
| 97 | + |
| 98 | +test('`rates` without either the `from` or `to` currency', t => { |
| 99 | + const error = t.throws(() => { |
| 100 | + convert(10, {from: 'CHF', to: 'EUR', base: 'EUR', rates}); |
| 101 | + }, {instanceOf: Error}); |
| 102 | + |
| 103 | + t.is(error.message, 'The `rates` object does not contain either the `from` or `to` currency.'); |
| 104 | +}); |
| 105 | + |
| 106 | +test('parsing without a correct amount', t => { |
| 107 | + const error = t.throws(() => { |
| 108 | + convert('', {base: 'EUR', rates}); |
| 109 | + }, {instanceOf: Error}); |
| 110 | + |
| 111 | + t.is(error.message, 'Could not parse the expression. Make sure it includes at least a valid amount.'); |
| 112 | +}); |
| 113 | + |
| 114 | +test('avoiding floating point issues with Big.js', t => { |
| 115 | + const rates = { |
| 116 | + USD: 1, |
| 117 | + EUR: 0.8235, |
| 118 | + }; |
| 119 | + |
| 120 | + t.is(convert(1, {from: 'USD', to: 'EUR', base: 'USD', rates, BigJs: Big}), 0.8235); |
| 121 | +}); |
0 commit comments