Files
AndreaSlopFotografie/web/src/lib/slideCounter.test.ts
T
SluijsensandClaude Sonnet 5.5 b03b18f58e Redesigns the testimonials section
A landscape photo overlaps a card with the shoot type, quote and names. The controls are two arrows with an X / Y counter; the dots are gone and nothing moves by itself. A change slides the whole slide horizontally, also on a swipe. Reviews get an optional imagePosition for the crop, normalised by normalizeImagePosition; slideCounter writes the counter. Both helpers have property tests next to example tests.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-06 01:24:14 +01:00

81 lines
2.5 KiB
TypeScript

import fc from 'fast-check';
import { describe, expect, it } from 'vitest';
import { slideCounter } from './slideCounter';
// A position within a carousel: 1 to 20 slides, index within range
const position = fc
.integer({ min: 1, max: 20 })
.chain((count) =>
fc
.integer({ min: 0, max: count - 1 })
.map((index) => ({ index, count })),
);
// Any number at all, including the odd ones
const anyNumber = fc.oneof(
fc.integer(),
fc.double(),
fc.constantFrom(
Number.NaN,
Infinity,
-Infinity,
0,
-0,
1.5,
Number.MAX_VALUE,
),
);
describe('slideCounter (examples)', () => {
it('counts from one', () => {
expect(slideCounter(0, 2)).toBe('1 / 2');
expect(slideCounter(1, 2)).toBe('2 / 2');
expect(slideCounter(4, 12)).toBe('5 / 12');
});
it('stays within the range for odd input', () => {
expect(slideCounter(5, 2)).toBe('2 / 2');
expect(slideCounter(-3, 2)).toBe('1 / 2');
expect(slideCounter(0, 0)).toBe('1 / 1');
expect(slideCounter(Number.NaN, 3)).toBe('1 / 3');
expect(slideCounter(1, Number.NaN)).toBe('1 / 1');
expect(slideCounter(1.9, 3)).toBe('2 / 3');
});
});
describe('slideCounter (properties)', () => {
it('always reads "X / Y" with 1 <= X <= Y, for any numbers (invariant)', () => {
fc.assert(
fc.property(anyNumber, anyNumber, (index, count) => {
const match = /^(\d+) \/ (\d+)$/.exec(
slideCounter(index, count),
);
expect(match).not.toBeNull();
const [x, y] = [Number(match?.[1]), Number(match?.[2])];
expect(x >= 1 && x <= y).toBe(true);
}),
);
});
it('gives back its numbers for a valid position (round-trip)', () => {
fc.assert(
fc.property(position, ({ index, count }) => {
const [, x, y] =
/^(\d+) \/ (\d+)$/.exec(slideCounter(index, count)) ?? [];
expect(Number(x)).toBe(index + 1);
expect(Number(y)).toBe(count);
}),
);
});
it('equals the plain template for a valid position (oracle)', () => {
fc.assert(
fc.property(position, ({ index, count }) => {
expect(slideCounter(index, count)).toBe(
`${index + 1} / ${count}`,
);
}),
);
});
});