Adds the fallbacks for visitors without JavaScript
The mobile menu works through CSS, and controls that need JavaScript are hidden without it. The contact photo gets its own preload, and the tests cover the new Reveal behaviour, the first row and the noscript markup. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,9 +1,12 @@
|
||||
import { fireEvent, screen, within } from '@testing-library/react';
|
||||
import userEvent from '@testing-library/user-event';
|
||||
import { describe, expect, it, vi } from 'vitest';
|
||||
import { GRID_COLUMNS } from '@/lib/firstRow';
|
||||
import { masonryRowSpan } from '@/lib/masonryRowSpan';
|
||||
import { axeResults } from '@/test/axe';
|
||||
import { cssRulesFor } from '@/test/cssRules';
|
||||
import { galleryItems } from '@/test/galleryFixtures';
|
||||
import { hydratePage, serverHtml } from '@/test/hydrate';
|
||||
import { renderWithProviders } from '@/test/renderWithProviders';
|
||||
import PortfolioGrid from './PortfolioGrid';
|
||||
|
||||
@@ -31,13 +34,9 @@ describe('PortfolioGrid', () => {
|
||||
expect(await axeResults(container)).toHaveNoViolations();
|
||||
});
|
||||
|
||||
it('loads the first photos straight away and defers the rest', async () => {
|
||||
it('loads the first row straight away and defers the rest', async () => {
|
||||
await renderWithProviders(
|
||||
<PortfolioGrid
|
||||
items={galleryItems}
|
||||
eagerFirstRow
|
||||
onOpen={() => {}}
|
||||
/>,
|
||||
<PortfolioGrid items={galleryItems} onOpen={() => {}} />,
|
||||
);
|
||||
const images = screen.getAllByRole('img');
|
||||
images.forEach((image, index) => {
|
||||
@@ -56,6 +55,28 @@ describe('PortfolioGrid', () => {
|
||||
String(galleryItems[index].thumb.width),
|
||||
);
|
||||
});
|
||||
// Only tile 1 is plain; tiles 2 and 3 have a source of their own
|
||||
expect(images[0].closest('picture')).toBeNull();
|
||||
expect(images[1].closest('picture')).not.toBeNull();
|
||||
expect(images[2].closest('picture')).not.toBeNull();
|
||||
expect(images[3].closest('picture')).toBeNull();
|
||||
});
|
||||
|
||||
it('defers every photo when the first row is not eager', async () => {
|
||||
await renderWithProviders(
|
||||
<PortfolioGrid
|
||||
items={galleryItems}
|
||||
eagerFirstRow={false}
|
||||
onOpen={() => {}}
|
||||
/>,
|
||||
);
|
||||
const images = screen.getAllByRole('img');
|
||||
expect(images).toHaveLength(galleryItems.length);
|
||||
for (const image of images) {
|
||||
expect(image).not.toHaveAttribute('fetchpriority');
|
||||
expect(image).toHaveAttribute('loading', 'lazy');
|
||||
expect(image.closest('picture')).toBeNull();
|
||||
}
|
||||
});
|
||||
|
||||
it('asks to open the lightbox on a plain click or Enter', async () => {
|
||||
@@ -107,3 +128,112 @@ describe('PortfolioGrid', () => {
|
||||
expect(screen.getByTestId('portfolio-grid')).toBeEmptyDOMElement();
|
||||
});
|
||||
});
|
||||
|
||||
describe('PortfolioGrid in the prerendered HTML', () => {
|
||||
const tileOf = (page: HTMLElement, number: number) =>
|
||||
page.querySelector(`[data-testid="portfolio-tile-${number}"]`)!;
|
||||
|
||||
const prerendered = async (eagerFirstRow?: boolean) => {
|
||||
const page = document.createElement('div');
|
||||
page.innerHTML = await serverHtml(
|
||||
<PortfolioGrid
|
||||
items={galleryItems}
|
||||
eagerFirstRow={eagerFirstRow}
|
||||
onOpen={() => {}}
|
||||
/>,
|
||||
);
|
||||
return page;
|
||||
};
|
||||
|
||||
it('gives tile 1 a plain photo with its src, whatever the screen width', async () => {
|
||||
const image = tileOf(await prerendered(), 1).querySelector('img')!;
|
||||
expect(image.closest('picture')).toBeNull();
|
||||
expect(image).toHaveAttribute('src', galleryItems[0].thumb.src);
|
||||
expect(image).toHaveAttribute('fetchpriority', 'high');
|
||||
expect(image).not.toHaveAttribute('data-deferred');
|
||||
});
|
||||
|
||||
it('puts tiles 2 and 3 in a picture whose source starts at the width of their column (BR-PRE-10)', async () => {
|
||||
const page = await prerendered();
|
||||
[1, 2].forEach((index) => {
|
||||
const tile = tileOf(page, index + 1);
|
||||
const source = tile.querySelector('picture > source')!;
|
||||
expect(source).toHaveAttribute('media', GRID_COLUMNS[index].media);
|
||||
expect(source).toHaveAttribute(
|
||||
'srcset',
|
||||
galleryItems[index].thumb.src,
|
||||
);
|
||||
const image = tile.querySelector('picture > img')!;
|
||||
expect(image).toHaveAttribute('data-deferred');
|
||||
expect(image).not.toHaveAttribute('src');
|
||||
expect(image).toHaveAttribute('loading', 'eager');
|
||||
expect(image).toHaveAttribute('fetchpriority', 'high');
|
||||
});
|
||||
});
|
||||
|
||||
it('leaves tile 4 and up deferred, without a src', async () => {
|
||||
const page = await prerendered();
|
||||
expect(galleryItems.length).toBeGreaterThan(3);
|
||||
for (let number = 4; number <= galleryItems.length; number++) {
|
||||
const tile = tileOf(page, number);
|
||||
const image = tile.querySelector('img')!;
|
||||
expect(tile.querySelector('picture')).toBeNull();
|
||||
expect(image).toHaveAttribute('data-deferred');
|
||||
expect(image).not.toHaveAttribute('src');
|
||||
expect(image).toHaveAttribute('loading', 'lazy');
|
||||
}
|
||||
});
|
||||
|
||||
it('defers every tile when the first row is not eager', async () => {
|
||||
const page = await prerendered(false);
|
||||
for (let number = 1; number <= galleryItems.length; number++) {
|
||||
const tile = tileOf(page, number);
|
||||
const image = tile.querySelector('img')!;
|
||||
expect(tile.querySelector('picture, source')).toBeNull();
|
||||
expect(image).toHaveAttribute('data-deferred');
|
||||
expect(image).not.toHaveAttribute('src');
|
||||
}
|
||||
});
|
||||
|
||||
it('hydrates without a mismatch, with the first row and without it', async () => {
|
||||
const eager = await hydratePage(
|
||||
<PortfolioGrid items={galleryItems} onOpen={() => {}} />,
|
||||
);
|
||||
expect(eager.errors).toEqual([]);
|
||||
eager.unmount();
|
||||
|
||||
const deferred = await hydratePage(
|
||||
<PortfolioGrid
|
||||
items={galleryItems}
|
||||
eagerFirstRow={false}
|
||||
onOpen={() => {}}
|
||||
/>,
|
||||
);
|
||||
expect(deferred.errors).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('PortfolioGrid columns', () => {
|
||||
it('lays out as many columns per width as GRID_COLUMNS names, the ones the first row follows', async () => {
|
||||
await renderWithProviders(
|
||||
<PortfolioGrid items={galleryItems} onOpen={() => {}} />,
|
||||
);
|
||||
const rules = cssRulesFor(screen.getByTestId('portfolio-grid'));
|
||||
const [base, ...wider] = GRID_COLUMNS;
|
||||
const unqueried = rules.filter((rule) => !rule.startsWith('@media'));
|
||||
expect(unqueried.join()).toContain('grid-template-columns: 1fr');
|
||||
expect(unqueried.join()).toContain(`--cols: ${base.columns}`);
|
||||
for (const { media, columns } of wider) {
|
||||
// Chakra writes its breakpoints in rem, the first row in em; both
|
||||
// are relative to the browser's font size
|
||||
const query = media.replace('em)', 'rem)');
|
||||
const inQuery = rules
|
||||
.filter((rule) => rule.startsWith(`@media screen and ${query}`))
|
||||
.join();
|
||||
expect(inQuery).toContain(
|
||||
`grid-template-columns: repeat(${columns}, 1fr)`,
|
||||
);
|
||||
expect(inQuery).toContain(`--cols: ${columns}`);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
@@ -9,13 +9,20 @@ import {
|
||||
import DeferredImage from '@/components/DeferredImage/DeferredImage';
|
||||
import Reveal from '@/components/Reveal/Reveal';
|
||||
import type { GalleryItem } from '@/content/types';
|
||||
import { firstRow } from '@/lib/firstRow';
|
||||
import { firstRow, GRID_COLUMNS } from '@/lib/firstRow';
|
||||
import {
|
||||
GAP_ROWS,
|
||||
masonryRowSpan,
|
||||
ROWS_PER_COLUMN_WIDTH,
|
||||
} from '@/lib/masonryRowSpan';
|
||||
|
||||
const [BASE_COLUMNS, MD_COLUMNS, LG_COLUMNS] = GRID_COLUMNS.map(
|
||||
(entry) => entry.columns,
|
||||
);
|
||||
|
||||
const columnTemplate = (columns: number) =>
|
||||
columns === 1 ? '1fr' : `repeat(${columns}, 1fr)`;
|
||||
|
||||
type PortfolioGridProps = {
|
||||
items: readonly GalleryItem[];
|
||||
// The photos of the first row (one column per screen width, see firstRow)
|
||||
@@ -109,17 +116,17 @@ export default function PortfolioGrid({
|
||||
listStyleType="none"
|
||||
display="grid"
|
||||
gridTemplateColumns={{
|
||||
base: '1fr',
|
||||
md: 'repeat(2, 1fr)',
|
||||
lg: 'repeat(3, 1fr)',
|
||||
base: columnTemplate(BASE_COLUMNS),
|
||||
md: columnTemplate(MD_COLUMNS),
|
||||
lg: columnTemplate(LG_COLUMNS),
|
||||
}}
|
||||
columnGap="gridGap"
|
||||
// One implicit row = column width / ROWS_PER_COLUMN_WIDTH
|
||||
gridAutoRows="var(--row)"
|
||||
css={{
|
||||
'--cols': '1',
|
||||
md: { '--cols': '2' },
|
||||
lg: { '--cols': '3' },
|
||||
'--cols': String(BASE_COLUMNS),
|
||||
md: { '--cols': String(MD_COLUMNS) },
|
||||
lg: { '--cols': String(LG_COLUMNS) },
|
||||
'--row': `calc((100cqi - (var(--cols) - 1) * {spacing.gridGap}) / var(--cols) / ${ROWS_PER_COLUMN_WIDTH})`,
|
||||
}}
|
||||
>
|
||||
|
||||
Reference in New Issue
Block a user