js-libp2p-crypto/test/random-bytes.spec.js
dirkmc 0b686d363c chore: add error codes (#155)
* chore: add error codes

* chore: create errors with new Error()

* fix: better error testin

* refactor: simplify random bytes error checks
2019-07-22 11:16:02 +01:00

28 lines
759 B
JavaScript

/* eslint-env mocha */
'use strict'
const chai = require('chai')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)
const randomBytes = require('../src/random-bytes')
describe('randomBytes', () => {
it('produces random bytes', () => {
expect(randomBytes(16)).to.have.length(16)
})
it('throws if length is 0', () => {
expect(() => randomBytes(0)).to.throw(Error).with.property('code', 'ERR_INVALID_LENGTH')
})
it('throws if length is < 0', () => {
expect(() => randomBytes(-1)).to.throw(Error).with.property('code', 'ERR_INVALID_LENGTH')
})
it('throws if length is not a number', () => {
expect(() => randomBytes('hi')).to.throw(Error).with.property('code', 'ERR_INVALID_LENGTH')
})
})