From 0612312e134a579f9fc6f2f10dd39adf48324272 Mon Sep 17 00:00:00 2001 From: phntxx Date: Mon, 21 Jun 2021 16:52:16 +0200 Subject: [PATCH] Add tests --- .eslintrc.js | 33 +--- src/components/app.tsx | 1 - src/components/icon.tsx | 9 +- src/components/imprint.tsx | 10 +- src/components/modal.tsx | 20 ++- src/components/searchBar.tsx | 59 +++++--- src/components/settings.tsx | 37 +++-- src/lib/fetcher.tsx | 10 +- src/lib/theme.tsx | 11 +- .../__snapshots__/elements.spec.tsx.snap | 9 ++ .../__snapshots__/imprint.spec.tsx.snap | 5 + .../__snapshots__/modal.spec.tsx.snap | 9 ++ .../__snapshots__/searchBar.spec.tsx.snap | 3 + .../__snapshots__/settings.spec.tsx.snap | 15 ++ src/test/components/elements.spec.tsx | 39 +++++ src/test/components/imprint.spec.tsx | 61 ++++++++ src/test/components/modal.spec.tsx | 78 ++++++++++ src/test/components/searchBar.spec.tsx | 101 +++++++++++++ src/test/components/settings.spec.tsx | 143 ++++++++++++++++++ src/test/lib/fetcher.spec.tsx | 46 ++++++ src/test/lib/theme.spec.tsx | 56 ++++--- 21 files changed, 642 insertions(+), 113 deletions(-) create mode 100644 src/test/components/__snapshots__/elements.spec.tsx.snap create mode 100644 src/test/components/__snapshots__/imprint.spec.tsx.snap create mode 100644 src/test/components/__snapshots__/modal.spec.tsx.snap create mode 100644 src/test/components/__snapshots__/searchBar.spec.tsx.snap create mode 100644 src/test/components/__snapshots__/settings.spec.tsx.snap create mode 100644 src/test/components/elements.spec.tsx create mode 100644 src/test/components/imprint.spec.tsx create mode 100644 src/test/components/modal.spec.tsx create mode 100644 src/test/components/searchBar.spec.tsx create mode 100644 src/test/components/settings.spec.tsx create mode 100644 src/test/lib/fetcher.spec.tsx diff --git a/.eslintrc.js b/.eslintrc.js index 8340487..d46a4a1 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -1,34 +1,9 @@ module.exports = { extends: ["eslint:recommended", "plugin:react-hooks/recommended"], rules: { - orderedImports: true, - completedDocs: [ - true, - { - enums: true, - functions: { - visibilities: ["exported"], - }, - interfaces: { - visibilities: ["exported"], - }, - methods: { - tags: { - content: {}, - existence: ["inheritdoc", "override"], - }, - }, - types: { - visibilities: ["exported"], - }, - variables: { - visibilities: ["exported"], - }, - }, - ], - maxClassesPerFile: false, - maxLineLength: false, - memberOrdering: false, - variableName: false, + maxClassesPerFile: 0, + maxLineLength: 0, + memberOrdering: 0, + variableName: 0, }, }; diff --git a/src/components/app.tsx b/src/components/app.tsx index acde868..9c48be3 100644 --- a/src/components/app.tsx +++ b/src/components/app.tsx @@ -1,4 +1,3 @@ -import { useEffect } from "react"; import Icon from "./icon"; import styled from "styled-components"; import selectedTheme from "../lib/theme"; diff --git a/src/components/icon.tsx b/src/components/icon.tsx index 18f7f21..a3fb907 100644 --- a/src/components/icon.tsx +++ b/src/components/icon.tsx @@ -8,6 +8,7 @@ interface IIconProps { } interface IIconButtonProps { + testid?: string; icon: string; onClick: (e: React.FormEvent) => void; } @@ -38,7 +39,7 @@ const IconContainer = styled.i` text-rendering: optimizeLegibility; -moz-osx-font-smoothing: grayscale; font-feature-settings: "liga"; - font-size: ${(props) => props.theme}; + font-size: ${(props) => props.about}; color: ${(props) => props.color}; `; @@ -48,7 +49,7 @@ const IconContainer = styled.i` * @returns {React.ReactNode} the icon node */ export const Icon = ({ name, size }: IIconProps) => ( - + {name} ); @@ -58,8 +59,8 @@ export const Icon = ({ name, size }: IIconProps) => ( * @param {IIconProps} props - The props of the given IconButton * @returns {React.ReactNode} the icon button node */ -export const IconButton = ({ icon, onClick }: IIconButtonProps) => ( - +export const IconButton = ({ testid, icon, onClick }: IIconButtonProps) => ( + ); diff --git a/src/components/imprint.tsx b/src/components/imprint.tsx index 2f14782..ad2f828 100644 --- a/src/components/imprint.tsx +++ b/src/components/imprint.tsx @@ -41,7 +41,7 @@ export interface IImprintProps { text: string; } -interface IImprintComponentProps { +export interface IImprintComponentProps { imprint: IImprintProps; } @@ -59,7 +59,7 @@ interface IImprintFieldProps { * @param {IImprintFieldComponentProps} props data for the field * @returns {React.ReactNode} the imprint field component */ -const ImprintField = ({ field }: IImprintFieldComponentProps) => ( +export const ImprintField = ({ field }: IImprintFieldComponentProps) => ( {field.text} ); @@ -82,8 +82,10 @@ const Imprint = ({ imprint }: IImprintComponentProps) => ( condition={!window.location.href.endsWith("#imprint")} onClose={() => { if (window.location.href.endsWith("#imprint")) { - let location = window.location.href.replace("#imprint", ""); - window.location.href = location; + window.location.href = window.location.href.replace( + "#imprint", + "", + ); } }} > diff --git a/src/components/modal.tsx b/src/components/modal.tsx index 5691220..8b18bea 100644 --- a/src/components/modal.tsx +++ b/src/components/modal.tsx @@ -38,7 +38,7 @@ const TitleContainer = styled.div` justify-content: space-between; `; -interface IModalProps { +export interface IModalProps { element: string; icon?: string; text?: string; @@ -72,15 +72,27 @@ const Modal = ({ return ( <> {element === "icon" && ( - closeModal()} /> + closeModal()} + /> )} - {element === "text" && closeModal()}>{text}} + {element === "text" && ( + closeModal()}> + {text} + + )} diff --git a/src/components/searchBar.tsx b/src/components/searchBar.tsx index 2b1a95b..8e200a8 100644 --- a/src/components/searchBar.tsx +++ b/src/components/searchBar.tsx @@ -43,10 +43,35 @@ export interface ISearchProviderProps { prefix: string; } -interface ISearchBarProps { +export interface ISearchBarProps { providers: Array | undefined; } +export const handleQueryWithProvider = ( + providers: Array | undefined, + query: string, +) => { + let queryArray: Array = query.split(" "); + let prefix: string = queryArray[0]; + + queryArray.shift(); + + let searchQuery: string = queryArray.join(" "); + + let providerFound: boolean = false; + if (providers) { + providers.forEach((provider: ISearchProviderProps) => { + if (provider.prefix === prefix) { + providerFound = true; + window.location.href = provider.url + searchQuery; + } + }); + } + + if (!providerFound) + window.location.href = "https://google.com/search?q=" + query; +}; + /** * Renders a search bar * @param {ISearchBarProps} props - The search providers for the search bar to use @@ -61,7 +86,7 @@ const SearchBar = ({ providers }: ISearchBarProps) => { var query: string = input || ""; if (query.split(" ")[0].includes("/")) { - handleQueryWithProvider(query); + handleQueryWithProvider(providers, query); } else { window.location.href = "https://google.com/search?q=" + query; } @@ -69,32 +94,11 @@ const SearchBar = ({ providers }: ISearchBarProps) => { e.preventDefault(); }; - const handleQueryWithProvider = (query: string) => { - let queryArray: Array = query.split(" "); - let prefix: string = queryArray[0]; - - queryArray.shift(); - - let searchQuery: string = queryArray.join(" "); - - let providerFound: boolean = false; - if (providers) { - providers.forEach((provider: ISearchProviderProps) => { - if (provider.prefix === prefix) { - providerFound = true; - window.location.href = provider.url + searchQuery; - } - }); - } - - if (!providerFound) - window.location.href = "https://google.com/search?q=" + query; - }; - return ( handleSearchQuery(e)}> ) => setInput(e.target.value) @@ -102,12 +106,17 @@ const SearchBar = ({ providers }: ISearchBarProps) => { > setInput("")} hidden={buttonsHidden} > Clear - diff --git a/src/components/settings.tsx b/src/components/settings.tsx index 56bbbb6..8e507fc 100644 --- a/src/components/settings.tsx +++ b/src/components/settings.tsx @@ -4,47 +4,51 @@ import styled from "styled-components"; import Select, { ValueType } from "react-select"; import { ISearchProviderProps } from "./searchBar"; -import selectedTheme, { setTheme, IThemeProps } from "../lib/theme"; +import selectedTheme, { + defaultTheme, + setTheme, + IThemeProps, +} from "../lib/theme"; import { Button, SubHeadline } from "./elements"; import Modal from "./modal"; -const FormContainer = styled.div` +export const FormContainer = styled.div` display: grid; grid-template-columns: auto auto auto; `; -const Table = styled.table` +export const Table = styled.table` font-weight: 400; background: none; color: ${selectedTheme.mainColor}; `; -const TableRow = styled.tr` +export const TableRow = styled.tr` border-bottom: 1px solid ${selectedTheme.mainColor}; `; -const TableCell = styled.td` +export const TableCell = styled.td` background: none; padding-top: 0.5rem; `; -const HeadCell = styled.th` +export const HeadCell = styled.th` font-weight: 700; background: none; `; -const Section = styled.div` +export const Section = styled.div` padding: 1rem 0; `; -const SectionHeadline = styled(SubHeadline)` +export const SectionHeadline = styled(SubHeadline)` width: 100%; border-bottom: 1px solid ${selectedTheme.accentColor}; margin-bottom: 0.5rem; `; -const SelectorStyle: any = { +export const SelectorStyle: any = { container: (base: any): any => ({ ...base, margin: "0 2px", @@ -113,7 +117,7 @@ interface ISettingsProps { * @param {Array} providers - the list of search providers */ const Settings = ({ themes, providers }: ISettingsProps) => { - const [newTheme, setNewTheme] = useState(); + const [newTheme, setNewTheme] = useState(defaultTheme); if (themes || providers) { return ( @@ -123,6 +127,7 @@ const Settings = ({ themes, providers }: ISettingsProps) => { Theme: