Files
FitOS/app/test/availability.test.ts

165 lines
4.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { describe, it, expect } from 'vitest';
import {
formatAvailability,
formatAvailabilityParts,
formatDayRange,
formatDayRangeClock,
clockTime,
shortTime,
emptyAvailability,
} from '../src/lib/availability';
import type { DayAvailability } from '../src/api/types';
const day = (start: string, end: string): DayAvailability => ({ start, end });
const week = (...days: (DayAvailability | null)[]): (DayAvailability | null)[] => [
...days,
...Array(Math.max(0, 7 - days.length)).fill(null),
];
describe('formatAvailability', () => {
it('returns empty string for null, empty and all-null input', () => {
expect(formatAvailability(null)).toBe('');
expect(formatAvailability(undefined)).toBe('');
expect(formatAvailability([])).toBe('');
expect(formatAvailability(emptyAvailability())).toBe('');
});
it('formats a single day without a dash', () => {
expect(formatAvailability(week(day('06:00', '22:00')))).toBe('Pon 06:00–22:00');
});
it('groups consecutive days with identical hours', () => {
expect(
formatAvailability(
week(
day('18:00', '20:00'),
day('18:00', '20:00'),
day('18:00', '20:00'),
day('18:00', '20:00'),
day('18:00', '20:00'),
),
),
).toBe('Pon–Pt 18:00–20:00');
});
it('separates a trailing day with different hours', () => {
expect(
formatAvailability(
week(
day('18:00', '20:00'),
day('18:00', '20:00'),
day('18:00', '20:00'),
day('18:00', '20:00'),
day('18:00', '20:00'),
day('12:00', '14:00'),
),
),
).toBe('Pon–Pt 18:00–20:00, Sob 12:00–14:00');
});
it('groups all seven days', () => {
expect(
formatAvailability(
week(
day('18:00', '20:00'),
day('18:00', '20:00'),
day('18:00', '20:00'),
day('18:00', '20:00'),
day('18:00', '20:00'),
day('18:00', '20:00'),
day('18:00', '20:00'),
),
),
).toBe('Pon–Niedz 18:00–20:00');
});
it('splits runs broken by a gap in the middle', () => {
expect(
formatAvailability(
week(
day('18:00', '20:00'),
day('18:00', '20:00'),
null,
day('18:00', '20:00'),
day('18:00', '20:00'),
),
),
).toBe('Pon–Wt 18:00–20:00, Czw–Pt 18:00–20:00');
});
it('does not group alternating hours', () => {
const hours = ['06:00', '07:00', '08:00', '09:00', '10:00', '11:00', '12:00'];
expect(formatAvailability(week(...hours.map((h) => day(h, '22:00'))))).toBe(
'Pon 06:00–22:00, Wt 07:00–22:00, Śr 08:00–22:00, Czw 09:00–22:00, Pt 10:00–22:00, Sob 11:00–22:00, Niedz 12:00–22:00',
);
});
it('does not group a wrap-around range (Pt + Pon)', () => {
expect(
formatAvailability(
week(null, null, null, null, day('18:00', '20:00'), null, day('18:00', '20:00')),
),
).toBe('Pt 18:00–20:00, Niedz 18:00–20:00');
});
});
describe('formatAvailabilityParts', () => {
it('returns an empty array when there is no availability', () => {
expect(formatAvailabilityParts(null)).toEqual([]);
expect(formatAvailabilityParts(emptyAvailability())).toEqual([]);
});
it('returns one entry per run of consecutive same-time days', () => {
expect(
formatAvailabilityParts(
week(
day('18:00', '20:00'),
day('18:00', '20:00'),
day('18:00', '20:00'),
day('18:00', '20:00'),
day('18:00', '20:00'),
day('12:00', '14:00'),
),
),
).toEqual(['Pon–Pt 18:00–20:00', 'Sob 12:00–14:00']);
});
it('joins to the same string as formatAvailability', () => {
const value = week(
day('18:00', '20:00'),
day('18:00', '20:00'),
null,
day('18:00', '20:00'),
day('18:00', '20:00'),
);
expect(formatAvailabilityParts(value).join(', ')).toBe(formatAvailability(value));
});
});
describe('shortTime / formatDayRange', () => {
it('drops :00 minutes but keeps half hours', () => {
expect(shortTime('18:00')).toBe('18');
expect(shortTime('21:30')).toBe('21:30');
});
it('formats compact day ranges for the week grid', () => {
expect(formatDayRange(day('18:00', '20:00'))).toBe('18–20');
expect(formatDayRange(day('18:00', '21:30'))).toBe('18–21:30');
expect(formatDayRange(day('06:30', '22:00'))).toBe('06:30–22');
});
});
describe('clockTime / formatDayRangeClock', () => {
it('keeps minutes and only drops the leading zero', () => {
expect(clockTime('06:00')).toBe('6:00');
expect(clockTime('22:00')).toBe('22:00');
expect(clockTime('06:30')).toBe('6:30');
});
it('formats day ranges with minutes for the hours band', () => {
expect(formatDayRangeClock(day('06:00', '22:00'))).toBe('6:00–22:00');
expect(formatDayRangeClock(day('06:30', '21:45'))).toBe('6:30–21:45');
});
});