Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('should find handlers by route name', function () {
const sut = new RouteRecognizer();
sut.add([staticRoute]);
assert.strictEqual(sut.hasRoute('static'), true, `sut.hasRoute('static')`);
assert.strictEqual(sut.handlersFor('static')[0].handler, staticRoute.handler, `sut.handlersFor('static')[0].handler`);
});
it('should register unnamed routes', function () {
const sut = new RouteRecognizer();
sut.add([{'path': 'b', 'handler': {}}]);
assert.deepStrictEqual(sut.names, {}, `sut.names`);
assert.strictEqual(!!sut.recognize('/b'), true, `!!sut.recognize('/b')`);
});
it('should find a handler by multiple names', function () {
const sut = new RouteRecognizer();
sut.add([multiNameRoute]);
assert.strictEqual(
sut.handlersFor('static-multiple')[0].handler,
sut.handlersFor('static-multiple-alias')[0].handler
);
});
it('should distinguish between dynamic and static parts', function () {
const sut = new RouteRecognizer();
const similarRoute = { 'path': 'optionalToo/:id?', 'handler': { 'name': 'similar' }};
sut.add([optionalRoute, similarRoute]);
const result = sut.recognize('optionalToo');
assert.strictEqual(result.length, 1, `result.length`);
assert.strictEqual(result[0].handler.name, 'similar', `result[0].handler.name`);
});
it('should reject default parameter values', function () {
const sut = new RouteRecognizer();
assert.throws(() => sut.add([{'path': 'user/:id=1', 'handler': {}}]), void 0, `() => sut.add([{'path': 'user/:id=1', 'handler': {}}])`);
});
it('should find handlers by route name', function () {
const sut = new RouteRecognizer();
sut.add([staticRoute]);
assert.strictEqual(sut.hasRoute('static'), true, `sut.hasRoute('static')`);
assert.strictEqual(sut.handlersFor('static')[0].handler, staticRoute.handler, `sut.handlersFor('static')[0].handler`);
});
xit('should reject unknown routes', function () {
const sut = new RouteRecognizer();
assert.strictEqual(sut.hasRoute('static'), false, `sut.hasRoute('static')`);
assert.throws(() => sut.handlersFor('static'), void 0, `() => sut.handlersFor('static')`);
assert.throws(() => sut.generate('static'), void 0, `() => sut.generate('static')`);
assert.strictEqual(sut.recognize('/notfound'), undefined, `sut.recognize('/notfound')`);
});
it('should find a handler by multiple names', function () {
const sut = new RouteRecognizer();
sut.add([multiNameRoute]);
assert.strictEqual(
sut.handlersFor('static-multiple')[0].handler,
sut.handlersFor('static-multiple-alias')[0].handler
);
});
it('should distinguish between dynamic and static parts', function () {
const sut = new RouteRecognizer();
const similarRoute = { 'path': 'optionalToo/:id?', 'handler': { 'name': 'similar' }};
sut.add([optionalRoute, similarRoute]);
const result = sut.recognize('optionalToo');
assert.strictEqual(result.length, 1, `result.length`);
assert.strictEqual(result[0].handler.name, 'similar', `result[0].handler.name`);
});
it('should require dynamic segment parameters when generating', function () {
const sut = new RouteRecognizer();
sut.add([dynamicRoute]);
assert.throws(() => sut.generate('dynamic'), void 0, `() => sut.generate('dynamic')`);
assert.throws(() => sut.generate('dynamic', {}), void 0, `() => sut.generate('dynamic', {})`);
assert.throws(() => sut.generate('dynamic', { id: null }), void 0, `() => sut.generate('dynamic', { id: null })`);
});