79 lines
2.0 KiB
TypeScript
79 lines
2.0 KiB
TypeScript
import { describe, expect, it, vi } from "vitest";
|
|
import { appRouter } from "./routers";
|
|
import type { TrpcContext } from "./_core/context";
|
|
import * as notification from "./_core/notification";
|
|
|
|
// Mock the notification module
|
|
vi.mock("./_core/notification", () => ({
|
|
notifyOwner: vi.fn().mockResolvedValue(true),
|
|
}));
|
|
|
|
function createTestContext(): TrpcContext {
|
|
return {
|
|
user: null,
|
|
req: {
|
|
protocol: "https",
|
|
headers: {},
|
|
} as TrpcContext["req"],
|
|
res: {} as TrpcContext["res"],
|
|
};
|
|
}
|
|
|
|
describe("contact.submit", () => {
|
|
it("accepts valid contact form submission", async () => {
|
|
const ctx = createTestContext();
|
|
const caller = appRouter.createCaller(ctx);
|
|
|
|
const result = await caller.contact.submit({
|
|
name: "Test User",
|
|
email: "test@example.com",
|
|
message: "This is a test message",
|
|
});
|
|
|
|
expect(result).toEqual({ success: true });
|
|
expect(notification.notifyOwner).toHaveBeenCalledWith({
|
|
title: "Neue Kontaktanfrage",
|
|
content: expect.stringContaining("Test User"),
|
|
});
|
|
});
|
|
|
|
it("rejects submission with invalid email", async () => {
|
|
const ctx = createTestContext();
|
|
const caller = appRouter.createCaller(ctx);
|
|
|
|
await expect(
|
|
caller.contact.submit({
|
|
name: "Test User",
|
|
email: "invalid-email",
|
|
message: "This is a test message",
|
|
})
|
|
).rejects.toThrow();
|
|
});
|
|
|
|
it("rejects submission with empty name", async () => {
|
|
const ctx = createTestContext();
|
|
const caller = appRouter.createCaller(ctx);
|
|
|
|
await expect(
|
|
caller.contact.submit({
|
|
name: "",
|
|
email: "test@example.com",
|
|
message: "This is a test message",
|
|
})
|
|
).rejects.toThrow();
|
|
});
|
|
|
|
it("rejects submission with empty message", async () => {
|
|
const ctx = createTestContext();
|
|
const caller = appRouter.createCaller(ctx);
|
|
|
|
await expect(
|
|
caller.contact.submit({
|
|
name: "Test User",
|
|
email: "test@example.com",
|
|
message: "",
|
|
})
|
|
).rejects.toThrow();
|
|
});
|
|
});
|