132 lines
4.3 KiB
TypeScript
132 lines
4.3 KiB
TypeScript
import { Test, TestingModule } from "@nestjs/testing";
|
|
import { RevalidationService } from "./revalidation.service";
|
|
|
|
describe("RevalidationService", () => {
|
|
let service: RevalidationService;
|
|
const originalEnv = process.env;
|
|
|
|
beforeEach(async () => {
|
|
process.env = { ...originalEnv };
|
|
process.env.REVALIDATION_SECRET = "test-secret-123";
|
|
process.env.FRONTEND_INTERNAL_URL = "http://frontend_mock:8080";
|
|
|
|
const module: TestingModule = await Test.createTestingModule({
|
|
providers: [RevalidationService],
|
|
}).compile();
|
|
|
|
service = module.get<RevalidationService>(RevalidationService);
|
|
});
|
|
|
|
afterEach(() => {
|
|
process.env = originalEnv;
|
|
jest.restoreAllMocks();
|
|
});
|
|
|
|
it("should be defined", () => {
|
|
expect(service).toBeDefined();
|
|
});
|
|
|
|
describe("revalidateTag", () => {
|
|
it("should send POST request with tag and secret header and return true on 200", async () => {
|
|
const mockFetch = jest.spyOn(global, "fetch" as any).mockResolvedValue({
|
|
ok: true,
|
|
status: 200,
|
|
} as any);
|
|
|
|
const result = await service.revalidateTag("products");
|
|
|
|
expect(result).toBe(true);
|
|
expect(mockFetch).toHaveBeenCalledWith(
|
|
"http://frontend_mock:8080/api/revalidate",
|
|
expect.objectContaining({
|
|
method: "POST",
|
|
headers: expect.objectContaining({
|
|
"x-revalidate-secret": "test-secret-123",
|
|
}),
|
|
body: JSON.stringify({ tag: "products", secret: "test-secret-123" }),
|
|
}),
|
|
);
|
|
});
|
|
|
|
it("should return false if frontend responds with non-ok status", async () => {
|
|
jest.spyOn(global, "fetch" as any).mockResolvedValue({
|
|
ok: false,
|
|
status: 500,
|
|
statusText: "Internal Error",
|
|
} as any);
|
|
|
|
const result = await service.revalidateTag("products");
|
|
expect(result).toBe(false);
|
|
});
|
|
|
|
it("should catch network errors gracefully and return false", async () => {
|
|
jest
|
|
.spyOn(global, "fetch" as any)
|
|
.mockRejectedValue(new Error("Connection refused"));
|
|
|
|
const result = await service.revalidateTag("products");
|
|
expect(result).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe("revalidatePath", () => {
|
|
it("should send POST request with path and secret and return true on success", async () => {
|
|
const mockFetch = jest.spyOn(global, "fetch" as any).mockResolvedValue({
|
|
ok: true,
|
|
status: 200,
|
|
} as any);
|
|
|
|
const result = await service.revalidatePath("/shop");
|
|
|
|
expect(result).toBe(true);
|
|
expect(mockFetch).toHaveBeenCalledWith(
|
|
"http://frontend_mock:8080/api/revalidate",
|
|
expect.objectContaining({
|
|
method: "POST",
|
|
body: JSON.stringify({ path: "/shop", secret: "test-secret-123" }),
|
|
}),
|
|
);
|
|
});
|
|
});
|
|
|
|
describe("revalidateProduct", () => {
|
|
it("should trigger all tags and paths for product slug and artNo", async () => {
|
|
const tagSpy = jest
|
|
.spyOn(service, "revalidateTag")
|
|
.mockResolvedValue(true);
|
|
const pathSpy = jest
|
|
.spyOn(service, "revalidatePath")
|
|
.mockResolvedValue(true);
|
|
|
|
await service.revalidateProduct("canivita-tonic", "ART-100");
|
|
|
|
expect(tagSpy).toHaveBeenCalledWith("products");
|
|
expect(tagSpy).toHaveBeenCalledWith("home-data");
|
|
expect(tagSpy).toHaveBeenCalledWith("product-canivita-tonic");
|
|
expect(tagSpy).toHaveBeenCalledWith("product-ART-100");
|
|
expect(pathSpy).toHaveBeenCalledWith("/shop");
|
|
expect(pathSpy).toHaveBeenCalledWith("/");
|
|
expect(pathSpy).toHaveBeenCalledWith("/shop/canivita-tonic");
|
|
expect(pathSpy).toHaveBeenCalledWith("/shop/ART-100");
|
|
});
|
|
});
|
|
|
|
describe("revalidate batch", () => {
|
|
it("should send deduplicated tags and paths in one request", async () => {
|
|
const mockFetch = jest.spyOn(global, "fetch" as any).mockResolvedValue({ ok: true, status: 200 } as any);
|
|
|
|
const result = await service.revalidate(["blogs", "blogs"], ["/blog", "/blog"]);
|
|
|
|
expect(result).toBe(true);
|
|
expect(mockFetch).toHaveBeenCalledTimes(1);
|
|
expect(mockFetch).toHaveBeenCalledWith(
|
|
"http://frontend_mock:8080/api/revalidate",
|
|
expect.objectContaining({
|
|
method: "POST",
|
|
body: JSON.stringify({ tags: ["blogs"], paths: ["/blog"] }),
|
|
}),
|
|
);
|
|
});
|
|
});
|
|
});
|