import { screen, within } from '@testing-library/react'; import fc from 'fast-check'; import { describe, expect, it } from 'vitest'; import siteContent from '@/content/siteContent'; import { validEmail } from '@/test/arbitraries'; import { axeResults } from '@/test/axe'; import { hydratePage, serverHtml } from '@/test/hydrate'; import { renderWithProviders } from '@/test/renderWithProviders'; import ContactDetails from './ContactDetails'; type Props = Partial>; const card = (props: Props = {}) => ( ); const portrait = { src: '/portret.jpg', alt: 'Andrea', width: 144, height: 144, }; describe('ContactDetails', () => { it('lists e-mail, Instagram and Facebook under a named heading', async () => { const { container } = await renderWithProviders(card()); expect( screen.getByRole('heading', { level: 2, name: 'Direct contact' }), ).toHaveAttribute('id', 'details-title'); const list = screen.getByRole('list', { name: 'Direct contact' }); const names = within(list) .getAllByRole('link') .map((link) => link.textContent); expect(names).toEqual([ 'info@andreaslop.nl', '@andreaslopfotografie (opent in nieuw tabblad)', 'Andrea Slop Fotografie (opent in nieuw tabblad)', ]); expect(await axeResults(container)).toHaveNoViolations(); }); it('mails with the default subject in the same tab', async () => { await renderWithProviders(card()); const mail = screen.getByTestId('contact-details-email'); expect(mail).toHaveAttribute( 'href', 'mailto:info@andreaslop.nl?subject=Vraag%20via%20andreaslop.nl', ); expect(mail).not.toHaveAttribute('target'); }); it('opens the socials safely in a new tab and says so', async () => { await renderWithProviders(card()); const instagram = screen.getByRole('link', { name: '@andreaslopfotografie (opent in nieuw tabblad)', }); expect(instagram).toHaveAttribute('target', '_blank'); expect(instagram).toHaveAttribute('rel', 'noopener noreferrer'); expect(instagram).toHaveAttribute( 'href', 'https://www.instagram.com/andreaslopfotografie/', ); }); it('hides the icons from assistive technology', async () => { await renderWithProviders(card()); const icons = screen.getByTestId('details').querySelectorAll('svg'); expect(icons).toHaveLength(3); icons.forEach((icon) => expect(icon).toHaveAttribute('aria-hidden', 'true'), ); }); it('renders nothing without a valid channel', async () => { const { container } = await renderWithProviders( card({ email: { user: '', domain: '' }, socials: [] }), ); expect(screen.queryByTestId('details')).toBeNull(); expect(container.querySelector('h2')).toBeNull(); }); it('shows the portrait as decoration next to the heading', async () => { const { container } = await renderWithProviders(card({ portrait })); const photo = screen.getByTestId('contact-details-portrait'); expect(photo).toHaveAttribute('src', '/portret.jpg'); expect(photo).toHaveAttribute('alt', ''); expect(await axeResults(container)).toHaveNoViolations(); }); it('can appear twice on a page with its own heading ids', async () => { const { container } = await renderWithProviders( <> {card({ titleId: 'details-a', 'data-testid': 'a' })} {card({ titleId: 'details-b', 'data-testid': 'b' })} , ); expect( screen.getAllByRole('list', { name: 'Direct contact' }), ).toHaveLength(2); expect(await axeResults(container)).toHaveNoViolations(); }); }); // The address is only there after hydrating; before, the first item is an // empty placeholder of the same height (BR-MAIL-04, BR-MAIL-07) describe('ContactDetails in the prerendered HTML', () => { it('holds a placeholder first and the socials, but no address', async () => { const html = await serverHtml(card()); expect(html.toLowerCase()).not.toContain('info@andreaslop.nl'); expect(html).not.toContain('mailto:'); expect(html).toContain('data-testid="contact-details-email-pending"'); expect(html.indexOf('contact-details-email-pending')).toBeLessThan( html.indexOf('contact-details-instagram'), ); expect(html).toContain('contact-details-facebook'); }); it('never holds the address, whatever the content (invariant)', async () => { await fc.assert( fc.asyncProperty(validEmail, async (email) => { const html = await serverHtml(card({ email })); expect(html.toLowerCase()).not.toContain( `${email.user}@${email.domain}`.toLowerCase(), ); expect(html).not.toContain('mailto:'); }), ); }); it('swaps the placeholder for the link after hydrating', async () => { const { errors } = await hydratePage(card()); const mail = await screen.findByTestId('contact-details-email'); expect(mail).toHaveAttribute( 'href', 'mailto:info@andreaslop.nl?subject=Vraag%20via%20andreaslop.nl', ); expect( screen.queryByTestId('contact-details-email-pending'), ).not.toBeInTheDocument(); expect(errors).toEqual([]); }); it('marks the placeholder hidden, empty and without a link', async () => { const html = await serverHtml(card()); const doc = new DOMParser().parseFromString(html, 'text/html'); const pending = doc.querySelector('[data-email-pending]')!; expect(pending.getAttribute('aria-hidden')).toBe('true'); expect(pending.textContent).toBe(''); expect(pending.querySelector('a')).toBeNull(); }); it('has no placeholder on a plain mount', async () => { const { container } = await renderWithProviders(card()); expect(container.querySelector('[data-email-pending]')).toBeNull(); }); it('has no placeholder without a valid address, only the socials', async () => { const html = await serverHtml( card({ email: { user: '', domain: 'andreaslop.nl' } }), ); expect(html).not.toContain('email-pending'); expect(html).toContain('contact-details-instagram'); }); it('renders nothing without an address and without socials', async () => { const html = await serverHtml( card({ email: { user: '', domain: '' }, socials: [] }), ); expect(html).not.toContain('data-testid="details"'); }); it('loads the small portrait lazily, so a hidden one is never fetched', async () => { const html = await serverHtml(card({ portrait })); const doc = new DOMParser().parseFromString(html, 'text/html'); expect( doc .querySelector('[data-testid="contact-details-portrait"]') ?.getAttribute('loading'), ).toBe('lazy'); }); });