Skip to content

Commit

Permalink
allow traits to be functions
Browse files Browse the repository at this point in the history
  • Loading branch information
danielspaniel committed Jan 4, 2018
1 parent c49f70c commit 994777a
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 24 deletions.
33 changes: 21 additions & 12 deletions addon/model-definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,28 +120,37 @@ class ModelDefinition {
@param {String} traitArgs array of traits
@returns {Object} json
*/
build(name, opts, traitArgs = [], buildType = 'build') {
let traitsObj = {};
traitArgs.forEach(trait => {
Ember.assert(`You're trying to use a trait [${trait}] for model ${this.modelName} but that trait can't be found.`, this.traits[trait]);
assign(traitsObj, this.traits[trait]);
});
build(name, opts={}, traitNames = [], buildType = 'build') {

let modelAttributes = this.namedModels[name] || {};
// merge default, modelAttributes, traits and opts to get the rough fixture
let fixture = assign({}, this.default, modelAttributes, traitsObj, opts);

if (this.notPolymorphic !== undefined) {
fixture._notPolymorphic = true;
}
// merge default, modelAttributes, traits and opts to get the rough fixture
let fixture = assign({}, this.default, modelAttributes);

// set the id, unless it was already set in opts
if (!fixture.id) {
if (!fixture.id && !opts.id) {
// Setting a flag to indicate that this is a generated an id,
// so it can be rolled back if the fixture throws an error.
fixture._generatedId = true;
fixture.id = this.nextId();
}

if (this.notPolymorphic !== undefined) {
fixture._notPolymorphic = true;
}

traitNames.forEach(traitName => {
let trait = this.traits[traitName];
Ember.assert(
`You're trying to use a trait [${traitName}] for model ${this.modelName} but that trait can't be found.`, trait);
if (Ember.typeOf(trait) === 'function') {
trait(fixture);
}
assign(fixture, trait);
});

assign(fixture, opts);

try {
// deal with attributes that are functions or objects
for (let attribute in fixture) {
Expand Down
25 changes: 14 additions & 11 deletions tests/dummy/app/tests/factories/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@ import FactoryGuy from 'ember-data-factory-guy';

FactoryGuy.define("project", {
sequences: {
title: (num)=> `Project${num}`
title: (num) => `Project${num}`
},
traits: {
big: { title: 'Big Project' },
small: { title: 'Small Project' },
with_title_sequence: { title: FactoryGuy.generate('title') },
with_user: { user: {} },
with_parent: { project: {} },
with_user_having_hats: { user: FactoryGuy.belongsTo('user', 'with_hats') },
with_user_having_hats_belonging_to_user: { user: FactoryGuy.belongsTo('user', 'with_hats_belonging_to_user') },
with_user_having_hats_belonging_to_outfit: { user: FactoryGuy.belongsTo('user', 'with_hats_belonging_to_outfit') },
with_dude: { user: {name: 'Dude'} },
with_admin: { user: FactoryGuy.belongsTo('admin') }
big: {title: 'Big Project'},
medium: (f) => { // using function for trait
f.title = `Medium Project ${f.id}`
},
small: {title: 'Small Project'},
with_title_sequence: {title: FactoryGuy.generate('title')},
with_user: {user: {}},
with_parent: {project: {}},
with_user_having_hats: {user: FactoryGuy.belongsTo('user', 'with_hats')},
with_user_having_hats_belonging_to_user: {user: FactoryGuy.belongsTo('user', 'with_hats_belonging_to_user')},
with_user_having_hats_belonging_to_outfit: {user: FactoryGuy.belongsTo('user', 'with_hats_belonging_to_outfit')},
with_dude: {user: {name: 'Dude'}},
with_admin: {user: FactoryGuy.belongsTo('admin')}
},
default: {
title: FactoryGuy.generate('title')
Expand Down
24 changes: 23 additions & 1 deletion tests/unit/factory-guy-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,23 @@ test("named types are not inherited", function(assert) {
assert.equal(definition.matchesName('dude'), undefined);
});

test("id can be a function", function(assert) {
test("id can be a function in default attributes", function(assert) {
FactoryGuy.define('stoner', {

sequences: {
stonerId: (i) => `stoner-${i}`,
},

default: {
id: FactoryGuy.generate('stonerId')
}
});

let json = FactoryGuy.build('stoner').data;
assert.equal(json.id, 'stoner-1');
});

test("id can be a function in named attributes", function(assert) {
FactoryGuy.define('stoner', {

sequences: {
Expand Down Expand Up @@ -638,6 +654,12 @@ test("with traits defining model attributes", function(assert) {
assert.deepEqual(json, expected);
});

test("with traits that are functions", function(assert) {
let json = FactoryGuy.buildRaw({name: 'project', traits: ['medium']});
let expected = {id: 1, title: 'Medium Project 1'};
assert.deepEqual(json, expected);
});

test("with traits defining belongsTo association", function(assert) {
let json = FactoryGuy.buildRaw({name: 'project', traits: ['with_user']});
let expected = {
Expand Down

0 comments on commit 994777a

Please sign in to comment.