-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.spec.ts
116 lines (102 loc) · 3.7 KB
/
Utils.spec.ts
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
import { assert } from 'chai'
import {
all,
allMoney,
isFloat,
assertIdenticalCurrencies,
assertAllMoney,
assertSupportedISOCurrency,
assertValidMonies,
} from '../lib'
import { Money as ISOMoney } from '../lib'
describe('Utils', () => {
it('all() is true when all input is the same', () => {
assert.isTrue(all<string>('a', 'a', 'a'))
})
it('all() is false when one input is different', () => {
assert.isFalse(all<string>('b', 'a', 'a'))
})
it('allMoney() is true when all input is money', () => {
const m0 = new ISOMoney('XTS', 123)
const m1 = new ISOMoney('USD', 456)
assert.isTrue(allMoney(m0, m1))
})
it('allMoney() is false when one input is NOT money', () => {
const m0 = new ISOMoney('XTS', 123)
const m1 = new ISOMoney('USD', 456)
assert.isFalse(allMoney(m0, m1, 'foo'))
})
it('isFloat() correctly detects floats', () => {
assert.isTrue(isFloat(123.45))
assert.isTrue(isFloat(-22.49))
assert.isFalse(isFloat('129.43'))
assert.isFalse(isFloat('-22.49'))
assert.isFalse(isFloat('blah'))
assert.isFalse(isFloat(true))
assert.isFalse(isFloat({ foo: true }))
assert.isFalse(isFloat(12345))
assert.isFalse(isFloat(-6789))
})
it('assertIdenticalCurrencies() does not throw when all currency is same', () => {
const m0 = new ISOMoney('USD', 456)
const m1 = new ISOMoney('USD', 111)
// no-op
assertIdenticalCurrencies(m0.getCurrency(), m1.getCurrency())
})
it('assertIdenticalCurrencies() throws when one currency is different', () => {
const m0 = new ISOMoney('USD', 456)
const m1 = new ISOMoney('XTS', 111)
assert.throws(
() => assertIdenticalCurrencies(m0.getCurrency(), m1.getCurrency()),
RangeError,
)
})
it('assertAllMoney() throws when one is NOT money', () => {
const m0 = new ISOMoney('XTS', 123)
assert.throws(() => assertAllMoney(m0, 'foo'), TypeError)
assert.throws(() => assertAllMoney(m0, 123), TypeError)
assert.throws(() => assertAllMoney(m0, true), TypeError)
assert.throws(() => assertAllMoney(m0, 34.1), TypeError)
assert.throws(() => assertAllMoney(m0, { foo: 'bar' }), TypeError)
})
it('assertAllMoney() does not throw when all is money', () => {
const m0 = new ISOMoney('XTS', 123)
const m1 = new ISOMoney('USD', 456)
const m2 = new ISOMoney('EUR', 789)
// no-op
assertAllMoney(m0, m1, m2)
})
it('assertSupportedISOCurrency() throws when ISO currency does not exist', () => {
// @ts-expect-error
assert.throws(() => assertSupportedISOCurrency('FOO'), RangeError)
})
it('assertSupportedISOCurrency() does not throw when ISO currency exists', () => {
// no-op
// @ts-expect-error
;['USD', 'GBP', 'EUR', 'JPY'].map((c) => assertSupportedISOCurrency(c))
})
it('assertValidMonies() does not throw when all is valid', () => {
const m0 = new ISOMoney('USD', 456)
const m1 = new ISOMoney('USD', 111)
// no-op
assertValidMonies(m0, m1)
})
it('assertValidMonies() throws on different currencies', () => {
const m0 = new ISOMoney('USD', 456)
const m1 = new ISOMoney('XTS', 111)
assert.throws(() => assertValidMonies(m0, m1), RangeError)
})
it('assertValidMonies() throws on not money', () => {
const m0 = new ISOMoney('USD', 456)
// @ts-expect-error
assert.throws(() => assertValidMonies(m0, 'foo'), TypeError)
// @ts-expect-error
assert.throws(() => assertValidMonies(m0, 123), TypeError)
// @ts-expect-error
assert.throws(() => assertValidMonies(m0, true), TypeError)
// @ts-expect-error
assert.throws(() => assertValidMonies(m0, 34.1), TypeError)
// @ts-expect-error
assert.throws(() => assertValidMonies(m0, { foo: 'bar' }), TypeError)
})
})