useFillTimeseriesSorted.test.ts106 lines · main
1import { describe, expect, it } from 'vitest'
2
3import { hasValidTimestamp, sortByTimestamp } from './useFillTimeseriesSorted'
4
5describe('hasValidTimestamp', () => {
6 it('should return true when data has valid timestamp key', () => {
7 const data = [{ timestamp: '2024-01-01T00:00:00Z', value: 1 }]
8 expect(hasValidTimestamp(data, 'timestamp')).toBe(true)
9 })
10
11 it('should return false when data is empty', () => {
12 expect(hasValidTimestamp([], 'timestamp')).toBe(false)
13 })
14
15 it('should return false when timestamp key does not exist', () => {
16 const data = [{ value: 1 }]
17 expect(hasValidTimestamp(data, 'timestamp')).toBe(false)
18 })
19
20 it('should work with custom timestamp key', () => {
21 const data = [{ period_start: '2024-01-01T00:00:00Z', value: 1 }]
22 expect(hasValidTimestamp(data, 'period_start')).toBe(true)
23 })
24})
25
26describe('sortByTimestamp', () => {
27 it('should sort data in ascending order by timestamp', () => {
28 const data = [
29 { timestamp: '2024-01-03T00:00:00Z', value: 3 },
30 { timestamp: '2024-01-01T00:00:00Z', value: 1 },
31 { timestamp: '2024-01-02T00:00:00Z', value: 2 },
32 ]
33
34 const sorted = sortByTimestamp(data, 'timestamp')
35
36 expect(sorted[0].value).toBe(1)
37 expect(sorted[1].value).toBe(2)
38 expect(sorted[2].value).toBe(3)
39 })
40
41 it('should handle already sorted data', () => {
42 const data = [
43 { timestamp: '2024-01-01T00:00:00Z', value: 1 },
44 { timestamp: '2024-01-02T00:00:00Z', value: 2 },
45 ]
46
47 const sorted = sortByTimestamp(data, 'timestamp')
48
49 expect(sorted[0].value).toBe(1)
50 expect(sorted[1].value).toBe(2)
51 })
52
53 it('should handle empty array', () => {
54 const sorted = sortByTimestamp([], 'timestamp')
55 expect(sorted).toEqual([])
56 })
57
58 it('should handle single item', () => {
59 const data = [{ timestamp: '2024-01-01T00:00:00Z', value: 1 }]
60 const sorted = sortByTimestamp(data, 'timestamp')
61 expect(sorted).toEqual(data)
62 })
63
64 it('should work with custom timestamp key', () => {
65 const data = [
66 { period_start: '2024-01-03T00:00:00Z', value: 3 },
67 { period_start: '2024-01-01T00:00:00Z', value: 1 },
68 { period_start: '2024-01-02T00:00:00Z', value: 2 },
69 ]
70
71 const sorted = sortByTimestamp(data, 'period_start')
72
73 expect(sorted[0].value).toBe(1)
74 expect(sorted[1].value).toBe(2)
75 expect(sorted[2].value).toBe(3)
76 })
77
78 it('should handle timestamps with different time zones', () => {
79 const data = [
80 { timestamp: '2024-01-01T12:00:00Z', value: 2 },
81 { timestamp: '2024-01-01T00:00:00Z', value: 1 },
82 { timestamp: '2024-01-01T18:00:00Z', value: 3 },
83 ]
84
85 const sorted = sortByTimestamp(data, 'timestamp')
86
87 expect(sorted[0].value).toBe(1)
88 expect(sorted[1].value).toBe(2)
89 expect(sorted[2].value).toBe(3)
90 })
91
92 it('should maintain stable sort for equal timestamps', () => {
93 const data = [
94 { timestamp: '2024-01-01T00:00:00Z', value: 1, id: 'a' },
95 { timestamp: '2024-01-01T00:00:00Z', value: 2, id: 'b' },
96 { timestamp: '2024-01-01T00:00:00Z', value: 3, id: 'c' },
97 ]
98
99 const sorted = sortByTimestamp(data, 'timestamp')
100
101 // Should maintain original order for equal timestamps
102 expect(sorted[0].id).toBe('a')
103 expect(sorted[1].id).toBe('b')
104 expect(sorted[2].id).toBe('c')
105 })
106})