Add tests

This commit is contained in:
phntxx 2021-06-21 16:52:16 +02:00
parent aa61b2fb76
commit 0612312e13
21 changed files with 642 additions and 113 deletions

View file

@ -17,7 +17,7 @@ const inProduction = process.env.NODE_ENV === "production";
* @returns - The response in JSON
* @throws - Error with given error message if request failed
*/
const handleResponse = (response: Response) => {
export const handleResponse = (response: Response) => {
if (response.ok) return response.json();
throw new Error(errorMessage);
};
@ -56,7 +56,7 @@ export interface IGreeterDataProps {
/**
* Default values for the respective state variables
*/
const defaults = {
export const defaults = {
app: {
categories: [],
apps: [],
@ -143,7 +143,7 @@ const defaults = {
* @param {string} type - The type of fetch request that threw an error
* @param {Error} error - The error itself
*/
const handleError = (status: string, error: Error) => {
export const handleError = (status: string, error: Error) => {
switch (status) {
case "apps":
return { ...defaults.app, error: error.message };
@ -165,7 +165,7 @@ const handleError = (status: string, error: Error) => {
/**
* Fetches all of the data by doing fetch requests (only available in production)
*/
const fetchProduction = Promise.all([
export const fetchProduction = Promise.all([
fetch("/data/apps.json")
.then(handleResponse)
.catch((error: Error) => handleError("apps", error)),
@ -189,7 +189,7 @@ const fetchProduction = Promise.all([
/**
* Fetches all of the data by importing it (only available in development)
*/
const fetchDevelopment = Promise.all([
export const fetchDevelopment = Promise.all([
import("../data/apps.json"),
import("../data/bookmarks.json"),
import("../data/search.json"),

View file

@ -6,7 +6,7 @@ export interface IThemeProps {
backgroundColor: string;
}
const defaultTheme: IThemeProps = {
export const defaultTheme: IThemeProps = {
label: "Classic",
value: 0,
mainColor: "#000000",
@ -18,8 +18,8 @@ const defaultTheme: IThemeProps = {
* Writes a given theme into localStorage
* @param {string} theme - the theme that shall be saved (in stringified JSON)
*/
export const setTheme = (theme: string) => {
if (theme !== undefined) localStorage.setItem("theme", theme);
export const setTheme = (theme: IThemeProps) => {
localStorage.setItem("theme", JSON.stringify(theme));
window.location.reload();
};
@ -30,9 +30,8 @@ export const setTheme = (theme: string) => {
export const getTheme = (): IThemeProps => {
let selectedTheme = defaultTheme;
if (localStorage.getItem("theme") !== null) {
selectedTheme = JSON.parse(localStorage.getItem("theme") || "{}");
}
let theme = localStorage.getItem("theme");
if (theme !== null) selectedTheme = JSON.parse(theme || "{}");
return selectedTheme;
};