111 lines
2.9 KiB
TypeScript
111 lines
2.9 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
import { sendContactFormAction } from '../app/actions/contact';
|
|
import * as mailer from '@/lib/mail/mailer';
|
|
import { render } from '@mintel/mail';
|
|
import React from 'react';
|
|
|
|
// Mock Next.js headers
|
|
vi.mock('next/headers', () => ({
|
|
headers: () =>
|
|
Promise.resolve(
|
|
new Map([
|
|
['user-agent', 'test-agent'],
|
|
['accept-language', 'de-DE,de;q=0.9'],
|
|
['referer', 'http://localhost:3000/de/kontakt'],
|
|
['x-forwarded-for', '127.0.0.1'],
|
|
]),
|
|
),
|
|
}));
|
|
|
|
// Mock services
|
|
vi.mock('@/lib/services/create-services.server', () => ({
|
|
getServerAppServices: () => ({
|
|
logger: {
|
|
child: () => ({
|
|
info: vi.fn(),
|
|
error: vi.fn(),
|
|
warn: vi.fn(),
|
|
}),
|
|
},
|
|
analytics: {
|
|
track: vi.fn(),
|
|
setServerContext: vi.fn(),
|
|
},
|
|
errors: {
|
|
captureException: vi.fn(),
|
|
},
|
|
notifications: {
|
|
notify: vi.fn(),
|
|
},
|
|
}),
|
|
}));
|
|
|
|
// Mock mailer
|
|
vi.mock('@/lib/mail/mailer', () => ({
|
|
sendEmail: vi.fn().mockResolvedValue({ success: true, messageId: 'test-id' }),
|
|
}));
|
|
|
|
// Mock env
|
|
vi.mock('@/lib/env', () => ({
|
|
env: {
|
|
MAIL_RECIPIENTS: 'internal@mintel.me',
|
|
},
|
|
}));
|
|
|
|
describe('sendContactFormAction', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
});
|
|
|
|
it('should successfully process a valid contact form submission', async () => {
|
|
const formData = new FormData();
|
|
formData.append('name', 'John Doe');
|
|
formData.append('email', 'john@example.com');
|
|
formData.append('message', 'Hello, this is a test message.');
|
|
|
|
const result = await sendContactFormAction(formData);
|
|
|
|
expect(result.success).toBe(true);
|
|
|
|
// Verify internal notification was sent
|
|
expect(mailer.sendEmail).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
replyTo: 'john@example.com',
|
|
subject: 'New Contact Form Submission',
|
|
}),
|
|
);
|
|
|
|
// Verify customer confirmation was sent
|
|
expect(mailer.sendEmail).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
to: 'john@example.com',
|
|
subject: 'Thank you for your inquiry',
|
|
}),
|
|
);
|
|
});
|
|
|
|
it('should fail if required fields are missing', async () => {
|
|
const formData = new FormData();
|
|
formData.append('name', 'John Doe');
|
|
// Missing email and message
|
|
|
|
const result = await sendContactFormAction(formData);
|
|
|
|
expect(result.success).toBe(false);
|
|
expect(result.error).toBe('Missing required fields');
|
|
expect(mailer.sendEmail).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('should skip email sending for test submissions', async () => {
|
|
const formData = new FormData();
|
|
formData.append('name', 'Tester');
|
|
formData.append('email', 'testing@mintel.me');
|
|
formData.append('message', 'Test');
|
|
|
|
const result = await sendContactFormAction(formData);
|
|
|
|
expect(result.success).toBe(true);
|
|
expect(mailer.sendEmail).not.toHaveBeenCalled();
|
|
});
|
|
});
|