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"');
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user