Skip to content

Commit

Permalink
enabled passing a factory function to objectWithKeys to close #18
Browse files Browse the repository at this point in the history
  • Loading branch information
travi committed Jun 11, 2016
1 parent 5631679 commit a09dbbd
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
8 changes: 6 additions & 2 deletions any.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,15 @@ function simpleObject() {
return object;
}

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

keys.forEach((key) => {
object[key] = string();
if (options.factory) {
object[key] = options.factory();
} else {
object[key] = string();
}
});

return object;
Expand Down
21 changes: 21 additions & 0 deletions test/unit/any-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,5 +175,26 @@ suite('random data generator', () => {
assert.equal(object[key], strings[index]);
});
});

test('that a factory function can be supplied for values', () => {
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.objectWithKeys(keys, {factory});

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

0 comments on commit a09dbbd

Please sign in to comment.