From 90d785766c545abc5fcc886cf8e4a450095fd555 Mon Sep 17 00:00:00 2001 From: Haris Bikovic Date: Mon, 30 Oct 2023 13:49:18 +0100 Subject: [PATCH] add tests for the Tabs component Signed-off-by: Haris Bikovic --- frontend/src/components/Tabs.test.tsx | 207 ++++++++++++++++++++++++++ 1 file changed, 207 insertions(+) create mode 100644 frontend/src/components/Tabs.test.tsx diff --git a/frontend/src/components/Tabs.test.tsx b/frontend/src/components/Tabs.test.tsx new file mode 100644 index 000000000..b5579af28 --- /dev/null +++ b/frontend/src/components/Tabs.test.tsx @@ -0,0 +1,207 @@ +/* + This file is part of Edgehog. + + Copyright 2023-2024 SECO Mind Srl + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + + SPDX-License-Identifier: Apache-2.0 +*/ + +import { it, expect, describe } from "vitest"; +import { render, screen } from "@testing-library/react"; +import userEvent from "@testing-library/user-event"; + +import Tabs, { Tab } from "./Tabs"; + +describe("Tabs", () => { + it("assigns className to the Tabs component", () => { + render(Tabs Content); + + const tabsElement = screen.getByText("Tabs Content"); + expect(tabsElement).toHaveClass("custom-tabs-class"); + }); + + describe("defaultActiveKey", () => { + it("when not specified, makes the first tab active", () => { + render( + + +
+ + +
+ + , + ); + + expect( + screen.getByRole("tab", { name: "Tab 1", selected: true }), + ).toBeVisible(); + expect(screen.getByTestId("tab1-content")).toBeVisible(); + }); + + it("when specified, renders the content of the active tab", () => { + render( + + +
+ + +
+ + , + ); + + expect( + screen.getByRole("tab", { name: "Tab 2", selected: true }), + ).toBeVisible(); + expect(screen.getByTestId("tab2-content")).toBeVisible(); + }); + + it("does not render inactive tab content", () => { + render( + + +
+ + +
+ + , + ); + + expect( + screen.getByRole("tab", { name: "Tab 2", selected: false }), + ).toBeVisible(); + expect(screen.queryByTestId("tab2-content")).not.toBeInTheDocument(); + }); + }); + + it("changes active tab correctly", async () => { + render( + + +
+ + +
+ + , + ); + + expect( + screen.getByRole("tab", { name: "Tab 1", selected: true }), + ).toBeVisible(); + expect(screen.getByTestId("tab1-content")).toBeVisible(); + + expect( + screen.getByRole("tab", { name: "Tab 2", selected: false }), + ).toBeVisible(); + expect(screen.queryByTestId("tab2-content")).not.toBeInTheDocument(); + + await userEvent.click(screen.getByRole("tab", { name: "Tab 2" })); + + expect( + screen.getByRole("tab", { name: "Tab 1", selected: false }), + ).toBeVisible(); + expect(screen.queryByTestId("tab1-content")).not.toBeInTheDocument(); + + expect( + screen.getByRole("tab", { name: "Tab 2", selected: true }), + ).toBeVisible(); + expect(screen.getByTestId("tab2-content")).toBeVisible(); + }); + + describe("tabsOrder", () => { + it("when not specified, respects children order", () => { + render( + + + + + , + ); + const tabs = screen.getAllByRole("tab"); + expect(tabs[0]).toHaveTextContent("Tab 1"); + expect(tabs[1]).toHaveTextContent("Tab 2"); + expect(tabs[2]).toHaveTextContent("Tab 3"); + }); + + it("when specified, renders tabs in the requested order", () => { + render( + + + + + , + ); + const tabs = screen.getAllByRole("tab"); + expect(tabs[0]).toHaveTextContent("Tab 2"); + expect(tabs[1]).toHaveTextContent("Tab 1"); + expect(tabs[2]).toHaveTextContent("Tab 3"); + }); + + it("renders the tabs specified in the tabsOrder before the other tabs", () => { + render( + + + + + + , + ); + const tabs = screen.getAllByRole("tab"); + expect(tabs[0]).toHaveTextContent("Tab 3"); + expect(tabs[1]).toHaveTextContent("Tab 1"); + expect(tabs[2]).toHaveTextContent("Tab 2"); + expect(tabs[3]).toHaveTextContent("Tab 4"); + }); + }); +}); + +describe("Tab", () => { + it("renders tab title correctly", () => { + render( + + + , + ); + + expect(screen.getByRole("tab", { name: "Tab 1" })).toBeVisible(); + }); + + it("renders tab content correctly", () => { + render( + + +
+ + , + ); + expect(screen.getByTestId("tab1-content")).toBeVisible(); + }); + + it("assigns className to the Tab component", () => { + render( + + + Tab 1 + + , + ); + + const tab1 = screen.getByText("Tab 1"); + expect(tab1).toHaveClass("custom-tab-class"); + }); +});