Raw
1 // @vitest-environment jsdom
2 import { renderHook } from "@testing-library/react"
3 import { createElement } from "react"
4 import { beforeEach, describe, expect, it, vi } from "vitest"
5
6 const useMatchesMock = vi.fn()
7
8 vi.mock("react-router-dom", () => ({
9 useMatches: () => useMatchesMock(),
10 }))
11
12 import { useDocumentTitle } from "../use-document-title"
13
14 describe("useDocumentTitle", () => {
15 beforeEach(() => {
16 useMatchesMock.mockReset()
17 })
18
19 it("returns the default title when no match yields a title", () => {
20 useMatchesMock.mockReturnValue([
21 { id: "root", pathname: "/", params: {}, data: undefined, handle: {} },
22 ])
23
24 const { result } = renderHook(() => useDocumentTitle())
25
26 expect(result.current).toBe("Medusa")
27 })
28
29 it("appends the page title from a string breadcrumb", () => {
30 useMatchesMock.mockReturnValue([
31 {
32 id: "products",
33 pathname: "/products",
34 params: {},
35 loaderData: undefined,
36 handle: { breadcrumb: () => "Products" },
37 },
38 ])
39
40 const { result } = renderHook(() => useDocumentTitle())
41
42 expect(result.current).toBe("Products - Medusa")
43 })
44
45 it("uses a route's seo resolver for detail pages", () => {
46 useMatchesMock.mockReturnValue([
47 {
48 id: "products",
49 pathname: "/products",
50 params: {},
51 loaderData: undefined,
52 handle: { breadcrumb: () => "Products" },
53 },
54 {
55 id: "product-detail",
56 pathname: "/products/prod_1",
57 params: { id: "prod_1" },
58 loaderData: { product: { title: "Medusa Sweatpants" } },
59 handle: {
60 breadcrumb: () => createElement("span", null, "Hidden"),
61 seo: (match: { loaderData: { product: { title: string } } }) => ({
62 title: match.loaderData?.product?.title,
63 }),
64 },
65 },
66 ])
67
68 const { result } = renderHook(() => useDocumentTitle())
69
70 expect(result.current).toBe("Medusa Sweatpants - Medusa")
71 })
72
73 it("uses the most specific (last) match that yields a title", () => {
74 useMatchesMock.mockReturnValue([
75 {
76 id: "products",
77 pathname: "/products",
78 params: {},
79 loaderData: undefined,
80 handle: { breadcrumb: () => "Products" },
81 },
82 {
83 id: "product-detail",
84 pathname: "/products/prod_1",
85 params: { id: "prod_1" },
86 loaderData: { product: { title: "Medusa Sweatpants" } },
87 handle: { seo: () => ({ title: "Medusa Sweatpants" }) },
88 },
89 ])
90
91 const { result } = renderHook(() => useDocumentTitle())
92
93 expect(result.current).toBe("Medusa Sweatpants - Medusa")
94 })
95
96 it("skips a detail match without a title and uses the parent breadcrumb", () => {
97 useMatchesMock.mockReturnValue([
98 {
99 id: "products",
100 pathname: "/products",
101 params: {},
102 loaderData: undefined,
103 handle: { breadcrumb: () => "Products" },
104 },
105 {
106 id: "product-detail",
107 pathname: "/products/prod_1",
108 params: { id: "prod_1" },
109 loaderData: undefined,
110 handle: {
111 breadcrumb: () => createElement("span", null, "Hidden"),
112 seo: () => ({ title: undefined }),
113 },
114 },
115 ])
116
117 const { result } = renderHook(() => useDocumentTitle())
118
119 expect(result.current).toBe("Products - Medusa")
120 })
121 })