Skip to content

Commit

Permalink
exported methods through the default export to close #32
Browse files Browse the repository at this point in the history
  • Loading branch information
travi committed Jul 3, 2016
1 parent cc625f3 commit 596731a
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 31 deletions.
24 changes: 12 additions & 12 deletions any.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ const chance = new Chance();

const DEFAULT_SIZE_RANGE = {max: 20, min: 1};

const integer = (options) => chance.natural(options);
const string = (options) => chance.string(options);
const word = () => chance.word();
const url = (options) => chance.url(options);
const boolean = () => chance.bool();
const email = () => chance.email();
const date = () => chance.date({string: true});
export const integer = (options) => chance.natural(options);
export const string = (options) => chance.string(options);
export const word = () => chance.word();
export const url = (options) => chance.url(options);
export const boolean = () => chance.bool();
export const email = () => chance.email();
export const date = () => chance.date({string: true});

function simpleObject() {
export function simpleObject() {
const
object = {},
size = integer(DEFAULT_SIZE_RANGE);
Expand All @@ -23,7 +23,7 @@ function simpleObject() {
return object;
}

function objectWithKeys(keys, options = {}) {
export function objectWithKeys(keys, options = {}) {
const object = {};

keys.forEach((key) => {
Expand All @@ -37,7 +37,7 @@ function objectWithKeys(keys, options = {}) {
return object;
}

function listOf(factory, options = {}) {
export function listOf(factory, options = {}) {
const
list = [],
listSize = options.size || integer(Object.assign({}, DEFAULT_SIZE_RANGE, options));
Expand All @@ -49,11 +49,11 @@ function listOf(factory, options = {}) {
return list;
}

function fromList(list) {
export function fromList(list) {
return list[integer({min: 0, max: list.length})];
}

export {
export default {
string,
word,
integer,
Expand Down
154 changes: 135 additions & 19 deletions test/unit/any-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ function randomListOfStrings() {

return list;
}

suite('random data generator', () => {
let sandbox, any, chanceStub;
const options = {foo: 'bar'};
Expand All @@ -39,74 +40,109 @@ suite('random data generator', () => {
chanceStub.natural.withArgs(options).returns(int);

assert.equal(any.integer(options), int);
assert.equal(any.default.integer(options), int);
});

test('that a string is generated', () => {
const string = chance.string();
chanceStub.string.withArgs(options).returns(string);

assert.equal(any.string(options), string);
assert.equal(any.default.string(options), string);
});

test('that a url is generated', () => {
const url = chance.url();
chanceStub.url.withArgs(options).returns(url);

assert.equal(any.url(options), url);
assert.equal(any.default.url(options), url);
});

test('that a url is generated', () => {
const word = chance.word();
chanceStub.word.returns(word);

assert.equal(any.word(options), word);
assert.equal(any.default.word(options), word);
});

test('that a boolean is generated', () => {
const boolean = chance.bool();
chanceStub.bool.returns(boolean);

assert.equal(any.boolean(options), boolean);
assert.equal(any.default.boolean(options), boolean);
});

test('that an email is generated', () => {
const email = chance.email();
chanceStub.email.returns(email);

assert.equal(any.email(options), email);
assert.equal(any.default.email(options), email);
});

test('that a date string is generated', () => {
const date = chance.date();
chanceStub.date.withArgs({string: true}).returns(date);

assert.equal(any.date(options), date);
assert.equal(any.default.date(options), date);
});

test('that the object size is randomly set', () => {
const
strings = [],
words = [],
objectSize = chance.natural(INTEGER_RANGE);
chanceStub.natural.withArgs({min: 1, max: 20}).returns(objectSize);
for (let i = 0; i < objectSize; i += 1) {
suite('simple object', () => {
test('that the object size is randomly set', () => {
const
strings = [],
words = [],
objectSize = chance.natural(INTEGER_RANGE);
chanceStub.natural.withArgs({min: 1, max: 20}).returns(objectSize);
for (let i = 0; i < objectSize; i += 1) {
const
string = chance.string(),
word = chance.word();

strings[i] = string;
words[i] = word;

chanceStub.string.onCall(i).returns(string);
chanceStub.word.onCall(i).returns(word);
}

const object = any.simpleObject();

assert.equal(Object.keys(object).length, objectSize);
for (let i = 0; i < objectSize; i += 1) {
assert.equal(object[words[i]], strings[i]);
}
});

test('that the object size is randomly set when accessed through the default export', () => {
const
string = chance.string(),
word = chance.word();
strings = [],
words = [],
objectSize = chance.natural(INTEGER_RANGE);
chanceStub.natural.withArgs({min: 1, max: 20}).returns(objectSize);
for (let i = 0; i < objectSize; i += 1) {
const
string = chance.string(),
word = chance.word();

strings[i] = string;
words[i] = word;
strings[i] = string;
words[i] = word;

chanceStub.string.onCall(i).returns(string);
chanceStub.word.onCall(i).returns(word);
}
chanceStub.string.onCall(i).returns(string);
chanceStub.word.onCall(i).returns(word);
}

const object = any.simpleObject();
const object = any.default.simpleObject();

assert.equal(Object.keys(object).length, objectSize);
for (let i = 0; i < objectSize; i += 1) {
assert.equal(object[words[i]], strings[i]);
}
assert.equal(Object.keys(object).length, objectSize);
for (let i = 0; i < objectSize; i += 1) {
assert.equal(object[words[i]], strings[i]);
}
});
});

suite('list of', () => {
Expand All @@ -126,6 +162,15 @@ suite('random data generator', () => {
assert.callCount(factory, listSize);
});

test('that a list of random size is returned by default when accessed through the default export', () => {
const
factory = sinon.spy(),
list = any.default.listOf(factory);

assert.equal(list.length, listSize);
assert.callCount(factory, listSize);
});

test('that the list size can be set through the options', () => {
const
size = chance.natural(INTEGER_RANGE),
Expand All @@ -134,12 +179,31 @@ suite('random data generator', () => {
assert.equal(list.length, size);
});


test('that the list size can be set through the options when accessed through the default export', () => {
const
size = chance.natural(INTEGER_RANGE),
list = any.default.listOf(sinon.spy(), {size});

assert.equal(list.length, size);
});

test('that the minimum range limit can be set through the options', () => {
const min = chance.natural(INTEGER_RANGE);
chanceStub.natural.withArgs({min, max: 20}).returns(listSize);

assert.equal(any.listOf(sinon.spy(), {min}).length, listSize);
});

test(
'that the minimum range limit can be set through the options when accessed through the default export',
() => {
const min = chance.natural(INTEGER_RANGE);
chanceStub.natural.withArgs({min, max: 20}).returns(listSize);

assert.equal(any.default.listOf(sinon.spy(), {min}).length, listSize);
}
);
});

suite('from list', () => {
Expand All @@ -152,6 +216,16 @@ suite('random data generator', () => {

assert.equal(any.fromList(list), list[index]);
});

test('that an item from the provided list is returned when accessed through the default export', () => {
const
list = randomListOfStrings(),
indexRange = {min: 0, max: list.length},
index = chance.natural(indexRange);
chanceStub.natural.withArgs(indexRange).returns(index);

assert.equal(any.default.fromList(list), list[index]);
});
});

suite('object with keys', () => {
Expand All @@ -176,6 +250,27 @@ suite('random data generator', () => {
});
});

test('that an object is generated from the list of keys when accessed through the default export', () => {
const
keys = randomListOfStrings(),
strings = [];

for (let i = 0; i < keys.length; i += 1) {
const string = chance.string();

strings[i] = string;

chanceStub.string.onCall(i).returns(string);
}

const object = any.default.objectWithKeys(keys);

assert.deepEqual(Object.keys(object), keys);
keys.forEach((key, index) => {
assert.equal(object[key], strings[index]);
});
});

test('that a factory function can be supplied for values', () => {
const
keys = randomListOfStrings(),
Expand All @@ -196,5 +291,26 @@ suite('random data generator', () => {
assert.equal(object[key], values[index]);
});
});

test('that a factory function can be supplied for values when accessed through the default export', () => {
const
keys = randomListOfStrings(),
factory = sinon.stub(),
values = [];

for (let i = 0; i < keys.length; i += 1) {
const value = chance.string();

values[i] = value;

factory.onCall(i).returns(value);
}

const object = any.default.objectWithKeys(keys, {factory});

keys.forEach((key, index) => {
assert.equal(object[key], values[index]);
});
});
});
});

0 comments on commit 596731a

Please sign in to comment.