From 1385bf7dcce06d3200b27422a21c16711f342fd9 Mon Sep 17 00:00:00 2001 From: phntxx Date: Mon, 21 Jun 2021 20:16:48 +0200 Subject: [PATCH] Update tests --- src/test/components/imprint.spec.tsx | 13 ++--- src/test/components/searchBar.spec.tsx | 78 +++++++++++++++----------- src/test/components/settings.spec.tsx | 54 +++++++++--------- 3 files changed, 77 insertions(+), 68 deletions(-) diff --git a/src/test/components/imprint.spec.tsx b/src/test/components/imprint.spec.tsx index 51810b5..206460f 100644 --- a/src/test/components/imprint.spec.tsx +++ b/src/test/components/imprint.spec.tsx @@ -27,13 +27,12 @@ const props: IImprintProps = { describe("imprint.tsx", () => { beforeEach(() => { - delete global.window.location; - global.window = Object.create(window); - global.window.location = { - port: "123", - protocol: "http:", - hostname: "localhost", - href: "test", + // @ts-ignore + delete window.location; + + window.location = { + ...location, + reload: jest.fn(), }; }); diff --git a/src/test/components/searchBar.spec.tsx b/src/test/components/searchBar.spec.tsx index c9e80e1..79fae2d 100644 --- a/src/test/components/searchBar.spec.tsx +++ b/src/test/components/searchBar.spec.tsx @@ -2,29 +2,32 @@ import { fireEvent, render } from "@testing-library/react"; import SearchBar, { handleQueryWithProvider, ISearchProviderProps, - ISearchBarProps, + ISearchProps, } from "../../components/searchBar"; -const providers: Array = [ - { - 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 props: 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 setup = () => { - const searchBar = render(); + const searchBar = render(); const input = searchBar.getByTestId("search-input"); const clearButton = searchBar.getByTestId("search-clear"); const submitButton = searchBar.getByTestId("search-submit"); @@ -37,37 +40,44 @@ const setup = () => { }; }; +const location: Location = window.location; + describe("searchBar.tsx", () => { beforeEach(() => { - delete global.window.location; - global.window = Object.create(window); - global.window.location = { - port: "123", - protocol: "http:", - hostname: "localhost", + // @ts-ignore + delete window.location; + + window.location = { + ...location, + reload: jest.fn(), }; }); it("Tests SearchBar rendering", () => { - const { asFragment } = render(); + const { asFragment } = render(); expect(asFragment).toMatchSnapshot(); }); it("Tests handleQueryWithProvider", () => { - providers.forEach((provider: ISearchProviderProps) => { - handleQueryWithProvider(providers, provider.prefix + " test"); + props.providers?.forEach((provider: ISearchProviderProps) => { + handleQueryWithProvider(props, provider.prefix + " test"); expect(window.location.href).toEqual(provider.url + "test"); }); }); it("Tests handleQueryWithProvider default", () => { - handleQueryWithProvider(providers, "test"); - expect(window.location.href).toEqual("https://google.com/search?q=test"); + handleQueryWithProvider(props, "test"); + expect(window.location.href).toEqual(props.defaultProvider + "test"); }); it("Tests handleQueryWithProvider without providers", () => { - handleQueryWithProvider(undefined, "test"); - expect(window.location.href).toEqual("https://google.com/search?q=test"); + const test: ISearchProps = { + 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", () => { @@ -75,7 +85,7 @@ describe("searchBar.tsx", () => { fireEvent.change(input, { target: { value: "test" } }); 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", () => { @@ -96,6 +106,6 @@ describe("searchBar.tsx", () => { fireEvent.click(clearButton); fireEvent.click(submitButton); - expect(window.location.href).toEqual("https://google.com/search?q="); + expect(window.location.href).toEqual(props.defaultProvider); }); }); diff --git a/src/test/components/settings.spec.tsx b/src/test/components/settings.spec.tsx index df9d728..7dcb7df 100644 --- a/src/test/components/settings.spec.tsx +++ b/src/test/components/settings.spec.tsx @@ -9,7 +9,7 @@ import Settings, { SectionHeadline, SelectorStyle, } from "../../components/settings"; -import { ISearchProviderProps } from "../../components/searchBar"; +import { ISearchProps } from "../../components/searchBar"; import { IThemeProps } from "../../lib/theme"; const themes: Array = [ @@ -29,40 +29,43 @@ const themes: Array = [ }, ]; -const providers: Array = [ - { - 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 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, - providers: providers, + search: search, }, { themes: themes, - providers: undefined, + search: undefined, }, { themes: undefined, - providers: providers, + search: search, }, { themes: undefined, - providers: undefined, + search: undefined, }, ]; @@ -115,7 +118,7 @@ describe("settings.tsx", () => { it("Tests settings rendering", () => { propsList.forEach((props) => { const settings = render( - , + , ); expect(settings.asFragment).toMatchSnapshot(); @@ -125,10 +128,7 @@ describe("settings.tsx", () => { // TODO: Finish this test it("Tests theme setting", () => { const settings = render( - , + , ); const toggleButton = settings.getByTestId("toggle-button");