import * as React from "react"; import { render, focus, click, press } from "reakit-test-utils"; import { Tab, TabList, TabPanel, useTabState } from ".."; test("first tab is selected", () => { const Test = () => { const tab = useTabState(); return ( <> tab1 tab2 tab3 tabpanel1 tabpanel2 tabpanel3 ); }; const { getByText: $ } = render(); expect($("tabpanel1")).toBeVisible(); expect($("tabpanel2")).not.toBeVisible(); expect($("tabpanel3")).not.toBeVisible(); }); test("first tab is selected with tablist below", () => { const Test = () => { const tab = useTabState(); return ( <> tabpanel1 tabpanel2 tabpanel3 tab1 tab2 tab3 ); }; const { getByText: $ } = render(); expect($("tabpanel1")).toBeVisible(); expect($("tabpanel2")).not.toBeVisible(); expect($("tabpanel3")).not.toBeVisible(); }); test("focusing tab reveals the panel", () => { const Test = () => { const tab = useTabState(); return ( <> tab1 tab2 tab3 tabpanel1 tabpanel2 tabpanel3 ); }; const { getByText: $ } = render(); expect($("tabpanel2")).not.toBeVisible(); focus($("tab2")); expect($("tabpanel1")).not.toBeVisible(); expect($("tabpanel2")).toBeVisible(); expect($("tabpanel3")).not.toBeVisible(); }); test("focusing tab does not reveal the panel when manual is true", () => { const Test = () => { const tab = useTabState({ manual: true }); return ( <> tab1 tab2 tab3 tabpanel1 tabpanel2 tabpanel3 ); }; const { getByText: $ } = render(); expect($("tabpanel2")).not.toBeVisible(); focus($("tab2")); expect($("tabpanel2")).not.toBeVisible(); }); test("clicking on tab reveals the panel", () => { const Test = () => { const tab = useTabState(); return ( <> tab1 tab2 tab3 tabpanel1 tabpanel2 tabpanel3 ); }; const { getByText: $ } = render(); expect($("tabpanel3")).not.toBeVisible(); click($("tab3")); expect($("tabpanel3")).toBeVisible(); }); test("clicking on tab reveals the panel when manual is true", () => { const Test = () => { const tab = useTabState({ manual: true }); return ( <> tab1 tab2 tab3 tabpanel1 tabpanel2 tabpanel3 ); }; const { getByText: $ } = render(); expect($("tabpanel3")).not.toBeVisible(); click($("tab3")); expect($("tabpanel3")).toBeVisible(); }); test("tab is selected based on the value of selectedId", () => { const Test = () => { const tab = useTabState({ selectedId: "tab2" }); return ( <> tab1 tab2 tab3 tabpanel1 tabpanel2 tabpanel3 ); }; const { getByText: $ } = render(); expect($("tabpanel1")).not.toBeVisible(); expect($("tabpanel2")).toBeVisible(); expect($("tabpanel3")).not.toBeVisible(); }); test("tab is selected based on the value of selectedId with tablist below", () => { const Test = () => { const tab = useTabState({ selectedId: "tab2" }); return ( <> tabpanel1 tabpanel2 tabpanel3 tab1 tab2 tab3 ); }; const { getByText: $ } = render(); expect($("tabpanel1")).not.toBeVisible(); expect($("tabpanel2")).toBeVisible(); expect($("tabpanel3")).not.toBeVisible(); }); test("no tab is selected if selectedId is null", () => { const Test = () => { const tab = useTabState({ selectedId: null }); return ( <> tab1 tab2 tab3 tabpanel1 tabpanel2 tabpanel3 ); }; const { getByText: $ } = render(); expect($("tabpanel1")).not.toBeVisible(); expect($("tabpanel2")).not.toBeVisible(); expect($("tabpanel3")).not.toBeVisible(); }); test("select the last selected tab when the current one is unmounted", () => { const Test = ({ renderTab2 = false }) => { const tab = useTabState(); return ( <> tab1 {renderTab2 && tab2} tab3 tab4 tabpanel1 {renderTab2 && tabpanel2} tabpanel3 tabpanel4 ); }; const { getByText: $, rerender } = render(); expect($("tabpanel1")).toBeVisible(); click($("tab4")); expect($("tabpanel4")).toBeVisible(); click($("tab2")); expect($("tabpanel2")).toBeVisible(); rerender(); expect($("tabpanel4")).toBeVisible(); }); test("select the last selected tab when the current one is unmounted with tablist below", () => { const Test = ({ renderTab2 = false }) => { const tab = useTabState(); return ( <> tabpanel1 {renderTab2 && tabpanel2} tabpanel3 tabpanel4 tab1 {renderTab2 && tab2} tab3 tab4 ); }; const { getByText: $, rerender } = render(); expect($("tabpanel1")).toBeVisible(); click($("tab4")); expect($("tabpanel4")).toBeVisible(); click($("tab2")); expect($("tabpanel2")).toBeVisible(); rerender(); expect($("tabpanel4")).toBeVisible(); }); test("select the last selected tab when the current one is unmounted when manual is true", () => { const Test = ({ renderTab2 = false }) => { const tab = useTabState({ manual: true }); return ( <> tab1 {renderTab2 && tab2} tab3 tab4 tabpanel1 {renderTab2 && tabpanel2} tabpanel3 tabpanel4 ); }; const { getByText: $, rerender } = render(); expect($("tabpanel1")).toBeVisible(); click($("tab4")); expect($("tabpanel4")).toBeVisible(); click($("tab2")); expect($("tabpanel2")).toBeVisible(); rerender(); expect($("tabpanel4")).toBeVisible(); }); test("select the last selected tab when the current one is unmounted when id is passed", () => { const Test = ({ renderTab2 = false }) => { const tab = useTabState({ manual: true }); return ( <> tab1 {renderTab2 && tab2} tab3 tab4 tabpanel1 {renderTab2 && tabpanel2} tabpanel3 tabpanel4 ); }; const { getByText: $, rerender } = render(); expect($("tabpanel1")).toBeVisible(); click($("tab4")); expect($("tabpanel4")).toBeVisible(); click($("tab2")); expect($("tabpanel2")).toBeVisible(); rerender(); expect($("tabpanel4")).toBeVisible(); }); test("can't select disabled tab", () => { const Test = () => { const tab = useTabState(); return ( <> tab1 tab2 tab3 tabpanel1 tabpanel2 tabpanel3 ); }; const { getByText: $ } = render(); click($("tab2")); expect($("tab2")).not.toHaveFocus(); expect($("tabpanel2")).not.toBeVisible(); press.Tab(); expect($("tab1")).toHaveFocus(); expect($("tabpanel1")).toBeVisible(); press.ArrowRight(); expect($("tab2")).toHaveFocus(); expect($("tabpanel2")).not.toBeVisible(); }); test("tab panel with tabId different order", () => { const Test = () => { const tab = useTabState({ baseId: "base" }); return ( <> tab1 tab2 tab3 tabpanel2 tabpanel1 tabpanel3 ); }; const { container } = render(); expect(container).toMatchInlineSnapshot(`
tabpanel1
`); });