Skip to content
This repository has been archived by the owner on Jul 25, 2019. It is now read-only.

Commit

Permalink
Include mockData module to generate random data samples based on JSON…
Browse files Browse the repository at this point in the history
… Schemas #40
  • Loading branch information
bernardodiasc authored and hdamaich committed Nov 7, 2017
1 parent 77d8510 commit 6e9f34b
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
`$ yarn test`

### Modo watch
`$ yarn test --watch`
`$ yarn test -- --watch`
18 changes: 18 additions & 0 deletions config/mockData.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import jsf from 'json-schema-faker'

/**
* Get random samples
* @param {object} schema schema of the desired model to get samples
* @param {number} amount amount of samples to return
* @return {array} retrun list of samples
*/
const mockData = (schema = {}, amount) => {
const validAmount = (typeof amount == 'number' && !isNaN(amount)) ? parseInt(amount, 10) : 1
const samples = new Array(validAmount)
for (var i = samples.length - 1; i >= 0; i--) {
samples[i] = jsf(schema)
}
return samples
}

export default mockData
94 changes: 94 additions & 0 deletions config/mockData.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import mockData from './mockData.js'

// Sample of a JSON Schema
const schema = {
"properties": {
"UID": {
"properties": {
"conhecimentos": {
"items": {
"type": "string"
},
"type": "array"
},
"contatos": {
"properties": {
"FaceBook": {
"type": "string"
},
"GitHub": {
"type": "string"
},
"LinkedIn": {
"type": "string"
},
"Site": {
"type": "string"
},
"Twitter": {
"type": "string"
},
"Youtube": {
"type": "string"
}
},
"type": "object"
},
"email": {
"type": "string"
},
"nome": {
"type": "string"
},
"termos": {
"type": "boolean"
}
},
"type": "object"
}
},
"type": "object"
}

describe('mockData() with random samples', () => {

it('mockData(schema) return 1 sample', () => {
const sampleData = mockData(schema)
expect(sampleData.length).toBe(1)
})

it('mockData(schema, 1) return 1 sample', () => {
const sampleData = mockData(schema, 1)
expect(sampleData.length).toBe(1)
})

it('mockData(schema, 5) return 5 samples', () => {
const sampleData = mockData(schema, 5)
expect(sampleData.length).toBe(5)
})

it('mockData(schema, 0) return 0 samples', () => {
const sampleData = mockData(schema, 0)
expect(sampleData.length).toBe(0)
})

it('mockData(schema, undefined) return 1 sample', () => {
const sampleData = mockData(undefined)
expect(sampleData.length).toBe(1)
})

it('mockData(schema, null) return 1 sample', () => {
const sampleData = mockData(schema, null)
expect(sampleData.length).toBe(1)
})

it('mockData(schema, "asdasd") return 1 sample', () => {
const sampleData = mockData(schema, 'asdasd')
expect(sampleData.length).toBe(1)
})

it('mockData(schema, NaN) return 1 sample', () => {
const sampleData = mockData(schema, NaN)
expect(sampleData.length).toBe(1)
})
})
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ module.exports = {
'^src[/](.+)': '<rootDir>/src/$1',
'^assets[/](.+)': '<rootDir>/assets/$1'
},
setupFiles: ['<rootDir>/config/setupEnzyme.js']
setupFiles: ['raf/polyfill', '<rootDir>/config/setupEnzyme.js']
};
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
"styled-components": "^2.2.1"
},
"devDependencies": {
"@storybook/addon-actions": "^3.2.12",
"@storybook/addon-links": "^3.2.12",
"@storybook/react": "^3.2.12",
"babel-core": "^6.26.0",
"babel-eslint": "^8.0.1",
"babel-jest": "^21.2.0",
Expand All @@ -51,17 +54,16 @@
"html-webpack-plugin": "^2.30.1",
"jest": "^21.2.1",
"jest-enzyme": "^4.0.0",
"json-schema-faker": "^0.4.6",
"prettier": "^1.7.4",
"raf": "^3.4.0",
"react-hot-loader": "next",
"react-test-renderer": "^16.0.0",
"redux-logger": "^3.0.6",
"redux-mock-store": "^1.3.0",
"uglifyjs-webpack-plugin": "^0.4.6",
"webpack": "^3.6.0",
"webpack-dev-server": "^2.9.1",
"webpack-merge": "^4.1.0",
"@storybook/react": "^3.2.12",
"@storybook/addon-actions": "^3.2.12",
"@storybook/addon-links": "^3.2.12"
"webpack-merge": "^4.1.0"
}
}
34 changes: 31 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1855,6 +1855,10 @@ chalk@^2.0.0, chalk@^2.0.1, chalk@^2.1.0:
escape-string-regexp "^1.0.5"
supports-color "^4.0.0"

