Skip to content

Commit 0aa1de3

Browse files
committed
test: make sure all inputs have leading slash
1 parent c22b957 commit 0aa1de3

File tree

1 file changed

+119
-113
lines changed

1 file changed

+119
-113
lines changed

test/router.test.ts

Lines changed: 119 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -89,52 +89,52 @@ describe("Router lookup", function () {
8989
describe("retrieve placeholders", function () {
9090
testRouter(
9191
[
92-
"blog/*",
93-
"carbon/:element",
94-
"carbon/:element/test/:testing",
95-
"this/:route/has/:cool/stuff",
92+
"/blog/*",
93+
"/carbon/:element",
94+
"/carbon/:element/test/:testing",
95+
"/this/:route/has/:cool/stuff",
9696
],
9797
(router) =>
9898
expect(formatTree(router.root)).toMatchInlineSnapshot(`
9999
"<root>
100100
├── /blog
101-
│ ├── /* ┈> [GET] blog/*
101+
│ ├── /* ┈> [GET] /blog/*
102102
├── /carbon
103-
│ ├── /* ┈> [GET] carbon/:element
103+
│ ├── /* ┈> [GET] /carbon/:element
104104
│ │ ├── /test
105-
│ │ │ ├── /* ┈> [GET] carbon/:element/test/:testing
105+
│ │ │ ├── /* ┈> [GET] /carbon/:element/test/:testing
106106
├── /this
107107
│ ├── /*
108108
│ │ ├── /has
109109
│ │ │ ├── /*
110-
│ │ │ │ ├── /stuff ┈> [GET] this/:route/has/:cool/stuff"
110+
│ │ │ │ ├── /stuff ┈> [GET] /this/:route/has/:cool/stuff"
111111
`),
112112
{
113-
"carbon/test1": {
114-
data: { path: "carbon/:element" },
113+
"/carbon/test1": {
114+
data: { path: "/carbon/:element" },
115115
params: {
116116
element: "test1",
117117
},
118118
},
119119
"/carbon": undefined,
120-
"carbon/": undefined,
121-
"carbon/test2/test/test23": {
122-
data: { path: "carbon/:element/test/:testing" },
120+
"/carbon/": undefined,
121+
"/carbon/test2/test/test23": {
122+
data: { path: "/carbon/:element/test/:testing" },
123123
params: {
124124
element: "test2",
125125
testing: "test23",
126126
},
127127
},
128-
"this/test/has/more/stuff": {
129-
data: { path: "this/:route/has/:cool/stuff" },
128+
"/this/test/has/more/stuff": {
129+
data: { path: "/this/:route/has/:cool/stuff" },
130130
params: {
131131
route: "test",
132132
cool: "more",
133133
},
134134
},
135-
"/blog": { data: { path: "blog/*" } },
136-
"blog/": { data: { path: "blog/*" } },
137-
"blog/123": { data: { path: "blog/*" } },
135+
"/blog": { data: { path: "/blog/*" } },
136+
"/blog/": { data: { path: "/blog/*" } },
137+
"/blog/123": { data: { path: "/blog/*" } },
138138
},
139139
);
140140

@@ -227,31 +227,35 @@ describe("Router lookup", function () {
227227

228228
describe("should be able to perform wildcard lookups", () => {
229229
testRouter(
230-
["polymer/**:id", "polymer/another/route", "route/:p1/something/**:rest"],
230+
[
231+
"/polymer/**:id",
232+
"/polymer/another/route",
233+
"/route/:p1/something/**:rest",
234+
],
231235
(router) =>
232236
expect(formatTree(router.root)).toMatchInlineSnapshot(`
233237
"<root>
234238
├── /polymer
235239
│ ├── /another
236-
│ │ ├── /route ┈> [GET] polymer/another/route
237-
│ ├── /** ┈> [GET] polymer/**:id
240+
│ │ ├── /route ┈> [GET] /polymer/another/route
241+
│ ├── /** ┈> [GET] /polymer/**:id
238242
├── /route
239243
│ ├── /*
240244
│ │ ├── /something
241-
│ │ │ ├── /** ┈> [GET] route/:p1/something/**:rest"
245+
│ │ │ ├── /** ┈> [GET] /route/:p1/something/**:rest"
242246
`),
243247
{
244-
"polymer/another/route": { data: { path: "polymer/another/route" } },
245-
"polymer/anon": {
246-
data: { path: "polymer/**:id" },
248+
"/polymer/another/route": { data: { path: "/polymer/another/route" } },
249+
"/polymer/anon": {
250+
data: { path: "/polymer/**:id" },
247251
params: { id: "anon" },
248252
},
249-
"polymer/foo/bar/baz": {
250-
data: { path: "polymer/**:id" },
253+
"/polymer/foo/bar/baz": {
254+
data: { path: "/polymer/**:id" },
251255
params: { id: "foo/bar/baz" },
252256
},
253-
"route/param1/something/c/d": {
254-
data: { path: "route/:p1/something/**:rest" },
257+
"/route/param1/something/c/d": {
258+
data: { path: "/route/:p1/something/**:rest" },
255259
params: { p1: "param1", rest: "c/d" },
256260
},
257261
},
@@ -305,26 +309,26 @@ describe("Router lookup", function () {
305309

306310
describe("unnamed placeholders", function () {
307311
testRouter(
308-
["polymer/**", "polymer/route/*"],
312+
["/polymer/**", "/polymer/route/*"],
309313
(router) =>
310314
expect(formatTree(router.root)).toMatchInlineSnapshot(`
311315
"<root>
312316
├── /polymer
313317
│ ├── /route
314-
│ │ ├── /* ┈> [GET] polymer/route/*
315-
│ ├── /** ┈> [GET] polymer/**"
318+
│ │ ├── /* ┈> [GET] /polymer/route/*
319+
│ ├── /** ┈> [GET] /polymer/**"
316320
`),
317321
{
318-
"polymer/foo/bar": {
319-
data: { path: "polymer/**" },
322+
"/polymer/foo/bar": {
323+
data: { path: "/polymer/**" },
320324
params: { _: "foo/bar" },
321325
},
322-
"polymer/route/anon": {
323-
data: { path: "polymer/route/*" },
326+
"/polymer/route/anon": {
327+
data: { path: "/polymer/route/*" },
324328
params: { _0: "anon" },
325329
},
326-
"polymer/constructor": {
327-
data: { path: "polymer/**" },
330+
"/polymer/constructor": {
331+
data: { path: "/polymer/**" },
328332
params: { _: "constructor" },
329333
},
330334
},
@@ -354,30 +358,30 @@ describe("Router lookup", function () {
354358

355359
describe("should be able to match routes with trailing slash", function () {
356360
testRouter(
357-
["route/without/trailing/slash", "route/with/trailing/slash/"],
361+
["/route/without/trailing/slash", "/route/with/trailing/slash/"],
358362
(router) =>
359363
expect(formatTree(router.root)).toMatchInlineSnapshot(`
360364
"<root>
361365
├── /route
362366
│ ├── /without
363367
│ │ ├── /trailing
364-
│ │ │ ├── /slash ┈> [GET] route/without/trailing/slash
368+
│ │ │ ├── /slash ┈> [GET] /route/without/trailing/slash
365369
│ ├── /with
366370
│ │ ├── /trailing
367-
│ │ │ ├── /slash ┈> [GET] route/with/trailing/slash/"
371+
│ │ │ ├── /slash ┈> [GET] /route/with/trailing/slash/"
368372
`),
369373
{
370-
"route/without/trailing/slash": {
371-
data: { path: "route/without/trailing/slash" },
374+
"/route/without/trailing/slash": {
375+
data: { path: "/route/without/trailing/slash" },
372376
},
373-
"route/with/trailing/slash/": {
374-
data: { path: "route/with/trailing/slash/" },
377+
"/route/with/trailing/slash/": {
378+
data: { path: "/route/with/trailing/slash/" },
375379
},
376-
"route/without/trailing/slash/": {
377-
data: { path: "route/without/trailing/slash" },
380+
"/route/without/trailing/slash/": {
381+
data: { path: "/route/without/trailing/slash" },
378382
},
379-
"route/with/trailing/slash": {
380-
data: { path: "route/with/trailing/slash/" },
383+
"/route/with/trailing/slash": {
384+
data: { path: "/route/with/trailing/slash/" },
381385
},
382386
},
383387
);
@@ -387,133 +391,135 @@ describe("Router lookup", function () {
387391
describe("Router insert", () => {
388392
it("should be able to insert nodes correctly into the tree", () => {
389393
const router = createRouter([
390-
"hello",
391-
"cool",
392-
"hi",
393-
"helium",
394+
"/hello",
395+
"/cool",
396+
"/hi",
397+
"/helium",
394398
"/choo",
395-
"coooool",
396-
"chrome",
397-
"choot",
398-
"choot/:choo",
399-
"ui/**",
400-
"ui/components/**",
399+
"/coooool",
400+
"/chrome",
401+
"/choot",
402+
"/choot/:choo",
403+
"/ui/**",
404+
"/ui/components/**",
401405
"/api/v1",
402406
"/api/v2",
403407
"/api/v3",
404408
]);
405409

406-
addRoute(router, "/api/v3", "", {
410+
addRoute(router, "", "/api/v3", {
407411
path: "/api/v3(overridden)",
408412
});
409413

410414
expect(formatTree(router.root)).toMatchInlineSnapshot(`
411-
"<root> ┈> [/api/v3] /api/v3(overridden)
412-
├── /hello ┈> [GET] hello
413-
├── /cool ┈> [GET] cool
414-
├── /hi ┈> [GET] hi
415-
├── /helium ┈> [GET] helium
415+
"<root>
416+
├── /hello ┈> [GET] /hello
417+
├── /cool ┈> [GET] /cool
418+
├── /hi ┈> [GET] /hi
419+
├── /helium ┈> [GET] /helium
416420
├── /choo ┈> [GET] /choo
417-
├── /coooool ┈> [GET] coooool
418-
├── /chrome ┈> [GET] chrome
419-
├── /choot ┈> [GET] choot
420-
│ ├── /* ┈> [GET] choot/:choo
421+
├── /coooool ┈> [GET] /coooool
422+
├── /chrome ┈> [GET] /chrome
423+
├── /choot ┈> [GET] /choot
424+
│ ├── /* ┈> [GET] /choot/:choo
421425
├── /ui
422426
│ ├── /components
423-
│ │ ├── /** ┈> [GET] ui/components/**
424-
│ ├── /** ┈> [GET] ui/**
427+
│ │ ├── /** ┈> [GET] /ui/components/**
428+
│ ├── /** ┈> [GET] /ui/**
425429
├── /api
426430
│ ├── /v1 ┈> [GET] /api/v1
427431
│ ├── /v2 ┈> [GET] /api/v2
428-
│ ├── /v3 ┈> [GET] /api/v3"
432+
│ ├── /v3 ┈> [GET] /api/v3, [*] /api/v3(overridden)"
429433
`);
430434
});
431435
});
432436

433437
describe("Router remove", function () {
434438
it("should be able to remove nodes", function () {
435439
const router = createRouter([
436-
"hello",
437-
"cool",
438-
"hi",
439-
"helium",
440-
"coooool",
441-
"chrome",
442-
"choot",
443-
"choot/:choo",
444-
"ui/**",
445-
"ui/components/**",
440+
"/hello",
441+
"/cool",
442+
"/hi",
443+
"/helium",
444+
"/coooool",
445+
"/chrome",
446+
"/choot",
447+
"/choot/:choo",
448+
"/ui/**",
449+
"/ui/components/**",
446450
]);
447451

448452
removeRoute(router, "GET", "choot");
449453
expect(findRoute(router, "GET", "choot")).to.deep.equal(undefined);
450454
removeRoute(router, "GET", "choot/*");
451455
expect(findRoute(router, "GET", "choot")).to.deep.equal(undefined);
452456

453-
expect(findRoute(router, "GET", "ui/components/snackbars")).to.deep.equal({
454-
data: { path: "ui/components/**" },
457+
expect(findRoute(router, "GET", "/ui/components/snackbars")).to.deep.equal({
458+
data: { path: "/ui/components/**" },
455459
params: { _: "snackbars" },
456460
});
457461

458-
removeRoute(router, "GET", "ui/components/**");
459-
expect(findRoute(router, "GET", "ui/components/snackbars")).to.deep.equal({
460-
data: { path: "ui/**" },
462+
removeRoute(router, "GET", "/ui/components/**");
463+
expect(findRoute(router, "GET", "/ui/components/snackbars")).to.deep.equal({
464+
data: { path: "/ui/**" },
461465
params: { _: "components/snackbars" },
462466
});
463467
});
464468

465469
it("removes data but does not delete a node if it has children", function () {
466-
const router = createRouter(["a/b", "a/b/*"]);
470+
const router = createRouter(["/a/b", "/a/b/*"]);
467471

468-
removeRoute(router, "GET", "a/b");
469-
expect(findRoute(router, "GET", "a/b")).to.deep.equal({
470-
data: { path: "a/b/*" },
472+
removeRoute(router, "GET", "/a/b");
473+
expect(findRoute(router, "GET", "/a/b")).to.deep.equal({
474+
data: { path: "/a/b/*" },
471475
params: { _0: undefined },
472476
});
473-
expect(findRoute(router, "GET", "a/b/c")).to.deep.equal({
477+
expect(findRoute(router, "GET", "/a/b/c")).to.deep.equal({
474478
params: { _0: "c" },
475-
data: { path: "a/b/*" },
479+
data: { path: "/a/b/*" },
476480
});
477-
removeRoute(router, "GET", "a/b/*");
478-
expect(findRoute(router, "GET", "a/b")).to.deep.equal(undefined);
481+
removeRoute(router, "GET", "/a/b/*");
482+
expect(findRoute(router, "GET", "/a/b")).to.deep.equal(undefined);
479483
});
480484

481485
it("should be able to remove placeholder routes", function () {
482486
const router = createRouter([
483-
"placeholder/:choo",
484-
"placeholder/:choo/:choo2",
487+
"/placeholder/:choo",
488+
"/placeholder/:choo/:choo2",
485489
]);
486490

487-
expect(findRoute(router, "GET", "placeholder/route")).to.deep.equal({
488-
data: { path: "placeholder/:choo" },
491+
expect(findRoute(router, "GET", "/placeholder/route")).to.deep.equal({
492+
data: { path: "/placeholder/:choo" },
489493
params: {
490494
choo: "route",
491495
},
492496
});
493497

494498
// TODO
495-
// removeRoute(router, "GET", "placeholder/:choo");
496-
// expect(findRoute(router,"placeholder/route")).to.deep.equal(undefined);
499+
// removeRoute(router, "GET", "/placeholder/:choo");
500+
// expect(findRoute(router,"/placeholder/route")).to.deep.equal(undefined);
497501

498-
expect(findRoute(router, "GET", "placeholder/route/route2")).to.deep.equal({
499-
data: { path: "placeholder/:choo/:choo2" },
500-
params: {
501-
choo: "route",
502-
choo2: "route2",
502+
expect(findRoute(router, "GET", "/placeholder/route/route2")).to.deep.equal(
503+
{
504+
data: { path: "/placeholder/:choo/:choo2" },
505+
params: {
506+
choo: "route",
507+
choo2: "route2",
508+
},
503509
},
504-
});
510+
);
505511
});
506512

507513
it("should be able to remove wildcard routes", function () {
508-
const router = createRouter(["ui/**", "ui/components/**"]);
514+
const router = createRouter(["/ui/**", "/ui/components/**"]);
509515

510-
expect(findRoute(router, "GET", "ui/components/snackbars")).to.deep.equal({
511-
data: { path: "ui/components/**" },
516+
expect(findRoute(router, "GET", "/ui/components/snackbars")).to.deep.equal({
517+
data: { path: "/ui/components/**" },
512518
params: { _: "snackbars" },
513519
});
514-
removeRoute(router, "GET", "ui/components/**");
515-
expect(findRoute(router, "GET", "ui/components/snackbars")).to.deep.equal({
516-
data: { path: "ui/**" },
520+
removeRoute(router, "GET", "/ui/components/**");
521+
expect(findRoute(router, "GET", "/ui/components/snackbars")).to.deep.equal({
522+
data: { path: "/ui/**" },
517523
params: { _: "components/snackbars" },
518524
});
519525
});

0 commit comments

Comments
 (0)