Makes the first render match the prerendered HTML
Search values, the 404 buttons and the footer year now apply after hydration. The e-mail address stays out of the HTML behind a placeholder, and all review texts are in the page, so crawlers can read them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
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';
|
||||
|
||||
@@ -104,3 +107,72 @@ describe('ContactDetails', () => {
|
||||
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"');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -11,6 +11,7 @@ import { Icon } from '@iconify/react/offline';
|
||||
import Eyebrow from '@/components/Eyebrow/Eyebrow';
|
||||
import socialPlatforms from '@/content/socialPlatforms';
|
||||
import type { ContentImage, EmailAddress, SocialLink } from '@/content/types';
|
||||
import useHydrated from '@/hooks/useHydrated';
|
||||
import { contactChannels, type ContactChannel } from '@/lib/contactChannels';
|
||||
|
||||
type ContactDetailsProps = {
|
||||
@@ -25,13 +26,18 @@ type ContactDetailsProps = {
|
||||
'data-testid'?: string;
|
||||
};
|
||||
|
||||
const iconOf = (channel: ContactChannel) =>
|
||||
// Channels that render as a link; the pending e-mail item is not one
|
||||
type LinkChannel = Exclude<ContactChannel, { kind: 'email-pending' }>;
|
||||
|
||||
const iconOf = (channel: LinkChannel) =>
|
||||
channel.kind === 'email'
|
||||
? mailFilled
|
||||
: socialPlatforms[channel.platform].icon;
|
||||
|
||||
// "Direct contact": a card with the ways to reach Andrea, each an icon and a
|
||||
// link (BR-CD-01..08). Renders nothing when no channel is valid.
|
||||
// link (BR-CD-01..08). Renders nothing when no channel is valid. Before
|
||||
// hydration the e-mail item is an empty placeholder of the same height
|
||||
// (BR-MAIL-04).
|
||||
export default function ContactDetails({
|
||||
titleId,
|
||||
title,
|
||||
@@ -40,7 +46,8 @@ export default function ContactDetails({
|
||||
portrait,
|
||||
'data-testid': testId,
|
||||
}: ContactDetailsProps) {
|
||||
const channels = contactChannels(email, socials);
|
||||
// The address is only assembled after hydration (BR-MAIL-07)
|
||||
const channels = contactChannels(email, socials, useHydrated());
|
||||
if (channels.length === 0) return null;
|
||||
|
||||
return (
|
||||
@@ -71,47 +78,57 @@ export default function ContactDetails({
|
||||
</Eyebrow>
|
||||
</Flex>
|
||||
<chakra.ul listStyleType="none" aria-labelledby={titleId}>
|
||||
{channels.map((channel) => (
|
||||
<chakra.li key={channel.href}>
|
||||
<Link
|
||||
variant="subtle"
|
||||
href={channel.href}
|
||||
display="flex"
|
||||
alignItems="center"
|
||||
gap={4}
|
||||
{channels.map((channel) =>
|
||||
channel.kind === 'email-pending' ? (
|
||||
<chakra.li
|
||||
key={channel.kind}
|
||||
aria-hidden="true"
|
||||
minH="44px"
|
||||
{...(channel.external && {
|
||||
target: '_blank',
|
||||
rel: 'noopener noreferrer',
|
||||
})}
|
||||
data-testid={`contact-details-${
|
||||
channel.kind === 'email'
|
||||
? 'email'
|
||||
: channel.platform
|
||||
}`}
|
||||
>
|
||||
<Box
|
||||
as="span"
|
||||
color="accent.deep"
|
||||
fontSize="1.375rem"
|
||||
display="inline-flex"
|
||||
data-email-pending
|
||||
data-testid="contact-details-email-pending"
|
||||
/>
|
||||
) : (
|
||||
<chakra.li key={channel.href}>
|
||||
<Link
|
||||
variant="subtle"
|
||||
href={channel.href}
|
||||
display="flex"
|
||||
alignItems="center"
|
||||
gap={4}
|
||||
minH="44px"
|
||||
{...(channel.external && {
|
||||
target: '_blank',
|
||||
rel: 'noopener noreferrer',
|
||||
})}
|
||||
data-testid={`contact-details-${
|
||||
channel.kind === 'email'
|
||||
? 'email'
|
||||
: channel.platform
|
||||
}`}
|
||||
>
|
||||
<Icon icon={iconOf(channel)} aria-hidden />
|
||||
</Box>
|
||||
{/* Space outside the hidden text, or it drops out
|
||||
<Box
|
||||
as="span"
|
||||
color="accent.deep"
|
||||
fontSize="1.375rem"
|
||||
display="inline-flex"
|
||||
>
|
||||
<Icon icon={iconOf(channel)} aria-hidden />
|
||||
</Box>
|
||||
{/* Space outside the hidden text, or it drops out
|
||||
of the announced name */}
|
||||
{channel.label}
|
||||
{channel.external && (
|
||||
<>
|
||||
{' '}
|
||||
<VisuallyHidden>
|
||||
(opent in nieuw tabblad)
|
||||
</VisuallyHidden>
|
||||
</>
|
||||
)}
|
||||
</Link>
|
||||
</chakra.li>
|
||||
))}
|
||||
{channel.label}
|
||||
{channel.external && (
|
||||
<>
|
||||
{' '}
|
||||
<VisuallyHidden>
|
||||
(opent in nieuw tabblad)
|
||||
</VisuallyHidden>
|
||||
</>
|
||||
)}
|
||||
</Link>
|
||||
</chakra.li>
|
||||
),
|
||||
)}
|
||||
</chakra.ul>
|
||||
</Box>
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user