From 29855e5766aa8dce74abc40e1d5dc2ac653c6fb8 Mon Sep 17 00:00:00 2001 From: Sam Selikoff Date: Tue, 10 May 2016 21:57:37 -0400 Subject: [PATCH] [closes #371] Dont require factories for server.create (#737) --- addon/server.js | 19 +++++++------- ...ation-test.js => factory-creation-test.js} | 26 +++++++++++++++++-- tests/unit/server-test.js | 16 ------------ 3 files changed, 33 insertions(+), 28 deletions(-) rename tests/integration/server/{model-creation-test.js => factory-creation-test.js} (70%) diff --git a/addon/server.js b/addon/server.js index 7301c9eab..3b62de24c 100644 --- a/addon/server.js +++ b/addon/server.js @@ -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) { diff --git a/tests/integration/server/model-creation-test.js b/tests/integration/server/factory-creation-test.js similarity index 70% rename from tests/integration/server/model-creation-test.js rename to tests/integration/server/factory-creation-test.js index 70a352a59..362baa19f 100644 --- a/tests/integration/server/model-creation-test.js +++ b/tests/integration/server/factory-creation-test.js @@ -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, @@ -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); +}); diff --git a/tests/unit/server-test.js b/tests/unit/server-test.js index ea43e7ce8..fda88a0cb 100644 --- a/tests/unit/server-test.js +++ b/tests/unit/server-test.js @@ -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' })