A masonry photo grid that keeps the reading order (pure CSS, no measuring in the browser), an accessible lightbox on Chakra's Dialog with wrap-around browsing, keys, swipe and preloading, and a filter bar of toggle buttons. They work on generic gallery items, so other pages can use them. The swipe logic moves out of the hero into a shared hook; the hero behaves as before. The rules behind them live in pure helpers with property-based tests. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
81 lines
2.5 KiB
TypeScript
81 lines
2.5 KiB
TypeScript
import fc from 'fast-check';
|
|
import { describe, expect, it } from 'vitest';
|
|
import { imageSize } from '../test/arbitraries';
|
|
import {
|
|
GAP_ROWS,
|
|
masonryRowSpan,
|
|
ROWS_PER_COLUMN_WIDTH,
|
|
} from './masonryRowSpan';
|
|
|
|
const K = ROWS_PER_COLUMN_WIDTH;
|
|
|
|
describe('masonryRowSpan', () => {
|
|
it('spans rows by aspect ratio plus the gap', () => {
|
|
// Portrait 2:3, landscape 3:2, square
|
|
expect(masonryRowSpan({ width: 800, height: 1200 })).toBe(
|
|
60 + GAP_ROWS,
|
|
);
|
|
expect(masonryRowSpan({ width: 1200, height: 800 })).toBe(
|
|
27 + GAP_ROWS,
|
|
);
|
|
expect(masonryRowSpan({ width: 800, height: 800 })).toBe(40 + GAP_ROWS);
|
|
});
|
|
|
|
it('gives even a very wide photo at least one row', () => {
|
|
expect(masonryRowSpan({ width: 10000, height: 10 })).toBe(1 + GAP_ROWS);
|
|
});
|
|
|
|
it('is at least one row plus the gap (invariant)', () => {
|
|
fc.assert(
|
|
fc.property(imageSize, (size) => {
|
|
expect(masonryRowSpan(size)).toBeGreaterThanOrEqual(
|
|
GAP_ROWS + 1,
|
|
);
|
|
}),
|
|
);
|
|
});
|
|
|
|
it('grows with the photo height relative to its width (invariant)', () => {
|
|
fc.assert(
|
|
fc.property(imageSize, imageSize, (a, b) => {
|
|
const ratioA = a.height / a.width;
|
|
const ratioB = b.height / b.width;
|
|
if (ratioA <= ratioB) {
|
|
expect(masonryRowSpan(a)).toBeLessThanOrEqual(
|
|
masonryRowSpan(b),
|
|
);
|
|
}
|
|
}),
|
|
);
|
|
});
|
|
|
|
it('depends on the shape, not the file size (invariant)', () => {
|
|
fc.assert(
|
|
fc.property(
|
|
imageSize,
|
|
fc.integer({ min: 2, max: 4 }),
|
|
(size, factor) => {
|
|
expect(
|
|
masonryRowSpan({
|
|
width: size.width * factor,
|
|
height: size.height * factor,
|
|
}),
|
|
).toBe(masonryRowSpan(size));
|
|
},
|
|
),
|
|
);
|
|
});
|
|
|
|
it('crops at most half a row (easy verification)', () => {
|
|
fc.assert(
|
|
fc.property(imageSize, (size) => {
|
|
const exact = (K * size.height) / size.width;
|
|
if (exact >= 1) {
|
|
const rows = masonryRowSpan(size) - GAP_ROWS;
|
|
expect(Math.abs(rows - exact)).toBeLessThanOrEqual(0.5);
|
|
}
|
|
}),
|
|
);
|
|
});
|
|
});
|