|
| 1 | +/*! |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +import "@testing-library/jest-dom"; |
| 20 | +import { render, screen } from "@testing-library/react"; |
| 21 | +import type { PropsWithChildren } from "react"; |
| 22 | +import { FiHome } from "react-icons/fi"; |
| 23 | +import { MemoryRouter } from "react-router-dom"; |
| 24 | +import { describe, expect, it } from "vitest"; |
| 25 | + |
| 26 | +import { BaseWrapper } from "src/utils/Wrapper"; |
| 27 | + |
| 28 | +import { NavButton } from "./NavButton"; |
| 29 | + |
| 30 | +const wrapperAt = (path: string) => { |
| 31 | + const wrapper = ({ children }: PropsWithChildren) => ( |
| 32 | + <BaseWrapper> |
| 33 | + <MemoryRouter initialEntries={[path]}>{children}</MemoryRouter> |
| 34 | + </BaseWrapper> |
| 35 | + ); |
| 36 | + |
| 37 | + return wrapper; |
| 38 | +}; |
| 39 | + |
| 40 | +describe("NavButton", () => { |
| 41 | + describe("single `to`", () => { |
| 42 | + it("renders as a link to that destination", () => { |
| 43 | + render(<NavButton icon={FiHome} title="Dags" to="dags" />, { wrapper: wrapperAt("/") }); |
| 44 | + |
| 45 | + expect(screen.getByRole("link", { name: "Dags" })).toHaveAttribute("href", "/dags"); |
| 46 | + }); |
| 47 | + |
| 48 | + it("is active when the current route matches", () => { |
| 49 | + render(<NavButton icon={FiHome} title="Dags" to="dags" />, { wrapper: wrapperAt("/dags") }); |
| 50 | + |
| 51 | + expect(screen.getByRole("link", { name: "Dags" })).toHaveAttribute("aria-current", "page"); |
| 52 | + }); |
| 53 | + |
| 54 | + it("is active on a nested route under the destination", () => { |
| 55 | + render(<NavButton icon={FiHome} title="Dags" to="dags" />, { |
| 56 | + wrapper: wrapperAt("/dags/my_dag/runs"), |
| 57 | + }); |
| 58 | + |
| 59 | + expect(screen.getByRole("link", { name: "Dags" })).toHaveAttribute("aria-current", "page"); |
| 60 | + }); |
| 61 | + |
| 62 | + it("is not active on an unrelated route", () => { |
| 63 | + render(<NavButton icon={FiHome} title="Dags" to="dags" />, { wrapper: wrapperAt("/assets") }); |
| 64 | + |
| 65 | + expect(screen.getByRole("link", { name: "Dags" })).not.toHaveAttribute("aria-current"); |
| 66 | + }); |
| 67 | + }); |
| 68 | + |
| 69 | + describe("multiple `to`", () => { |
| 70 | + it("renders as a plain button, not a link", () => { |
| 71 | + render(<NavButton icon={FiHome} title="Browse" to={["events", "jobs"]} />, { |
| 72 | + wrapper: wrapperAt("/"), |
| 73 | + }); |
| 74 | + |
| 75 | + const button = screen.getByRole("button", { name: "Browse" }); |
| 76 | + |
| 77 | + expect(button).not.toHaveAttribute("href"); |
| 78 | + }); |
| 79 | + |
| 80 | + it("is active when the current route matches any of the destinations", () => { |
| 81 | + render(<NavButton icon={FiHome} title="Browse" to={["events", "jobs"]} />, { |
| 82 | + wrapper: wrapperAt("/jobs"), |
| 83 | + }); |
| 84 | + |
| 85 | + expect(screen.getByRole("button", { name: "Browse" })).toHaveAttribute("aria-current", "page"); |
| 86 | + }); |
| 87 | + |
| 88 | + it("is not active when the current route matches none of the destinations", () => { |
| 89 | + render(<NavButton icon={FiHome} title="Browse" to={["events", "jobs"]} />, { |
| 90 | + wrapper: wrapperAt("/xcoms"), |
| 91 | + }); |
| 92 | + |
| 93 | + expect(screen.getByRole("button", { name: "Browse" })).not.toHaveAttribute("aria-current"); |
| 94 | + }); |
| 95 | + }); |
| 96 | + |
| 97 | + describe("matchPaths", () => { |
| 98 | + it("is active on an extra match path even though `to` points elsewhere", () => { |
| 99 | + render(<NavButton icon={FiHome} matchPaths={["dag_runs", "task_instances"]} title="Dags" to="dags" />, { |
| 100 | + wrapper: wrapperAt("/dag_runs"), |
| 101 | + }); |
| 102 | + |
| 103 | + expect(screen.getByRole("link", { name: "Dags" })).toHaveAttribute("aria-current", "page"); |
| 104 | + }); |
| 105 | + |
| 106 | + it("is not active on a route outside both `to` and matchPaths", () => { |
| 107 | + render(<NavButton icon={FiHome} matchPaths={["dag_runs", "task_instances"]} title="Dags" to="dags" />, { |
| 108 | + wrapper: wrapperAt("/assets"), |
| 109 | + }); |
| 110 | + |
| 111 | + expect(screen.getByRole("link", { name: "Dags" })).not.toHaveAttribute("aria-current"); |
| 112 | + }); |
| 113 | + }); |
| 114 | +}); |
0 commit comments