useFillTimeseriesSorted.test.ts 3.2 KB

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