diff --git a/Gruntfile.js b/Gruntfile.js index 2d362689..205feff8 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -66,7 +66,8 @@ module.exports = function(grunt) { files: [ 'test/support.js', 'test/model.test.js', - 'test/geo-point.test.js' + 'test/geo-point.test.js', + 'test/app.test.js' ], // list of files to exclude diff --git a/lib/browser-express.js b/lib/browser-express.js index 386e8159..82aba2fa 100644 --- a/lib/browser-express.js +++ b/lib/browser-express.js @@ -1,7 +1,25 @@ module.exports = browserExpress; function browserExpress() { - return {}; + return new BrowserExpress(); } browserExpress.errorHandler = {}; + +function BrowserExpress() { + this.settings = {}; +} + +BrowserExpress.prototype.set = function(key, value) { + if (arguments.length == 1) { + return this.get(key); + } + + this.settings[key] = value; + + return this; // fluent API +}; + +BrowserExpress.prototype.get = function(key) { + return this.settings[key]; +}; diff --git a/test/app.test.js b/test/app.test.js index 0d38ff2f..2ab4e287 100644 --- a/test/app.test.js +++ b/test/app.test.js @@ -3,6 +3,9 @@ var SIMPLE_APP = path.join(__dirname, 'fixtures', 'simple-app'); var loopback = require('../'); var DataModel = loopback.DataModel; +var describe = require('./util/describe'); +var it = require('./util/it'); + describe('app', function() { describe('app.model(Model)', function() { @@ -35,7 +38,7 @@ describe('app', function() { expect(classes).to.contain('color'); }); - it('updates REST API when a new model is added', function(done) { + it.onServer('updates REST API when a new model is added', function(done) { app.use(loopback.rest()); request(app).get('/colors').expect(404, function(err, res) { if (err) return done(err); @@ -76,6 +79,8 @@ describe('app', function() { app = loopback(); app.boot({ app: {port: 3000, host: '127.0.0.1'}, + // prevent loading of models.json, it is not available in the browser + models: {}, dataSources: { db: { connector: 'memory' @@ -144,7 +149,7 @@ describe('app', function() { }); }); - describe('app.boot([options])', function () { + describe.onServer('app.boot([options])', function () { beforeEach(function () { app.boot({ app: { @@ -313,7 +318,7 @@ describe('app', function() { }); }); - describe('app.boot(appRootDir)', function () { + describe.onServer('app.boot(appRootDir)', function () { it('Load config files', function () { var app = loopback(); @@ -327,7 +332,7 @@ describe('app', function() { }); }); - describe('listen()', function() { + describe.onServer('listen()', function() { it('starts http server', function(done) { var app = loopback(); app.set('port', 0); @@ -387,7 +392,7 @@ describe('app', function() { }); }); - describe('enableAuth', function() { + describe.onServer('enableAuth', function() { it('should set app.isAuthEnabled to true', function() { expect(app.isAuthEnabled).to.not.equal(true); app.enableAuth(); @@ -395,7 +400,7 @@ describe('app', function() { }); }); - describe('app.get("/", loopback.status())', function () { + describe.onServer('app.get("/", loopback.status())', function () { it('should return the status of the application', function (done) { var app = loopback(); app.get('/', loopback.status()); @@ -456,4 +461,26 @@ describe('app', function() { expect(app.connectors.FOOBAR).to.equal(loopback.Memory); }); }); + + describe('app.settings', function() { + it('can be altered via `app.set(key, value)`', function() { + app.set('write-key', 'write-value'); + expect(app.settings).to.have.property('write-key', 'write-value'); + }); + + it('can be read via `app.get(key)`', function() { + app.settings['read-key'] = 'read-value'; + expect(app.get('read-key')).to.equal('read-value'); + }); + + it('is unique per app instance', function() { + var app1 = loopback(); + var app2 = loopback(); + + expect(app1.settings).to.not.equal(app2.settings); + + app1.set('key', 'value'); + expect(app2.get('key'), 'app2 value').to.equal(undefined); + }); + }); }); diff --git a/test/model.test.js b/test/model.test.js index b00f41de..10d02a7b 100644 --- a/test/model.test.js +++ b/test/model.test.js @@ -5,6 +5,8 @@ var Change = loopback.Change; var defineModelTestsWithDataSource = require('./util/model-tests'); var DataModel = loopback.DataModel; +var describe = require('./util/describe'); + describe('Model / DataModel', function() { defineModelTestsWithDataSource({ dataSource: { diff --git a/test/util/it.js b/test/util/it.js new file mode 100644 index 00000000..f1b004e2 --- /dev/null +++ b/test/util/it.js @@ -0,0 +1,19 @@ +var loopback = require('../../'); + +module.exports = it; + +it.onServer = function itOnServer(name, fn) { + if (loopback.isServer) { + it(name, fn); + } else { + it.skip(name, fn); + } +}; + +it.inBrowser = function itInBrowser(name, fn) { + if (loopback.isBrowser) { + it(name, fn); + } else { + it.skip(name, fn); + } +};