regard
Version:
Sugar-interface to access multiple data sources.
103 lines (88 loc) • 2.98 kB
JavaScript
var _ = require('lodash'),
Chai = require('chai'),
Connector = require('../lib/connector'),
Endpoint = require('../lib/endpoint');
var expect = Chai.expect;
var clazz = Endpoint,
foo, bar;
function createPromise(func) {
return new Rsvp.Promise(func);
}
describe('Endpoint', function () {
beforeEach(function () {
foo = clazz('foo');
});
it('should return Endpoint instance', function () {
expect(foo).is.be.instanceof(Endpoint);
foo = new clazz('foo');
expect(foo).is.be.instanceof(Endpoint);
});
it('should must have key', function () {
expect(clazz).to.throw();
expect(_.partial(clazz, 'foo')).to.not.throw();
expect(clazz('foo').key).to.eql('foo');
});
it('should must have context', function () {
expect(foo).to.have.property('context');
expect(foo.context).to.be.an('object');
});
it('should have connector, path, and parent properties', function () {
_.forEach(['connector', 'parent', 'path'], function (value) {
expect(foo).to.have.property(value);
});
});
it('should have init, afterProcess and beforeProcess methods', function () {
_.forEach(['init', 'afterProcess', 'beforeProcess'], function (value) {
expect(foo).to.have.property(value);
});
});
it('should have afterProcess and beforeProcess to equals undefined', function () {
expect(foo.afterProcess).to.not.be.ok;
expect(foo.beforeProcess).to.not.be.ok;
});
it('should have init ( [path] [context] [before] [after] ) method', function () {
var func = function() {};
foo.init('bar');
expect(foo.path).to.eql('bar');
expect(foo.beforeProcess).to.not.be.ok;
foo.init(func);
expect(foo.beforeProcess).to.be.a('function');
foo = clazz('foo');
expect(foo.beforeProcess).to.not.be.ok;
foo.init('bar', func);
expect(foo.path).to.eql('bar');
expect(foo.beforeProcess).to.be.a('function');
foo.init({foo:'bar'});
expect(foo.context).to.eql({foo:'bar'});
foo.init('baz', 'fs');
expect(foo.path).to.eql('baz');
expect(foo.connector).to.eql('fs');
foo = clazz('foo');
expect(foo.before).to.not.be.ok;
foo.init({ $path:'bar/', $before:func, $after:func, $connector: 'fs'});
expect(foo.path).to.eql('bar');
expect(foo.connector).to.eql('fs');
expect(foo.beforeProcess).to.be.a('function');
foo = clazz('foo');
foo.init(func, func);
expect(foo.beforeProcess).to.be.a('function');
});
it('should walk to super parent', function () {
bar = clazz('bar');
var bax = clazz('bax');
bax.parent = bar;
var baz = clazz('baz');
baz.parent = bax;
var counter = 0;
baz.walk(function () { counter++; });
expect(counter).to.eql(3);
var stack = [];
bax.walk(stack);
expect(stack.length).to.eql(2);
expect(stack).to.eql([bar, bax]);
stack = [];
bax.walk(stack, 'key');
expect(stack.length).to.eql(2);
expect(stack).to.eql(['bar', 'bax']);
});
});