swaf/test/common/Routing.test.ts

37 lines
1.7 KiB
TypeScript
Raw Permalink Normal View History

import {route} from "../../src/common/Routing.js";
import Controller from "../../src/Controller.js";
describe('route()', () => {
new class extends Controller {
public routes(): void {
const emptyHandler = () => {
//
};
this.get('/test/no-param', emptyHandler, 'test.no-param');
this.get('/test/no-param/', emptyHandler, 'test.no-param-slash');
this.get('/test/one-param/:param', emptyHandler, 'test.one-param');
this.get('/test/two-params/:param1/:param2', emptyHandler, 'test.two-params');
this.get('/test/paginated/:page([0-9]+)?', emptyHandler, 'test.paginated');
this.get('/test/slug/:slug([a-zA-Z0-9]+)?', emptyHandler, 'test.slug');
}
}().setupRoutes();
test('Empty params', () => {
expect(route('test.no-param')).toBe('/test/no-param');
expect(route('test.no-param-slash')).toBe('/test/no-param/');
expect(route('test.one-param')).toBe('/test/one-param/');
expect(route('test.two-params')).toBe('/test/two-params/');
expect(route('test.paginated')).toBe('/test/paginated/');
expect(route('test.slug')).toBe('/test/slug/');
});
test('Populated params', () => {
expect(route('test.no-param')).toBe('/test/no-param');
expect(route('test.no-param-slash')).toBe('/test/no-param/');
expect(route('test.one-param', {param: 'value'})).toBe('/test/one-param/value');
expect(route('test.two-params', {param1: 'value1', param2: 'value2'})).toBe('/test/two-params/value1/value2');
expect(route('test.paginated', {page: 5})).toBe('/test/paginated/5');
expect(route('test.slug', {slug: 'abc'})).toBe('/test/slug/abc');
});
});