kimai-plugin-heatmap/assets/test/date-utils.test.ts

61 lines
2.1 KiB
TypeScript

import { describe, it, expect } from 'vitest';
import { buildDateMap, generateCells, getWeekInterval } from '../src/shared/date-utils';
import { timeMonday, timeSunday } from 'd3-time';
import type { DayEntry } from '../src/types';
describe('date-utils', () => {
const days: DayEntry[] = [
{ date: '2025-01-06', hours: 4, count: 2 },
{ date: '2025-01-07', hours: 2, count: 1 },
];
describe('buildDateMap', () => {
it('creates Map keyed by date string', () => {
const map = buildDateMap(days);
expect(map).toBeInstanceOf(Map);
expect(map.size).toBe(2);
expect(map.get('2025-01-06')).toEqual(days[0]);
expect(map.get('2025-01-07')).toEqual(days[1]);
expect(map.get('2025-01-08')).toBeUndefined();
});
});
describe('getWeekInterval', () => {
it('returns timeMonday for monday', () => {
expect(getWeekInterval('monday')).toBe(timeMonday);
});
it('returns timeSunday for sunday', () => {
expect(getWeekInterval('sunday')).toBe(timeSunday);
});
});
describe('generateCells', () => {
it('returns correct count for a 7-day range', () => {
const begin = new Date('2025-01-06'); // Monday
const end = new Date('2025-01-12'); // Sunday
const map = buildDateMap(days);
const cells = generateCells(begin, end, map, 'monday');
expect(cells.length).toBe(7);
});
it('marks weekend cells correctly', () => {
const begin = new Date('2025-01-06');
const end = new Date('2025-01-12');
const map = buildDateMap(days);
const cells = generateCells(begin, end, map, 'monday');
const weekendCells = cells.filter(c => c.isWeekend);
expect(weekendCells.length).toBe(2); // Saturday + Sunday
});
it('links entries from dateMap', () => {
const begin = new Date('2025-01-06');
const end = new Date('2025-01-08');
const map = buildDateMap(days);
const cells = generateCells(begin, end, map, 'monday');
expect(cells[0].entry).toEqual(days[0]);
expect(cells[1].entry).toEqual(days[1]);
expect(cells[2].entry).toBeNull();
});
});
});