Files
portfolio/app/__tests__/api/sitemap.test.tsx
2026-01-12 15:27:35 +00:00

81 lines
2.1 KiB
TypeScript

jest.mock("next/server", () => {
const mockNextResponse = function (
body: string | object,
init?: { headers?: Record<string, string> },
) {
// Return an object that mimics NextResponse
const mockResponse = {
body,
init,
text: async () => {
if (typeof body === "string") {
return body;
} else if (body && typeof body === "object") {
return JSON.stringify(body);
}
return "";
},
json: async () => {
if (typeof body === "object") {
return body;
}
try {
return JSON.parse(body as string);
} catch {
return {};
}
},
};
return mockResponse;
};
return {
NextResponse: mockNextResponse,
};
});
jest.mock("@/lib/sitemap", () => ({
getSitemapEntries: jest.fn(async () => [
{
url: "https://dki.one/en",
lastModified: "2025-01-01T00:00:00.000Z",
},
{
url: "https://dki.one/de",
lastModified: "2025-01-01T00:00:00.000Z",
},
{
url: "https://dki.one/en/projects/blockchain-based-voting-system",
lastModified: "2025-02-13T16:54:42.000Z",
},
{
url: "https://dki.one/de/projects/blockchain-based-voting-system",
lastModified: "2025-02-13T16:54:42.000Z",
},
]),
generateSitemapXml: jest.fn(
() =>
'<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="https://www.sitemaps.org/schemas/sitemap/0.9"><url><loc>https://dki.one/en</loc></url></urlset>',
),
}));
describe("GET /api/sitemap", () => {
beforeAll(() => {
process.env.NEXT_PUBLIC_BASE_URL = "https://dki.one";
});
it("should return a sitemap", async () => {
const { GET } = await import("@/app/api/sitemap/route");
const response = await GET();
// Get the body text from the NextResponse
const body = await response.text();
expect(body).toContain(
'<urlset xmlns="https://www.sitemaps.org/schemas/sitemap/0.9">',
);
expect(body).toContain("<loc>https://dki.one/en</loc>");
// Note: Headers are not available in test environment
});
});