UNPKG

1.69 kBJavaScriptView Raw
1var chai = require('chai');
2var assert = chai.assert;
3var randomHex = require('./src/index.js');
4
5
6describe('randomHex', function () {
7 it('should generate random bytes with a specific length sync', function () {
8
9 assert.equal(randomHex(0).length, 2 + 0)
10 assert.equal(randomHex(3).length, 2 + 6)
11 assert.equal(randomHex(30).length, 2 + 60)
12 assert.equal(randomHex(300).length, 2 + 600)
13 assert.isTrue(/^0x[a-f0-9]+$/.test(randomHex(300)));
14 });
15
16 it('should generate random bytes with a specific length async', function (done) {
17
18 randomHex(0, function (err, resp) {
19 if (err) throw err
20
21 assert.equal(resp.length, 2 + 0);
22 done();
23 })
24 });
25 it('should generate random bytes with a specific length async', function (done) {
26
27 randomHex(3, function (err, resp) {
28 if (err) throw err
29
30 assert.equal(resp.length, 2 + 6);
31 done();
32 })
33 });
34 it('should generate random bytes with a specific length async', function (done) {
35
36 randomHex(30, function (err, resp) {
37 if (err) throw err
38
39 assert.equal(resp.length, 2 + 60);
40 assert.isTrue(/^0x[a-f0-9]+$/.test(resp));
41 done();
42 })
43 });
44 it('should generate random bytes with a specific length async', function (done) {
45
46 randomHex(300, function (err, resp) {
47 if (err) throw err
48
49 assert.equal(resp.length, 2 + 600);
50 done();
51 })
52 });
53
54
55 it('requesting to much throws sync', function () {
56 assert.throws(randomHex.bind(null, 65537));
57 });
58
59 it('requesting to much throws async', function (done) {
60
61 randomHex(65537, function (err, res) {
62
63 assert.isTrue(err instanceof Error);
64 done();
65 })
66
67 });
68});
69