Files
feuerwerk/tests/ui/math.test.ts
T

36 lines
1.1 KiB
TypeScript

import { describe, expect, it } from 'vitest';
import { clamp, centreY, zoomFactor } from '../../src/ui/math';
describe('clamp', () => {
it('keeps a value inside the range', () => {
expect(clamp(5, 0, 10)).toBe(5);
expect(clamp(-1, 0, 10)).toBe(0);
expect(clamp(11, 0, 10)).toBe(10);
});
it('lets the lower bound win when the range is inverted', () => {
// A popup wider than the screen: pinning it to the left edge beats pinning it to the right.
expect(clamp(5, 10, 0)).toBe(10);
});
});
describe('centreY', () => {
it('centres on whole pixels', () => {
expect(centreY(30, 20)).toBe(5);
expect(centreY(31, 20)).toBe(6);
});
});
describe('zoomFactor', () => {
it('zooms in for a negative delta and out for a positive one', () => {
expect(zoomFactor(-100)).toBeGreaterThan(1);
expect(zoomFactor(100)).toBeLessThan(1);
expect(zoomFactor(0)).toBe(1);
});
it('returns exactly to the starting zoom after scrolling back', () => {
// Why exponential: the scene view and the graph editor both rely on this cancelling out.
expect(1 * zoomFactor(-120) * zoomFactor(120)).toBeCloseTo(1, 12);
});
});