Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jmosul committed Mar 7, 2019
0 parents commit bd80820
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
47 changes: 47 additions & 0 deletions README.md
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()
}));
```
85 changes: 85 additions & 0 deletions index.js
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;
};
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions package.json
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"
}
}

0 comments on commit bd80820

Please sign in to comment.