From 6d059ce3b835f771cfae1d7c1fd52d4f4bef37f5 Mon Sep 17 00:00:00 2001 From: Bastian Meissner Date: Sun, 13 Feb 2022 21:12:05 +0100 Subject: [PATCH] Update tests --- .../__snapshots__/greeter.spec.tsx.snap | 4 +- .../__snapshots__/searchBar.spec.tsx.snap | 4 +- src/test/components/greeter.spec.tsx | 9 ++- src/test/components/searchBar.spec.tsx | 7 +- src/test/components/settings.spec.tsx | 36 ++++----- src/test/lib/useTheme.spec.tsx | 76 +++++++++++++++++++ 6 files changed, 114 insertions(+), 22 deletions(-) create mode 100644 src/test/lib/useTheme.spec.tsx diff --git a/src/test/components/__snapshots__/greeter.spec.tsx.snap b/src/test/components/__snapshots__/greeter.spec.tsx.snap index 7e0c23b..09f6c9f 100644 --- a/src/test/components/__snapshots__/greeter.spec.tsx.snap +++ b/src/test/components/__snapshots__/greeter.spec.tsx.snap @@ -1,3 +1,5 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`Greeter snapshot test 1`] = `[Function]`; +exports[`Greeter snapshot test with properties 1`] = `[Function]`; + +exports[`Greeter snapshot test without properties 1`] = `[Function]`; diff --git a/src/test/components/__snapshots__/searchBar.spec.tsx.snap b/src/test/components/__snapshots__/searchBar.spec.tsx.snap index 8003134..0203d30 100644 --- a/src/test/components/__snapshots__/searchBar.spec.tsx.snap +++ b/src/test/components/__snapshots__/searchBar.spec.tsx.snap @@ -1,3 +1,5 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`searchBar.tsx Tests SearchBar rendering 1`] = `[Function]`; +exports[`searchBar.tsx Tests SearchBar rendering with properties 1`] = `[Function]`; + +exports[`searchBar.tsx Tests searchBar rendering without properties 1`] = `[Function]`; diff --git a/src/test/components/greeter.spec.tsx b/src/test/components/greeter.spec.tsx index f9d04ab..a2adcfe 100644 --- a/src/test/components/greeter.spec.tsx +++ b/src/test/components/greeter.spec.tsx @@ -98,7 +98,12 @@ it("getExtension test", () => { expect(getExtension(31)).toEqual("st"); }); -it("Greeter snapshot test", () => { - const { asFragment } = render(); +it("Greeter snapshot test with properties", () => { + const { asFragment } = render(); + expect(asFragment).toMatchSnapshot(); +}); + +it("Greeter snapshot test without properties", () => { + const { asFragment } = render(); expect(asFragment).toMatchSnapshot(); }); diff --git a/src/test/components/searchBar.spec.tsx b/src/test/components/searchBar.spec.tsx index 6e7c132..3930ac8 100644 --- a/src/test/components/searchBar.spec.tsx +++ b/src/test/components/searchBar.spec.tsx @@ -54,11 +54,16 @@ describe("searchBar.tsx", () => { }; }); - it("Tests SearchBar rendering", () => { + it("Tests SearchBar rendering with properties", () => { const { asFragment } = render(); expect(asFragment).toMatchSnapshot(); }); + it("Tests searchBar rendering without properties", () => { + const { asFragment } = render(); + expect(asFragment).toMatchSnapshot(); + }); + it("Tests handleQueryWithProvider", () => { props.providers?.forEach((provider: ISearchProviderProps) => { handleQueryWithProvider(props, provider.prefix + " test"); diff --git a/src/test/components/settings.spec.tsx b/src/test/components/settings.spec.tsx index 57ea099..77b122c 100644 --- a/src/test/components/settings.spec.tsx +++ b/src/test/components/settings.spec.tsx @@ -9,24 +9,26 @@ import Settings, { SectionHeadline, } from "../../components/settings"; import { ISearchProps } from "../../components/searchBar"; -import { IThemeProps } from "../../lib/useTheme"; +import { IThemeDataProps } from "../../lib/useTheme"; -const themes: Array = [ - { - label: "Classic", - value: 0, - mainColor: "#000000", - accentColor: "#1e272e", - backgroundColor: "#ffffff", - }, - { - label: "Classic", - value: 1, - mainColor: "#000000", - accentColor: "#1e272e", - backgroundColor: "#ffffff", - }, -]; +const themes: IThemeDataProps = { + themes: [ + { + 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=", diff --git a/src/test/lib/useTheme.spec.tsx b/src/test/lib/useTheme.spec.tsx new file mode 100644 index 0000000..250dea4 --- /dev/null +++ b/src/test/lib/useTheme.spec.tsx @@ -0,0 +1,76 @@ +import { getTheme, IThemeProps, setScheme, setTheme } from "../../lib/useTheme"; + +const props: IThemeProps = { + label: "Classic", + value: 0, + mainColor: "#000000", + accentColor: "#1e272e", + backgroundColor: "#ffffff", +}; + +const location: Location = window.location; +const setup = () => { + Object.defineProperty(window, "localStorage", { + value: { + getItem: jest.fn(() => JSON.stringify(props)), + setItem: jest.fn(() => null), + }, + writable: true, + }); + + // @ts-ignore + delete window.location; + + window.location = { + ...location, + reload: jest.fn(), + }; +}; + +describe("theme.tsx", () => { + it("Tests setScheme", () => { + setup(); + + let value = "dark"; + + setScheme(value); + expect(window.localStorage.setItem).toHaveBeenCalledTimes(1); + expect(window.localStorage.setItem).toHaveBeenCalledWith("theme", value); + }); + + it("setTheme light test", () => { + setup(); + + setTheme("light", props); + expect(window.localStorage.setItem).toHaveBeenCalledTimes(2); + expect(window.localStorage.setItem).toHaveBeenCalledWith( + "light-theme", + JSON.stringify(props), + ); + expect(window.location.reload).toHaveBeenCalledTimes(1); + }); + + it("setTheme dark test", () => { + setup(); + + setTheme("dark", props); + expect(window.localStorage.setItem).toHaveBeenCalledTimes(2); + expect(window.localStorage.setItem).toHaveBeenCalledWith( + "dark-theme", + JSON.stringify(props), + ); + expect(window.location.reload).toHaveBeenCalledTimes(1); + }); + + it("Tests getTheme", () => { + setup(); + + let themeTest = getTheme(); + expect(themeTest).toEqual(props); + }); + + it("Tests getTheme with empty parameters", () => { + localStorage.setItem("theme", ""); + expect(getTheme()).toEqual({}); + }); +});