2022-03-09 18:40:21 +00:00
|
|
|
import React from "react";
|
|
|
|
|
import "@testing-library/jest-dom";
|
2023-01-13 11:05:59 +00:00
|
|
|
import { ThemeProvider } from "styled-components";
|
2023-05-19 18:37:06 +00:00
|
|
|
import { render } from "@testing-library/react";
|
2022-03-09 18:40:21 +00:00
|
|
|
import userEvent from "@testing-library/user-event";
|
|
|
|
|
import { Provider } from "react-redux";
|
|
|
|
|
|
|
|
|
|
import { lightTheme } from "selectors/themeSelectors";
|
2022-04-12 10:50:01 +00:00
|
|
|
import store from "store";
|
2023-09-12 14:14:02 +00:00
|
|
|
import { MainContainerWidthToggles } from "./MainContainerWidthToggles";
|
2022-03-09 18:40:21 +00:00
|
|
|
|
2024-02-12 15:59:10 +00:00
|
|
|
async function navigateWithArrowKeys(key: string, noOfPresses: number) {
|
2022-03-09 18:40:21 +00:00
|
|
|
for (let i = 0; i < noOfPresses; i++) {
|
2024-02-12 15:59:10 +00:00
|
|
|
await userEvent.keyboard(key);
|
2022-03-09 18:40:21 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-12 14:14:02 +00:00
|
|
|
describe("<MainContainerWidthToggles />", () => {
|
2022-03-09 18:40:21 +00:00
|
|
|
const getTestComponent = () => (
|
|
|
|
|
<ThemeProvider theme={lightTheme}>
|
|
|
|
|
<Provider store={store}>
|
2023-09-12 14:14:02 +00:00
|
|
|
<MainContainerWidthToggles />
|
2022-03-09 18:40:21 +00:00
|
|
|
</Provider>
|
|
|
|
|
</ThemeProvider>
|
|
|
|
|
);
|
|
|
|
|
|
2024-02-12 15:59:10 +00:00
|
|
|
it("Pressing tab should focus on the first component", async () => {
|
2023-05-19 18:37:06 +00:00
|
|
|
const { container } = render(getTestComponent());
|
2024-02-12 15:59:10 +00:00
|
|
|
await userEvent.tab();
|
2022-03-09 18:40:21 +00:00
|
|
|
|
|
|
|
|
// Should focus on the first component
|
2023-05-19 18:37:06 +00:00
|
|
|
const tab = container.getElementsByClassName(
|
|
|
|
|
"ads-v2-segmented-control__segments-container",
|
|
|
|
|
)[0];
|
2022-03-09 18:40:21 +00:00
|
|
|
expect(tab).toHaveFocus();
|
|
|
|
|
});
|
|
|
|
|
|
2024-02-12 15:59:10 +00:00
|
|
|
it("{ArrowRight} should focus the next item", async () => {
|
2023-05-19 18:37:06 +00:00
|
|
|
const { container } = render(getTestComponent());
|
|
|
|
|
const tabs = container.getElementsByClassName(
|
|
|
|
|
"ads-v2-segmented-control__segments-container",
|
|
|
|
|
);
|
2024-02-12 15:59:10 +00:00
|
|
|
await userEvent.tab();
|
2022-03-09 18:40:21 +00:00
|
|
|
|
2024-02-12 15:59:10 +00:00
|
|
|
await navigateWithArrowKeys("{ArrowRight}", 1);
|
2022-03-09 18:40:21 +00:00
|
|
|
expect(tabs[1]).toHaveFocus();
|
|
|
|
|
|
|
|
|
|
// Focus back on the first element
|
2024-02-12 15:59:10 +00:00
|
|
|
await userEvent.keyboard("{ArrowLeft}");
|
2022-03-09 18:40:21 +00:00
|
|
|
|
|
|
|
|
// Arrow Right after the last item should focus the first item again
|
2024-02-12 15:59:10 +00:00
|
|
|
await navigateWithArrowKeys("{ArrowRight}", tabs.length);
|
2022-03-09 18:40:21 +00:00
|
|
|
expect(tabs[0]).toHaveFocus();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("{ArrowLeft} should focus the next item", async () => {
|
2023-05-19 18:37:06 +00:00
|
|
|
const { container } = render(getTestComponent());
|
|
|
|
|
const tabs = container.getElementsByClassName(
|
|
|
|
|
"ads-v2-segmented-control__segments-container",
|
|
|
|
|
);
|
|
|
|
|
|
2024-02-12 15:59:10 +00:00
|
|
|
await userEvent.tab();
|
2022-03-09 18:40:21 +00:00
|
|
|
|
|
|
|
|
// Arrow Left on the First item should focus on the last item
|
2024-02-12 15:59:10 +00:00
|
|
|
await navigateWithArrowKeys("{ArrowLeft}", 1);
|
2022-03-09 18:40:21 +00:00
|
|
|
expect(tabs[tabs.length - 1]).toHaveFocus();
|
|
|
|
|
|
2024-02-12 15:59:10 +00:00
|
|
|
await navigateWithArrowKeys("{ArrowLeft}", tabs.length - 1);
|
2022-03-09 18:40:21 +00:00
|
|
|
|
|
|
|
|
expect(tabs[0]).toHaveFocus();
|
|
|
|
|
});
|
|
|
|
|
});
|