Skip to content

Commit

Permalink
Test RandomHash
Browse files Browse the repository at this point in the history
  • Loading branch information
pablosichert committed Sep 25, 2016
1 parent 01f28ec commit 5e9fa22
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import expect from 'unexpected';

import {
generateHash,
RandomHash,
baseN,
CharsetLengthError
} from '../lib';
Expand Down Expand Up @@ -175,3 +176,62 @@ describe('baseN', () => {
}
});
});

describe('RandomHash', () => {
it('should return a generateHash() instance', () => {
const generateHash = new RandomHash;

expect(generateHash, 'to be a', RandomHash);
expect(generateHash, 'to be a function');
});

it('should save its options', () => {
const length = 123;
const charset = 'abcd';
const rng = () => [];

const generateHash = new RandomHash({
length,
charset,
rng
});

expect(generateHash.length, 'to be', 123);
expect(generateHash.charset, 'to be', 'abcd');
expect(generateHash.rng, 'to be', rng);
});

it('should be able to modify options', () => {
const length = 123;
const charset = 'abcd';
const rng = () => [];

const generateHash = new RandomHash;

generateHash.length = length;
generateHash.charset = charset;
generateHash.rng = rng;

expect(generateHash.length, 'to be', 123);
expect(generateHash.charset, 'to be', 'abcd');
expect(generateHash.rng, 'to be', rng);
});

it('should call generateHash depending on its internal options', () => {
const generateHash = new RandomHash;

expect(generateHash(), 'to match', /[a-zA-Z0-9_-]{6}/);

generateHash.length = 10;

expect(generateHash(), 'to match', /[a-zA-Z0-9_-]{10}/);

generateHash.charset = ['😁', '😎', '😍', '😇'];

expect(generateHash(), 'to match', /[😁😎😍😇]{10}/);

generateHash.rng = byteLength => new Array(byteLength).fill(0);

expect(generateHash(), 'to match', /[😁]{10}/);
});
});

0 comments on commit 5e9fa22

Please sign in to comment.