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,16 @@
|
||||
import { act, renderHook } from '@testing-library/react';
|
||||
import { act, render, renderHook } from '@testing-library/react';
|
||||
import { describe, expect, it, vi } from 'vitest';
|
||||
import { hydrate, renderToString } from '@/test/hydrate';
|
||||
import usePageLoaded from './usePageLoaded';
|
||||
|
||||
const seen: boolean[] = [];
|
||||
|
||||
function Probe() {
|
||||
const loaded = usePageLoaded();
|
||||
seen.push(loaded);
|
||||
return <p>{String(loaded)}</p>;
|
||||
}
|
||||
|
||||
describe('usePageLoaded', () => {
|
||||
it('is true when the page had already loaded', () => {
|
||||
const { result } = renderHook(() => usePageLoaded());
|
||||
@@ -22,4 +31,38 @@ describe('usePageLoaded', () => {
|
||||
});
|
||||
expect(result.current).toBe(true);
|
||||
});
|
||||
|
||||
it('is false in the server render, even when the page has loaded', () => {
|
||||
expect(renderToString(<Probe />)).toBe('<p>false</p>');
|
||||
});
|
||||
|
||||
it('is false in the hydrating render and true right after on a loaded page', () => {
|
||||
seen.length = 0;
|
||||
const { container, errors } = hydrate(<Probe />);
|
||||
expect(seen[0]).toBe(false);
|
||||
expect(seen.at(-1)).toBe(true);
|
||||
expect(container).toHaveTextContent('true');
|
||||
expect(errors).toEqual([]);
|
||||
});
|
||||
|
||||
it('stays false after hydrating until the load event', () => {
|
||||
const readyState = vi
|
||||
.spyOn(document, 'readyState', 'get')
|
||||
.mockReturnValue('interactive');
|
||||
const { container, errors } = hydrate(<Probe />);
|
||||
expect(container).toHaveTextContent('false');
|
||||
|
||||
act(() => {
|
||||
readyState.mockReturnValue('complete');
|
||||
window.dispatchEvent(new Event('load'));
|
||||
});
|
||||
expect(container).toHaveTextContent('true');
|
||||
expect(errors).toEqual([]);
|
||||
});
|
||||
|
||||
it('is true at once for a component that mounts on a loaded page', () => {
|
||||
seen.length = 0;
|
||||
render(<Probe />);
|
||||
expect(seen).toEqual([true]);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user