UNPKG

regard

Version:

Sugar-interface to access multiple data sources.

98 lines (81 loc) 2.61 kB
var _ = require('lodash'), Chai = require('chai'), HttpConnector = require('../lib/connectors/http'), Q = require('q'), Manager = require('../lib/manager'), Regards = require('..'); var assert = Chai.assert, expect = Chai.expect; var clazz = Regards, foo, bar; var mockServer = require('./mockHttpServer'); describe('API', function () { before(mockServer.start); after(mockServer.stop); it('should can chain calls', function () { var man = Manager(); foo = man.api; foo .$$(Regards.FsConnector) .$$(Regards.HttpConnector) .$('mb', 'http://localhost:3001', {$cache:true}) .$('root', __dirname+'/..') ; return foo.mb('request').then(function (res) { bar = res.body; return foo.root('write', 'sample.json', bar); }, function () { assert.fail(); }).then(function () { return foo.root('sample.json'); }, function () { assert.fail(); }).then(function (res) { expect(res).to.eql(bar); }, function () { assert.fail(); }); }); it('should can accept multiple connector', function () { var man = Manager(); foo = man.api; expect(man.connectors.data.length).to.eql(0); foo.$$('fs'); expect(man.connectors.data.length).to.eql(1); foo.$$('fs'); expect(man.connectors.data.length).to.eql(1); foo.$$('http'); expect(man.connectors.data.length).to.eql(2); foo.$$('https'); expect(man.connectors.data.length).to.eql(3); }); it('should can support FsConnector helpers', function () { foo = clazz('fs'); foo.$('root', __dirname+'/../'); foo.root.$('sample', 'sample.json'); return foo.root.sample.write({foo:'bar'}) .then(foo.root.sample.read(), console.log) .then(function (res) { expect(res).to.eql({foo:'bar'}); return foo.root.write('sample.json', {foo:'baz'}); }, console.log) .then(function (res) { return foo.root.read('sample.json'); }, console.log) .then(function (res) { expect(res).to.eql({foo:'baz'}); }, console.log); }); it('should can support HttpConnector helpers', function (done) { foo = clazz('http'); foo.$('mb', 'http://localhost:3001'); foo.mb.$('req', 'request'); _.forEach(HttpConnector.Methods, function (value) { var func = foo.mb.req[value.toLowerCase()]; func().then(function (res) { expect(_.result(_.find(foo.$staging._cache, 'value.settings.method', value), 'value.settings.method')).to.eql(value); }); }); setTimeout(done, 1000); }); });