|
|
|
var expect = require('expect')
|
|
|
|
var RouteNode = require('../lib/route-node')
|
|
|
|
|
|
|
|
module.exports = describe('RouteNode', function () {
|
|
|
|
it('→ exists', function () {
|
|
|
|
expect(new RouteNode()).toExist()
|
|
|
|
})
|
|
|
|
|
|
|
|
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).toBeA(Error)
|
|
|
|
expect(err.message).toMatch(/not found/)
|
|
|
|
expect(func).toNotExist()
|
|
|
|
expect(cbContext).toNotExist()
|
|
|
|
expect(args).toNotExist()
|
|
|
|
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()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
})
|
|
|
|
})
|