Makes Reveal, deferred images and the first row work from the HTML
Reveal content is visible in the HTML and only content below the viewport hides after hydration. Deferred photos get a noscript twin, and the first row of the portfolio loads eagerly per column count. Some old tests still expect the former behaviour; they are rewritten next, so this commit's test run is not fully green. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
import { StrictMode } from 'react';
|
||||
import { act } from '@testing-library/react';
|
||||
import { ChakraProvider } from '@chakra-ui/react';
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||
import Reveal from '@/components/Reveal/Reveal';
|
||||
import { FakeIntersectionObserver as FakeObserver } from '@/test/fakeIntersectionObserver';
|
||||
import { hydrate } from '@/test/hydrate';
|
||||
import system from './theme';
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
vi.unstubAllGlobals();
|
||||
FakeObserver.reset();
|
||||
});
|
||||
|
||||
// jsdom has no layout: place the elements by their test id
|
||||
function stubTops(tops: Record<string, number>) {
|
||||
vi.spyOn(Element.prototype, 'getBoundingClientRect').mockImplementation(
|
||||
function (this: Element) {
|
||||
const top = tops[this.getAttribute('data-testid') ?? ''] ?? 0;
|
||||
return { top } as DOMRect;
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
describe('Reveal while hydrating (BR-RVL-01, 02)', () => {
|
||||
const page = (
|
||||
<StrictMode>
|
||||
<ChakraProvider value={system}>
|
||||
<Reveal data-testid="in-view">in view</Reveal>
|
||||
<Reveal data-testid="below-fold">below the fold</Reveal>
|
||||
</ChakraProvider>
|
||||
</StrictMode>
|
||||
);
|
||||
|
||||
it('keeps an element in view visible and hides one below the fold only after hydration', () => {
|
||||
vi.stubGlobal('IntersectionObserver', FakeObserver);
|
||||
stubTops({ 'in-view': innerHeight - 1, 'below-fold': innerHeight });
|
||||
const { container, errors } = hydrate(page);
|
||||
|
||||
const inView = container.querySelector('[data-testid="in-view"]')!;
|
||||
const belowFold = container.querySelector(
|
||||
'[data-testid="below-fold"]',
|
||||
)!;
|
||||
expect(errors).toEqual([]);
|
||||
expect(inView).toHaveAttribute('data-reveal', 'visible');
|
||||
expect(belowFold).toHaveAttribute('data-reveal', 'hidden');
|
||||
|
||||
act(() => FakeObserver.instances.at(-1)!.trigger(true));
|
||||
expect(belowFold).toHaveAttribute('data-reveal', 'visible');
|
||||
expect(inView).toHaveAttribute('data-reveal', 'visible');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user