diff --git a/lib/mockBuilder.js b/lib/mockBuilder.js index 35f9b91..831f3b9 100644 --- a/lib/mockBuilder.js +++ b/lib/mockBuilder.js @@ -1,9 +1,13 @@ 'use strict' const schemaParser = require('easygraphql-parser') -const { memoizedField, clearMemoizedFields } = require('../util') +const settings = require('./settings') + +function mockBuilder (schemaCode, customMock, seed) { + if (seed) settings['seed'] = seed + + const { memoizedField, clearMemoizedFields } = require('../util') -function mockBuilder (schemaCode, customMock) { // Parse the schema on the GQL file to create a cutom object easier to loop // and create mocks. const schema = schemaParser(schemaCode) diff --git a/lib/settings.js b/lib/settings.js new file mode 100644 index 0000000..2917d95 --- /dev/null +++ b/lib/settings.js @@ -0,0 +1,2 @@ +const settings = {} +module.exports = settings diff --git a/util/chance.js b/util/chance.js new file mode 100644 index 0000000..6c8f6a4 --- /dev/null +++ b/util/chance.js @@ -0,0 +1,7 @@ +const settings = require('../lib/settings') + +const Chance = require('chance') + +const chance = settings.seed ? new Chance(settings.seed) : new Chance() + +module.exports = chance diff --git a/util/index.js b/util/index.js index 11196d1..d289f11 100644 --- a/util/index.js +++ b/util/index.js @@ -9,7 +9,7 @@ const randomBooleanData = require('./randomData/boolean') const { randomNumber } = require('./utils') let cache = {} -const memoize = (fn) => { +const memoize = fn => { return (type, customMock, schema) => { if (type in cache) { return cache[type] @@ -17,7 +17,10 @@ const memoize = (fn) => { // To handle cycles in schema types put a reference to the mocked field in // the cache before attempting to compute its properties. const result = {} - if (schema[type].type === 'InterfaceTypeDefinition' && schema[type].implementedTypes.length) { + if ( + schema[type].type === 'InterfaceTypeDefinition' && + schema[type].implementedTypes.length + ) { result['__typename'] = schema[type].implementedTypes[0] } else { result['__typename'] = type @@ -99,16 +102,14 @@ function createData (field, schemaName, customMock = {}, schema) { // Validate if is enun value if (schema[field.type].values.length > 0) { dataArr.push(selecteEnumVal(field, schema)) - // validate if is union + // validate if is union } else if (schema[field.type].types.length > 0) { const types = getUnionVals(field, schema) types.forEach(type => dataArr.push(memoizedField(type, customMock, schema)) ) } else { - dataArr.push( - memoizedField(field.type, customMock, schema) - ) + dataArr.push(memoizedField(field.type, customMock, schema)) } } return dataArr diff --git a/util/randomData/number.js b/util/randomData/number.js index 58a8804..b5321ae 100644 --- a/util/randomData/number.js +++ b/util/randomData/number.js @@ -1,11 +1,9 @@ 'use strict' -const Chance = require('chance') +const chance = require('../chance') const constants = require('../constants') const { randomNumber } = require('../utils') -const chance = new Chance() - function randomNumberData (field, float) { let data @@ -73,11 +71,10 @@ function createDataType (field, float) { function createRandomNumber (float) { if (float) { - const precision = 100 - return Math.floor(Math.random() * (10 * precision - 1 * precision) + 1 * precision) / (1 * precision) + return chance.floating({ min: 0, max: 10, fixed: 2 }) } - return Math.floor(Math.random() * 11) + return chance.integer({ min: 0, max: 10 }) } module.exports = randomNumberData diff --git a/util/randomData/string.js b/util/randomData/string.js index d4d07ab..cab4a59 100644 --- a/util/randomData/string.js +++ b/util/randomData/string.js @@ -1,11 +1,9 @@ 'use strict' -const Chance = require('chance') +const chance = require('../chance') const constants = require('../constants') const { randomNumber } = require('../utils') -const chance = new Chance() - function randomStringData (field) { let data @@ -102,7 +100,7 @@ function createRandomString (length) { const chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' length = length || randomNumber(5, 20) let result = '' - for (let i = length; i > 0; --i) result += chars[randomNumber(0, chars.length)] + for (let i = length; i > 0; --i) { result += chars[randomNumber(0, chars.length)] } return result } diff --git a/util/utils.js b/util/utils.js index a4667b3..2bd7e33 100644 --- a/util/utils.js +++ b/util/utils.js @@ -1,5 +1,7 @@ +const chance = require('./chance') + function randomNumber (min, max) { - return Math.floor(Math.random() * (max - min + 1)) + min + return chance.integer({ min, max }) } module.exports = { randomNumber }