UNPKG

6.37 kBJavaScriptView Raw
1import test from 'ava'
2import Chance from '../chance.js'
3import _ from 'lodash'
4
5const chance = new Chance()
6
7// chance.cc()
8test('cc() passes the luhn algorithm', t => {
9 _.times(1000, () => {
10 let number = chance.cc()
11 t.true(chance.luhn_check(number))
12 })
13})
14
15test('cc() can take a type arg and obey it', t => {
16 _.times(1000, () => {
17 let type = chance.cc_type({ raw: true })
18 let number = chance.cc({ type: type.name })
19 t.is(number.length, type.length)
20 })
21})
22
23test('cc() can take a short_name type arg and obey it', t => {
24 _.times(1000, () => {
25 let type = chance.cc_type({ raw: true })
26 let number = chance.cc({ type: type.short_name })
27 t.is(number.length, type.length)
28 })
29})
30
31// chance.cc_type()
32test('cc_type() returns a random credit card type', t => {
33 _.times(1000, () => {
34 t.true(_.isString(chance.cc_type()))
35 })
36})
37
38test('cc_type() can take a raw arg and obey it', t => {
39 _.times(1000, () => {
40 let type = chance.cc_type({ raw: true })
41 t.truthy(type.name)
42 t.truthy(type.short_name)
43 t.truthy(type.prefix)
44 t.truthy(type.length)
45 })
46})
47
48test('cc_type() can take a name arg and obey it', t => {
49 _.times(1000, () => {
50 let type = chance.cc_type({ name: 'Visa', raw: true })
51 t.is(type.name, 'Visa')
52 })
53})
54
55test('cc_type() with bogus type throws', t => {
56 const fn = () => chance.cc_type({ name: 'potato' })
57 t.throws(fn, 'Chance: Credit card type \'potato\' is not supported')
58})
59
60// chance.cc_types()
61test('cc_types() returns an array of credit card types', t => {
62 t.true(_.isArray(chance.cc_types()))
63})
64
65// chance.currency()
66test('currency() returns a currency', t => {
67 _.times(1000, () => {
68 let currency = chance.currency()
69 t.true(_.isObject(currency))
70 t.truthy(currency.code)
71 t.is(currency.code.length, 3)
72 t.truthy(currency.name)
73 })
74})
75
76// chance.currency_pair()
77test('currency_pair() returns a currency pair', t => {
78 _.times(1000, () => {
79 let currency_pair = chance.currency_pair()
80 t.true(_.isArray(currency_pair))
81 t.is(currency_pair.length, 2)
82 t.not(currency_pair[0].code, currency_pair[1].code)
83 })
84})
85
86test('currency_pair() can return string version', t => {
87 _.times(1000, () => {
88 let currency_pair = chance.currency_pair(true)
89 t.true(_.isString(currency_pair))
90 t.is(currency_pair.length, 7)
91 t.true(/^[A-Z][A-Z][A-Z]+\/[A-Z][A-Z][A-Z]$/.test(currency_pair))
92 })
93})
94
95// chance.dollar()
96test('dollar() returns a proper dollar amount', t => {
97 let dollar = chance.dollar()
98 t.true(/\$[0-9]+\.[0-9]+/.test(dollar))
99 let dollarFloat = parseFloat(dollar.substring(1, dollar.length))
100 t.true(dollarFloat < 10001)
101})
102
103test('dollar() obeys min and max, if provided', t => {
104 _.times(1000, () => {
105 let dollar = chance.dollar({ max: 20 })
106 let dollarFloat = parseFloat(dollar.substring(1, dollar.length))
107 t.true(dollarFloat <= 20)
108 })
109
110 _.times(1000, () => {
111 let dollar = chance.dollar({ min: 20 })
112 let dollarFloat = parseFloat(dollar.substring(1, dollar.length))
113 t.true(dollarFloat >= 20)
114 })
115})
116
117test('dollar() can take negative min and max', t => {
118 _.times(1000, () => {
119 let dollar = chance.dollar({ min: -200, max: -1 })
120 t.is(dollar.charAt(0), '-')
121 let dollarFloat = parseFloat(dollar.substring(2, dollar.length))
122 // This is necessary because we strip the - when parsing
123 dollarFloat *= -1
124 t.true(dollarFloat <= -1)
125 t.true(dollarFloat >= -200)
126 })
127})
128
129// chance.euro()
130test('euro() returns a proper euro amount', t => {
131 let euro = chance.euro()
132 t.true(/[0-9]+,?\.?[0-9]+?€/.test(euro))
133 let euroFloat = parseFloat(euro.substring(euro.length, -1))
134 t.true(euroFloat < 10001)
135})
136
137// chance.exp()
138test('exp() looks correct', t => {
139 _.times(1000, () => {
140 let exp = chance.exp()
141 t.true(_.isString(exp))
142 t.is(exp.length, 7)
143 t.true(/([0-9]{2})\/([0-9]{4})/.test(exp))
144 })
145})
146
147test('exp() will respect object argument', t => {
148 _.times(1000, () => {
149 let exp = chance.exp({ raw: true })
150 t.true(_.isObject(exp))
151 t.truthy(exp.month)
152 t.true(_.isString(exp.month))
153 t.truthy(exp.year)
154 t.true(_.isString(exp.year))
155 })
156})
157
158test('exp() returs a valid credit card expiration (in a future date)', t => {
159 _.times(1000, () => {
160 let exp = chance.exp({ raw: true })
161 let now = new Date()
162 let nowMonth = now.getMonth() + 1
163 let nowYear = now.getFullYear()
164 let expMonth = parseInt(exp.month, 10)
165 let expYear = parseInt(exp.year, 10)
166
167 t.true(expYear >= nowYear)
168 if (expYear === nowYear) {
169 t.true(expMonth >= nowMonth)
170 }
171 })
172})
173
174// chance.exp_month()
175test('exp_month() returns a numeric month with leading 0', t => {
176 _.times(1000, () => {
177 let month = chance.exp_month()
178 t.true(_.isString(month))
179 t.is(month.length, 2)
180 })
181})
182
183// chance.exp_year()
184test('exp_year() returns an expiration year', t => {
185 _.times(1000, () => {
186 let year = chance.exp_year()
187 t.true(_.isString(year))
188 let parsedYear = parseInt(year, 10)
189 let curYear = new Date().getFullYear()
190 t.true(parsedYear >= curYear)
191 t.true(parsedYear <= curYear + 10)
192 })
193})
194
195test('exp_month() will return a future month if requested', t => {
196 _.times(1000, () => {
197 let nowMonth = new Date().getMonth() + 1
198 let expMonth = parseInt(chance.exp_month({ future: true }), 10)
199 if (nowMonth !== 12) {
200 t.true(expMonth > nowMonth)
201 } else {
202 t.true(expMonth >= 1)
203 t.true(expMonth <= 12)
204 }
205 })
206})
207
208// chance.luhn_check()
209test('luhn_check() properly checks if number passes the Luhn algorithm', t => {
210 t.true(chance.luhn_check(49927398716))
211 t.true(chance.luhn_check(1234567812345670))
212 t.false(chance.luhn_check(49927398717))
213 t.false(chance.luhn_check(1234567812345678))
214})
215
216test('iban() returns an iban', t => {
217 let iban = chance.iban()
218 t.true(_.isString(iban))
219 t.true(/^[A-Z]{2}[0-9]{2}[A-Z0-9]{4}[0-9]{1,26}$/.test(iban))
220})