Repository navigation
Expand file tree
/
Copy pathdateRange.test.ts
More file actions
143 lines (125 loc) · 4.58 KB
/
Copy pathdateRange.test.ts
File metadata and controls
143 lines (125 loc) · 4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import { describe, it, expect, vi, afterEach } from 'vitest';
import { formatDateRange, getDefaultDateRange, DEFAULT_DATE_RANGE } from './dateRange';
describe('formatDateRange', () => {
it('returns full date range for valid 4-digit year', () => {
const result = formatDateRange('2024');
expect(result).toEqual({
from: '2024-01-01T00:00:00Z',
to: '2024-12-31T23:59:59Z',
});
});
it('handles partial year with 2 digits - prepends 20', () => {
const result = formatDateRange('24');
expect(result).toEqual({
from: '2024-01-01T00:00:00Z',
to: '2024-12-31T23:59:59Z',
});
});
it('handles partial year with 1 digit - maps to 202x', () => {
const result = formatDateRange('4');
expect(result).toEqual({
from: '2024-01-01T00:00:00Z',
to: '2024-12-31T23:59:59Z',
});
});
it('returns fallback default range for empty string year', () => {
const result = formatDateRange('');
const currentYear = new Date().getUTCFullYear();
expect(result).toEqual({
from: `${currentYear}-01-01T00:00:00Z`,
to: `${currentYear}-12-31T23:59:59Z`,
});
});
it('returns fallback default range for undefined year', () => {
const result = formatDateRange(undefined);
const currentYear = new Date().getUTCFullYear();
expect(result).toEqual({
from: `${currentYear}-01-01T00:00:00Z`,
to: `${currentYear}-12-31T23:59:59Z`,
});
});
it('returns fallback default range for whitespace-only year', () => {
const result = formatDateRange(' ');
const currentYear = new Date().getUTCFullYear();
expect(result).toEqual({
from: `${currentYear}-01-01T00:00:00Z`,
to: `${currentYear}-12-31T23:59:59Z`,
});
});
it('returns fallback default range for year before GitHub founding (2008)', () => {
const result = formatDateRange('2007');
const currentYear = new Date().getUTCFullYear();
expect(result).toEqual({
from: `${currentYear}-01-01T00:00:00Z`,
to: `${currentYear}-12-31T23:59:59Z`,
});
});
it('returns fallback default range for far future year', () => {
const result = formatDateRange('3000');
const currentYear = new Date().getUTCFullYear();
expect(result).toEqual({
from: `${currentYear}-01-01T00:00:00Z`,
to: `${currentYear}-12-31T23:59:59Z`,
});
});
it('returns fallback default range for non-numeric year', () => {
const result = formatDateRange('abc');
const currentYear = new Date().getUTCFullYear();
expect(result).toEqual({
from: `${currentYear}-01-01T00:00:00Z`,
to: `${currentYear}-12-31T23:59:59Z`,
});
});
it('returns fallback default range for invalid numeric year', () => {
const result = formatDateRange('-1');
const currentYear = new Date().getUTCFullYear();
expect(result).toEqual({
from: `${currentYear}-01-01T00:00:00Z`,
to: `${currentYear}-12-31T23:59:59Z`,
});
});
it('returns correct range for GitHub founding year (2008)', () => {
const result = formatDateRange('2008');
expect(result).toEqual({
from: '2008-01-01T00:00:00Z',
to: '2008-12-31T23:59:59Z',
});
});
it('returns result object matching DateRange interface', () => {
const result = formatDateRange('2025');
expect(result).toHaveProperty('from');
expect(result).toHaveProperty('to');
expect(typeof result.from).toBe('string');
expect(typeof result.to).toBe('string');
});
it('getDefaultDateRange uses the current UTC year', () => {
const currentYear = new Date().getUTCFullYear();
const range = getDefaultDateRange();
expect(range.from).toBe(`${currentYear}-01-01T00:00:00Z`);
expect(range.to).toBe(`${currentYear}-12-31T23:59:59Z`);
});
it('DEFAULT_DATE_RANGE uses current UTC year', () => {
const currentYear = new Date().getUTCFullYear();
expect(DEFAULT_DATE_RANGE.from).toBe(`${currentYear}-01-01T00:00:00Z`);
expect(DEFAULT_DATE_RANGE.to).toBe(`${currentYear}-12-31T23:59:59Z`);
});
});
describe('getDefaultDateRange', () => {
afterEach(() => {
vi.useRealTimers();
});
it('reflects the current year lazily rather than a value captured at module load', () => {
vi.useFakeTimers();
vi.setSystemTime(new Date('2030-06-15T00:00:00Z'));
expect(getDefaultDateRange()).toEqual({
from: '2030-01-01T00:00:00Z',
to: '2030-12-31T23:59:59Z',
});
// Crossing into a new year must be reflected on the next call.
vi.setSystemTime(new Date('2031-01-02T00:00:00Z'));
expect(getDefaultDateRange()).toEqual({
from: '2031-01-01T00:00:00Z',
to: '2031-12-31T23:59:59Z',
});
});
});