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

@ -0,0 +1,46 @@
import { ok } from "assert";
import useFetcher, {
defaults,
handleResponse,
handleError,
fetchProduction,
fetchDevelopment,
} from "../../lib/fetcher";
describe("fetcher.tsx", () => {
it("Tests handleResponse", () => {});
it("Tests handleError", () => {
expect(handleError("apps", Error("Test!"))).toEqual({
...defaults.app,
error: "Test!",
});
expect(handleError("bookmark", Error("Test!"))).toEqual({
...defaults.bookmark,
error: "Test!",
});
expect(handleError("searchProvider", Error("Test!"))).toEqual({
...defaults.search,
error: "Test!",
});
expect(handleError("theme", Error("Test!"))).toEqual({
...defaults.theme,
error: "Test!",
});
expect(handleError("imprint", Error("Test!"))).toEqual({
...defaults.imprint,
error: "Test!",
});
expect(handleError("greeter", Error("Test!"))).toEqual({
...defaults.greeter,
error: "Test!",
});
expect(handleError("", Error("Test!"))).toEqual(undefined);
});
});

View file

@ -1,8 +1,4 @@
import selectedTheme, {
getTheme,
IThemeProps,
setTheme,
} from "../../lib/theme";
import { getTheme, IThemeProps, setTheme } from "../../lib/theme";
const props: IThemeProps = {
label: "Classic",
@ -12,33 +8,47 @@ const props: IThemeProps = {
backgroundColor: "#ffffff",
};
const theme = JSON.stringify(props);
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", () => {
beforeEach(() => {
Object.defineProperty(window, "localStorage", {
value: {
getItem: jest.fn(() => null),
setItem: jest.fn(() => null),
},
writable: true,
});
});
it("setTheme test", () => {
setTheme(theme);
setup();
setTheme(props);
expect(window.localStorage.setItem).toHaveBeenCalledTimes(1);
expect(window.localStorage.setItem).toHaveBeenCalledWith("theme", theme);
expect(window.localStorage.setItem).toHaveBeenCalledWith(
"theme",
JSON.stringify(props),
);
expect(window.location.reload).toHaveBeenCalledTimes(1);
});
it("getTheme test", () => {
const themeTest = getTheme();
it("Tests getTheme", () => {
setup();
let themeTest = getTheme();
expect(themeTest).toEqual(props);
});
it("selectedTheme test", () => {
const themeTest = selectedTheme;
expect(themeTest).toEqual(props);
it("Tests getTheme with empty parameters", () => {
localStorage.setItem("theme", "");
expect(getTheme()).toEqual({});
});
});