Skip to content

Commit

Permalink
enabled listOf to ensure uniqueness for a single property to close #34
Browse files Browse the repository at this point in the history
  • Loading branch information
travi committed Jul 7, 2016
1 parent b035adc commit 99ea614
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 32 deletions.
28 changes: 20 additions & 8 deletions any.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import _ from 'lodash';
import Chance from 'chance';
const chance = new Chance();

Expand Down Expand Up @@ -38,19 +39,30 @@ export function objectWithKeys(keys, options = {}) {
}

export function listOf(factory, options = {}) {
const
list = [],
listSize = options.size || integer(Object.assign({}, DEFAULT_SIZE_RANGE, options));
const listSize = options.size || integer(Object.assign({}, DEFAULT_SIZE_RANGE, options));

for (let i = 0; i < listSize; i += 1) {
list.push(factory());
}
if (options.uniqueOn) {
const uniqueValues = {};

while (Object.keys(uniqueValues).length < listSize) {
const item = factory();
uniqueValues[item[options.uniqueOn]] = item;
}

return list;
return _.values(uniqueValues);
} else {
const list = [];

for (let i = 0; i < listSize; i += 1) {
list.push(factory());
}

return list;
}
}

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

export default {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"grunt-rollup": "0.7.1",
"istanbul": "1.0.0-alpha.2",
"load-grunt-config": "0.19.2",
"lodash": "4.13.1",
"mocha": "2.5.3",
"proxyquire": "1.7.10",
"referee": "1.2.0",
Expand Down
16 changes: 16 additions & 0 deletions test/helpers/data-generator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import Chance from 'chance';
export const chance = new Chance();

export const INTEGER_RANGE = {min: 1, max: 10};

export function randomListOfStrings() {
const
list = [],
listSize = chance.natural(INTEGER_RANGE, {min: 3});

for (let i = 0; i < listSize; i += 1) {
list.push(chance.string());
}

return list;
}
29 changes: 9 additions & 20 deletions test/unit/any-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,7 @@ import proxyquire from 'proxyquire';
import sinon from 'sinon';
import {assert} from 'chai';
import Chance from 'chance';

const
chance = new Chance(),
INTEGER_RANGE = {min: 1, max: 10};

function randomListOfStrings() {
const
list = [],
listSize = chance.natural(INTEGER_RANGE);

for (let i = 0; i < listSize; i += 1) {
list.push(chance.string());
}

return list;
}
import {chance, INTEGER_RANGE, randomListOfStrings} from '../helpers/data-generator';

suite('random data generator', () => {
let sandbox, any, chanceStub;
Expand Down Expand Up @@ -149,21 +134,25 @@ suite('random data generator', () => {
test('that an item from the provided list is returned', () => {
const
list = randomListOfStrings(),
indexRange = {min: 0, max: list.length},
indexRange = {min: 0, max: list.length - 1},
index = chance.natural(indexRange);
chanceStub.natural.withArgs(indexRange).returns(index);

assert.equal(any.fromList(list), list[index]);
const item = any.fromList(list);
assert.isDefined(item);
assert.equal(item, 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},
indexRange = {min: 0, max: list.length - 1},
index = chance.natural(indexRange);
chanceStub.natural.withArgs(indexRange).returns(index);

assert.equal(any.default.fromList(list), list[index]);
const item = any.default.fromList(list);
assert.isDefined(item);
assert.equal(item, list[index]);
});
});

Expand Down
30 changes: 26 additions & 4 deletions test/unit/list-of-test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import realAny from '../../any';
import Chance from 'chance';
import sinon from 'sinon';
import {assert} from 'chai';
import proxyquire from 'proxyquire';

const
chance = new Chance(),
INTEGER_RANGE = {min: 1, max: 10};
import _ from 'lodash';
import {chance, INTEGER_RANGE, randomListOfStrings} from '../helpers/data-generator';

suite('list of', () => {
let sandbox, any, chanceStub, listSize;
Expand Down Expand Up @@ -76,4 +75,27 @@ suite('list of', () => {
assert.equal(any.default.listOf(sinon.spy(), {min}).length, listSize);
}
);

test('that uniqueness is enforced', () => {
const uniqueOn = chance.word();
const nonUniqueValue = randomListOfStrings();
const min = chance.natural(INTEGER_RANGE);
const factory = () => {
return Object.assign({}, realAny.simpleObject(), {
[uniqueOn]: realAny.fromList([nonUniqueValue, chance.string()])
});
};
chanceStub.natural.returns(listSize);
chanceStub.natural.withArgs(sinon.match({min})).returns(min);

const listOf = any.listOf(factory, {uniqueOn});
const minListOf = any.default.listOf(factory, {uniqueOn, min});
assert.lengthOf(_.uniqBy(listOf, uniqueOn), listOf.length);
assert.isAtLeast(minListOf.length, min);

const defaultListOf = any.default.listOf(factory, {uniqueOn});
const minDefaultListOf = any.default.listOf(factory, {uniqueOn, min});
assert.lengthOf(_.uniqBy(defaultListOf, uniqueOn), defaultListOf.length);
assert.isAtLeast(minDefaultListOf.length, min);
});
});

0 comments on commit 99ea614

Please sign in to comment.