basic-factory
Advanced tools
Comparing version 0.0.1 to 0.0.2
@@ -0,10 +1,16 @@ | ||
import { overwriteMerge } from './lib/objectMerge' | ||
export default class Factory { | ||
registeredTypes = {} | ||
constructor() { | ||
this.registeredTypes = {} | ||
} | ||
register = (type, generator) => { | ||
if (this.registeredTypes[type]) | ||
if (this.registeredTypes[type]) { | ||
throw new Error(`Type ${type} already exists.`) | ||
} | ||
if (typeof generator() != 'object') | ||
if (typeof generator() != 'object') { | ||
throw new Error('generator must return an object') | ||
} | ||
@@ -14,15 +20,35 @@ this.registeredTypes[type] = generator | ||
create = type => { | ||
if (this.registeredTypes[type] === undefined) | ||
create = (type, overwrites) => { | ||
if (this.registeredTypes[type] === undefined) { | ||
throw new Error(`Type ${type} does not exist.`) | ||
} | ||
if (overwrites) { | ||
return overwriteMerge(this.registeredTypes[type](), overwrites) | ||
} | ||
return this.registeredTypes[type]() | ||
} | ||
createMany = (type, count) => { | ||
if (this.registeredTypes[type] === undefined) | ||
createMany = (type, count, overwrites) => { | ||
if (this.registeredTypes[type] === undefined) { | ||
throw new Error(`Type ${type} does not exist.`) | ||
} | ||
if (isNaN(count)) { | ||
throw new Error(`Count must be provided as a number.`) | ||
} | ||
if (count <= 0) { | ||
throw new Error(`Count must be greater than 0.`) | ||
} | ||
if (overwrites) { | ||
return [...Array(count)].map(i => | ||
overwriteMerge(this.registeredTypes[type](), overwrites) | ||
) | ||
} | ||
return [...Array(count)].map(i => this.registeredTypes[type]()) | ||
} | ||
} |
{ | ||
"name": "basic-factory", | ||
"version": "0.0.1", | ||
"main": "index.js", | ||
"version": "0.0.2", | ||
"main": "./dist/Factory.js", | ||
"repository": "https://github.com/bkd705/basic-factory.git", | ||
@@ -9,6 +9,7 @@ "author": "bkd705 <brennenkdenomme@gmail.com>", | ||
"scripts": { | ||
"test": "mocha --compilers js:babel-register" | ||
"test": "mocha --compilers js:babel-register", | ||
"build": "NODE_ENV=production webpack --progress --colors" | ||
}, | ||
"devDependencies": { | ||
"babel": "^6.23.0", | ||
"babel-loader": "^7.1.2", | ||
"babel-preset-es2015": "^6.24.1", | ||
@@ -19,4 +20,8 @@ "babel-preset-stage-2": "^6.24.1", | ||
"flow": "^0.2.3", | ||
"mocha": "^3.5.0" | ||
"mocha": "^3.5.0", | ||
"webpack": "^3.5.5" | ||
}, | ||
"dependencies": { | ||
"deepmerge": "^1.5.1" | ||
} | ||
} |
# Basic Factory | ||
[![Build Status](https://travis-ci.org/bkd705/basic-factory.svg?branch=master)](https://travis-ci.org/bkd705/basic-factory) | ||
A basic tool providing an easy way to generate blocks of mock data for tests or as placeholders. | ||
@@ -15,7 +17,11 @@ | ||
```javascript | ||
import Factory from 'simple-factory' | ||
// ES6 | ||
import BasicFactory from 'basic-factory' | ||
//ES5 | ||
const BasicFactory = require('basic-factory').default | ||
``` | ||
3. Initialize a new Factory | ||
```javascript | ||
const MyFactory = new Factory() | ||
const MyFactory = new BasicFactory() | ||
``` | ||
@@ -22,0 +28,0 @@ 4. Register your Factory generators |
@@ -9,2 +9,6 @@ import { expect } from 'chai' | ||
TestFactory = new Factory() | ||
TestFactory.register('Test', () => ({ | ||
name: 'brennen' | ||
})) | ||
}) | ||
@@ -14,13 +18,11 @@ | ||
it('should add a new type to registeredTypes', () => { | ||
TestFactory.register('test', () => ({})) | ||
expect(TestFactory.registeredTypes).to.have.property('test') | ||
expect(TestFactory.registeredTypes).to.have.property('Test') | ||
}) | ||
it('should throw error if type already exists', () => { | ||
TestFactory.register('test', () => ({})) | ||
expect(() => TestFactory.register('test', () => {})).to.throw() | ||
expect(() => TestFactory.register('Test', () => {})).to.throw() | ||
}) | ||
it('should throw an error if return type of generator is not an object', () => { | ||
expect(() => TestFactory.register('test', f => f)).to.throw() | ||
expect(() => TestFactory.register('NoopTest', f => f)).to.throw() | ||
}) | ||
@@ -35,8 +37,10 @@ }) | ||
it('should return a object from type generator', () => { | ||
TestFactory.register('test', () => ({ | ||
name: 'brennen' | ||
})) | ||
const output = TestFactory.create('test') | ||
const output = TestFactory.create('Test') | ||
expect(output).to.deep.equal({ name: 'brennen' }) | ||
}) | ||
it('should overwrite generated values if provided with overwrites', () => { | ||
const output = TestFactory.create('Test', { name: 'kyle' }) | ||
expect(output.name).to.equal('kyle') | ||
}) | ||
}) | ||
@@ -50,14 +54,19 @@ | ||
it('should throw an error if count is not provided', () => { | ||
expect(() => TestFactory.createMany('gravy')).to.throw() | ||
expect(() => TestFactory.createMany('Test')).to.throw() | ||
}) | ||
it('should throw an error if count is not greater than 0', () => { | ||
expect(() => TestFactory.createMany('pulledpork', 0)).to.throw() | ||
expect(() => TestFactory.createMany('Test', 0)).to.throw() | ||
}) | ||
it('should return an array of generated types', () => { | ||
TestFactory.register('user', () => ({ username: 'bkd705' })) | ||
expect(TestFactory.createMany('user', 2)).to.have.length(2) | ||
expect(TestFactory.createMany('Test', 2)).to.have.length(2) | ||
}) | ||
it('should overwrite generated values if provided with overwrites', () => { | ||
const output = TestFactory.createMany('Test', 2, { name: 'kyle' }) | ||
expect(output[0].name).to.equal('kyle') | ||
expect(output[1].name).to.equal('kyle') | ||
}) | ||
}) | ||
}) |
Sorry, the diff of this file is not supported yet
111332
12
147
57
1
8
+ Addeddeepmerge@^1.5.1
+ Addeddeepmerge@1.5.2(transitive)