loopback/test/app.test.js

142 lines
3.7 KiB
JavaScript
Raw Normal View History

2013-06-07 19:57:51 +00:00
describe('app', function() {
2013-06-06 00:11:21 +00:00
2013-06-07 19:57:51 +00:00
describe('app.model(Model)', function() {
2013-07-16 20:46:33 +00:00
it("Expose a `Model` to remote clients", function() {
2013-10-29 21:12:23 +00:00
var app = loopback();
2013-07-16 17:49:25 +00:00
var memory = loopback.createDataSource({connector: loopback.Memory});
2013-06-11 16:01:44 +00:00
var Color = memory.createModel('color', {name: String});
2013-06-07 19:57:51 +00:00
app.model(Color);
2013-06-11 16:01:44 +00:00
assert.equal(app.models().length, 1);
2013-06-07 19:57:51 +00:00
});
2013-06-06 00:11:21 +00:00
});
2013-06-07 19:57:51 +00:00
2013-10-29 21:12:23 +00:00
describe('app.model(name, properties, options)', function () {
it('Sugar for defining a fully built model', function () {
var app = loopback();
app.boot({
app: {port: 3000, host: '127.0.0.1'},
dataSources: {
db: {
connector: 'memory'
}
}
});
app.model('foo', {
dataSource: 'db'
});
var Foo = app.models.foo;
var f = new Foo;
assert(f instanceof loopback.Model);
});
})
// describe('app.models()', function() {
// it("Get the app's exposed models", function() {
// var app = loopback();
// var models = app.models();
// models.forEach(function(m) {
// console.log(m.modelName);
// })
2013-06-07 19:57:51 +00:00
// assert.equal(models.length, 1);
// assert.equal(models[0].modelName, 'color');
// });
// });
2013-10-29 21:12:23 +00:00
describe('app.boot([options])', function () {
beforeEach(function () {
var app = this.app = loopback();
app.boot({
app: {
port: 3000,
host: '127.0.0.1'
},
models: {
'foo-bar-bat-baz': {
options: {
plural: 'foo-bar-bat-bazzies'
},
dataSource: 'the-db'
}
},
dataSources: {
'the-db': {
connector: 'memory'
}
}
});
});
it('Load configuration', function () {
assert.equal(this.app.get('port'), 3000);
assert.equal(this.app.get('host'), '127.0.0.1');
});
it('Instantiate models', function () {
assert(app.models);
assert(app.models.FooBarBatBaz);
assert(app.models.fooBarBatBaz);
assertValidDataSource(app.models.FooBarBatBaz.dataSource);
assert.isFunc(app.models.FooBarBatBaz, 'find');
assert.isFunc(app.models.FooBarBatBaz, 'create');
});
it('Attach models to data sources', function () {
assert.equal(app.models.FooBarBatBaz.dataSource, app.dataSources.theDb);
});
it('Instantiate data sources', function () {
assert(app.dataSources);
assert(app.dataSources.theDb);
assertValidDataSource(app.dataSources.theDb);
assert(app.dataSources.TheDb);
});
});
describe('app.boot(appRootDir)', function () {
2013-10-29 21:12:23 +00:00
it('Load config files', function () {
var app = loopback();
app.boot(require('path').join(__dirname, 'fixtures', 'simple-app'));
2013-10-29 21:12:23 +00:00
assert(app.models.foo);
assert(app.models.Foo);
assert(app.models.Foo.dataSource);
assert.isFunc(app.models.Foo, 'find');
assert.isFunc(app.models.Foo, 'create');
});
});
2013-11-19 20:35:29 +00:00
describe('app.get("/", loopback.status())', function () {
it('should return the status of the application', function (done) {
var app = loopback();
app.get('/', loopback.status());
request(app)
.get('/')
.expect(200)
.end(function(err, res) {
if(err) return done(err);
assert.equal(typeof res.body, 'object');
assert(res.body.started);
assert(res.body.uptime);
var elapsed = Date.now() - Number(new Date(res.body.started));
// elapsed should be a positive number...
assert(elapsed > 0);
// less than 100 milliseconds
assert(elapsed < 100);
done();
});
});
});
2013-10-29 21:12:23 +00:00
});