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:
@@ -0,0 +1,160 @@
|
||||
import fc from 'fast-check';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import {
|
||||
columnMap,
|
||||
galleryItemsWithThumbs,
|
||||
tileIndex,
|
||||
type ColumnMap,
|
||||
} from '../test/firstRowArbitraries';
|
||||
import { firstRow, firstRowPreloads, GRID_COLUMNS } from './firstRow';
|
||||
|
||||
const widthOf = (media: string | null) =>
|
||||
media === null ? 0 : Number(/\(min-width: (\d+)em\)/.exec(media)![1]);
|
||||
|
||||
const columnsAt = (map: ColumnMap, width: number) =>
|
||||
map.filter((entry) => widthOf(entry.media) <= width).at(-1)!.columns;
|
||||
|
||||
// Whether a tile is in the first row at a screen width, as the CSS shows it
|
||||
const eagerAt = (map: ColumnMap, index: number, width: number) => {
|
||||
const row = firstRow(index, map);
|
||||
return row === 'always' || (row !== null && widthOf(row.media) <= width);
|
||||
};
|
||||
|
||||
const widths = (map: ColumnMap) =>
|
||||
[...new Set([...map.map((entry) => widthOf(entry.media)), 0, 200])].sort(
|
||||
(a, b) => a - b,
|
||||
);
|
||||
|
||||
describe('firstRow', () => {
|
||||
it('knows the columns of the portfolio grid', () => {
|
||||
expect(GRID_COLUMNS.map((entry) => entry.columns)).toEqual([1, 2, 3]);
|
||||
expect(GRID_COLUMNS.map((entry) => entry.media)).toEqual([
|
||||
null,
|
||||
'(min-width: 48em)',
|
||||
'(min-width: 64em)',
|
||||
]);
|
||||
});
|
||||
|
||||
it('puts tile 1 in the first row always, tile 2 from 48em, tile 3 from 64em and the rest never', () => {
|
||||
expect(firstRow(0)).toBe('always');
|
||||
expect(firstRow(1)).toEqual({ media: '(min-width: 48em)' });
|
||||
expect(firstRow(2)).toEqual({ media: '(min-width: 64em)' });
|
||||
expect(firstRow(3)).toBeNull();
|
||||
expect(firstRow(11)).toBeNull();
|
||||
});
|
||||
|
||||
it('answers for another column map', () => {
|
||||
const map = [
|
||||
{ media: null, columns: 2 },
|
||||
{ media: '(min-width: 30em)', columns: 4 },
|
||||
];
|
||||
expect(firstRow(1, map)).toBe('always');
|
||||
expect(firstRow(2, map)).toEqual({ media: '(min-width: 30em)' });
|
||||
expect(firstRow(4, map)).toBeNull();
|
||||
});
|
||||
|
||||
it('is always below the base count, never from the largest count, and else the first breakpoint that fits (invariant)', () => {
|
||||
fc.assert(
|
||||
fc.property(columnMap, tileIndex, (map, index) => {
|
||||
const row = firstRow(index, map);
|
||||
const largest = map.at(-1)!.columns;
|
||||
if (index < map[0].columns) {
|
||||
expect(row).toBe('always');
|
||||
} else if (index >= largest) {
|
||||
expect(row).toBeNull();
|
||||
} else {
|
||||
const first = map.find((entry) => index < entry.columns)!;
|
||||
expect(row).toEqual({ media: first.media });
|
||||
}
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it('keeps a tile in the first row at every wider screen, and fills exactly the columns (metamorphic, monotonic)', () => {
|
||||
fc.assert(
|
||||
fc.property(columnMap, (map) => {
|
||||
const indexes = Array.from(
|
||||
{ length: map.at(-1)!.columns + 3 },
|
||||
(_, index) => index,
|
||||
);
|
||||
const screens = widths(map);
|
||||
for (const [position, width] of screens.entries()) {
|
||||
const eager = indexes.filter((index) =>
|
||||
eagerAt(map, index, width),
|
||||
);
|
||||
expect(eager).toHaveLength(columnsAt(map, width));
|
||||
for (const wider of screens.slice(position + 1)) {
|
||||
for (const index of eager) {
|
||||
expect(eagerAt(map, index, wider)).toBe(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('firstRowPreloads', () => {
|
||||
const thumb = (id: string) => ({
|
||||
src: `/${id}.jpg`,
|
||||
alt: id,
|
||||
width: 400,
|
||||
height: 600,
|
||||
});
|
||||
|
||||
it('binds the preload of tiles 2 and 3 to the query of their source', () => {
|
||||
const items = ['a', 'b', 'c', 'd', 'e'].map((id) => ({
|
||||
thumb: thumb(id),
|
||||
}));
|
||||
expect(firstRowPreloads(items)).toEqual([
|
||||
{
|
||||
rel: 'preload',
|
||||
as: 'image',
|
||||
href: '/b.jpg',
|
||||
fetchPriority: 'high',
|
||||
media: '(min-width: 48em)',
|
||||
},
|
||||
{
|
||||
rel: 'preload',
|
||||
as: 'image',
|
||||
href: '/c.jpg',
|
||||
fetchPriority: 'high',
|
||||
media: '(min-width: 64em)',
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it('has fewer links when the list is short, and none for an empty one', () => {
|
||||
expect(firstRowPreloads([{ thumb: thumb('a') }])).toEqual([]);
|
||||
expect(
|
||||
firstRowPreloads([{ thumb: thumb('a') }, { thumb: thumb('b') }]),
|
||||
).toHaveLength(1);
|
||||
expect(firstRowPreloads([])).toEqual([]);
|
||||
});
|
||||
|
||||
it('makes one link per tile with a media, none for the other tiles, and no duplicates (metamorphic, monotonic)', () => {
|
||||
fc.assert(
|
||||
fc.property(galleryItemsWithThumbs, columnMap, (items, map) => {
|
||||
const links = firstRowPreloads(items, map);
|
||||
const expected = items.flatMap((item, index) => {
|
||||
const row = firstRow(index, map);
|
||||
return row && row !== 'always'
|
||||
? [{ href: item.thumb.src, media: row.media }]
|
||||
: [];
|
||||
});
|
||||
expect(
|
||||
links.map((link) => ({
|
||||
href: link.href,
|
||||
media: link.media,
|
||||
})),
|
||||
).toEqual(expected);
|
||||
expect(links.length).toBeLessThanOrEqual(
|
||||
map.at(-1)!.columns - map[0].columns,
|
||||
);
|
||||
expect(new Set(links.map((link) => link.href)).size).toBe(
|
||||
links.length,
|
||||
);
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,126 @@
|
||||
import fc from 'fast-check';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import {
|
||||
elementTop,
|
||||
revealEvent,
|
||||
revealEvents,
|
||||
revealPhase,
|
||||
topNearViewportEdge,
|
||||
viewportHeight,
|
||||
} from '../test/revealArbitraries';
|
||||
import { nextRevealPhase, startsHidden, type RevealPhase } from './revealPhase';
|
||||
|
||||
describe('startsHidden', () => {
|
||||
it('hides an element wholly below the viewport only', () => {
|
||||
expect(startsHidden(823, 823)).toBe(true);
|
||||
expect(startsHidden(5000, 823)).toBe(true);
|
||||
expect(startsHidden(822.5, 823)).toBe(false);
|
||||
expect(startsHidden(0, 823)).toBe(false);
|
||||
// Above the viewport (scroll restoration, #anchor jump)
|
||||
expect(startsHidden(-300, 823)).toBe(false);
|
||||
});
|
||||
|
||||
it('is hidden exactly when the top is at or below the viewport height (invariant)', () => {
|
||||
fc.assert(
|
||||
fc.property(elementTop, viewportHeight, (top, height) => {
|
||||
expect(startsHidden(top, height)).toBe(top >= height);
|
||||
}),
|
||||
);
|
||||
fc.assert(
|
||||
fc.property(topNearViewportEdge, ({ top, viewportHeight }) => {
|
||||
expect(startsHidden(top, viewportHeight)).toBe(
|
||||
top >= viewportHeight,
|
||||
);
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it('stays hidden further down the page (metamorphic, monotonic)', () => {
|
||||
fc.assert(
|
||||
fc.property(
|
||||
elementTop,
|
||||
fc.double({ min: 0, max: 50000, noNaN: true }),
|
||||
viewportHeight,
|
||||
(top, distance, height) => {
|
||||
if (startsHidden(top, height)) {
|
||||
expect(startsHidden(top + distance, height)).toBe(true);
|
||||
}
|
||||
},
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
it('does not change when the page is shifted as a whole (metamorphic, translation)', () => {
|
||||
fc.assert(
|
||||
fc.property(
|
||||
fc.integer({ min: -20000, max: 20000 }),
|
||||
fc.integer({ min: 1, max: 4000 }),
|
||||
fc.integer({ min: -5000, max: 5000 }),
|
||||
(top, height, offset) => {
|
||||
expect(startsHidden(top + offset, height + offset)).toBe(
|
||||
startsHidden(top, height),
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('nextRevealPhase', () => {
|
||||
it('hides a settled element that is below the viewport and keeps one in view', () => {
|
||||
expect(nextRevealPhase('settled', 'belowViewport')).toBe('hidden');
|
||||
expect(nextRevealPhase('settled', 'inView')).toBe('settled');
|
||||
expect(nextRevealPhase('settled', 'intersected')).toBe('settled');
|
||||
});
|
||||
|
||||
it('reveals a hidden element once it intersects', () => {
|
||||
expect(nextRevealPhase('hidden', 'intersected')).toBe('revealed');
|
||||
expect(nextRevealPhase('hidden', 'belowViewport')).toBe('hidden');
|
||||
expect(nextRevealPhase('hidden', 'inView')).toBe('hidden');
|
||||
});
|
||||
|
||||
it('never leaves revealed', () => {
|
||||
for (const event of [
|
||||
'belowViewport',
|
||||
'inView',
|
||||
'intersected',
|
||||
] as const) {
|
||||
expect(nextRevealPhase('revealed', event)).toBe('revealed');
|
||||
}
|
||||
});
|
||||
|
||||
it('never goes back, and never reveals an element that was not hidden (invariant, state machine)', () => {
|
||||
fc.assert(
|
||||
fc.property(
|
||||
fc.constantFrom<RevealPhase[]>('settled', 'hidden'),
|
||||
revealEvents,
|
||||
(start, events) => {
|
||||
let phase = start;
|
||||
let hasBeenHidden = start === 'hidden';
|
||||
let hasBeenRevealed = false;
|
||||
for (const event of events) {
|
||||
phase = nextRevealPhase(phase, event);
|
||||
if (phase === 'hidden') hasBeenHidden = true;
|
||||
if (hasBeenRevealed) expect(phase).toBe('revealed');
|
||||
if (phase === 'revealed') {
|
||||
hasBeenRevealed = true;
|
||||
expect(hasBeenHidden).toBe(true);
|
||||
}
|
||||
if (start === 'settled' && !hasBeenHidden) {
|
||||
expect(phase).toBe('settled');
|
||||
}
|
||||
}
|
||||
},
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
it('gives the same phase when an event is applied twice (idempotent)', () => {
|
||||
fc.assert(
|
||||
fc.property(revealPhase, revealEvent, (phase, event) => {
|
||||
const once = nextRevealPhase(phase, event);
|
||||
expect(nextRevealPhase(once, event)).toBe(once);
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user