Skip to content

Commit

Permalink
feat: add more mock
Browse files Browse the repository at this point in the history
  • Loading branch information
anderson-oki committed Jan 17, 2025
1 parent 2b07443 commit fb2afb2
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 12 deletions.
9 changes: 9 additions & 0 deletions frontend/src/App/app.test.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
import { http } from "msw";
import { HttpResponse } from "msw";
import { describe, it } from "vitest";
import { render } from "@/tests";
import server from "@/tests/mocks/node";
import App from ".";

describe("App", () => {
it("should render without crash", () => {
server.use(
http.get("/api/system/searches", () => {
return HttpResponse.json({});
}),
);

render(<App />);
});
});
12 changes: 0 additions & 12 deletions frontend/src/pages/Settings/components/Section.test.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,12 @@
import { Text } from "@mantine/core";
import { http, HttpResponse } from "msw";
import { describe, it } from "vitest";
import { render, screen } from "@/tests";
import server from "@/tests/mocks/node";
import { Section } from "./Section";

describe("Settings section", () => {
const header = "Section Header";

it("should show header", () => {
server.use(
http.get("/api/system/settings", () => {
return HttpResponse.json({
general: {
theme: "auto",
},
});
}),
);

render(<Section header="Section Header"></Section>);

expect(screen.getByText(header)).toBeDefined();
Expand Down
47 changes: 47 additions & 0 deletions frontend/src/tests/render.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { FunctionComponent } from "react";
import { HttpResponse } from "msw";
import { http } from "msw";
import server from "./mocks/node";
import { render } from ".";

export interface RenderTestCase {
Expand All @@ -8,6 +11,50 @@ export interface RenderTestCase {

export function renderTest(name: string, cases: RenderTestCase[]) {
describe(name, () => {
beforeEach(() => {
server.use(
http.get("/api/movies/history", () => {
return HttpResponse.json({});
}),
http.get("/api/episodes/history", () => {
return HttpResponse.json({});
}),
http.get("/api/system/searches", () => {
return HttpResponse.json({});
}),
http.get("/api/providers", () => {
return HttpResponse.json({
data: [
{
name: "Provider 1",
retry: "-",
status: "History",
},
],
});
}),
http.get("/api/system/languages", () => {
return HttpResponse.json({});
}),
http.get("/api/history/stats", () => {
return HttpResponse.json({
movies: [
{
date: "2025-01-17",
count: 1,
},
],
series: [
{
date: "2025-01-17",
count: 1,
},
],
});
}),
);
});

cases.forEach((element) => {
it(`${element.name.toLowerCase()} should render`, () => {
render(<element.ui />);
Expand Down
12 changes: 12 additions & 0 deletions frontend/src/tests/setup.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/* eslint-disable @typescript-eslint/no-empty-function */

import { http } from "msw";
import { HttpResponse } from "msw";
import { vitest } from "vitest";
import "@testing-library/jest-dom";
import server from "./mocks/node";
Expand Down Expand Up @@ -32,6 +34,16 @@ window.scrollTo = () => {};

beforeAll(() => {
server.listen({ onUnhandledRequest: "error" });

server.use(
http.get("/api/system/settings", () => {
return HttpResponse.json({
general: {
theme: "auto",
},
});
}),
);
});

afterEach(() => server.resetHandlers());
Expand Down

0 comments on commit fb2afb2

Please sign in to comment.