mirror of
https://github.com/OHTU-German-learning-website/OHTU-German-learning-website.git
synced 2026-10-12 19:13:51 +03:00
Merge pull request #431 from OHTU-German-learning-website/411-implements-tests-for-editor-component
added tests for editor functionality saving
This commit is contained in:
@@ -1,22 +1,29 @@
|
||||
import { describe, it, expect, vi, beforeEach } from "vitest";
|
||||
import DOMPurify from "dompurify";
|
||||
|
||||
vi.mock("@/backend/html-services", () => ({
|
||||
getHTMLContent: vi.fn(),
|
||||
updateHTMLContent: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("dompurify", () => ({
|
||||
__esModule: true,
|
||||
default: () => ({ sanitize: (html) => html }),
|
||||
default: vi.fn(() => ({ sanitize: (html) => html })),
|
||||
}));
|
||||
|
||||
vi.mock("jsdom", () => ({
|
||||
JSDOM: vi.fn().mockImplementation(() => ({ window: {} })),
|
||||
}));
|
||||
|
||||
import { GET } from "./route";
|
||||
vi.mock("@/backend/middleware/withAuth", () => ({
|
||||
withAuth: vi.fn((handler) => handler),
|
||||
}));
|
||||
|
||||
import { GET, PUT } from "./route";
|
||||
import { useTestDatabase } from "@/backend/test/testdb";
|
||||
import { useTestRequest } from "@/backend/test/mock-request";
|
||||
import { getHTMLContent } from "@/backend/html-services";
|
||||
import { updateHTMLContent } from "@/backend/html-services";
|
||||
|
||||
describe("GET /api/html-content/[type]/[id]", () => {
|
||||
useTestDatabase();
|
||||
@@ -60,3 +67,66 @@ describe("GET /api/html-content/[type]/[id]", () => {
|
||||
expect(getHTMLContent).toHaveBeenCalledWith("", 3);
|
||||
});
|
||||
});
|
||||
|
||||
describe("PUT /api/html-content/[type]/[id]", () => {
|
||||
useTestDatabase();
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
const putRequest = async (type, id, content) => {
|
||||
const { mockPut } = useTestRequest();
|
||||
const req = mockPut(`/api/html-content/${type}/${id}`, {
|
||||
body: { content },
|
||||
});
|
||||
req.json = async () => ({ content });
|
||||
return PUT(req, { params: { type, id } });
|
||||
};
|
||||
|
||||
it("should sanitize and update HTML content", async () => {
|
||||
updateHTMLContent.mockResolvedValueOnce(true);
|
||||
|
||||
const response = await putRequest("resources", 1, "<p>Mocked HTML</p>");
|
||||
expect(response.status).toBe(200);
|
||||
expect(updateHTMLContent).toHaveBeenCalledWith(
|
||||
"learning_pages_html",
|
||||
1,
|
||||
"<p>Mocked HTML</p>"
|
||||
);
|
||||
});
|
||||
|
||||
it("should sanitize HTML using DOMPurify before saving", async () => {
|
||||
const sanitizeMock = vi.fn(() => "<p>clean</p>");
|
||||
vi.mocked(DOMPurify).mockReturnValueOnce({ sanitize: sanitizeMock });
|
||||
updateHTMLContent.mockResolvedValueOnce(true);
|
||||
|
||||
const response = await putRequest("resources", 1, "<script><kbd></script>");
|
||||
|
||||
expect(sanitizeMock).toHaveBeenCalledWith("<script><kbd></script>", {
|
||||
ADD_ATTR: ["target"],
|
||||
});
|
||||
expect(updateHTMLContent).toHaveBeenCalledWith(
|
||||
"learning_pages_html",
|
||||
1,
|
||||
"<p>clean</p>"
|
||||
);
|
||||
expect(response.status).toBe(200);
|
||||
});
|
||||
|
||||
it("should allow an admin to save an empty editor file", async () => {
|
||||
updateHTMLContent.mockResolvedValueOnce(true);
|
||||
const response = await putRequest("resources", 1, "");
|
||||
expect(updateHTMLContent).toHaveBeenCalledWith(
|
||||
"learning_pages_html",
|
||||
1,
|
||||
""
|
||||
);
|
||||
expect(response.status).toBe(200);
|
||||
});
|
||||
|
||||
it("should return error if updateHTMLContent fails", async () => {
|
||||
updateHTMLContent.mockResolvedValueOnce(false);
|
||||
const response = await putRequest("resources", 1, "failure");
|
||||
expect(response.status).toBe(400);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user