-
-
Notifications
You must be signed in to change notification settings - Fork 416
/
Copy pathindex.spec.ts
141 lines (122 loc) · 3.78 KB
/
index.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import { describe, it, expect } from "vitest";
import { parse, compile, match, stringify } from "./index.js";
import {
PARSER_TESTS,
COMPILE_TESTS,
MATCH_TESTS,
STRINGIFY_TESTS,
} from "./cases.spec.js";
/**
* Dynamically generate the entire test suite.
*/
describe("path-to-regexp", () => {
describe("parse errors", () => {
it("should throw on unbalanced group", () => {
expect(() => parse("/{:foo,")).toThrow(
new TypeError(
"Unexpected END at 7, expected }: https://git.new/pathToRegexpError",
),
);
});
it("should throw on nested unbalanced group", () => {
expect(() => parse("/{:foo/{x,y}")).toThrow(
new TypeError(
"Unexpected END at 12, expected }: https://git.new/pathToRegexpError",
),
);
});
it("should throw on missing param name", () => {
expect(() => parse("/:/")).toThrow(
new TypeError(
"Missing parameter name at 2: https://git.new/pathToRegexpError",
),
);
});
it("should throw on missing wildcard name", () => {
expect(() => parse("/*/")).toThrow(
new TypeError(
"Missing parameter name at 2: https://git.new/pathToRegexpError",
),
);
});
it("should throw on unterminated quote", () => {
expect(() => parse('/:"foo')).toThrow(
new TypeError(
"Unterminated quote at 2: https://git.new/pathToRegexpError",
),
);
});
});
describe("compile errors", () => {
it("should throw when a param is missing", () => {
const toPath = compile("/a/:b/c");
expect(() => {
toPath();
}).toThrow(new TypeError("Missing parameters: b"));
});
it("should throw when expecting a repeated value", () => {
const toPath = compile("/*foo");
expect(() => {
toPath({ foo: [] });
}).toThrow(new TypeError('Expected "foo" to be a non-empty array'));
});
it("should throw when param gets an array", () => {
const toPath = compile("/:foo");
expect(() => {
toPath({ foo: [] });
}).toThrow(new TypeError('Expected "foo" to be a string'));
});
it("should throw when a wildcard is not an array", () => {
const toPath = compile("/*foo");
expect(() => {
toPath({ foo: "a" });
}).toThrow(new TypeError('Expected "foo" to be a non-empty array'));
});
it("should throw when a wildcard array value is not a string", () => {
const toPath = compile("/*foo");
expect(() => {
toPath({ foo: [1, "a"] as any });
}).toThrow(new TypeError('Expected "foo/0" to be a string'));
});
});
describe.each(PARSER_TESTS)(
"parse $path with $options",
({ path, options, expected }) => {
it("should parse the path", () => {
const data = parse(path, options);
expect(data).toEqual(expected);
});
},
);
describe.each(STRINGIFY_TESTS)(
"stringify $tokens with $options",
({ data, expected }) => {
it("should stringify the path", () => {
const path = stringify(data);
expect(path).toEqual(expected);
});
},
);
describe.each(COMPILE_TESTS)(
"compile $path with $options",
({ path, options, tests }) => {
it.each(tests)("should compile $input", ({ input, expected }) => {
const toPath = compile(path, options);
if (expected === null) {
expect(() => toPath(input)).toThrow();
} else {
expect(toPath(input)).toEqual(expected);
}
});
},
);
describe.each(MATCH_TESTS)(
"match $path with $options",
({ path, options, tests }) => {
it.each(tests)("should match $input", ({ input, expected }) => {
const fn = match(path, options);
expect(fn(input)).toEqual(expected);
});
},
);
});