chance@^1.0.11:
version "1.0.11"
resolved "https://registry.yarnpkg.com/chance/-/chance-1.0.11.tgz#48e82f7583df356053e0ad122d4654c5066c570d"

cheerio@^1.0.0-rc.2:
version "1.0.0-rc.2"
resolved "https://registry.yarnpkg.com/cheerio/-/cheerio-1.0.0-rc.2.tgz#4b9f53a81b27e4d5dac31c0ffd0cfa03cc6830db"
Expand Down Expand Up @@ -2381,7 +2385,7 @@ deep-equal@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/deep-equal/-/deep-equal-1.0.1.tgz#f5d260292b660e084eff4cdbc9f08ad3247448b5"

deep-extend@~0.4.0:
deep-extend@^0.4.0, deep-extend@~0.4.0:
version "0.4.2"
resolved "https://registry.yarnpkg.com/deep-extend/-/deep-extend-0.4.2.tgz#48b699c27e334bf89f10892be432f6e4c7d34a7f"

Expand Down Expand Up @@ -2441,6 +2445,12 @@ [email protected], depd@~1.1.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/depd/-/depd-1.1.1.tgz#5783b4e1c459f06fa5ca27f991f3d06e7a310359"

deref@^0.7.0:
version "0.7.0"
resolved "https://registry.yarnpkg.com/deref/-/deref-0.7.0.tgz#f862485b971f51e9aad590d149b9eb46dcc074c1"
dependencies:
deep-extend "^0.4.0"

des.js@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/des.js/-/des.js-1.0.0.tgz#c074d2e2aa6a8a9a07dbd61f9a15c2cd83ec8ecc"
Expand Down Expand Up @@ -3159,6 +3169,10 @@ [email protected], extsprintf@^1.2.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/extsprintf/-/extsprintf-1.3.0.tgz#96918440e3041a7a414f8c52e3c574eb3c3e1e05"

faker@^4.1.0:
version "4.1.0"
resolved "https://registry.yarnpkg.com/faker/-/faker-4.1.0.tgz#1e45bbbecc6774b3c195fad2835109c6d748cc3f"

fast-deep-equal@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/fast-deep-equal/-/fast-deep-equal-1.0.0.tgz#96256a3bc975595eb36d82e9929d060d893439ff"
Expand Down Expand Up @@ -4590,6 +4604,16 @@ json-loader@^0.5.4, json-loader@^0.5.7:
version "0.5.7"
resolved "https://registry.yarnpkg.com/json-loader/-/json-loader-0.5.7.tgz#dca14a70235ff82f0ac9a3abeb60d337a365185d"

json-schema-faker@^0.4.6:
version "0.4.6"
resolved "https://registry.yarnpkg.com/json-schema-faker/-/json-schema-faker-0.4.6.tgz#34ba1b3aa32690f390db5939376c10544a73e8bb"
dependencies:
chance "^1.0.11"
deref "^0.7.0"
faker "^4.1.0"
randexp "^0.4.6"
tslib "^1.8.0"

json-schema-traverse@^0.3.0:
version "0.3.1"
resolved "https://registry.yarnpkg.com/json-schema-traverse/-/json-schema-traverse-0.3.1.tgz#349a6d44c53a51de89b40805c5d5e59b417d3340"
Expand Down Expand Up @@ -6060,7 +6084,7 @@ radium@^0.19.0:
inline-style-prefixer "^2.0.5"
prop-types "^15.5.8"

raf@^3.3.2:
raf@^3.3.2, raf@^3.4.0:
version "3.4.0"
resolved "https://registry.yarnpkg.com/raf/-/raf-3.4.0.tgz#a28876881b4bc2ca9117d4138163ddb80f781575"
dependencies:
Expand All @@ -6070,7 +6094,7 @@ railroad-diagrams@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/railroad-diagrams/-/railroad-diagrams-1.0.0.tgz#eb7e6267548ddedfb899c1b90e57374559cddb7e"

randexp@^0.4.2:
randexp@^0.4.2, randexp@^0.4.6:
version "0.4.6"
resolved "https://registry.yarnpkg.com/randexp/-/randexp-0.4.6.tgz#e986ad5e5e31dae13ddd6f7b3019aa7c87f60ca3"
dependencies:
Expand Down Expand Up @@ -7310,6 +7334,10 @@ tryit@^1.0.1:
version "1.0.3"
resolved "https://registry.yarnpkg.com/tryit/-/tryit-1.0.3.tgz#393be730a9446fd1ead6da59a014308f36c289cb"

tslib@^1.8.0:
version "1.8.0"
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.8.0.tgz#dc604ebad64bcbf696d613da6c954aa0e7ea1eb6"

[email protected]:
version "0.0.0"
resolved "https://registry.yarnpkg.com/tty-browserify/-/tty-browserify-0.0.0.tgz#a157ba402da24e9bf957f9aa69d524eed42901a6"
Expand Down

0 comments on commit 6e9f34b

Please sign in to comment.