-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.js
51 lines (44 loc) · 1.12 KB
/
test.js
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
import test from 'ava';
import { pick, Picker } from './index.js';
// Mock random number generation.
const randomGen = function (sequence = []) {
return function () {
const n = sequence.shift();
sequence.push(n);
return n;
};
};
test('Should return ’undefined’ with empty lists', t => {
t.assert(pick([]) === undefined);
});
test('Should work with lists of one item', t => {
const picker = new Picker(randomGen([0.1, 0.6, 0.3, 0.4]));
const data = [
['a', 1],
];
t.assert(picker.pick(data) === 'a');
t.assert(picker.pick(data) !== 'b');
});
test('Should work with lists of multiple items', t => {
const picker = new Picker(randomGen([0.1, 0.6, 0.3, 0.4]));
const data = [
['a', 2],
['b', 2],
['c', 4],
['d', 3],
];
t.is(picker.pick(data), 'a');
t.is(picker.pick(data), 'c');
t.is(picker.pick(data), 'b');
t.is(picker.pick(data), 'c');
});
test('Should validate input', t => {
t.throws(() => pick(), {
instanceOf: TypeError,
message: 'Expected an Array as the first argument',
});
t.throws(() => pick('bad'), {
instanceOf: TypeError,
message: 'Expected an Array as the first argument',
});
});