Files
portfolio/app/__tests__/api/sitemap.test.tsx
2026-01-08 11:40:42 +01:00

128 lines
3.8 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,
};
});
import { GET } from "@/app/api/sitemap/route";
// Mock node-fetch so we don't perform real network requests in tests
jest.mock("node-fetch", () => ({
__esModule: true,
default: jest.fn(() =>
Promise.resolve({
ok: true,
json: () =>
Promise.resolve({
posts: [
{
id: "67ac8dfa709c60000117d312",
title: "Just Doing Some Testing",
meta_description: "Hello bla bla bla bla",
slug: "just-doing-some-testing",
updated_at: "2025-02-13T14:25:38.000+00:00",
},
{
id: "67aaffc3709c60000117d2d9",
title: "Blockchain Based Voting System",
meta_description:
"This project aims to revolutionize voting systems by leveraging blockchain to ensure security, transparency, and immutability.",
slug: "blockchain-based-voting-system",
updated_at: "2025-02-13T16:54:42.000+00:00",
},
],
meta: {
pagination: {
limit: "all",
next: null,
page: 1,
pages: 1,
prev: null,
total: 2,
},
},
}),
}),
),
}));
describe("GET /api/sitemap", () => {
beforeAll(() => {
process.env.GHOST_API_URL = "http://localhost:2368";
process.env.GHOST_API_KEY = "test-api-key";
process.env.NEXT_PUBLIC_BASE_URL = "https://dki.one";
// Provide mock posts via env so route can use them without fetching
process.env.GHOST_MOCK_POSTS = JSON.stringify({
posts: [
{
id: "67ac8dfa709c60000117d312",
title: "Just Doing Some Testing",
meta_description: "Hello bla bla bla bla",
slug: "just-doing-some-testing",
updated_at: "2025-02-13T14:25:38.000+00:00",
},
{
id: "67aaffc3709c60000117d2d9",
title: "Blockchain Based Voting System",
meta_description:
"This project aims to revolutionize voting systems by leveraging blockchain to ensure security, transparency, and immutability.",
slug: "blockchain-based-voting-system",
updated_at: "2025-02-13T16:54:42.000+00:00",
},
],
});
});
it("should return a sitemap", async () => {
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/</loc>");
expect(body).toContain("<loc>https://dki.one/legal-notice</loc>");
expect(body).toContain("<loc>https://dki.one/privacy-policy</loc>");
expect(body).toContain(
"<loc>https://dki.one/projects/just-doing-some-testing</loc>",
);
expect(body).toContain(
"<loc>https://dki.one/projects/blockchain-based-voting-system</loc>",
);
// Note: Headers are not available in test environment
});
});