types: fix test types

This commit is contained in:
Justineo
2025-11-10 18:10:24 +08:00
committed by GU Yiling
parent b30a1ab2b5
commit c39cf9bde7

View File

@@ -1,7 +1,14 @@
import { vi } from "vitest"; import { vi } from "vitest";
type InitFn = (typeof import("echarts/core"))["init"]; import type { Mock } from "vitest";
type ThrottleFn = (typeof import("echarts/core"))["throttle"]; import type {
init as echartsInit,
throttle as echartsThrottle,
} from "echarts/core";
import type { EChartsType } from "../../src/types";
type InitFn = typeof echartsInit;
type ThrottleFn = typeof echartsThrottle;
type Throttled = ReturnType<ThrottleFn>; type Throttled = ReturnType<ThrottleFn>;
export const init = vi.fn<InitFn>(); export const init = vi.fn<InitFn>();
@@ -14,18 +21,33 @@ export function createEChartsModule() {
} satisfies Partial<Record<string, unknown>>; } satisfies Partial<Record<string, unknown>>;
} }
export interface ChartStub { type ZRenderStub = {
setOption: ReturnType<typeof vi.fn>; on: Mock;
getOption: ReturnType<typeof vi.fn>; off: Mock;
resize: ReturnType<typeof vi.fn>; };
dispose: ReturnType<typeof vi.fn>;
isDisposed: ReturnType<typeof vi.fn>; type MockedMethod<T> = T extends (...args: infer Args) => infer R
getZr: ReturnType<typeof vi.fn>; ? Mock<(...args: Args) => R>
on: ReturnType<typeof vi.fn>; : never;
off: ReturnType<typeof vi.fn>;
setTheme: ReturnType<typeof vi.fn>; type ChartMethodKeys =
showLoading: ReturnType<typeof vi.fn>; | "setOption"
hideLoading: ReturnType<typeof vi.fn>; | "resize"
| "dispose"
| "isDisposed"
| "setTheme"
| "showLoading"
| "hideLoading";
type ChartMethodMocks = {
[K in ChartMethodKeys]: MockedMethod<EChartsType[K]>;
};
export interface ChartStub extends ChartMethodMocks {
getOption: Mock<() => unknown>;
getZr: Mock<() => ZRenderStub>;
on: Mock<(event: string, handler: (...args: unknown[]) => void) => void>;
off: Mock<(event: string, handler: (...args: unknown[]) => void) => void>;
group: string | undefined; group: string | undefined;
} }
@@ -33,18 +55,16 @@ const queue: ChartStub[] = [];
let cursor = 0; let cursor = 0;
export function createChartStub(): ChartStub { export function createChartStub(): ChartStub {
const zr = { const zr: ZRenderStub = {
on: vi.fn(), on: vi.fn(),
off: vi.fn(), off: vi.fn(),
}; };
let lastOption: unknown; let lastOption: unknown;
const setOption = vi.fn((option: unknown) => {
lastOption = option;
});
return { return {
setOption, setOption: vi.fn((option: unknown) => {
lastOption = option;
}),
getOption: vi.fn(() => lastOption), getOption: vi.fn(() => lastOption),
resize: vi.fn(), resize: vi.fn(),
dispose: vi.fn(), dispose: vi.fn(),