regard
Version:
Sugar-interface to access multiple data sources.
252 lines (219 loc) • 7.34 kB
JavaScript
var _ = require('lodash'),
Chai = require('chai'),
Connector = require('../lib/connector'),
FsConnector = require('../lib/connectors/fs'),
Endpoint = require('../lib/endpoint'),
Manager = require('../lib/manager'),
Regard = require('../lib/regard'),
Q = require('q');
var assert = Chai.assert,
expect = Chai.expect;
var clazz = Regard,
man, foo, bar;
function createPromise(func) {
return Q.Promise(func);
}
describe('Regard', function () {
beforeEach(function () {
man = Manager();
foo = man.api;
});
it('should have connectors, endpoints and requests properties', function () {
_.forEach(['connectors', 'endpoints', 'requests'], function (value) {
expect(man).to.have.property(value);
});
});
it('should have sugar methods', function () {
_.forEach([foo, foo.$, foo.$$], function (value) {
expect(value).to.be.a('function');
});
});
it('should add new connector to $connectors', function () {
foo.$$(FsConnector);
expect(_.includes(man.connectors.index, 'fs')).to.be.ok;
});
it('should add new endpoint to $endpoints', function () {
foo.$$(FsConnector);
foo.$('foo', '.');
expect(_.includes(man.endpoints.index, 'foo')).to.be.ok;
});
it('should create sugar nested endpoint factory method', function () {
foo.$$(FsConnector);
foo.$('foo', '.');
foo.foo.$('bar');
expect(man.endpoints.get('foo/bar').key).to.eql('foo/bar');
expect(foo.foo.bar.$).to.be.a('function');
});
it('should throw error when no connector was found', function () {
expect(_.partial(foo.$, 'root', '.')).to.throw();
});
it('should resolve connector with current params', function () {
foo.$$(FsConnector);
foo.$('foo', '.');
expect(man.endpoints.get('foo').connector.key).to.eql('fs');
foo.$('bar', '.', 'fs');
expect(man.endpoints.get('bar').connector.key).to.eql('fs');
foo.$('baz', '.', {connector:man.connectors.get('fs')});
expect(man.endpoints.get('baz').connector.key).to.eql('fs');
});
it('should process request created by endpoint', function () {
foo.$$(FsConnector);
foo.$('root', __dirname+'/..');
return foo.root('package.json').then(function (res) {
expect(res.name).to.eql('regard');
}, function (err) {
assert.fail();
});
});
it('should throw error when bad endpoint is given', function () {
expect(foo).to.throw();
});
it('should throw error when bad connector is given', function () {
expect(foo.$$).to.throw();
expect(_.partial(foo.$$, FsConnector)).to.not.throw();
expect(_.partial(foo.$$, 'fs')).to.not.throw();
expect(_.partial(foo.$$, 'foo')).to.throw();
});
it('should cache request when $cache settings was set', function () {
foo
.$$(FsConnector)
.$('root', __dirname+'/..')
.$('cache/root', __dirname+'/..', {$cache:true});
return foo.root('package.json')
.then(function () {
return foo.cache.root('package.json');
}, function () {
assert.fail();
})
.then(function (res) {
bar = res;
return foo.cache.root('package.json');
}, function () {
assert.fail();
})
.then(function (res) {
expect(res).to.eql(bar);
}, function (err) {
assert.fail();
});
});
it('should cache during few seconds', function (done) {
man = Manager(200, 1);
foo = man.api;
foo
.$$(FsConnector)
.$('root', __dirname+'/..', {$cache:true});
var hash;
foo.root('package.json')
.then(function () {
hash = _.first(_.values(man.requests.data._cache)).value.key;
return man.requests.promises.has(hash);
}, function () {
assert.fail();
})
.then(function(res) {
expect(res).to.be.ok;
setTimeout(function () {
man.requests.promises.has(hash)
.then(function (res) {
expect(res).to.not.be.ok;
done();
});
}, 400);
}, function () {
assert.fail();
});
});
it('should load multiple connectors in times', function () {
expect(man.connectors.data.length).to.eql(0);
foo.$$('fs');
expect(man.connectors.data.length).to.eql(1);
foo.$$(['fs', 'http']);
expect(man.connectors.data.length).to.eql(2);
});
it('should create request which wrap hash, path, settings, endpoint and connector', function () {
foo.$$(FsConnector);
foo.$('bar', '.');
foo.bar();
bar = _.first(_.values(man.requests.data._cache)).value;
_.forEach(['key', 'path', 'context', 'endpoint'], function (value) {
expect(bar).to.have.property(value);
});
});
it('should prepare path recursively', function () {
foo.$$(FsConnector);
foo.$('bar', './alpha');
foo.bar.$('baz', 'beta');
foo.bar.baz();
bar = _.first(_.values(man.requests.data._cache)).value;
expect(bar.path).to.eql('./alpha/beta');
});
it('should prepare settings recursively', function () {
foo.$$(FsConnector);
foo.$('bar', './alpha', {foo:'alpha'});
foo.bar.$('baz', 'beta', {bar:'beta'});
foo.bar.baz();
bar = _.first(_.values(man.requests.data._cache)).value;
expect(bar.settings).to.eql(_.merge(FsConnector().settings, {foo:'alpha', bar:'beta'}));
});
it('should call beforeProcess only if exists', function () {
function CustomConnector() {
if (!(this instanceof CustomConnector)) return new CustomConnector();
Connector.call(this, 'foo');
this.checkPath = _.constant(true);
this.handler(_.constant(_.constant));
};
CustomConnector.prototype = _.create(Connector.prototype);
bar = false;
foo.$$(CustomConnector);
foo.$('bar', '.');
foo.bar();
expect(bar).to.eql(false);
foo = Manager().api;
foo.$$(CustomConnector);
foo.$('bar', '.', { $before: function(){ bar = true; }});
foo.bar();
expect(bar).to.eql(true);
bar = false;
foo = Manager().api;
foo.$$(function () {
var conn = CustomConnector();
conn.handler(_.constant(_.constant), function () { bar = true; });
return conn;
});
foo.$('bar', '.');
foo.bar();
expect(bar).to.eql(true);
});
it('should can focus to endpoint', function () {
foo.$$(FsConnector);
expect(foo.to('.')).to.eql(foo[_.first(_.values(man.endpoints.data)).key]);
});
it('should support beforeProcess and afterProcess methods', function () {
function CustomConnector() {
if (!(this instanceof CustomConnector)) return new CustomConnector();
Connector.call(this, 'foo');
this.checkPath = _.constant(true);
this.handler(
function (request, resolve) { two = true; resolve(); },
function() { one = true; },
function(res) { three = true; return res; });
};
CustomConnector.prototype = _.create(Connector.prototype);
var one = false,
two = false,
three = false;
foo.$$(CustomConnector);
foo.$('bar', __dirname);
expect(one).to.not.be.ok;
expect(two).to.not.be.ok;
expect(three).to.not.be.ok;
return foo.bar().then(function () {
expect(one).to.be.ok;
expect(two).to.be.ok;
expect(three).to.be.ok;
}, function (err) {
assert.fail();
});
});
});