loopback/test/relations.integration.js

99 lines
3.1 KiB
JavaScript

var loopback = require('../');
var lt = require('loopback-testing');
var path = require('path');
var SIMPLE_APP = path.join(__dirname, 'fixtures', 'simple-integration-app');
var app = require(path.join(SIMPLE_APP, 'app.js'));
var assert = require('assert');
describe('relations - integration', function () {
lt.beforeEach.withApp(app);
lt.beforeEach.givenModel('store');
beforeEach(function(done) {
this.widgetName = 'foo';
this.store.widgets.create({
name: this.widgetName
}, function() {
done();
});
});
afterEach(function(done) {
this.app.models.widget.destroyAll(done);
});
describe('/store/:id/widgets', function () {
beforeEach(function() {
this.url = '/api/stores/' + this.store.id + '/widgets';
});
lt.describe.whenCalledRemotely('GET', '/api/stores/:id/widgets', function() {
it('should succeed with statusCode 200', function() {
assert.equal(this.res.statusCode, 200);
});
describe('widgets (response.body)', function() {
beforeEach(function() {
this.widgets = this.res.body;
this.widget = this.res.body[0];
});
it('should be an array', function() {
assert(Array.isArray(this.widgets));
});
it('should include a single widget', function() {
assert(this.widgets.length === 1);
assert(this.widget);
});
it('should be a valid widget', function() {
assert(this.widget.id);
assert.equal(this.widget.storeId, this.store.id);
assert.equal(this.widget.name, this.widgetName);
});
});
});
describe('POST /api/store/:id/widgets', function() {
beforeEach(function() {
this.newWidgetName = 'baz';
this.newWidget = {
name: this.newWidgetName
};
});
beforeEach(function(done) {
this.http = this.post(this.url, this.newWidget);
this.http.send(this.newWidget);
this.http.end(function(err) {
if(err) return done(err);
this.req = this.http.req;
this.res = this.http.res;
done();
}.bind(this));
});
it('should succeed with statusCode 200', function() {
assert.equal(this.res.statusCode, 200);
});
describe('widget (response.body)', function() {
beforeEach(function() {
this.widget = this.res.body;
});
it('should be an object', function() {
assert(typeof this.widget === 'object');
assert(!Array.isArray(this.widget));
});
it('should be a valid widget', function() {
assert(this.widget.id);
assert.equal(this.widget.storeId, this.store.id);
assert.equal(this.widget.name, this.newWidgetName);
});
});
it('should have a single widget with storeId', function (done) {
this.app.models.widget.count({
storeId: this.store.id
}, function(err, count) {
if(err) return done(err);
assert.equal(count, 2);
done();
});
});
});
});
});