cannery-adapter-rest
Version:
The Cannery REST adapter
120 lines (97 loc) • 3.83 kB
JavaScript
;
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var assert = require('assert');
var proxyquire = require('proxyquire');
var Car = require('../__mocks__/car_model.mock');
var Part = require('../__mocks__/part_model.mock');
var RestAdapter = proxyquire('../index', {
'then-request': require('../__mocks__/findAllWithin_request.mock')
});
describe('findAllWithin()', function () {
it('Should allow us to define our own url path', function (done) {
var adapter = new RestAdapter({}, {
'Car/Part': {
findAll: 'foo/bar/baz'
}
});
var car = new Car(1);
Part.prototype.getParent = function () {
return car;
};
adapter.findAllWithin(Part, car).then(function (data) {
assert.equal(data[0].id, 100);
done();
});
});
it('Should respond with an array of data', function (done) {
var adapter = new RestAdapter();
var car = new Car(1);
Part.prototype.getParent = function () {
return car;
};
adapter.findAllWithin(Part, car).then(function (data) {
assert.equal(data.length, 2);
assert.equal(data[0].id, 1);
done();
});
});
it('Should allow us to pass in an envelope for nested data', function (done) {
var car = new Car(1);
var adapter = new RestAdapter({
arrayEnvelope: 'parts'
});
Part.prototype.getParent = function () {
return car;
};
adapter.findAllWithin(Part, car, {
envelope: 'parts'
}).then(function (data) {
assert.equal(data[1].id, 2);
done();
});
});
it('Should allow us to specify a urlRoot', function (done) {
var car = new Car(1);
var adapter = new RestAdapter({
urlRoot: 'api/v1/'
});
Part.prototype.getParent = function () {
return car;
};
adapter.findAllWithin(Part, car).then(function (data) {
assert.equal(data[0].name, 'Starter');
done();
});
});
it('Should allow us to traverse more than one parent', function (done) {
var adapter = new RestAdapter();
var car = new Car(1);
var part = new Part(2);
Part.prototype.getParent = function () {
return car;
};
var PartMaker = function () {
function PartMaker(id) {
_classCallCheck(this, PartMaker);
this.id = id;
}
_createClass(PartMaker, [{
key: 'getName',
value: function getName() {
return 'part_makers';
}
}, {
key: 'getParent',
value: function getParent() {
return part;
}
}]);
return PartMaker;
}();
adapter.findAllWithin(PartMaker, part).then(function (data) {
assert.equal(data[0].name, 'Big John');
done();
});
});
});