-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathLink.browser.test.tsx
More file actions
165 lines (158 loc) · 4.46 KB
/
Link.browser.test.tsx
File metadata and controls
165 lines (158 loc) · 4.46 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
import { waitFor } from "@testing-library/react"
import { userEvent } from "@vitest/browser/context"
import { useLocation } from "react-router"
import type { StubRouteEntry } from "tests/setup.browser"
import { Link, type LinkProps } from "./link"
const getEntries: (linkProps?: LinkProps) => StubRouteEntry[] = (linkProps) => [
{
path: "/first",
Component: () => {
const url = useLocation()
return (
<>
<p>
{url.pathname} + {url.search}
</p>
<Link {...linkProps} to="/second">
go
</Link>
</>
)
},
},
{
path: "/second",
Component: () => {
const url = useLocation()
return (
<>
<p>
{url.pathname}
{url.search}
</p>
<Link to="/first">go</Link>
</>
)
},
},
]
describe("Link", () => {
it("if the url is /first and you redirect to /second nothing is added to the url", async ({ renderStub }) => {
const { getByText } = await renderStub({
entries: getEntries(),
props: {
initialEntries: ["/first"],
},
})
const link = getByText("go")
await userEvent.click(link)
const url = getByText("/second")
expect(url).toBeDefined()
await waitFor(() => {
expect(url.element()).toBeDefined()
expect(url.element()).toHaveTextContent("/second")
})
})
it("if the url is /first?a=1 and you redirect to /second without keepSearchParams nothing is added to the url", async ({
renderStub,
}) => {
const { getByText } = await renderStub({
entries: getEntries(),
props: {
initialEntries: ["/first?a=1"],
},
})
const link = getByText("go")
await userEvent.click(link)
const url = getByText("/second")
await waitFor(() => {
expect(url.element()).toBeDefined()
expect(url.element()).toHaveTextContent("/second")
})
})
it("if the url is /first?a=1 and you redirect to /second with keepSearchParams search params are kept", async ({
renderStub,
}) => {
const { getByText } = await renderStub({
entries: getEntries({ keepSearchParams: true, to: "/second" }),
props: {
initialEntries: ["/first?a=1"],
},
})
const link = getByText("go")
await userEvent.click(link)
const url = getByText("/second")
await waitFor(() => {
expect(url.element()).toBeDefined()
expect(url.element()).toHaveTextContent("/second?a=1")
})
})
it("if the url is /first?a=1&lng=en and you redirect to /second with keepSearchParams search params and language are kept", async ({
renderStub,
}) => {
const { getByText } = await renderStub({
entries: getEntries({ keepSearchParams: true, to: "/second" }),
props: {
initialEntries: ["/first?a=1&lng=en"],
},
})
const link = getByText("go")
await userEvent.click(link)
const url = getByText("/second")
await waitFor(() => {
expect(url.element()).toBeDefined()
expect(url.element()).toHaveTextContent("/second?a=1&lng=en")
})
})
it("if the url is /first?a=1&lng=en and you redirect to /second without keepSearchParams language is kept", async ({
renderStub,
}) => {
const { getByText } = await renderStub({
entries: getEntries({ to: "/second" }),
props: {
initialEntries: ["/first?lng=en"],
},
})
const link = getByText("go")
await userEvent.click(link)
const url = getByText("/second")
await waitFor(() => {
expect(url.element()).toBeDefined()
expect(url.element()).toHaveTextContent("/second?lng=en")
})
})
it("if the url is /first?a=1&lng=en and you redirect to /second with a language override it is changed and search params are removed", async ({
renderStub,
}) => {
const { getByText } = await renderStub({
entries: getEntries({ to: "/second", language: "bs" }),
props: {
initialEntries: ["/first?lng=en"],
},
})
const link = getByText("go")
await userEvent.click(link)
const url = getByText("/second")
await waitFor(() => {
expect(url.element()).toBeDefined()
expect(url.element()).toHaveTextContent("/second?lng=bs")
})
})
it("if the url is /first?a=1&lng=en and you redirect to /second with a language override it is changed and search params are kept with keepSearchParams", async ({
renderStub,
}) => {
const { getByText } = await renderStub({
entries: getEntries({ to: "/second", language: "bs", keepSearchParams: true }),
props: {
initialEntries: ["/first?a=a&lng=en"],
},
})
const link = getByText("go")
await userEvent.click(link)
const url = getByText("/second")
await waitFor(() => {
expect(url.element()).toBeDefined()
expect(url.element()).toHaveTextContent("/second?a=a&lng=bs")
})
})
})