import { fireEvent, render } from "@testing-library/react"; import Settings, { FormContainer, Table, TableRow, TableCell, HeadCell, Section, SectionHeadline, } from "../../components/settings"; import { ISearchProps } from "../../components/searchBar"; import { IThemeProps } from "../../lib/theme"; const themes: Array = [ { label: "Classic", value: 0, mainColor: "#000000", accentColor: "#1e272e", backgroundColor: "#ffffff", }, { label: "Classic", value: 1, mainColor: "#000000", accentColor: "#1e272e", backgroundColor: "#ffffff", }, ]; const search: ISearchProps = { defaultProvider: "https://test.com?q=", providers: [ { name: "Allmusic", url: "https://www.allmusic.com/search/all/", prefix: "/a", }, { name: "Discogs", url: "https://www.discogs.com/search/?q=", prefix: "/di", }, { name: "Duck Duck Go", url: "https://duckduckgo.com/?q=", prefix: "/d", }, ], }; const propsList = [ { themes: themes, search: search, }, { themes: themes, search: undefined, }, { themes: undefined, search: search, }, { themes: undefined, search: undefined, }, ]; describe("settings.tsx", () => { const location: Location = window.location; beforeEach(() => { // @ts-ignore delete window.location; window.location = { ...location, reload: jest.fn(), }; }); it("Tests forms", () => { const { asFragment } = render(); expect(asFragment).toMatchSnapshot(); }); it("Tests tables", () => { const { asFragment } = render( Test Test
, ); expect(asFragment).toMatchSnapshot(); }); it("Tests sections", () => { const { asFragment } = render(
Test
, ); expect(asFragment).toMatchSnapshot(); }); it("Tests settings rendering", () => { propsList.forEach((props) => { const { asFragment } = render( , ); expect(asFragment).toMatchSnapshot(); }); }); it("Tests submit button", () => { const settings = render(); fireEvent.click(settings.getByTestId("button-refresh")); expect(window.location.reload).toHaveBeenCalledTimes(1); }); it("Tests theme selection", () => { const settings = render(); fireEvent.change(settings.getByTestId("select"), { target: { value: 0 } }); fireEvent.click(settings.getByTestId("button-submit")); expect(window.location.reload).toHaveBeenCalledTimes(1); }); it("Tests theme selection", () => { const settings = render(); fireEvent.change(settings.getByTestId("select"), { target: { value: 5 } }); fireEvent.click(settings.getByTestId("button-submit")); expect(window.location.reload).toHaveBeenCalledTimes(0); }); });