|
1
|
import { createElement } from "react" |
|
2
|
import type { UIMatch } from "react-router-dom" |
|
3
|
import { describe, expect, it } from "vitest" |
|
4
|
|
|
5
|
import { getTitleFromMatch, RouteHandle } from "../use-document-title" |
|
6
|
|
|
7
|
const buildMatch = ( |
|
8
|
overrides: Partial<UIMatch<unknown, RouteHandle>> = {} |
|
9
|
): UIMatch<unknown, RouteHandle> => |
|
10
|
({ |
|
11
|
id: "match", |
|
12
|
pathname: "/", |
|
13
|
params: {}, |
|
14
|
loaderData: undefined, |
|
15
|
handle: undefined, |
|
16
|
...overrides, |
|
17
|
} as UIMatch<unknown, RouteHandle>) |
|
18
|
|
|
19
|
describe("getTitleFromMatch", () => { |
|
20
|
it("returns null when the handle is undefined", () => { |
|
21
|
expect(getTitleFromMatch(buildMatch())).toBeNull() |
|
22
|
}) |
|
23
|
|
|
24
|
it("returns null when the handle has neither seo nor breadcrumb", () => { |
|
25
|
expect(getTitleFromMatch(buildMatch({ handle: {} }))).toBeNull() |
|
26
|
}) |
|
27
|
|
|
28
|
describe("seo resolver", () => { |
|
29
|
it("returns the resolved title", () => { |
|
30
|
const match = buildMatch({ |
|
31
|
loaderData: { order: { display_id: 12345 } }, |
|
32
|
handle: { |
|
33
|
seo: (m) => ({ |
|
34
|
title: `#${ |
|
35
|
(m.loaderData as { order: { display_id: number } }).order |
|
36
|
.display_id |
|
37
|
}`, |
|
38
|
}), |
|
39
|
}, |
|
40
|
}) |
|
41
|
|
|
42
|
expect(getTitleFromMatch(match)).toBe("#12345") |
|
43
|
}) |
|
44
|
|
|
45
|
it("takes precedence over a string breadcrumb", () => { |
|
46
|
const match = buildMatch({ |
|
47
|
handle: { |
|
48
|
breadcrumb: () => "Orders", |
|
49
|
seo: () => ({ title: "#12345" }), |
|
50
|
}, |
|
51
|
}) |
|
52
|
|
|
53
|
expect(getTitleFromMatch(match)).toBe("#12345") |
|
54
|
}) |
|
55
|
|
|
56
|
it("falls back to the breadcrumb when it returns undefined", () => { |
|
57
|
const match = buildMatch({ |
|
58
|
handle: { |
|
59
|
breadcrumb: () => "Orders", |
|
60
|
seo: () => ({ title: undefined }), |
|
61
|
}, |
|
62
|
}) |
|
63
|
|
|
64
|
expect(getTitleFromMatch(match)).toBe("Orders") |
|
65
|
}) |
|
66
|
|
|
67
|
it("ignores empty or whitespace-only titles", () => { |
|
68
|
expect( |
|
69
|
getTitleFromMatch( |
|
70
|
buildMatch({ handle: { seo: () => ({ title: "" }) } }) |
|
71
|
) |
|
72
|
).toBeNull() |
|
73
|
expect( |
|
74
|
getTitleFromMatch( |
|
75
|
buildMatch({ handle: { seo: () => ({ title: " " }) } }) |
|
76
|
) |
|
77
|
).toBeNull() |
|
78
|
}) |
|
79
|
|
|
80
|
it("returns null when the resolver throws", () => { |
|
81
|
const match = buildMatch({ |
|
82
|
handle: { |
|
83
|
seo: () => { |
|
84
|
throw new Error("boom") |
|
85
|
}, |
|
86
|
}, |
|
87
|
}) |
|
88
|
|
|
89
|
expect(getTitleFromMatch(match)).toBeNull() |
|
90
|
}) |
|
91
|
}) |
|
92
|
|
|
93
|
describe("breadcrumb fallback", () => { |
|
94
|
it("returns the breadcrumb string", () => { |
|
95
|
expect( |
|
96
|
getTitleFromMatch( |
|
97
|
buildMatch({ handle: { breadcrumb: () => "Products" } }) |
|
98
|
) |
|
99
|
).toBe("Products") |
|
100
|
}) |
|
101
|
|
|
102
|
it("ignores empty or whitespace-only breadcrumb strings", () => { |
|
103
|
expect( |
|
104
|
getTitleFromMatch(buildMatch({ handle: { breadcrumb: () => "" } })) |
|
105
|
).toBeNull() |
|
106
|
expect( |
|
107
|
getTitleFromMatch(buildMatch({ handle: { breadcrumb: () => " " } })) |
|
108
|
).toBeNull() |
|
109
|
}) |
|
110
|
|
|
111
|
it("returns null for React-element breadcrumbs", () => { |
|
112
|
const match = buildMatch({ |
|
113
|
loaderData: { product: { title: "Medusa Sweatpants" } }, |
|
114
|
handle: { breadcrumb: () => createElement("span", null, "Hidden") }, |
|
115
|
}) |
|
116
|
|
|
117
|
expect(getTitleFromMatch(match)).toBeNull() |
|
118
|
}) |
|
119
|
|
|
120
|
it("returns null for non-string, non-element breadcrumb values", () => { |
|
121
|
const match = buildMatch({ |
|
122
|
handle: { breadcrumb: () => 42 as unknown as string }, |
|
123
|
}) |
|
124
|
|
|
125
|
expect(getTitleFromMatch(match)).toBeNull() |
|
126
|
}) |
|
127
|
|
|
128
|
it("returns null when the breadcrumb throws", () => { |
|
129
|
const match = buildMatch({ |
|
130
|
handle: { |
|
131
|
breadcrumb: () => { |
|
132
|
throw new Error("boom") |
|
133
|
}, |
|
134
|
}, |
|
135
|
}) |
|
136
|
|
|
137
|
expect(getTitleFromMatch(match)).toBeNull() |
|
138
|
}) |
|
139
|
}) |
|
140
|
|
|
141
|
describe("label fallback", () => { |
|
142
|
it("returns the label when no seo or string breadcrumb is present", () => { |
|
143
|
expect( |
|
144
|
getTitleFromMatch(buildMatch({ handle: { label: "Analytics" } })) |
|
145
|
).toBe("Analytics") |
|
146
|
}) |
|
147
|
|
|
148
|
it("yields to seo but takes precedence over a string breadcrumb", () => { |
|
149
|
expect( |
|
150
|
getTitleFromMatch( |
|
151
|
buildMatch({ |
|
152
|
handle: { seo: () => ({ title: "#1" }), label: "Analytics" }, |
|
153
|
}) |
|
154
|
) |
|
155
|
).toBe("#1") |
|
156
|
|
|
157
|
expect( |
|
158
|
getTitleFromMatch( |
|
159
|
buildMatch({ |
|
160
|
handle: { breadcrumb: () => "Reports", label: "Analytics" }, |
|
161
|
}) |
|
162
|
) |
|
163
|
).toBe("Analytics") |
|
164
|
}) |
|
165
|
|
|
166
|
it("falls back to the breadcrumb when the label is empty", () => { |
|
167
|
expect( |
|
168
|
getTitleFromMatch( |
|
169
|
buildMatch({ |
|
170
|
handle: { breadcrumb: () => "Reports", label: " " }, |
|
171
|
}) |
|
172
|
) |
|
173
|
).toBe("Reports") |
|
174
|
}) |
|
175
|
|
|
176
|
it("ignores empty or whitespace-only labels", () => { |
|
177
|
expect( |
|
178
|
getTitleFromMatch(buildMatch({ handle: { label: "" } })) |
|
179
|
).toBeNull() |
|
180
|
expect( |
|
181
|
getTitleFromMatch(buildMatch({ handle: { label: " " } })) |
|
182
|
).toBeNull() |
|
183
|
}) |
|
184
|
}) |
|
185
|
}) |