|
1
|
import React from "react" |
|
2
|
import { describe, expect, test, vi } from "vitest" |
|
3
|
import { render } from "@testing-library/react" |
|
4
|
|
|
5
|
vi.mock("next/link", () => ({ |
|
6
|
default: ({ |
|
7
|
children, |
|
8
|
href, |
|
9
|
className, |
|
10
|
...props |
|
11
|
}: { |
|
12
|
children: React.ReactNode |
|
13
|
href: string |
|
14
|
className?: string |
|
15
|
[key: string]: unknown |
|
16
|
}) => ( |
|
17
|
<a href={href} className={className} {...props}> |
|
18
|
{children} |
|
19
|
</a> |
|
20
|
), |
|
21
|
})) |
|
22
|
|
|
23
|
import { Link } from "../../Link" |
|
24
|
|
|
25
|
describe("rendering", () => { |
|
26
|
test("renders link with href", () => { |
|
27
|
const { container } = render(<Link href="/test">Test Link</Link>) |
|
28
|
const link = container.querySelector("a") |
|
29
|
expect(link).toBeInTheDocument() |
|
30
|
expect(link).toHaveAttribute("href", "/test") |
|
31
|
expect(link).toHaveTextContent("Test Link") |
|
32
|
}) |
|
33
|
|
|
34
|
test("renders with default variant", () => { |
|
35
|
const { container } = render(<Link href="/test">Test Link</Link>) |
|
36
|
const link = container.querySelector("a") |
|
37
|
expect(link).toHaveClass( |
|
38
|
"text-medusa-fg-interactive hover:text-medusa-fg-interactive-hover" |
|
39
|
) |
|
40
|
}) |
|
41
|
|
|
42
|
test("renders with content variant", () => { |
|
43
|
const { container } = render( |
|
44
|
<Link href="/test" variant="content"> |
|
45
|
Test Link |
|
46
|
</Link> |
|
47
|
) |
|
48
|
const link = container.querySelector("a") |
|
49
|
expect(link).toHaveClass( |
|
50
|
"underline decoration-medusa-fg-muted hover:decoration-medusa-fg-interactive" |
|
51
|
) |
|
52
|
expect(link).toHaveClass( |
|
53
|
"decoration-[1.5px] font-medium transition-[text-decoration-color]" |
|
54
|
) |
|
55
|
}) |
|
56
|
|
|
57
|
test("applies custom className", () => { |
|
58
|
const { container } = render( |
|
59
|
<Link href="/test" className="custom-class"> |
|
60
|
Test Link |
|
61
|
</Link> |
|
62
|
) |
|
63
|
const link = container.querySelector("a") |
|
64
|
expect(link).toHaveClass("custom-class") |
|
65
|
}) |
|
66
|
|
|
67
|
test("renders with icon when withIcon is true", () => { |
|
68
|
const { container } = render( |
|
69
|
<Link href="/test" withIcon> |
|
70
|
Test Link |
|
71
|
</Link> |
|
72
|
) |
|
73
|
const link = container.querySelector("a") |
|
74
|
expect(link).toHaveClass("flex gap-0.25 items-center group") |
|
75
|
const icon = container.querySelector("svg") |
|
76
|
expect(icon).toBeInTheDocument() |
|
77
|
}) |
|
78
|
|
|
79
|
test("does not render icon when withIcon is false", () => { |
|
80
|
const { container } = render(<Link href="/test">Test Link</Link>) |
|
81
|
const icon = container.querySelector("svg") |
|
82
|
expect(icon).not.toBeInTheDocument() |
|
83
|
}) |
|
84
|
|
|
85
|
test("transforms page.mdx href", () => { |
|
86
|
const { container } = render(<Link href="/test/page.mdx">Test Link</Link>) |
|
87
|
const link = container.querySelector("a") |
|
88
|
expect(link).toHaveAttribute("href", "/test") |
|
89
|
}) |
|
90
|
|
|
91
|
test("transforms page.mdx href with hash", () => { |
|
92
|
const { container } = render( |
|
93
|
<Link href="/test/page.mdx#section">Test Link</Link> |
|
94
|
) |
|
95
|
const link = container.querySelector("a") |
|
96
|
expect(link).toHaveAttribute("href", "/test#section") |
|
97
|
}) |
|
98
|
|
|
99
|
test("handles empty href", () => { |
|
100
|
const { container } = render(<Link href="">Test Link</Link>) |
|
101
|
const link = container.querySelector("a") |
|
102
|
expect(link).toHaveAttribute("href", "") |
|
103
|
}) |
|
104
|
|
|
105
|
test("handles undefined href", () => { |
|
106
|
const { container } = render(<Link>Test Link</Link>) |
|
107
|
const link = container.querySelector("a") |
|
108
|
expect(link).toHaveAttribute("href", "") |
|
109
|
}) |
|
110
|
|
|
111
|
test("passes through other props", () => { |
|
112
|
const { container } = render( |
|
113
|
<Link href="/test" target="_blank" rel="noopener"> |
|
114
|
Test Link |
|
115
|
</Link> |
|
116
|
) |
|
117
|
const link = container.querySelector("a") |
|
118
|
expect(link).toHaveAttribute("target", "_blank") |
|
119
|
expect(link).toHaveAttribute("rel", "noopener") |
|
120
|
}) |
|
121
|
}) |