Skip to content

Commit

Permalink
new build 0.5 added new array config
Browse files Browse the repository at this point in the history
  • Loading branch information
danibram committed Dec 15, 2015
1 parent 3bc7360 commit 43858e7
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 17 deletions.
56 changes: 47 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,16 +121,19 @@ Inside every value you can put:
- ***length***: to know how many values
- ***fixedLength***: true to create always same amount of values in the array, false to generate a random number bettwen 0 and 'length' value.
```javascript
[{
//Any generator
//Faker
faker: 'random.arrayElement(db.users)[userId]'
//Chance
chance: 'integer'
//Function
function: function (){ return /**/ }
[{
//Any generator
//Faker
faker: 'random.arrayElement(db.users)[userId]'
//Chance
chance: 'integer'
//Function
function: function (){ return /**/ }

}, {length: 10, fixedLength: false}]
//Array config
length: 10,
fixedLength: false
}]
```
#### Optional fields
- ***[virtual]***: Boolean, if you pass this option, this mean that this field will not appear at the output entity. But you can use during the generation.
Expand Down Expand Up @@ -181,6 +184,41 @@ m.generate('user', 2)
## Release History
#### (0.5.0)
- Break Point with array config. Now is more clear.
Old config:
```javascript
[{
//Any generator
//Faker
faker: 'random.arrayElement(db.users)[userId]'
//Chance
chance: 'integer'
//Function
function: function (){ return /**/ }

}, //Array config
{length: 10, fixedLength: false}]
```
New configuration:
```javascript
[{
//Any generator
//Faker
faker: 'random.arrayElement(db.users)[userId]'
//Chance
chance: 'integer'
//Function
function: function (){ return /**/ }

//Array config
length: 10,
fixedLength: false
}]
```
#### (0.4.7)
- Add virtual fields
Expand Down
5 changes: 2 additions & 3 deletions build/mocker.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ return /******/ (function(modules) { // webpackBootstrap
var proccessNode = function proccessNode(obj, k, value, path) {
if (path) {
if (utils.isArray(value)) {
if (value[1].virtual) {
if (value[0].virtual) {
_this2.virtualPaths.push(path.toString());
}
} else {
Expand Down Expand Up @@ -255,9 +255,8 @@ return /******/ (function(modules) { // webpackBootstrap
value: function generator(field, cb) {
if (utils.isArray(field)) {
var fieldConfig = field[0];
var arrayConfig = field[1];
var array = [];
var length = utils.fieldArrayCalcLength(arrayConfig);
var length = utils.fieldArrayCalcLength(fieldConfig);
for (var i = 0; i < length; i++) {
array.push(this.generateNormalField(fieldConfig));
}
Expand Down
2 changes: 1 addition & 1 deletion build/mocker.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mocker-data-generator",
"version": "0.4.7",
"version": "0.5.0",
"description": "A simplified way to generate mock data, builds using a simple schema and with the FakerJs",
"main": "./build/mocker.min.js",
"repository": {
Expand Down
5 changes: 2 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export default class Mocker {
let proccessNode = (obj, k, value, path?) => {
if (path){
if ( utils.isArray(value) ){
if (value[1].virtual){
if (value[0].virtual){
this.virtualPaths.push(path.toString())
}
} else {
Expand Down Expand Up @@ -167,9 +167,8 @@ export default class Mocker {
generator(field, cb) {
if ( utils.isArray(field) ){
let fieldConfig = field[0]
let arrayConfig = field[1]
let array = []
let length = utils.fieldArrayCalcLength(arrayConfig)
let length = utils.fieldArrayCalcLength(fieldConfig)
for (let i = 0; i < length; i++) {
array.push(this.generateNormalField(fieldConfig))
}
Expand Down
125 changes: 125 additions & 0 deletions test/mocker.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,131 @@ describe('Mocker: Generators (Fields)', function() {
})
})

describe('Options: Array', function() {
it('It should recognise static field', function(done) {
var limit = 10
var model = 'hello'
var arr = []
for (var i = 0; i < limit; i++) {
arr.push(model)
}

var situation = {
sites: [{
static: model,
length: 10,
fixedLength: true
}]
}
var result = {
sites: arr
}

var m = mocker({
situation: situation
})
m.generateEntity(situation, function(data) {
expect(data)
.to.deep.equal(result)
.to.not.be.undefined
.to.not.be.null
done()
})
})

it('It should recognise functions field', function(done) {
var limit = 10
var model = 'hello'
var arr = []
for (var i = 0; i < limit; i++) {
arr.push(model)
}

var situation = {
sites: [{
function() {
return 'hello'
},

length: 10,
fixedLength: true
}]
}
var result = {
sites: arr
}

var m = mocker({
situation: situation
})
m.generateEntity(situation, function(data) {
expect(data)
.to.deep.equal(result)
.to.not.be.undefined
.to.not.be.null
done()
})
})

it('It should recognise fakerJs field', function(done) {
var situation = {
sites: [{
faker: 'lorem.words()[0]',
length: 10,
fixedLength: false
}]
}

var m = mocker({
situation: situation
})
m.generateEntity(situation, function(data) {
expect(data.sites)
.to.be.an('array')
.to.have.length.below(11)
.to.not.be.undefined
.to.not.be.null
for (var i = 0; i < data.sites.length; i++) {
expect(data.sites[i])
.to.be.a('string')
.to.not.be.undefined
.to.not.be.null
}

done()
})
})

it('It should recognise chanceJs field', function(done) {
var situation = {
sites: [{
chance: 'integer',
length: 10,
fixedLength: false
}]
}

var m = mocker({
situation: situation
})
m.generateEntity(situation, function(data) {
expect(data.sites)
.to.be.an('array')
.to.have.length.below(11)
.to.not.be.undefined
.to.not.be.null
for (var i = 0; i < data.sites.length; i++) {
expect(data.sites[i])
.to.be.a('number')
.to.not.be.undefined
.to.not.be.null
}

done()
})
})
})

describe('Options: Virtual Fields', function() {
var situation = {
exampleVirtual: {
Expand Down

0 comments on commit 43858e7

Please sign in to comment.