full upgrade to dev
This commit is contained in:
@@ -1,12 +1,38 @@
|
|||||||
jest.mock("next/server", () => ({
|
jest.mock("next/server", () => {
|
||||||
NextResponse: jest.fn().mockImplementation(function (body, init) {
|
const mockNextResponse = function (
|
||||||
// Use function and assign to `this` so `new NextResponse(...)` returns an instance with properties
|
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;
|
||||||
|
};
|
||||||
|
|
||||||
this.body = body;
|
return {
|
||||||
|
NextResponse: mockNextResponse,
|
||||||
this.init = init;
|
};
|
||||||
}),
|
});
|
||||||
}));
|
|
||||||
|
|
||||||
import { GET } from "@/app/api/sitemap/route";
|
import { GET } from "@/app/api/sitemap/route";
|
||||||
|
|
||||||
@@ -81,18 +107,19 @@ describe("GET /api/sitemap", () => {
|
|||||||
it("should return a sitemap", async () => {
|
it("should return a sitemap", async () => {
|
||||||
const response = await GET();
|
const response = await GET();
|
||||||
|
|
||||||
expect(response.body).toContain(
|
// 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">',
|
'<urlset xmlns="https://www.sitemaps.org/schemas/sitemap/0.9">',
|
||||||
);
|
);
|
||||||
expect(response.body).toContain("<loc>https://dki.one/</loc>");
|
expect(body).toContain("<loc>https://dki.one/</loc>");
|
||||||
expect(response.body).toContain("<loc>https://dki.one/legal-notice</loc>");
|
expect(body).toContain("<loc>https://dki.one/legal-notice</loc>");
|
||||||
expect(response.body).toContain(
|
expect(body).toContain("<loc>https://dki.one/privacy-policy</loc>");
|
||||||
"<loc>https://dki.one/privacy-policy</loc>",
|
expect(body).toContain(
|
||||||
);
|
|
||||||
expect(response.body).toContain(
|
|
||||||
"<loc>https://dki.one/projects/just-doing-some-testing</loc>",
|
"<loc>https://dki.one/projects/just-doing-some-testing</loc>",
|
||||||
);
|
);
|
||||||
expect(response.body).toContain(
|
expect(body).toContain(
|
||||||
"<loc>https://dki.one/projects/blockchain-based-voting-system</loc>",
|
"<loc>https://dki.one/projects/blockchain-based-voting-system</loc>",
|
||||||
);
|
);
|
||||||
// Note: Headers are not available in test environment
|
// Note: Headers are not available in test environment
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
import http from "http";
|
|
||||||
import NodeCache from "node-cache";
|
import NodeCache from "node-cache";
|
||||||
|
|
||||||
// Use a dynamic import for node-fetch so tests that mock it (via jest.mock) are respected
|
// Use a dynamic import for node-fetch so tests that mock it (via jest.mock) are respected
|
||||||
@@ -9,7 +8,7 @@ async function getFetch() {
|
|||||||
// support both CJS and ESM interop
|
// support both CJS and ESM interop
|
||||||
return (mod as { default: unknown }).default ?? mod;
|
return (mod as { default: unknown }).default ?? mod;
|
||||||
} catch (_err) {
|
} catch (_err) {
|
||||||
return (globalThis as unknown as { fetch: unknown }).fetch;
|
return globalThis.fetch;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -45,11 +44,9 @@ export async function GET() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const agent = new http.Agent({ keepAlive: true });
|
|
||||||
const fetchFn = await getFetch();
|
const fetchFn = await getFetch();
|
||||||
const response = await fetchFn(
|
const response = await (fetchFn as unknown as typeof fetch)(
|
||||||
`${GHOST_API_URL}/ghost/api/content/posts/?key=${GHOST_API_KEY}&limit=all`,
|
`${GHOST_API_URL}/ghost/api/content/posts/?key=${GHOST_API_KEY}&limit=all`,
|
||||||
{ agent: agent as unknown as undefined },
|
|
||||||
);
|
);
|
||||||
const posts: GhostPostsResponse =
|
const posts: GhostPostsResponse =
|
||||||
(await response.json()) as GhostPostsResponse;
|
(await response.json()) as GhostPostsResponse;
|
||||||
|
|||||||
@@ -81,10 +81,9 @@ export async function GET() {
|
|||||||
|
|
||||||
// For tests return a plain object so tests can inspect `.body` easily
|
// For tests return a plain object so tests can inspect `.body` easily
|
||||||
if (process.env.NODE_ENV === "test") {
|
if (process.env.NODE_ENV === "test") {
|
||||||
return {
|
return new NextResponse(xml, {
|
||||||
body: xml,
|
|
||||||
headers: { "Content-Type": "application/xml" },
|
headers: { "Content-Type": "application/xml" },
|
||||||
};
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return new NextResponse(xml, {
|
return new NextResponse(xml, {
|
||||||
@@ -115,7 +114,7 @@ export async function GET() {
|
|||||||
try {
|
try {
|
||||||
const mod = await import("node-fetch");
|
const mod = await import("node-fetch");
|
||||||
const nodeFetch = mod.default ?? mod;
|
const nodeFetch = mod.default ?? mod;
|
||||||
response = await nodeFetch(
|
response = await (nodeFetch as unknown as typeof fetch)(
|
||||||
`${process.env.GHOST_API_URL}/ghost/api/content/posts/?key=${process.env.GHOST_API_KEY}&limit=all`,
|
`${process.env.GHOST_API_URL}/ghost/api/content/posts/?key=${process.env.GHOST_API_KEY}&limit=all`,
|
||||||
);
|
);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|||||||
@@ -4,9 +4,6 @@ import React from "react";
|
|||||||
import { render } from "@testing-library/react";
|
import { render } from "@testing-library/react";
|
||||||
import { ToastProvider } from "@/components/Toast";
|
import { ToastProvider } from "@/components/Toast";
|
||||||
|
|
||||||
// Set test environment
|
|
||||||
process.env.NODE_ENV = "test";
|
|
||||||
|
|
||||||
// Mock Next.js router
|
// Mock Next.js router
|
||||||
jest.mock("next/navigation", () => ({
|
jest.mock("next/navigation", () => ({
|
||||||
useRouter() {
|
useRouter() {
|
||||||
|
|||||||
Reference in New Issue
Block a user