import "@testing-library/jest-dom";
import { GET } from "@/app/sitemap.xml/route";
jest.mock("next/server", () => ({
NextResponse: jest.fn().mockImplementation(function (body, init) {
this.body = body;
this.init = init;
}),
}));
// Sitemap XML used by node-fetch mock
const sitemapXml = `
https://dki.one/
https://dki.one/legal-notice
https://dki.one/privacy-policy
https://dki.one/projects/just-doing-some-testing
https://dki.one/projects/blockchain-based-voting-system
`;
// Mock node-fetch for sitemap endpoint (hoisted by Jest)
jest.mock("node-fetch", () => ({
__esModule: true,
default: jest.fn((_url: string) =>
Promise.resolve({ ok: true, text: () => Promise.resolve(sitemapXml) }),
),
}));
describe("Sitemap Component", () => {
beforeAll(() => {
process.env.NEXT_PUBLIC_BASE_URL = "https://dki.one";
// Provide sitemap XML directly so route uses it without fetching
process.env.GHOST_MOCK_SITEMAP = sitemapXml;
// Mock global.fetch too, to avoid any network calls
global.fetch = jest.fn().mockImplementation((url: string) => {
if (url.includes("/api/sitemap")) {
return Promise.resolve({
ok: true,
text: () => Promise.resolve(sitemapXml),
});
}
return Promise.reject(new Error(`Unknown URL: ${url}`));
});
});
it("should render the sitemap XML", async () => {
const response = await GET();
expect(response.body).toContain(
'',
);
expect(response.body).toContain("https://dki.one/");
expect(response.body).toContain("https://dki.one/legal-notice");
expect(response.body).toContain(
"https://dki.one/privacy-policy",
);
expect(response.body).toContain(
"https://dki.one/projects/just-doing-some-testing",
);
expect(response.body).toContain(
"https://dki.one/projects/blockchain-based-voting-system",
);
// Note: Headers are not available in test environment
});
});