You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
2.7 KiB
JavaScript

var expect = require('expect');
var RouteNode = require('../lib/route-node');
module.exports = describe('RouteNode', function () {
it('→ exists', function () {
expect(new RouteNode()).toBeInstanceOf(RouteNode);
});
describe('→ add nodes', function () {
var node = null;
var callback = function () {};
var context = {};
beforeEach(function () {
node = new RouteNode();
});
it('→ add root node', function (done) {
node.add([], callback, context);
node.get([], [], function (err, func, cbContext, args) {
expect(err).toBe(null);
expect(func).toBe(callback);
expect(cbContext).toBe(context);
expect(args).toEqual([]);
done();
});
});
it('→ get nested callback', function (done) {
node.add(['nested'], callback, context);
node.get(['nested'], [], function (err, func, cbContext, args) {
expect(err).toBe(null);
expect(func).toBe(callback);
expect(cbContext).toBe(context);
expect(args).toEqual([]);
done();
});
});
it('→ get error callback for not found', function (done) {
node.get(['fake', 'route'], [], function (err, func, cbContext, args) {
expect(err).toBeInstanceOf(Error);
expect(err.message).toMatch(/not found/);
expect(func).not.toBeDefined();
expect(cbContext).not.toBeDefined();
expect(args).not.toBeDefined();
done();
});
});
it('→ get route with regex', function (done) {
node.add(['by', 'id', /\d+/], callback, context);
node.get(['by', 'id', '123'], [], function (err, func, cbContext, args) {
expect(err).toBe(null);
expect(func).toBe(callback);
expect(cbContext).toBe(context);
expect(args).toEqual(['123']);
done();
});
});
it('→ get route with function', function (done) {
function toInt(part) {
var results = /^(\d+)$/.exec(part);
if (results) {
return parseInt(results[0], 10);
} else {
return null;
}
}
node.add(['by', 'id', toInt], callback, context);
node.get(['by', 'id', '123'], [], function (err, func, cbContext, args) {
expect(err).toBe(null, err && err.message);
expect(func).toBe(callback);
expect(cbContext).toBe(context);
expect(args).toEqual([123]);
done();
});
});
it('→ pass arguments to handler before parsing', function (done) {
var obj = {};
node.add(['id', /\d+/], callback);
node.get(['id', '123'], [obj], function (err, cb, ctx, args) {
expect(args).toEqual([obj, '123']);
done();
});
});
});
});