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.

151 lines
4.3 KiB
JavaScript

var expect = require('expect');
var Router = require('../lib/router');
var NotFoundError = require('../lib/error').NotFound;
describe('Router', function () {
it('→ exists', function () {
expect(Router).toBeDefined();
});
it('→ add root node', function (done) {
var router = new Router();
router.add('/', function () { return 'some value'; });
router.route('/', function (err, handlerResult) {
expect(err).toBe(null);
expect(handlerResult).toBe('some value');
done();
});
});
it('→ add/get nested route', function (done) {
var router = new Router();
router.add('/some/path/here', function () { return 'some value'; });
router.route('/some/path/here', function (err, handlerResult) {
expect(err).toBe(null);
expect(handlerResult).toBe('some value');
done();
});
});
it('→ partial route is a not found error', function (done) {
var router = new Router();
router.add(['/user', /^(\d+)$/], function (number) { return number; });
router.route('/user/', function (err) {
expect(err).toBeInstanceOf(NotFoundError);
done();
});
});
it('→ get error in callback for missing handler', function (done) {
var router = new Router();
router.route('/some/fake/path', function (err, handlerResult) {
expect(err).toBeInstanceOf(Error);
expect(err.message).toMatch(/not found/);
expect(handlerResult).not.toBeDefined();
done();
});
});
it('→ get routes based on regex', function (done) {
var router = new Router();
router.add(['/by/id', /\d+/], function (someId) {
expect(someId).toBe('123');
});
router.route('/by/id/123', function (err) {
expect(err).toBe(null, err && err.message);
done();
});
});
9 years ago
it('→ regex routes can return NotFoundError', function (done) {
var router = new Router();
router.add(['/by/id', /\d+/], function () {});
9 years ago
router.route('/by/id/not-an-id', function (err) {
expect(err).toBeInstanceOf(NotFoundError);
done();
});
});
9 years ago
it('→ get based on regex with multiple captures global', function (done) {
var router = new Router();
router.add(['by', 'order', /(\d+)/g], function (one, two, three) {
expect(one).toBe('1');
expect(two).toBe('2');
expect(three).toBe('3');
});
router.route('/by/order/1-2-3', function (err) {
expect(err).toBe(null, err && err.message);
done();
});
});
it('→ get based regex with multiple captures not global', function (done) {
var router = new Router();
router.add(['by', 'order', /(\d+)/], function (one, two, three) {
expect(one).toBe('1');
expect(two).not.toBeDefined();
expect(three).not.toBeDefined();
});
router.route('/by/order/1-2-3', function (err) {
expect(err).toBe(null, err && err.message);
done();
});
});
it('→ pass in arguments before capturing path arguments', function (done) {
var router = new Router();
var api = {key: 123, any: 'object is passed', method: function () {}};
router.add(['/by/order', /(\d+)/], function (inputApi, urlParam) {
expect(inputApi).toBe(api);
expect(urlParam).toBe('123');
done();
});
router.route('/by/order/123', {args: [api]});
});
9 years ago
function toInt(part) {
var results = /^(\d+)$/.exec(part);
9 years ago
if (results) {
return parseInt(results[0], 10);
9 years ago
} else {
return null;
}
9 years ago
}
it('→ get based on function', function (done) {
var router = new Router();
router.add(['by', 'order', toInt], function (val) {
expect(val).toBe(123);
});
router.route('/by/order/123', function (err) {
expect(err).toBe(null);
done();
});
});
9 years ago
it('→ function routes can return NotFoundError', function (done) {
var router = new Router();
router.add(['/by/order', toInt], function () {});
9 years ago
router.route('/by/order/not-found', function (err) {
expect(err).toBeInstanceOf(NotFoundError);
done();
});
});
9 years ago
it('→ check not found error', function (done) {
var router = new Router();
router.route('/not/real/id', function (err) {
expect(Router.isNotFound(err)).toBe(true);
done();
});
});
it('→ check not found error on not real error', function () {
expect(Router.isNotFound(new Error('Not the missing error'))).toBe(false);
});
});