Skip to content

Commit c39cf9b

Browse files
committed
types: fix test types
1 parent b30a1ab commit c39cf9b

File tree

1 file changed

+40
-20
lines changed

1 file changed

+40
-20
lines changed

tests/helpers/mock.ts

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
import { vi } from "vitest";
22

3-
type InitFn = (typeof import("echarts/core"))["init"];
4-
type ThrottleFn = (typeof import("echarts/core"))["throttle"];
3+
import type { Mock } from "vitest";
4+
import type {
5+
init as echartsInit,
6+
throttle as echartsThrottle,
7+
} from "echarts/core";
8+
import type { EChartsType } from "../../src/types";
9+
10+
type InitFn = typeof echartsInit;
11+
type ThrottleFn = typeof echartsThrottle;
512
type Throttled = ReturnType<ThrottleFn>;
613

714
export const init = vi.fn<InitFn>();
@@ -14,37 +21,50 @@ export function createEChartsModule() {
1421
} satisfies Partial<Record<string, unknown>>;
1522
}
1623

17-
export interface ChartStub {
18-
setOption: ReturnType<typeof vi.fn>;
19-
getOption: ReturnType<typeof vi.fn>;
20-
resize: ReturnType<typeof vi.fn>;
21-
dispose: ReturnType<typeof vi.fn>;
22-
isDisposed: ReturnType<typeof vi.fn>;
23-
getZr: ReturnType<typeof vi.fn>;
24-
on: ReturnType<typeof vi.fn>;
25-
off: ReturnType<typeof vi.fn>;
26-
setTheme: ReturnType<typeof vi.fn>;
27-
showLoading: ReturnType<typeof vi.fn>;
28-
hideLoading: ReturnType<typeof vi.fn>;
24+
type ZRenderStub = {
25+
on: Mock;
26+
off: Mock;
27+
};
28+
29+
type MockedMethod<T> = T extends (...args: infer Args) => infer R
30+
? Mock<(...args: Args) => R>
31+
: never;
32+
33+
type ChartMethodKeys =
34+
| "setOption"
35+
| "resize"
36+
| "dispose"
37+
| "isDisposed"
38+
| "setTheme"
39+
| "showLoading"
40+
| "hideLoading";
41+
42+
type ChartMethodMocks = {
43+
[K in ChartMethodKeys]: MockedMethod<EChartsType[K]>;
44+
};
45+
46+
export interface ChartStub extends ChartMethodMocks {
47+
getOption: Mock<() => unknown>;
48+
getZr: Mock<() => ZRenderStub>;
49+
on: Mock<(event: string, handler: (...args: unknown[]) => void) => void>;
50+
off: Mock<(event: string, handler: (...args: unknown[]) => void) => void>;
2951
group: string | undefined;
3052
}
3153

3254
const queue: ChartStub[] = [];
3355
let cursor = 0;
3456

3557
export function createChartStub(): ChartStub {
36-
const zr = {
58+
const zr: ZRenderStub = {
3759
on: vi.fn(),
3860
off: vi.fn(),
3961
};
4062
let lastOption: unknown;
4163

42-
const setOption = vi.fn((option: unknown) => {
43-
lastOption = option;
44-
});
45-
4664
return {
47-
setOption,
65+
setOption: vi.fn((option: unknown) => {
66+
lastOption = option;
67+
}),
4868
getOption: vi.fn(() => lastOption),
4969
resize: vi.fn(),
5070
dispose: vi.fn(),

0 commit comments

Comments
 (0)