summaryrefslogtreecommitdiff
path: root/node_modules/wrangler/templates/__tests__/pages-dev-util.test.ts
blob: 3dea566a66eb675f487e5443d248a77ea5891f78 (plain)
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
import { isRoutingRuleMatch } from "../pages-dev-util";

describe("isRoutingRuleMatch", () => {
	it("should match rules referencing root level correctly", () => {
		const routingRule = "/";

		expect(isRoutingRuleMatch("/", routingRule)).toBeTruthy();
		expect(isRoutingRuleMatch("/foo", routingRule)).toBeFalsy();
		expect(isRoutingRuleMatch("/foo/", routingRule)).toBeFalsy();
		expect(isRoutingRuleMatch("/foo/bar", routingRule)).toBeFalsy();
	});

	it("should match include-all rules correctly", () => {
		const routingRule = "/*";

		expect(isRoutingRuleMatch("/", routingRule)).toBeTruthy();
		expect(isRoutingRuleMatch("/foo", routingRule)).toBeTruthy();
		expect(isRoutingRuleMatch("/foo/", routingRule)).toBeTruthy();
		expect(isRoutingRuleMatch("/foo/bar", routingRule)).toBeTruthy();
		expect(isRoutingRuleMatch("/foo/bar/", routingRule)).toBeTruthy();
		expect(isRoutingRuleMatch("/foo/bar/baz", routingRule)).toBeTruthy();
		expect(isRoutingRuleMatch("/foo/bar/baz/", routingRule)).toBeTruthy();
	});

	it("should match `/*` suffix-ed rules correctly", () => {
		let routingRule = "/foo/*";

		expect(isRoutingRuleMatch("/foo", routingRule)).toBeTruthy();
		expect(isRoutingRuleMatch("/foo/", routingRule)).toBeTruthy();
		expect(isRoutingRuleMatch("/foobar", routingRule)).toBeFalsy();
		expect(isRoutingRuleMatch("/foo/bar", routingRule)).toBeTruthy();
		expect(isRoutingRuleMatch("/foo/bar/baz", routingRule)).toBeTruthy();
		expect(isRoutingRuleMatch("/bar/foo", routingRule)).toBeFalsy();
		expect(isRoutingRuleMatch("/bar/foo/baz", routingRule)).toBeFalsy();

		routingRule = "/foo/bar/*";

		expect(isRoutingRuleMatch("/foo", routingRule)).toBeFalsy();
		expect(isRoutingRuleMatch("/foo/", routingRule)).toBeFalsy();
		expect(isRoutingRuleMatch("/foo/bar", routingRule)).toBeTruthy();
		expect(isRoutingRuleMatch("/foo/bar/baz", routingRule)).toBeTruthy();
		expect(isRoutingRuleMatch("/foo/barfoo", routingRule)).toBeFalsy();
		expect(isRoutingRuleMatch("baz/foo/bar", routingRule)).toBeFalsy();
		expect(isRoutingRuleMatch("baz/foo/bar/", routingRule)).toBeFalsy();
	});

	it("should match `/` suffix-ed rules correctly", () => {
		let routingRule = "/foo/";
		expect(isRoutingRuleMatch("/foo/", routingRule)).toBeTruthy();
		expect(isRoutingRuleMatch("/foo", routingRule)).toBeTruthy();

		routingRule = "/foo/bar/";
		expect(isRoutingRuleMatch("/foo/bar/", routingRule)).toBeTruthy();
		expect(isRoutingRuleMatch("/foo/bar", routingRule)).toBeTruthy();
	});

	it("should match `*` suffix-ed rules correctly", () => {
		let routingRule = "/foo*";
		expect(isRoutingRuleMatch("/foo", routingRule)).toBeTruthy();
		expect(isRoutingRuleMatch("/foo/", routingRule)).toBeTruthy();
		expect(isRoutingRuleMatch("/foobar", routingRule)).toBeTruthy();
		expect(isRoutingRuleMatch("/barfoo", routingRule)).toBeFalsy();
		expect(isRoutingRuleMatch("/foo/bar", routingRule)).toBeTruthy();
		expect(isRoutingRuleMatch("/bar/foo", routingRule)).toBeFalsy();
		expect(isRoutingRuleMatch("/bar/foobar", routingRule)).toBeFalsy();
		expect(isRoutingRuleMatch("/foo/bar/baz", routingRule)).toBeTruthy();
		expect(isRoutingRuleMatch("/bar/foo/baz", routingRule)).toBeFalsy();

		routingRule = "/foo/bar*";
		expect(isRoutingRuleMatch("/foo/bar", routingRule)).toBeTruthy();
		expect(isRoutingRuleMatch("/foo/bar/", routingRule)).toBeTruthy();
		expect(isRoutingRuleMatch("/foo/barfoo", routingRule)).toBeTruthy();
		expect(isRoutingRuleMatch("/bar/foo/barfoo", routingRule)).toBeFalsy();
		expect(isRoutingRuleMatch("/foo/bar/baz", routingRule)).toBeTruthy();
		expect(isRoutingRuleMatch("/bar/foo/bar/baz", routingRule)).toBeFalsy();
	});

	it("should match rules without wildcards correctly", () => {
		let routingRule = "/foo";

		expect(isRoutingRuleMatch("/foo", routingRule)).toBeTruthy();
		expect(isRoutingRuleMatch("/foo/", routingRule)).toBeTruthy();
		expect(isRoutingRuleMatch("/foo/bar", routingRule)).toBeFalsy();
		expect(isRoutingRuleMatch("/bar/foo", routingRule)).toBeFalsy();

		routingRule = "/foo/bar";
		expect(isRoutingRuleMatch("/foo/bar", routingRule)).toBeTruthy();
		expect(isRoutingRuleMatch("/foo/bar/", routingRule)).toBeTruthy();
		expect(isRoutingRuleMatch("/foo/bar/baz", routingRule)).toBeFalsy();
		expect(isRoutingRuleMatch("/baz/foo/bar", routingRule)).toBeFalsy();
	});

	it("should throw an error if pathname or routing rule params are missing", () => {
		// MISSING PATHNAME
		expect(() =>
			// eslint-disable-next-line @typescript-eslint/ban-ts-comment
			// @ts-ignore: sanity check
			isRoutingRuleMatch(undefined, "/*")
		).toThrow("Pathname is undefined.");

		expect(() =>
			// eslint-disable-next-line @typescript-eslint/ban-ts-comment
			// @ts-ignore: sanity check
			isRoutingRuleMatch(null, "/*")
		).toThrow("Pathname is undefined.");

		expect(() => isRoutingRuleMatch("", "/*")).toThrow(
			"Pathname is undefined."
		);

		// MISSING ROUTING RULE
		expect(() =>
			// eslint-disable-next-line @typescript-eslint/ban-ts-comment
			// @ts-ignore: sanity check
			isRoutingRuleMatch("/foo", undefined)
		).toThrow("Routing rule is undefined.");

		expect(() =>
			// eslint-disable-next-line @typescript-eslint/ban-ts-comment
			// @ts-ignore: sanity check
			isRoutingRuleMatch("/foo", null)
		).toThrow("Routing rule is undefined.");

		expect(() => isRoutingRuleMatch("/foo", "")).toThrow(
			"Routing rule is undefined."
		);
	});
});