Update tests

This commit is contained in:
phntxx 2021-06-21 20:16:48 +02:00
parent 0ce5d3aea6
commit 1385bf7dcc
3 changed files with 77 additions and 68 deletions

View file

@ -27,13 +27,12 @@ const props: IImprintProps = {
describe("imprint.tsx", () => { describe("imprint.tsx", () => {
beforeEach(() => { beforeEach(() => {
delete global.window.location; // @ts-ignore
global.window = Object.create(window); delete window.location;
global.window.location = {
port: "123", window.location = {
protocol: "http:", ...location,
hostname: "localhost", reload: jest.fn(),
href: "test",
}; };
}); });

View file

@ -2,10 +2,12 @@ import { fireEvent, render } from "@testing-library/react";
import SearchBar, { import SearchBar, {
handleQueryWithProvider, handleQueryWithProvider,
ISearchProviderProps, ISearchProviderProps,
ISearchBarProps, ISearchProps,
} from "../../components/searchBar"; } from "../../components/searchBar";
const providers: Array<ISearchProviderProps> = [ const props: ISearchProps = {
defaultProvider: "https://test.com?q=",
providers: [
{ {
name: "Allmusic", name: "Allmusic",
url: "https://www.allmusic.com/search/all/", url: "https://www.allmusic.com/search/all/",
@ -21,10 +23,11 @@ const providers: Array<ISearchProviderProps> = [
url: "https://duckduckgo.com/?q=", url: "https://duckduckgo.com/?q=",
prefix: "/d", prefix: "/d",
}, },
]; ],
};
const setup = () => { const setup = () => {
const searchBar = render(<SearchBar providers={providers} />); const searchBar = render(<SearchBar search={props} />);
const input = searchBar.getByTestId("search-input"); const input = searchBar.getByTestId("search-input");
const clearButton = searchBar.getByTestId("search-clear"); const clearButton = searchBar.getByTestId("search-clear");
const submitButton = searchBar.getByTestId("search-submit"); const submitButton = searchBar.getByTestId("search-submit");
@ -37,37 +40,44 @@ const setup = () => {
}; };
}; };
const location: Location = window.location;
describe("searchBar.tsx", () => { describe("searchBar.tsx", () => {
beforeEach(() => { beforeEach(() => {
delete global.window.location; // @ts-ignore
global.window = Object.create(window); delete window.location;
global.window.location = {
port: "123", window.location = {
protocol: "http:", ...location,
hostname: "localhost", reload: jest.fn(),
}; };
}); });
it("Tests SearchBar rendering", () => { it("Tests SearchBar rendering", () => {
const { asFragment } = render(<SearchBar providers={providers} />); const { asFragment } = render(<SearchBar search={props} />);
expect(asFragment).toMatchSnapshot(); expect(asFragment).toMatchSnapshot();
}); });
it("Tests handleQueryWithProvider", () => { it("Tests handleQueryWithProvider", () => {
providers.forEach((provider: ISearchProviderProps) => { props.providers?.forEach((provider: ISearchProviderProps) => {
handleQueryWithProvider(providers, provider.prefix + " test"); handleQueryWithProvider(props, provider.prefix + " test");
expect(window.location.href).toEqual(provider.url + "test"); expect(window.location.href).toEqual(provider.url + "test");
}); });
}); });
it("Tests handleQueryWithProvider default", () => { it("Tests handleQueryWithProvider default", () => {
handleQueryWithProvider(providers, "test"); handleQueryWithProvider(props, "test");
expect(window.location.href).toEqual("https://google.com/search?q=test"); expect(window.location.href).toEqual(props.defaultProvider + "test");
}); });
it("Tests handleQueryWithProvider without providers", () => { it("Tests handleQueryWithProvider without providers", () => {
handleQueryWithProvider(undefined, "test"); const test: ISearchProps = {
expect(window.location.href).toEqual("https://google.com/search?q=test"); defaultProvider: "https://test.com?q=",
providers: undefined,
};
handleQueryWithProvider(test, "test");
expect(window.location.href).toEqual(test.defaultProvider + "test");
}); });
it("Tests SearchBar component with default search", () => { it("Tests SearchBar component with default search", () => {
@ -75,7 +85,7 @@ describe("searchBar.tsx", () => {
fireEvent.change(input, { target: { value: "test" } }); fireEvent.change(input, { target: { value: "test" } });
fireEvent.click(submitButton); fireEvent.click(submitButton);
expect(window.location.href).toEqual("https://google.com/search?q=test"); expect(window.location.href).toEqual(props.defaultProvider + "test");
}); });
it("Tests SearchBar component with other search", () => { it("Tests SearchBar component with other search", () => {
@ -96,6 +106,6 @@ describe("searchBar.tsx", () => {
fireEvent.click(clearButton); fireEvent.click(clearButton);
fireEvent.click(submitButton); fireEvent.click(submitButton);
expect(window.location.href).toEqual("https://google.com/search?q="); expect(window.location.href).toEqual(props.defaultProvider);
}); });
}); });

View file

@ -9,7 +9,7 @@ import Settings, {
SectionHeadline, SectionHeadline,
SelectorStyle, SelectorStyle,
} from "../../components/settings"; } from "../../components/settings";
import { ISearchProviderProps } from "../../components/searchBar"; import { ISearchProps } from "../../components/searchBar";
import { IThemeProps } from "../../lib/theme"; import { IThemeProps } from "../../lib/theme";
const themes: Array<IThemeProps> = [ const themes: Array<IThemeProps> = [
@ -29,7 +29,9 @@ const themes: Array<IThemeProps> = [
}, },
]; ];
const providers: Array<ISearchProviderProps> = [ const search: ISearchProps = {
defaultProvider: "https://test.com?q=",
providers: [
{ {
name: "Allmusic", name: "Allmusic",
url: "https://www.allmusic.com/search/all/", url: "https://www.allmusic.com/search/all/",
@ -45,24 +47,25 @@ const providers: Array<ISearchProviderProps> = [
url: "https://duckduckgo.com/?q=", url: "https://duckduckgo.com/?q=",
prefix: "/d", prefix: "/d",
}, },
]; ],
};
const propsList = [ const propsList = [
{ {
themes: themes, themes: themes,
providers: providers, search: search,
}, },
{ {
themes: themes, themes: themes,
providers: undefined, search: undefined,
}, },
{ {
themes: undefined, themes: undefined,
providers: providers, search: search,
}, },
{ {
themes: undefined, themes: undefined,
providers: undefined, search: undefined,
}, },
]; ];
@ -115,7 +118,7 @@ describe("settings.tsx", () => {
it("Tests settings rendering", () => { it("Tests settings rendering", () => {
propsList.forEach((props) => { propsList.forEach((props) => {
const settings = render( const settings = render(
<Settings themes={props.themes} providers={props.providers} />, <Settings themes={props.themes} search={props.search} />,
); );
expect(settings.asFragment).toMatchSnapshot(); expect(settings.asFragment).toMatchSnapshot();
@ -125,10 +128,7 @@ describe("settings.tsx", () => {
// TODO: Finish this test // TODO: Finish this test
it("Tests theme setting", () => { it("Tests theme setting", () => {
const settings = render( const settings = render(
<Settings <Settings themes={propsList[0].themes} search={propsList[0].search} />,
themes={propsList[0].themes}
providers={propsList[0].providers}
/>,
); );
const toggleButton = settings.getByTestId("toggle-button"); const toggleButton = settings.getByTestId("toggle-button");