|
| 1 | +import { waitFor } from "@testing-library/react" |
| 2 | +import { userEvent } from "@vitest/browser/context" |
| 3 | +import { useLocation } from "react-router" |
| 4 | +import type { StubRouteEntry } from "tests/setup.browser" |
| 5 | +import { Link, type LinkProps } from "./link" |
| 6 | +const getEntries: (linkProps?: LinkProps) => StubRouteEntry[] = (linkProps) => [ |
| 7 | + { |
| 8 | + path: "/first", |
| 9 | + Component: () => { |
| 10 | + const url = useLocation() |
| 11 | + return ( |
| 12 | + <> |
| 13 | + <p> |
| 14 | + {url.pathname} + {url.search} |
| 15 | + </p> |
| 16 | + <Link {...linkProps} to="/second"> |
| 17 | + go |
| 18 | + </Link> |
| 19 | + </> |
| 20 | + ) |
| 21 | + }, |
| 22 | + }, |
| 23 | + { |
| 24 | + path: "/second", |
| 25 | + Component: () => { |
| 26 | + const url = useLocation() |
| 27 | + return ( |
| 28 | + <> |
| 29 | + <p> |
| 30 | + {url.pathname} |
| 31 | + {url.search} |
| 32 | + </p> |
| 33 | + <Link to="/first">go</Link> |
| 34 | + </> |
| 35 | + ) |
| 36 | + }, |
| 37 | + }, |
| 38 | +] |
| 39 | +describe("Link", () => { |
| 40 | + it("if the url is /first and you redirect to /second nothing is added to the url", async ({ renderStub }) => { |
| 41 | + const { getByText } = await renderStub({ |
| 42 | + entries: getEntries(), |
| 43 | + props: { |
| 44 | + initialEntries: ["/first"], |
| 45 | + }, |
| 46 | + }) |
| 47 | + const link = getByText("go") |
| 48 | + await userEvent.click(link) |
| 49 | + const url = getByText("/second") |
| 50 | + expect(url).toBeDefined() |
| 51 | + await waitFor(() => { |
| 52 | + expect(url.element()).toBeDefined() |
| 53 | + expect(url.element()).toHaveTextContent("/second") |
| 54 | + }) |
| 55 | + }) |
| 56 | + |
| 57 | + it("if the url is /first?a=1 and you redirect to /second without keepSearchParams nothing is added to the url", async ({ |
| 58 | + renderStub, |
| 59 | + }) => { |
| 60 | + const { getByText } = await renderStub({ |
| 61 | + entries: getEntries(), |
| 62 | + props: { |
| 63 | + initialEntries: ["/first?a=1"], |
| 64 | + }, |
| 65 | + }) |
| 66 | + const link = getByText("go") |
| 67 | + await userEvent.click(link) |
| 68 | + const url = getByText("/second") |
| 69 | + await waitFor(() => { |
| 70 | + expect(url.element()).toBeDefined() |
| 71 | + expect(url.element()).toHaveTextContent("/second") |
| 72 | + }) |
| 73 | + }) |
| 74 | + |
| 75 | + it("if the url is /first?a=1 and you redirect to /second with keepSearchParams search params are kept", async ({ |
| 76 | + renderStub, |
| 77 | + }) => { |
| 78 | + const { getByText } = await renderStub({ |
| 79 | + entries: getEntries({ keepSearchParams: true, to: "/second" }), |
| 80 | + props: { |
| 81 | + initialEntries: ["/first?a=1"], |
| 82 | + }, |
| 83 | + }) |
| 84 | + const link = getByText("go") |
| 85 | + await userEvent.click(link) |
| 86 | + const url = getByText("/second") |
| 87 | + await waitFor(() => { |
| 88 | + expect(url.element()).toBeDefined() |
| 89 | + expect(url.element()).toHaveTextContent("/second?a=1") |
| 90 | + }) |
| 91 | + }) |
| 92 | + |
| 93 | + it("if the url is /first?a=1&lng=en and you redirect to /second with keepSearchParams search params and language are kept", async ({ |
| 94 | + renderStub, |
| 95 | + }) => { |
| 96 | + const { getByText } = await renderStub({ |
| 97 | + entries: getEntries({ keepSearchParams: true, to: "/second" }), |
| 98 | + props: { |
| 99 | + initialEntries: ["/first?a=1&lng=en"], |
| 100 | + }, |
| 101 | + }) |
| 102 | + const link = getByText("go") |
| 103 | + await userEvent.click(link) |
| 104 | + const url = getByText("/second") |
| 105 | + await waitFor(() => { |
| 106 | + expect(url.element()).toBeDefined() |
| 107 | + expect(url.element()).toHaveTextContent("/second?a=1&lng=en") |
| 108 | + }) |
| 109 | + }) |
| 110 | + |
| 111 | + it("if the url is /first?a=1&lng=en and you redirect to /second without keepSearchParams language is kept", async ({ |
| 112 | + renderStub, |
| 113 | + }) => { |
| 114 | + const { getByText } = await renderStub({ |
| 115 | + entries: getEntries({ to: "/second" }), |
| 116 | + props: { |
| 117 | + initialEntries: ["/first?lng=en"], |
| 118 | + }, |
| 119 | + }) |
| 120 | + const link = getByText("go") |
| 121 | + await userEvent.click(link) |
| 122 | + const url = getByText("/second") |
| 123 | + await waitFor(() => { |
| 124 | + expect(url.element()).toBeDefined() |
| 125 | + expect(url.element()).toHaveTextContent("/second?lng=en") |
| 126 | + }) |
| 127 | + }) |
| 128 | + |
| 129 | + 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 ({ |
| 130 | + renderStub, |
| 131 | + }) => { |
| 132 | + const { getByText } = await renderStub({ |
| 133 | + entries: getEntries({ to: "/second", language: "bs" }), |
| 134 | + props: { |
| 135 | + initialEntries: ["/first?lng=en"], |
| 136 | + }, |
| 137 | + }) |
| 138 | + const link = getByText("go") |
| 139 | + await userEvent.click(link) |
| 140 | + const url = getByText("/second") |
| 141 | + await waitFor(() => { |
| 142 | + expect(url.element()).toBeDefined() |
| 143 | + expect(url.element()).toHaveTextContent("/second?lng=bs") |
| 144 | + }) |
| 145 | + }) |
| 146 | + |
| 147 | + 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 ({ |
| 148 | + renderStub, |
| 149 | + }) => { |
| 150 | + const { getByText } = await renderStub({ |
| 151 | + entries: getEntries({ to: "/second", language: "bs", keepSearchParams: true }), |
| 152 | + props: { |
| 153 | + initialEntries: ["/first?a=a&lng=en"], |
| 154 | + }, |
| 155 | + }) |
| 156 | + const link = getByText("go") |
| 157 | + await userEvent.click(link) |
| 158 | + const url = getByText("/second") |
| 159 | + await waitFor(() => { |
| 160 | + expect(url.element()).toBeDefined() |
| 161 | + expect(url.element()).toHaveTextContent("/second?a=a&lng=bs") |
| 162 | + }) |
| 163 | + }) |
| 164 | +}) |
0 commit comments