Skip to content

Commit

Permalink
[test] Add faker.fake functional tests #386
Browse files Browse the repository at this point in the history
  - Now running all methods through faker.fake
  - Adds coverage for scope issues #376
  - Refactors functional test methods to helper
  • Loading branch information
Marak committed Oct 27, 2018
1 parent ed9c560 commit ad88ac3
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 34 deletions.
62 changes: 28 additions & 34 deletions test/all.functional.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,9 @@ if (typeof module !== 'undefined') {
var faker = require('../index');
}

var IGNORED_MODULES = ['locales', 'locale', 'localeFallback', 'definitions', 'fake', 'helpers'];
var IGNORED_METHODS = {
system: ['directoryPath', 'filePath'] // these are TODOs
};
var functionalHelpers = require('./support/function-helpers.js');

function isTestableModule(mod) {
return IGNORED_MODULES.indexOf(mod) === -1;
}

function isMethodOf(mod) {
return function(meth) {
return typeof faker[mod][meth] === 'function';
};
}

function isTestableMethod(mod) {
return function(meth) {
return !(mod in IGNORED_METHODS && IGNORED_METHODS[mod].indexOf(meth) >= 0);
};
}

function both(pred1, pred2) {
return function(value) {
return pred1(value) && pred2(value);
};
}

// Basic smoke tests to make sure each method is at least implemented and returns a value.

var modules = Object.keys(faker)
.filter(isTestableModule)
.reduce(function(result, mod) {
result[mod] = Object.keys(faker[mod]).filter(both(isMethodOf(mod), isTestableMethod(mod)));
return result;
}, {});
var modules = functionalHelpers.modulesList();

describe("functional tests", function () {
for(var locale in faker.locales) {
Expand All @@ -58,4 +26,30 @@ describe("functional tests", function () {
});
});
}
});

describe("faker.fake functional tests", function () {
for(var locale in faker.locales) {
faker.locale = locale;
faker.seed(1);
Object.keys(modules).forEach(function (module) {
describe(module, function () {
modules[module].forEach(function (meth) {
it(meth + "()", function () {
var result = faker.fake('{{' + module + '.' + meth + '}}');
// just make sure any result is returned
// an undefined result usually means an error
assert.ok(typeof result !== 'undefined');
/*
if (meth === 'boolean') {
assert.ok(result === true || result === false);
} else {
assert.ok(result);
}
*/
});
});
});
});
}
});
33 changes: 33 additions & 0 deletions test/helpers.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,46 @@ describe("helpers.js", function () {
});
});

describe("mustache()", function () {
it("returns empty string with no arguments", function () {
assert.equal(faker.helpers.mustache(), "");
});
});

describe("repeatString()", function () {
it("returns empty string with no arguments", function () {
assert.equal(faker.helpers.repeatString(), "");
});
});

describe("replaceSymbols()", function () {
it("returns empty string with no arguments", function () {
assert.equal(faker.helpers.replaceSymbols(), "");
});
});

/*
describe("replaceCreditCardSymbols()", function () {
it("returns empty string with no arguments", function () {
assert.equal(faker.helpers.replaceCreditCardSymbols(), "");
});
});
*/

describe("createCard()", function () {
it("returns an object", function () {
var card = faker.helpers.createCard();
assert.ok(typeof card === 'object');
});
});

describe("contextualCard()", function () {
it("returns an object", function () {
var card = faker.helpers.contextualCard();
assert.ok(typeof card === 'object');
});
});

describe("userCard()", function () {
it("returns an object", function () {
var card = faker.helpers.userCard();
Expand Down
50 changes: 50 additions & 0 deletions test/support/function-helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
if (typeof module !== 'undefined') {
var assert = require('assert');
var sinon = require('sinon');
var faker = require('../../index');
}

var functionHelpers = {};

module.exports = functionHelpers;


var IGNORED_MODULES = ['locales', 'locale', 'localeFallback', 'definitions', 'fake', 'helpers'];
var IGNORED_METHODS = {
system: ['directoryPath', 'filePath'] // these are TODOs
};

function isTestableModule(mod) {
return IGNORED_MODULES.indexOf(mod) === -1;
}

function isMethodOf(mod) {
return function(meth) {
return typeof faker[mod][meth] === 'function';
};
}

function isTestableMethod(mod) {
return function(meth) {
return !(mod in IGNORED_METHODS && IGNORED_METHODS[mod].indexOf(meth) >= 0);
};
}

function both(pred1, pred2) {
return function(value) {
return pred1(value) && pred2(value);
};
}

// Basic smoke tests to make sure each method is at least implemented and returns a value.

functionHelpers.modulesList = function modulesList () {
var modules = Object.keys(faker)
.filter(isTestableModule)
.reduce(function(result, mod) {
result[mod] = Object.keys(faker[mod]).filter(both(isMethodOf(mod), isTestableMethod(mod)));
return result;
}, {});

return modules;
}

0 comments on commit ad88ac3

Please sign in to comment.