Skip to content

Commit

Permalink
[closes #371] Dont require factories for server.create (#737)
Browse files Browse the repository at this point in the history
  • Loading branch information
samselikoff committed May 11, 2016
1 parent 3412c2e commit 29855e5
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 28 deletions.
19 changes: 9 additions & 10 deletions addon/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,17 +211,16 @@ export default class Server {
this.factorySequences = this.factorySequences || {};
this.factorySequences[camelizedType] = this.factorySequences[camelizedType] + 1 || 0;

assert(
this._factoryMap && this._factoryMap[camelizedType],
`You're trying to create a ${camelizedType}, but no factory for this type was found`
);

let OriginalFactory = this._factoryMap[camelizedType];
let Factory = OriginalFactory.extend(overrides);
let factory = new Factory();
if (this._factoryMap && this._factoryMap[camelizedType]) {
let OriginalFactory = this._factoryMap[camelizedType];
let Factory = OriginalFactory.extend(overrides);
let factory = new Factory();

let sequence = this.factorySequences[camelizedType];
return factory.build(sequence);
let sequence = this.factorySequences[camelizedType];
return factory.build(sequence);
} else {
return overrides;
}
}

buildList(type, amount, overrides) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@ import {module, test} from 'qunit';
import { Model, Factory } from 'ember-cli-mirage';
import Server from 'ember-cli-mirage/server';

module('Integration | Server | Model creation', {
module('Integration | Server | Factory creation', {
beforeEach() {
this.Contact = Model.extend();
this.AmazingContact = Model.extend();
this.Post = Model.extend();

this.server = new Server({
environment: 'test',
models: {
contact: this.Contact,
amazingContact: this.AmazingContact
amazingContact: this.AmazingContact,
post: this.Post
},
factories: {
contact: Factory,
Expand All @@ -27,20 +30,39 @@ module('Integration | Server | Model creation', {

test('create returns a Model if one is defined', function(assert) {
let contact = this.server.create('contact');

assert.ok(contact instanceof this.Contact, 'expected a Contact');
});

test('createList returns Models if one is defined', function(assert) {
let contacts = this.server.createList('contact', 1);

assert.ok(contacts[0] instanceof this.Contact, 'expected a Contactl');
});

test('create returns a Model if one is defined, when using a compound name', function(assert) {
let contact = this.server.create('amazing-contact');

assert.ok(contact instanceof this.AmazingContact, 'expected an AmazingContact');
});

test('createList returns Models if one is defined, when using a compound name', function(assert) {
let contacts = this.server.createList('amazing-contact', 1);

assert.ok(contacts[0] instanceof this.AmazingContact, 'expected an AmazingContact');
});

test('create falls back to a model if no factory is defined', function(assert) {
let post = this.server.create('post');

assert.ok(post instanceof this.Post);
assert.equal(post.id, 1);
});

test('createList falls back to a model if no factory is defined', function(assert) {
let posts = this.server.createList('post', 2);

assert.ok(posts[0] instanceof this.Post);
assert.equal(posts.length, 2);
assert.equal(posts[0].id, 1);
});
16 changes: 0 additions & 16 deletions tests/unit/server-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,22 +240,6 @@ module('Unit | Server #build', {
}
});

test('build fails when no factories are regisered', function(assert) {
assert.throws(function() {
this.server.build('contact');
});
});

test('build fails when an expected factory isn\'t registered', function(assert) {
this.server.loadFactories({
address: Factory.extend()
});

assert.throws(function() {
this.server.build('contact');
});
});

test('build does not add the data to the db', function(assert) {
this.server.loadFactories({
contact: Factory.extend({ name: 'Sam' })
Expand Down

0 comments on commit 29855e5

Please sign in to comment.