diff --git a/addon/serializer-registry.js b/addon/serializer-registry.js index 019b96bfa..9a901d532 100644 --- a/addon/serializer-registry.js +++ b/addon/serializer-registry.js @@ -75,4 +75,12 @@ export default class SerializerRegistry { return this._isModel(object) || this._isCollection(object); } + registerSerializers(newSerializerMaps) { + let currentSerializerMap = this._serializerMap || {}; + this._serializerMap = _assign( + currentSerializerMap, + newSerializerMaps + ); + } + } diff --git a/addon/server.js b/addon/server.js index a2e249457..e910cec78 100644 --- a/addon/server.js +++ b/addon/server.js @@ -8,6 +8,7 @@ import Schema from './orm/schema'; import assert from './assert'; import SerializerRegistry from './serializer-registry'; import RouteHandler from './route-handler'; +import Ember from 'ember'; import _isArray from 'lodash/lang/isArray'; import _keys from 'lodash/object/keys'; @@ -106,45 +107,58 @@ function extractRouteArguments(args) { export default class Server { constructor(options = {}) { - this.environment = options.environment || 'development'; - this.options = options; - this.timing = 400; - this.namespace = ''; - this.urlPrefix = ''; + this.config(options); + } + + config(config = {}) { + Ember.assert(!this.environment || this.environment === config.environment, + 'you can not modify the environment of ember-cli-mirage once server is created'); + this.environment = config.environment || 'development'; + + this.options = config; + + this.timing = this.timing || config.timing || 400; + this.namespace = this.namespace || config.namespace || ''; + this.urlPrefix = this.urlPrefix || config.urlPrefix || ''; this._defineRouteHandlerHelpers(); - this.db = new Db(); - this.schema = new Schema(this.db, options.models); - this.serializerOrRegistry = new SerializerRegistry(this.schema, options.serializers); + this.db = this.db || new Db(); + if (this.schema) { + this.schema.registerModels(config.models); + this.serializerOrRegistry.registerSerializers(config.serializers || {}); + } else { + this.schema = new Schema(this.db, config.models); + this.serializerOrRegistry = new SerializerRegistry(this.schema, config.serializers); + } - let hasFactories = this._hasModulesOfType(options, 'factories'); - let hasDefaultScenario = options.scenarios && options.scenarios.hasOwnProperty('default'); + let hasFactories = this._hasModulesOfType(config, 'factories'); + let hasDefaultScenario = config.scenarios && config.scenarios.hasOwnProperty('default'); - this.pretender = createPretender(this); + this.pretender = this.pretender || createPretender(this); - if (options.baseConfig) { - this.loadConfig(options.baseConfig); + if (config.baseConfig) { + this.loadConfig(config.baseConfig); } if (this.isTest()) { - if (options.testConfig) { - this.loadConfig(options.testConfig); + if (config.testConfig) { + this.loadConfig(config.testConfig); } window.server = this; // TODO: Better way to inject server into test env } if (this.isTest() && hasFactories) { - this.loadFactories(options.factories); + this.loadFactories(config.factories); } else if (!this.isTest() && hasDefaultScenario) { - this.loadFactories(options.factories); - options.scenarios.default(this); + this.loadFactories(config.factories); + config.scenarios.default(this); } else { this.loadFixtures(); } - if (options.useDefaultPassthroughs) { + if (config.useDefaultPassthroughs) { this._configureDefaultPassthroughs(); } } @@ -196,7 +210,8 @@ export default class Server { */ loadFactories(factoryMap) { // Store a reference to the factories - this._factoryMap = factoryMap; + let currentFactoryMap = this._factoryMap || {}; + this._factoryMap = _assign(currentFactoryMap, factoryMap); // Create a collection for each factory _keys(factoryMap).forEach(type => { diff --git a/tests/integration/server-config-test.js b/tests/integration/server-config-test.js index 719d3047c..0f39549b6 100644 --- a/tests/integration/server-config-test.js +++ b/tests/integration/server-config-test.js @@ -1,13 +1,19 @@ import {module, test} from 'qunit'; import { Model } from 'ember-cli-mirage'; import Server from 'ember-cli-mirage/server'; +import ActiveModelSerializer from 'ember-cli-mirage/serializers/active-model-serializer'; +import RestSerializer from 'ember-cli-mirage/serializers/rest-serializer'; module('Integration | Server Config', { beforeEach() { this.server = new Server({ environment: 'development', models: { - contact: Model + contact: Model, + post: Model + }, + serializers: { + contact: ActiveModelSerializer } }); this.server.timing = 0; @@ -201,3 +207,36 @@ test('namespace of / works', function(assert) { done(); }); }); + +test('redefining options using the config method works', function(assert) { + assert.expect(5); + let done = assert.async(); + let { server } = this; + + let contacts = [ + { id: '1', name: 'Link' }, + { id: '2', name: 'Zelda' } + ]; + server.config({ + namespace: 'api', + urlPrefix: 'http://localhost:3000', + timing: 1000, + serializers: { + post: RestSerializer + } + }); + server.db.loadData({ + contacts + }); + server.get('contacts'); + + assert.equal(server.timing, 1000); + $.getJSON('http://localhost:3000/api/contacts', function(data) { + assert.deepEqual(data, { contacts }); + done(); + }); + let serializerMap = server.serializerOrRegistry._serializerMap; + assert.equal(Object.keys(serializerMap).length, 2); + assert.equal(serializerMap.contact, ActiveModelSerializer); + assert.equal(serializerMap.post, RestSerializer); +});