QueryBlock.utils.test.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import { describe, expect, it } from 'vitest'
  2. import {
  3. checkHasNonPositiveValues,
  4. formatLogTick,
  5. getCumulativeResults,
  6. } from '@/components/ui/QueryBlock/QueryBlock.utils'
  7. describe('checkHasNonPositiveValues', () => {
  8. it('returns false for an empty array', () => {
  9. expect(checkHasNonPositiveValues([], 'value')).toBe(false)
  10. })
  11. it('returns false when all values are positive', () => {
  12. const data = [{ value: 1 }, { value: 2 }, { value: 100 }]
  13. expect(checkHasNonPositiveValues(data, 'value')).toBe(false)
  14. })
  15. it('returns true when a value is zero', () => {
  16. const data = [{ value: 1 }, { value: 0 }, { value: 3 }]
  17. expect(checkHasNonPositiveValues(data, 'value')).toBe(true)
  18. })
  19. it('returns true when a value is negative', () => {
  20. const data = [{ value: 5 }, { value: -1 }, { value: 3 }]
  21. expect(checkHasNonPositiveValues(data, 'value')).toBe(true)
  22. })
  23. it('returns true when all values are non-positive', () => {
  24. const data = [{ value: -5 }, { value: 0 }, { value: -1 }]
  25. expect(checkHasNonPositiveValues(data, 'value')).toBe(true)
  26. })
  27. it('checks only the specified key', () => {
  28. const data = [
  29. { x: -1, y: 5 },
  30. { x: 2, y: 10 },
  31. ]
  32. expect(checkHasNonPositiveValues(data, 'y')).toBe(false)
  33. expect(checkHasNonPositiveValues(data, 'x')).toBe(true)
  34. })
  35. it('returns false when key is absent (undefined cast to NaN is not <= 0)', () => {
  36. const data = [{ value: 1 }, { value: 2 }]
  37. expect(checkHasNonPositiveValues(data, 'missing')).toBe(false)
  38. })
  39. })
  40. describe('formatLogTick', () => {
  41. it('formats values below 1,000 as plain locale strings', () => {
  42. expect(formatLogTick(0)).toBe('0')
  43. expect(formatLogTick(1)).toBe('1')
  44. expect(formatLogTick(999)).toBe('999')
  45. expect(formatLogTick(500)).toBe('500')
  46. })
  47. it('formats values >= 1,000 with a "k" suffix', () => {
  48. expect(formatLogTick(1_000)).toBe('1k')
  49. expect(formatLogTick(1_500)).toBe('1.5k')
  50. expect(formatLogTick(10_000)).toBe('10k')
  51. expect(formatLogTick(999_999)).toBe('1,000k')
  52. })
  53. it('formats values >= 1,000,000 with an "M" suffix', () => {
  54. expect(formatLogTick(1_000_000)).toBe('1M')
  55. expect(formatLogTick(1_500_000)).toBe('1.5M')
  56. expect(formatLogTick(10_000_000)).toBe('10M')
  57. expect(formatLogTick(1_234_567)).toBe('1.2M')
  58. })
  59. it('respects maximumFractionDigits of 1', () => {
  60. // 1,050 → 1.05k, but max 1 decimal → "1.1k" (rounded)
  61. expect(formatLogTick(1_050)).toBe('1.1k')
  62. // 1,049 → 1.049k → "1k" (rounded down)
  63. expect(formatLogTick(1_049)).toBe('1k')
  64. })
  65. })
  66. describe('getCumulativeResults', () => {
  67. it('returns empty array when results are empty', () => {
  68. expect(
  69. getCumulativeResults(
  70. { rows: [] },
  71. { type: 'bar', xKey: 'x', yKey: 'y', cumulative: false, showLabels: false, showGrid: false }
  72. )
  73. ).toEqual([])
  74. })
  75. it('returns empty array when results are undefined', () => {
  76. expect(
  77. getCumulativeResults(undefined as any, {
  78. type: 'bar',
  79. xKey: 'x',
  80. yKey: 'y',
  81. cumulative: false,
  82. showLabels: false,
  83. showGrid: false,
  84. })
  85. ).toEqual([])
  86. })
  87. it('accumulates yKey values across rows', () => {
  88. const results = {
  89. rows: [
  90. { x: 'a', y: 10 },
  91. { x: 'b', y: 20 },
  92. { x: 'c', y: 5 },
  93. ],
  94. }
  95. const config = {
  96. type: 'bar' as const,
  97. xKey: 'x',
  98. yKey: 'y',
  99. cumulative: true,
  100. showLabels: false,
  101. showGrid: false,
  102. }
  103. const output = getCumulativeResults(results, config)
  104. expect(output).toEqual([
  105. { x: 'a', y: 10 },
  106. { x: 'b', y: 30 },
  107. { x: 'c', y: 35 },
  108. ])
  109. })
  110. it('preserves other keys on each row', () => {
  111. const results = {
  112. rows: [
  113. { x: 'a', y: 1, label: 'foo' },
  114. { x: 'b', y: 2, label: 'bar' },
  115. ],
  116. }
  117. const config = {
  118. type: 'bar' as const,
  119. xKey: 'x',
  120. yKey: 'y',
  121. cumulative: true,
  122. showLabels: false,
  123. showGrid: false,
  124. }
  125. const output = getCumulativeResults(results, config)
  126. expect(output[0]).toMatchObject({ x: 'a', y: 1, label: 'foo' })
  127. expect(output[1]).toMatchObject({ x: 'b', y: 3, label: 'bar' })
  128. })
  129. it('handles a single row', () => {
  130. const results = { rows: [{ x: 'a', y: 42 }] }
  131. const config = {
  132. type: 'bar' as const,
  133. xKey: 'x',
  134. yKey: 'y',
  135. cumulative: true,
  136. showLabels: false,
  137. showGrid: false,
  138. }
  139. const output = getCumulativeResults(results, config)
  140. expect(output).toEqual([{ x: 'a', y: 42 }])
  141. })
  142. })