static-path
Version:
Static-path uses TypeScript to prevent 404s and other path generation mistakes at compile time
131 lines • 5.61 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const ava_1 = __importDefault(require("ava"));
const _1 = require(".");
ava_1.default('routes paths with no params', (t) => {
const p = _1.path('/');
t.is(p({}), '/');
t.is(p.pattern, '/');
});
ava_1.default('can have constant path components', (t) => {
const p = _1.path('/courses');
t.is(p({}), '/courses');
t.is(p.pattern, '/courses');
});
ava_1.default('can have path holes', (t) => {
const p = _1.path('/courses/:courseId');
t.is(p({ courseId: 'course1' }), '/courses/course1');
t.is(p.pattern, '/courses/:courseId');
});
ava_1.default('must begin with with a slash', (t) => {
t.throws(() => _1.path('courses/:courseId'), {
message: /Paths must begin with slashes, but "courses\/:courseId" doesn't./,
});
});
ava_1.default('replaces repeated slashes with one slash', (t) => {
const p = _1.path('/one//two');
t.is(p({}), '/one/two');
t.is(p.pattern, '/one/two');
/* Normalization of repeated slashes is also reflected at the type level. */
const testString1 = '/one/two';
testString1;
// @ts-expect-error
const testString2 = '/one//two';
testString2;
});
ava_1.default('discards trailing slashes in paths', (t) => {
// In a single path:
const p1 = _1.path('/courses/');
t.is(p1({}), '/courses');
t.is(p1.pattern, '/courses');
// In a subpath:
const p2 = _1.path('/courses/').path('/lessons/');
t.is(p2.pattern, '/courses/lessons');
t.is(p2({}), '/courses/lessons');
/* Normalization of trailing slashes is also reflected at the type level. */
const testString1 = '/courses/lessons';
testString1;
// @ts-expect-error
const testString2 = '/courses/lessons/';
testString2;
});
ava_1.default("doesn't try to normalize the root path", (t) => {
/* Normalization of the root path '/' is a tricky case, so we check it
* explicitly. */
const root = _1.path('/');
t.is(root.pattern, '/');
const testString1 = '/';
testString1;
// @ts-expect-error
const testString2 = '';
testString2;
});
ava_1.default("normalizes the weird path '//'", (t) => {
const doubleSlash = _1.path('//');
t.is(doubleSlash.pattern, '/');
const testString1 = '/';
testString1;
});
ava_1.default('joins subpaths, whether they have a leading slash or not', (t) => {
// With leading slash on subpath:
const p1 = _1.path('/courses/:courseId').path('/lessons/:lessonId');
t.is(p1.pattern, '/courses/:courseId/lessons/:lessonId');
t.is(p1({ courseId: 'course1', lessonId: 'lesson1' }), '/courses/course1/lessons/lesson1');
// Without leading slash on subpath:
const p2 = _1.path('/courses/:courseId').path('lessons/:lessonId');
t.is(p2.pattern, '/courses/:courseId/lessons/:lessonId');
t.is(p2({ courseId: 'course1', lessonId: 'lesson1' }), '/courses/course1/lessons/lesson1');
});
ava_1.default('joins subpaths when only the parent has a parameter', (t) => {
const p = _1.path('/courses/:courseId').path('/lessons');
t.is(p.pattern, '/courses/:courseId/lessons');
t.is(p({ courseId: 'course1' }), '/courses/course1/lessons');
});
ava_1.default('joins subpaths when only the child has a parameter', (t) => {
const p = _1.path('/courses').path('/lessons/:lessonId');
t.is(p.pattern, '/courses/lessons/:lessonId');
t.is(p({ lessonId: 'lesson1' }), '/courses/lessons/lesson1');
});
ava_1.default('joins subpaths when neither path has a parameter', (t) => {
const p = _1.path('/courses').path('/lessons');
t.is(p.pattern, '/courses/lessons');
t.is(p({}), '/courses/lessons');
});
ava_1.default('joins subpaths onto the root path', (t) => {
// With leading slash on subpath:
const p1 = _1.path('/').path('/lessons');
t.is(p1.pattern, '/lessons');
t.is(p1({}), '/lessons');
// Without leading slash on subpath:
const p2 = _1.path('/').path('/lessons');
t.is(p2.pattern, '/lessons');
t.is(p2({}), '/lessons');
});
ava_1.default("type errors and throws when the provided params don't match the pattern", (t) => {
// With one param:
_1.path('/courses/:courseId')({ courseId: 'some value' });
// @ts-expect-error
t.throws(() => _1.path('/courses/:courseId')({}));
// @ts-expect-error
t.throws(() => _1.path('/courses/:courseId')({ unexpectedParam: 'some value' }));
// With two params:
_1.path('/courses/:courseId/lessons/:lessonId')({ courseId: 'some value', lessonId: 'some value' });
// @ts-expect-error
t.throws(() => _1.path('/courses/:courseId/lessons/:lessonId')({}));
// @ts-expect-error
t.throws(() => _1.path('/courses/:courseId/lessons/:lessonId')({ courseId: 'some value' }));
// @ts-expect-error
t.throws(() => _1.path('/courses/:courseId/lessons/:lessonId')({ lessonId: 'some value' }));
// With two params, one from a base path and one from a subpath:
_1.path('/courses/:courseId').path('/lessons/:lessonId')({ courseId: 'some value', lessonId: 'some value' });
// @ts-expect-error
t.throws(() => _1.path('/courses/:courseId').path('/lessons/:lessonId')({}));
// @ts-expect-error
t.throws(() => _1.path('/courses/:courseId').path('/lessons/:lessonId')({ courseId: 'some value' }));
// @ts-expect-error
t.throws(() => _1.path('/courses/:courseId').path('/lessons/:lessonId')({ lessonId: 'some value' }));
});
//# sourceMappingURL=index.test.js.map