-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit bd80820
Showing
5 changed files
with
171 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# Factorista | ||
|
||
A simple model factory tool bases on Laravel's Eloquent factories | ||
|
||
### Install | ||
|
||
`npm install designmynight/factorista --dev` | ||
|
||
### Usage | ||
|
||
This package is designed for easy use when no package builder (such as webpack) is being used. | ||
|
||
We use this tool with Karma, in `karam.conf.js`, add the files | ||
|
||
```javascript | ||
files : [ | ||
'node_modules/faker/build/build/faker.js', | ||
'node_modules/designmynight/factorista/index.js' | ||
] | ||
``` | ||
|
||
### Defining a factory | ||
|
||
All functions available with [Marak's Faker.js](https://github.com/Marak/faker.js) can be used | ||
|
||
```javascript | ||
factoryDefine(User, (faker) => ({ | ||
first_name: faker.name.firstName(), | ||
last_name: faker.name.lastName(), | ||
})); | ||
``` | ||
|
||
### Calling a factory | ||
|
||
```javascript | ||
// make a single user | ||
const user = factory(User).make(); | ||
|
||
// make mulitple users | ||
const users = factory(User, 5).make(); | ||
|
||
// make users with overrides | ||
const users = factory(User, 5).make((faker) => ({ | ||
first_name: 'Ralph', | ||
last_name: faker.lorem.word() | ||
})); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
class FactoryFloor { | ||
/** | ||
* @param {function} modelClass | ||
* @param {function} attributes | ||
*/ | ||
constructor(modelClass, attributes) { | ||
this.modelClass = modelClass; | ||
this.attributes = attributes; | ||
} | ||
|
||
/** | ||
* @param {number|undefined} count | ||
*/ | ||
set count(count) { | ||
this._count = count; | ||
} | ||
|
||
/** | ||
* @return {number} | ||
*/ | ||
get count() { | ||
return this._count || 1; | ||
} | ||
|
||
/** | ||
* @param {function|null} overrides | ||
* @return {Array} | ||
*/ | ||
make(overrides = null) { | ||
const models = []; | ||
|
||
for(let i=0; i < this.count; i++) { | ||
const data = this.attributes ? this.attributes(faker) : {}; | ||
const resolvedOverrides = FactoryFloor.resolveOverrides(overrides); | ||
|
||
Object.assign(data, resolvedOverrides); | ||
|
||
models.push(new this.modelClass(data)); | ||
} | ||
|
||
return this.count === 1 ? models.shift() : models; | ||
} | ||
|
||
/** | ||
* @param overrides | ||
* @return {function | {}} | ||
*/ | ||
static resolveOverrides(overrides){ | ||
const data = overrides ? overrides(faker) : {}; | ||
|
||
return Object.assign({}, data); | ||
} | ||
|
||
/** | ||
* @param {function} model | ||
* @return {FactoryFloor|undefined} | ||
*/ | ||
static getDefinition(model) { | ||
return new FactoryFloor( | ||
FactoryFloor._definitions[model.name].modelClass, | ||
FactoryFloor._definitions[model.name].attributes | ||
) | ||
} | ||
|
||
/** | ||
* @param {{modelClass: function, attributes:function}} definition | ||
*/ | ||
static set definition(definition) { | ||
FactoryFloor._definitions = FactoryFloor._definitions || {}; | ||
|
||
FactoryFloor._definitions[definition.modelClass.name] = definition; | ||
} | ||
} | ||
|
||
const factoryDefine = (modelClass, attributes) => { | ||
FactoryFloor.definition = {modelClass, attributes}; | ||
}; | ||
|
||
const factory = (modelClass, count) => { | ||
const factory = FactoryFloor.getDefinition(modelClass); | ||
|
||
factory.count = count; | ||
|
||
return factory; | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"name": "factorista", | ||
"version": "1.0.0", | ||
"description": "A simple model factory tool bases on Laravel's Eloquent factories", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"keywords": [ | ||
"javascript", | ||
"factory", | ||
"unit", | ||
"test", | ||
"karma", | ||
"jasmine" | ||
], | ||
"authors": [ | ||
"DesignMyNight <[email protected]>", | ||
"James O'Sullivan <[email protected]>" | ||
], | ||
"license": "ISC", | ||
"dependencies": { | ||
"faker": "^4.1.0" | ||
} | ||
} |