Repository navigation
Expand file tree
/
Copy pathecharts-real-theme.browser.test.ts
More file actions
248 lines (212 loc) · 7.15 KB
/
Copy pathecharts-real-theme.browser.test.ts
File metadata and controls
248 lines (212 loc) · 7.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import { computed, defineComponent, h, nextTick, ref, shallowRef } from "vue";
import type { VNodeRef } from "vue";
import { describe, expect, it } from "vitest";
import { use, registerTheme } from "echarts/core";
import { GraphChart } from "echarts/charts";
import { LegendComponent, TooltipComponent } from "echarts/components";
import { SVGRenderer } from "echarts/renderers";
import type { ComponentExposed } from "vue-component-type-helpers";
import ECharts from "../src/ECharts";
import type { EChartsType, Option, Theme } from "../src/types";
import { render } from "./helpers/testing";
import { flushAnimationFrame } from "./helpers/dom";
use([SVGRenderer, GraphChart, LegendComponent, TooltipComponent]);
registerTheme("dark", { backgroundColor: "#111827" });
type Exposed = ComponentExposed<typeof ECharts>;
enum Category {
ROOT = "Root",
LEAF = "Leaf",
}
function createExposeSetter(exposed: { value?: Exposed }): VNodeRef {
return (value) => {
exposed.value = value ? (value as Exposed) : undefined;
};
}
function getChart(exposed: Exposed | undefined): EChartsType {
if (!exposed) {
throw new Error("Expected exposed instance to be defined.");
}
const raw = (exposed as { chart?: unknown }).chart;
const chart = typeof raw === "object" && raw && "value" in raw ? (raw as any).value : raw;
if (!chart) {
throw new Error("Expected chart instance to be defined.");
}
return chart as EChartsType;
}
function getSeriesDataLength(chart: EChartsType): number {
const option = chart.getOption() as {
series?: Array<{ data?: unknown[] }>;
};
return option.series?.[0]?.data?.length ?? 0;
}
function getSeriesCount(chart: EChartsType): number {
return (chart.getOption() as { series?: unknown[] }).series?.length ?? 0;
}
async function flushFrames(count = 3): Promise<void> {
for (let i = 0; i < count; i++) {
await flushAnimationFrame();
}
}
function buildGraphData() {
return [
{ id: "root", category: Category.ROOT, x: 0, y: 0, symbolSize: 80 },
{ id: "leaf-a", category: Category.LEAF, x: 100, y: 0, symbolSize: 36 },
{ id: "leaf-b", category: Category.LEAF, x: -100, y: 0, symbolSize: 36 },
];
}
function buildGraphLinks() {
return [
{ source: "root", target: "leaf-a" },
{ source: "root", target: "leaf-b" },
];
}
describe("ECharts theme behavior (real echarts)", () => {
it("applies a theme change made before the first option", async () => {
const option = ref<Option>();
const theme = ref<Theme>("dark");
const exposed = shallowRef<Exposed>();
const Root = defineComponent({
setup() {
return () =>
h(ECharts, {
option: option.value,
theme: theme.value,
style: "width: 640px; height: 420px;",
ref: createExposeSetter(exposed),
});
},
});
await render(Root);
await nextTick();
const chart = getChart(exposed.value);
expect(chart.getOption()).toBeUndefined();
theme.value = "";
await nextTick();
option.value = { series: [] };
await nextTick();
await flushFrames();
expect(chart.getOption().backgroundColor).not.toBe("#111827");
});
it("preserves the latest automatic option while its source is temporarily absent", async () => {
const theme = ref<Theme>("dark");
const option = ref<Option | undefined>({
series: [
{ id: "keep", type: "graph", data: [{ id: "initial" }] },
{ id: "remove", type: "graph", data: [] },
],
});
const exposed = shallowRef<Exposed>();
const Root = defineComponent({
setup() {
return () =>
h(ECharts, {
option: option.value,
theme: theme.value,
style: "width: 640px; height: 420px;",
ref: createExposeSetter(exposed),
});
},
});
await render(Root);
await nextTick();
option.value = {
series: [{ id: "keep", type: "graph", data: buildGraphData() }],
};
await nextTick();
const chart = getChart(exposed.value);
expect(getSeriesDataLength(chart)).toBe(3);
expect(getSeriesCount(chart)).toBe(1);
option.value = undefined;
await nextTick();
theme.value = "";
await nextTick();
expect(getSeriesDataLength(chart)).toBe(3);
expect(getSeriesCount(chart)).toBe(1);
});
it("resets an empty theme without losing existing graph data", async () => {
const isDark = ref(true);
const theme = computed(() => (isDark.value ? "dark" : ""));
const option = ref<Option>({
series: {
type: "graph",
layout: "none",
data: buildGraphData(),
links: buildGraphLinks(),
} as Option["series"],
});
const exposed = shallowRef<Exposed>();
const Root = defineComponent({
setup() {
return () =>
h(ECharts, {
option: option.value,
theme: theme.value,
style: "width: 640px; height: 420px;",
ref: createExposeSetter(exposed),
});
},
});
await render(Root);
await nextTick();
await flushFrames();
const chart = getChart(exposed.value);
expect(getSeriesDataLength(chart)).toBe(3);
expect(chart.getOption().backgroundColor).toBe("#111827");
isDark.value = false;
await nextTick();
await flushFrames();
expect(getSeriesDataLength(chart)).toBe(3);
expect(chart.getOption().backgroundColor).not.toBe("#111827");
});
});
describe("ECharts callback slots (real echarts)", () => {
it("keeps a callback through theme changes and preserves legend state after removal", async () => {
const showTooltipSlot = ref(true);
const theme = ref<Theme | undefined>("dark");
const option: Option = {
legend: { data: ["A", "B"] },
series: [
{ id: "a", name: "A", type: "graph", data: [] },
{ id: "b", name: "B", type: "graph", data: [] },
],
};
const exposed = shallowRef<Exposed>();
const Root = defineComponent({
setup() {
return () =>
h(
ECharts,
{
option,
theme: theme.value,
style: "width: 640px; height: 420px;",
ref: createExposeSetter(exposed),
},
showTooltipSlot.value
? { "tooltip-series-0": () => [h("span", "custom-tooltip")] }
: undefined,
);
},
});
await render(Root);
await nextTick();
const chart = getChart(exposed.value);
const getFormatter = () =>
(chart.getOption() as { series?: Array<{ tooltip?: { formatter?: unknown } }> }).series?.[0]
?.tooltip?.formatter;
expect(getFormatter()).toBeTypeOf("function");
theme.value = undefined;
await nextTick();
expect(getFormatter()).toBeTypeOf("function");
chart.dispatchAction({ type: "legendUnSelect", name: "B" });
const getLegendSelection = () =>
(chart.getOption() as { legend?: Array<{ selected?: Record<string, boolean> }> }).legend?.[0]
?.selected?.B;
expect(getLegendSelection()).toBe(false);
showTooltipSlot.value = false;
await nextTick();
await nextTick();
expect(getFormatter()).not.toBeTypeOf("function");
expect(getLegendSelection()).toBe(false);
});
});