-
-
Notifications
You must be signed in to change notification settings - Fork 464
Expand file tree
/
Copy pathindex.spec.ts
More file actions
192 lines (169 loc) · 5.2 KB
/
index.spec.ts
File metadata and controls
192 lines (169 loc) · 5.2 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import { describe, it, expect } from "vitest";
import {
parse,
compile,
match,
stringify,
pathToRegexp,
TokenData,
} 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 index 7, expected }: /{:foo,; visit https://git.new/pathToRegexpError for more info",
),
);
});
it("should throw on nested unbalanced group", () => {
expect(() => parse("/{:foo/{x,y}")).toThrow(
new TypeError(
"Unexpected END at index 12, expected }: /{:foo/{x,y}; visit https://git.new/pathToRegexpError for more info",
),
);
});
it("should throw on missing param name", () => {
expect(() => parse("/:/")).toThrow(
new TypeError(
"Missing parameter name at index 2: /:/; visit https://git.new/pathToRegexpError for more info",
),
);
});
it("should throw on missing wildcard name", () => {
expect(() => parse("/*/")).toThrow(
new TypeError(
"Missing parameter name at index 2: /*/; visit https://git.new/pathToRegexpError for more info",
),
);
});
it("should throw on unterminated quote", () => {
expect(() => parse('/:"foo')).toThrow(
new TypeError(
'Unterminated quote at index 2: /:"foo; visit https://git.new/pathToRegexpError for more info',
),
);
});
});
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("pathToRegexp errors", () => {
it("should throw when missing text between params", () => {
expect(() => pathToRegexp("/:foo:bar")).toThrow(
new TypeError(
'Missing text before "bar": /:foo:bar; visit https://git.new/pathToRegexpError for more info',
),
);
});
it("should throw when missing text between params using TokenData", () => {
expect(() =>
pathToRegexp(
new TokenData([
{ type: "param", name: "a" },
{ type: "param", name: "b" },
]),
),
).toThrow(
new TypeError(
'Missing text before "b"; visit https://git.new/pathToRegexpError for more info',
),
);
});
it("should throw with `originalPath` when missing text between params using TokenData", () => {
expect(() =>
pathToRegexp(
new TokenData(
[
{ type: "param", name: "a" },
{ type: "param", name: "b" },
],
"/[a][b]",
),
),
).toThrow(
new TypeError(
'Missing text before "b": /[a][b]; visit https://git.new/pathToRegexpError for more info',
),
);
});
});
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);
});
},
);
});