78 lines
2.3 KiB
TypeScript
78 lines
2.3 KiB
TypeScript
import { describe, expect, it } from "vitest"
|
|
|
|
import type { SelectionChartPoint } from "../api/selection.types"
|
|
import { buildSelectionChartOption } from "./selection-chart-option"
|
|
|
|
function point(index: number): SelectionChartPoint {
|
|
return {
|
|
close: index + 10.5,
|
|
d: index + 20,
|
|
high: index + 11,
|
|
j: index + 30,
|
|
k: index + 10,
|
|
low: index + 9,
|
|
open: index + 10,
|
|
trade_date: `2026-${String(Math.floor(index / 28) + 1).padStart(2, "0")}-${String((index % 28) + 1).padStart(2, "0")}`,
|
|
volume: index + 1000,
|
|
}
|
|
}
|
|
|
|
describe("selection chart option", () => {
|
|
it("aligns all three grids and opens on exactly the last 120 points", () => {
|
|
const points = Array.from({ length: 250 }, (_, index) => point(index))
|
|
const option = buildSelectionChartOption(points)
|
|
const dataZoom = option.dataZoom as Array<{
|
|
endValue: string
|
|
startValue: string
|
|
xAxisIndex: number[]
|
|
}>
|
|
const series = option.series as Array<{
|
|
data: unknown[]
|
|
xAxisIndex: number
|
|
yAxisIndex: number
|
|
}>
|
|
|
|
expect(dataZoom).toHaveLength(2)
|
|
expect(dataZoom[0]).toMatchObject({
|
|
endValue: points[249].trade_date,
|
|
startValue: points[130].trade_date,
|
|
xAxisIndex: [0, 1, 2],
|
|
})
|
|
expect(dataZoom[1].xAxisIndex).toEqual([0, 1, 2])
|
|
expect(series[0]).toMatchObject({ xAxisIndex: 0, yAxisIndex: 0 })
|
|
expect(series[1]).toMatchObject({ xAxisIndex: 1, yAxisIndex: 1 })
|
|
expect(series.slice(2)).toEqual(
|
|
expect.arrayContaining([
|
|
expect.objectContaining({ xAxisIndex: 2, yAxisIndex: 2 }),
|
|
]),
|
|
)
|
|
expect(series[0].data[0]).toEqual([10, 10.5, 9, 11])
|
|
})
|
|
|
|
it("keeps missing candles and indicator values empty", () => {
|
|
const incomplete = {
|
|
...point(0),
|
|
close: null,
|
|
d: null,
|
|
high: null,
|
|
j: null,
|
|
k: null,
|
|
volume: null,
|
|
}
|
|
const option = buildSelectionChartOption([incomplete])
|
|
const series = option.series as Array<{
|
|
connectNulls?: boolean
|
|
data: unknown[]
|
|
}>
|
|
const candle = series[0].data[0] as number[]
|
|
|
|
expect(candle).toHaveLength(4)
|
|
expect(candle.every(Number.isNaN)).toBe(true)
|
|
expect(series[1].data[0]).toMatchObject({ value: null })
|
|
for (const indicator of series.slice(2)) {
|
|
expect(indicator.connectNulls).toBe(false)
|
|
expect(indicator.data).toEqual([null])
|
|
}
|
|
})
|
|
})
|