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>
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
import fc from 'fast-check';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import {
|
||||
imagePositionAny,
|
||||
imagePositionInvalid,
|
||||
imagePositionValid,
|
||||
} from '../test/arbitraries';
|
||||
import { normalizeImagePosition } from './normalizeImagePosition';
|
||||
|
||||
const CENTRE = '50% 50%';
|
||||
|
||||
// The shape every result has: two numbers with at most two decimals
|
||||
const SHAPE = /^(\d+(?:\.\d{1,2})?)% (\d+(?:\.\d{1,2})?)%$/;
|
||||
|
||||
describe('normalizeImagePosition (examples)', () => {
|
||||
it('keeps a good value', () => {
|
||||
expect(normalizeImagePosition('50% 42%')).toBe('50% 42%');
|
||||
expect(normalizeImagePosition('0% 100%')).toBe('0% 100%');
|
||||
});
|
||||
|
||||
it('writes the numbers tidily and ignores extra whitespace', () => {
|
||||
expect(normalizeImagePosition('050.0% 42%')).toBe('50% 42%');
|
||||
expect(normalizeImagePosition(' 12.50% 7.25% ')).toBe(
|
||||
'12.5% 7.25%',
|
||||
);
|
||||
expect(normalizeImagePosition('50%\t42%')).toBe('50% 42%');
|
||||
});
|
||||
|
||||
it('rounds tiny values instead of writing an exponent', () => {
|
||||
expect(normalizeImagePosition('0.0000001% 50%')).toBe('0% 50%');
|
||||
});
|
||||
|
||||
it('falls back to the centre for anything else', () => {
|
||||
for (const bad of [
|
||||
'120% 40%',
|
||||
'50 42',
|
||||
'50%',
|
||||
'-5% 10%',
|
||||
'1e1% 5%',
|
||||
'',
|
||||
'center',
|
||||
]) {
|
||||
expect(normalizeImagePosition(bad)).toBe(CENTRE);
|
||||
}
|
||||
expect(normalizeImagePosition(undefined)).toBe(CENTRE);
|
||||
expect(normalizeImagePosition(null)).toBe(CENTRE);
|
||||
expect(normalizeImagePosition(42)).toBe(CENTRE);
|
||||
});
|
||||
});
|
||||
|
||||
describe('normalizeImagePosition (properties)', () => {
|
||||
it('always gives two numbers from 0 to 100 (invariant)', () => {
|
||||
fc.assert(
|
||||
fc.property(imagePositionAny, (value) => {
|
||||
const match = SHAPE.exec(normalizeImagePosition(value));
|
||||
expect(match).not.toBeNull();
|
||||
const [x, y] = [Number(match?.[1]), Number(match?.[2])];
|
||||
expect(x >= 0 && x <= 100 && y >= 0 && y <= 100).toBe(true);
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it('does not change a value that is already normalised (idempotence)', () => {
|
||||
fc.assert(
|
||||
fc.property(imagePositionAny, (value) => {
|
||||
const once = normalizeImagePosition(value);
|
||||
expect(normalizeImagePosition(once)).toBe(once);
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it('reads any layout of whitespace as the plain form (round-trip)', () => {
|
||||
fc.assert(
|
||||
fc.property(imagePositionValid, ({ x, y, text }) => {
|
||||
expect(normalizeImagePosition(text)).toBe(`${x}% ${y}%`);
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it('gives exactly the centre for a value that is not acceptable (easy verification)', () => {
|
||||
fc.assert(
|
||||
fc.property(imagePositionInvalid, (value) => {
|
||||
expect(normalizeImagePosition(value)).toBe(CENTRE);
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it('never throws, whatever it is given', () => {
|
||||
fc.assert(
|
||||
fc.property(fc.anything(), (value) => {
|
||||
expect(typeof normalizeImagePosition(value)).toBe('string');
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,23 @@
|
||||
// The centre of a photo, used whenever a crop position can not be trusted
|
||||
const CENTRE = '50% 50%';
|
||||
|
||||
// Two numbers with a percent sign, separated by whitespace. Digits and an
|
||||
// optional decimal part only: no sign, no exponent.
|
||||
const NUMBER = '(\\d{1,3}(?:\\.\\d+)?)';
|
||||
const POSITION = new RegExp(`^${NUMBER}%\\s+${NUMBER}%$`);
|
||||
|
||||
// Rounded to two decimals: enough for a crop, and it keeps tiny values from
|
||||
// being written in exponent form, which would not read back
|
||||
const tidy = (n: number) => Math.round(n * 100) / 100;
|
||||
|
||||
// The crop position of a photo (CSS object-position) from content that a CMS
|
||||
// may deliver later: "X% Y%" with X and Y from 0 to 100, or the centre when
|
||||
// the value is missing or not exactly that. Never throws (FR-TR-11/12).
|
||||
export function normalizeImagePosition(value: unknown): string {
|
||||
if (typeof value !== 'string') return CENTRE;
|
||||
const match = POSITION.exec(value.trim());
|
||||
if (!match) return CENTRE;
|
||||
const [x, y] = [Number(match[1]), Number(match[2])];
|
||||
if (x > 100 || y > 100) return CENTRE;
|
||||
return `${tidy(x)}% ${tidy(y)}%`;
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
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}`,
|
||||
);
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,13 @@
|
||||
// The visible counter under the testimonials, e.g. "2 / 3": the current
|
||||
// review (counted from 1) out of the total. index is zero-based. Odd input
|
||||
// is made safe instead of thrown at: a total below 1 or not a finite number
|
||||
// counts as 1, and an index outside the range moves to the nearest end, so
|
||||
// the result always reads X / Y with 1 <= X <= Y (FR-TR-12).
|
||||
export function slideCounter(index: number, count: number): string {
|
||||
const total = Number.isFinite(count)
|
||||
? Math.min(Math.max(1, Math.trunc(count)), Number.MAX_SAFE_INTEGER)
|
||||
: 1;
|
||||
const current = Number.isFinite(index) ? Math.trunc(index) : 0;
|
||||
const position = Math.min(Math.max(current, 0), total - 1);
|
||||
return `${position + 1} / ${total}`;
|
||||
}
|
||||
@@ -1,7 +1,12 @@
|
||||
import fc from 'fast-check';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import testimonials from '../content/testimonials';
|
||||
import { anyTestimonial, uniqueTestimonials } from '../test/arbitraries';
|
||||
import type { Testimonial } from '../content/types';
|
||||
import {
|
||||
anyTestimonial,
|
||||
imagePositionAny,
|
||||
uniqueTestimonials,
|
||||
} from '../test/arbitraries';
|
||||
import { isValidTestimonial, visibleTestimonials } from './visibleTestimonials';
|
||||
|
||||
const valid = testimonials[0];
|
||||
@@ -63,6 +68,31 @@ describe('visibleTestimonials', () => {
|
||||
);
|
||||
});
|
||||
|
||||
it('does not let the crop position decide what is shown (invariant)', () => {
|
||||
const ids = (items: Testimonial[]) =>
|
||||
visibleTestimonials(items).map((item) => item.id);
|
||||
fc.assert(
|
||||
fc.property(
|
||||
fc.array(anyTestimonial),
|
||||
fc.array(imagePositionAny, { minLength: 1, maxLength: 5 }),
|
||||
(input, positions) => {
|
||||
const withOther = input.map((item, index) => ({
|
||||
...item,
|
||||
imagePosition: positions[index % positions.length] as
|
||||
string | undefined,
|
||||
}));
|
||||
const withNone = input.map((item) => {
|
||||
const copy = { ...item };
|
||||
delete copy.imagePosition;
|
||||
return copy;
|
||||
});
|
||||
expect(ids(withOther)).toEqual(ids(input));
|
||||
expect(ids(withNone)).toEqual(ids(input));
|
||||
},
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
it('returns valid, unique reviews unchanged (oracle)', () => {
|
||||
fc.assert(
|
||||
fc.property(uniqueTestimonials, (input) => {
|
||||
|
||||
Reference in New Issue
Block a user