72 lines
1.9 KiB
TypeScript
72 lines
1.9 KiB
TypeScript
|
|
import { beforeEach, describe, expect, it, vi } from "vitest"
|
||
|
|
|
||
|
|
const useQuery = vi.hoisted(() => vi.fn())
|
||
|
|
const api = vi.hoisted(() => ({
|
||
|
|
getSectorRadarDates: vi.fn(),
|
||
|
|
getSectorRadarRankings: vi.fn(),
|
||
|
|
}))
|
||
|
|
|
||
|
|
vi.mock("@tanstack/react-query", () => ({ useQuery }))
|
||
|
|
vi.mock("./sector-radar.api", () => api)
|
||
|
|
|
||
|
|
import {
|
||
|
|
sectorRadarDatesQueryKey,
|
||
|
|
sectorRadarRankingsQueryKey,
|
||
|
|
useSectorRadarDates,
|
||
|
|
useSectorRadarRankings,
|
||
|
|
} from "./sector-radar.query"
|
||
|
|
|
||
|
|
const query = {
|
||
|
|
tradeDate: "2026-08-28",
|
||
|
|
sectorType: "industry" as const,
|
||
|
|
view: "rank_change" as const,
|
||
|
|
rankChangeMetric: "ratio" as const,
|
||
|
|
rankChangeDays: 3,
|
||
|
|
side: "bottom" as const,
|
||
|
|
search: "银行",
|
||
|
|
page: 2,
|
||
|
|
pageSize: 50,
|
||
|
|
}
|
||
|
|
|
||
|
|
describe("sector radar query hooks", () => {
|
||
|
|
beforeEach(() => {
|
||
|
|
vi.clearAllMocks()
|
||
|
|
useQuery.mockImplementation((options) => options)
|
||
|
|
})
|
||
|
|
|
||
|
|
it("keeps every server-affecting filter in the ranking query key", () => {
|
||
|
|
expect(sectorRadarDatesQueryKey).toEqual(["sectorRadar", "dates"])
|
||
|
|
expect(sectorRadarRankingsQueryKey(query)).toEqual([
|
||
|
|
"sectorRadar",
|
||
|
|
"rankings",
|
||
|
|
"2026-08-28",
|
||
|
|
"industry",
|
||
|
|
"rank_change",
|
||
|
|
"ratio",
|
||
|
|
3,
|
||
|
|
"bottom",
|
||
|
|
"银行",
|
||
|
|
2,
|
||
|
|
50,
|
||
|
|
])
|
||
|
|
})
|
||
|
|
|
||
|
|
it("forwards React Query cancellation signals to both adapters", async () => {
|
||
|
|
useSectorRadarDates()
|
||
|
|
useSectorRadarRankings(query)
|
||
|
|
const datesOptions = useQuery.mock.calls[0]?.[0] as {
|
||
|
|
queryFn: (context: { signal: AbortSignal }) => unknown
|
||
|
|
}
|
||
|
|
const rankingsOptions = useQuery.mock.calls[1]?.[0] as {
|
||
|
|
queryFn: (context: { signal: AbortSignal }) => unknown
|
||
|
|
}
|
||
|
|
const signal = new AbortController().signal
|
||
|
|
|
||
|
|
await datesOptions.queryFn({ signal })
|
||
|
|
await rankingsOptions.queryFn({ signal })
|
||
|
|
|
||
|
|
expect(api.getSectorRadarDates).toHaveBeenCalledWith(signal)
|
||
|
|
expect(api.getSectorRadarRankings).toHaveBeenCalledWith(query, signal)
|
||
|
|
})
|
||
|
|
})
|