import React from 'react'; import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'; import { render, screen, fireEvent, cleanup } from '@testing-library/react'; import SupportPage from '../app/support/page'; import { SupportContactForm } from '../components/support-contact-form'; describe('WEB-W2: public Support Center', () => { afterEach(cleanup); it('renders a grounded FAQ and a real email contact channel', () => { render(); expect(screen.getByText('Frequently asked')).toBeDefined(); expect(screen.getByText(/Can I export my ride data\?/i)).toBeDefined(); expect(screen.getByText(/How do I turn on two-factor authentication/i)).toBeDefined(); // Email fallback is present as a real mailto link. const mailLinks = screen.getAllByRole('link', { name: /support@motorsignal\.app/i }); expect(mailLinks.length).toBeGreaterThan(0); expect(mailLinks[0].getAttribute('href')).toBe('mailto:support@motorsignal.app'); }); it('validates input and does not claim any submission on invalid data', () => { render(); fireEvent.click(screen.getByRole('button', { name: /Compose support email/i })); expect(screen.getByText(/tell us your name/i)).toBeDefined(); expect(screen.getByText(/valid email address/i)).toBeDefined(); expect(screen.getByText(/at least 10 characters/i)).toBeDefined(); // No success/opened confirmation on invalid submit. expect(screen.queryByText(/email app should have opened/i)).toBeNull(); // Truthful disclaimer is always present. expect(screen.getByText(/don.?t submit a ticket on your behalf/i)).toBeDefined(); }); it('opens a prefilled email (no simulated ticket) when the form is valid', () => { const originalLocation = window.location; const hrefSpy = vi.fn(); // Replace window.location with a mock that records href assignments. Object.defineProperty(window, 'location', { configurable: true, value: { set href(value: string) { hrefSpy(value); }, get href() { return ''; }, }, }); try { render(); fireEvent.change(screen.getByLabelText('Your name'), { target: { value: 'Alex Rider' } }); fireEvent.change(screen.getByLabelText('Email'), { target: { value: 'alex@example.com' } }); fireEvent.change(screen.getByLabelText('Topic'), { target: { value: 'Bug report' } }); fireEvent.change(screen.getByLabelText(/How can we help/i), { target: { value: 'The replay map tiles are not loading on my ride.' }, }); fireEvent.click(screen.getByRole('button', { name: /Compose support email/i })); expect(hrefSpy).toHaveBeenCalledTimes(1); const url = hrefSpy.mock.calls[0][0] as string; expect(url.startsWith('mailto:support@motorsignal.app?')).toBe(true); expect(decodeURIComponent(url)).toContain('[Bug report]'); expect(decodeURIComponent(url)).toContain('alex@example.com'); expect(screen.getByText(/email app should have opened/i)).toBeDefined(); } finally { Object.defineProperty(window, 'location', { configurable: true, value: originalLocation, }); } }); });