import { vi } from "vitest"; type InitFn = (typeof import("echarts/core"))["init"]; type ThrottleFn = (typeof import("echarts/core"))["throttle"]; type Throttled = ReturnType; export const init = vi.fn(); export const throttle = vi.fn(); export function createEChartsModule() { return { init, throttle, } satisfies Partial>; } export interface ChartStub { setOption: ReturnType; resize: ReturnType; dispose: ReturnType; isDisposed: ReturnType; getZr: ReturnType; on: ReturnType; off: ReturnType; setTheme: ReturnType; showLoading: ReturnType; hideLoading: ReturnType; group: string | undefined; } const queue: ChartStub[] = []; let cursor = 0; export function createChartStub(): ChartStub { const zr = { on: vi.fn(), off: vi.fn(), }; return { setOption: vi.fn(), resize: vi.fn(), dispose: vi.fn(), isDisposed: vi.fn(() => false), getZr: vi.fn(() => zr), on: vi.fn(), off: vi.fn(), setTheme: vi.fn(), showLoading: vi.fn(), hideLoading: vi.fn(), group: undefined, }; } function ensureStub(): ChartStub { if (cursor >= queue.length) { queue.push(createChartStub()); } return queue[cursor++]; } const defaultThrottleImplementation: ThrottleFn = ((fn: any) => { const wrapped = ((...args: any[]) => fn(...args)) as Throttled; (wrapped as any).clear = vi.fn(); (wrapped as any).dispose = vi.fn(); (wrapped as any).pending = vi.fn(() => false); return wrapped; }) as ThrottleFn; export function resetECharts(): void { queue.length = 0; cursor = 0; init.mockReset(); throttle.mockReset(); init.mockImplementation(((...args: Parameters) => { void args; return ensureStub() as unknown as ReturnType; }) as InitFn); throttle.mockImplementation(defaultThrottleImplementation); } export function enqueueChart(): ChartStub { const stub = createChartStub(); queue.push(stub); return stub; } resetECharts();