Skip to content

Commit

Permalink
Feature/extract config out of constructir (#865)
Browse files Browse the repository at this point in the history
* hotfix

* fix newModel.isNew is not a function

* prevent Faker exception

* :

* add configurable

* make sure things work if a user is not using update at first

* issue-842

* use getOwnPropertyDescriptor

* fixes

* rebase
  • Loading branch information
Leooo authored and samselikoff committed Sep 9, 2016
1 parent c64190b commit 3cf0bff
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 21 deletions.
8 changes: 8 additions & 0 deletions addon/serializer-registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}

}
55 changes: 35 additions & 20 deletions addon/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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();
}
}
Expand Down Expand Up @@ -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 => {
Expand Down
41 changes: 40 additions & 1 deletion tests/integration/server-config-test.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
});

0 comments on commit 3cf0bff

Please sign in to comment.