-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChartContainer.test.jsx
More file actions
215 lines (196 loc) · 6.59 KB
/
Copy pathChartContainer.test.jsx
File metadata and controls
215 lines (196 loc) · 6.59 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
import React from 'react';
import { render, screen, fireEvent } from '@testing-library/react';
import { describe, it, expect, vi } from 'vitest';
import ChartContainer from '../ChartContainer';
import i18n from '../../i18n';
// Mock chart.js and react-chartjs-2 to avoid canvas requirements
vi.mock('chart.js', () => {
const Chart = {
register: vi.fn(),
defaults: { plugins: { legend: { labels: { generateLabels: vi.fn(() => []) } } } }
};
return {
Chart,
ChartJS: Chart,
CategoryScale: {},
LinearScale: {},
PointElement: {},
LineElement: {},
Title: {},
Tooltip: {},
Legend: {},
};
});
vi.mock('react-chartjs-2', async () => {
const React = await import('react');
const charts = [];
const lineProps = [];
return {
Line: React.forwardRef((props, ref) => {
lineProps.push(props);
const chart = {
data: props.data,
setActiveElements: vi.fn(),
tooltip: { setActiveElements: vi.fn() },
draw: vi.fn(),
scales: { x: { getPixelForValue: vi.fn(() => 0) } },
};
charts.push(chart);
if (typeof ref === 'function') ref(chart);
return <div data-testid="line-chart" />;
}),
__charts: charts,
__lineProps: lineProps,
};
});
import { __charts, __lineProps } from 'react-chartjs-2';
vi.mock('chartjs-plugin-zoom', () => ({ default: {} }));
describe('ChartContainer', () => {
it('prompts to upload files when none provided', () => {
const onXRangeChange = vi.fn();
const onMaxStepChange = vi.fn();
render(
<ChartContainer
files={[]}
metrics={[{ name: 'loss', keyword: 'loss', mode: 'keyword' }]}
compareMode="normal"
onXRangeChange={onXRangeChange}
onMaxStepChange={onMaxStepChange}
/>
);
screen.getByText(i18n.t('chart.uploadPrompt'));
expect(onMaxStepChange).toHaveBeenCalledWith(0);
});
it('prompts to select metrics when none provided', () => {
const onXRangeChange = vi.fn();
const onMaxStepChange = vi.fn();
const files = [{ name: 'a.log', enabled: true, content: 'loss: 1' }];
render(
<ChartContainer
files={files}
metrics={[]}
compareMode="normal"
onXRangeChange={onXRangeChange}
onMaxStepChange={onMaxStepChange}
/>
);
screen.getByText(i18n.t('chart.selectPrompt'));
});
it('renders charts and statistics', async () => {
const onXRangeChange = vi.fn();
const onMaxStepChange = vi.fn();
const files = [
{ name: 'a.log', enabled: true, content: 'loss: 1\nloss: 2' },
{ name: 'b.log', enabled: true, content: 'loss: 1.5\nloss: 2.5' },
];
render(
<ChartContainer
files={files}
metrics={[{ name: 'loss', keyword: 'loss', mode: 'keyword' }]}
compareMode="relative"
onXRangeChange={onXRangeChange}
onMaxStepChange={onMaxStepChange}
/>
);
screen.getByText('📊 loss');
screen.getByText(new RegExp(i18n.t('chart.diffStats')));
expect(onMaxStepChange).toHaveBeenCalledWith(1);
// interaction and tooltip should use nearest mode for precise point selection
const opts = __lineProps[0].options;
expect(opts.interaction.mode).toBe('nearest');
expect(opts.interaction.axis).toBe('x');
expect(opts.plugins.tooltip.mode).toBe('nearest');
expect(opts.plugins.tooltip.axis).toBe('x');
expect(opts.scales.y.ticks.stepSize).toBe(0.1);
// simulate hover to trigger sync
const hover = __lineProps[0].options.onHover;
hover({}, [{ index: 0, datasetIndex: 0 }]);
expect(__charts[1].setActiveElements).toHaveBeenCalled();
expect(__charts[1].draw).toHaveBeenCalled();
});
it('parses metrics, applies range and triggers callbacks', () => {
const onXRangeChange = vi.fn();
const onMaxStepChange = vi.fn();
const files = [
{
name: 'a.log',
enabled: true,
content: 'loss: 1\nloss: 2\nloss: 3\nacc: 4\nacc: 5',
config: { dataRange: { start: 1, end: 3 } }
},
{
name: 'b.log',
enabled: true,
content: 'loss: 2\nloss: 4\nacc: 6\nacc: 8',
config: { dataRange: { start: 1, end: 3 } }
}
];
const metrics = [
{ keyword: 'loss', mode: 'keyword' },
{ regex: 'acc:(\\d+)', mode: 'regex' },
{}
];
const startIndex = __lineProps.length;
render(
<ChartContainer
files={files}
metrics={metrics}
compareMode="relative"
onXRangeChange={onXRangeChange}
onMaxStepChange={onMaxStepChange}
/>
);
// metric titles
expect(screen.getAllByText(/loss/)[0]).toBeTruthy();
screen.getByText(/metric2/);
screen.getByText(/metric3/);
// data range applied (start 1 end 3 => 2 points for loss)
const currentProps = __lineProps.slice(startIndex);
expect(currentProps[0].data.datasets[0].data).toHaveLength(2);
expect(currentProps[0].options.scales.y.ticks.stepSize).toBe(1);
// trigger container mouse leave
const container = screen.getAllByTestId('line-chart')[0].parentElement;
fireEvent.mouseLeave(container);
// invoke legend and tooltip callbacks
const opts = currentProps[0].options;
opts.plugins.legend.labels.generateLabels({ data: { datasets: [{}, { borderDash: [5,5] }] } });
const tt = opts.plugins.tooltip.callbacks;
tt.title([{ parsed: { x: 1 } }]);
tt.label({ parsed: { y: 1.2345 } });
tt.labelColor({ dataset: { borderColor: '#fff' } });
// invoke zoom callbacks
opts.plugins.zoom.pan.onPanComplete({ chart: { scales: { x: { min: 0, max: 10 } } } });
opts.plugins.zoom.zoom.onZoomComplete({ chart: { scales: { x: { min: 2, max: 4 } } } });
});
it('uses per-file metric configuration when provided', () => {
const onXRangeChange = vi.fn();
const onMaxStepChange = vi.fn();
const files = [
{ name: 'a.log', enabled: true, content: 'loss: 1\nloss: 2' },
{
name: 'b.log',
enabled: true,
content: 'train_loss: 3\ntrain_loss: 4',
config: { metrics: [{ mode: 'keyword', keyword: 'train_loss:' }] }
}
];
const metrics = [{ name: 'loss', mode: 'keyword', keyword: 'loss:' }];
render(
<ChartContainer
files={files}
metrics={metrics}
compareMode="normal"
onXRangeChange={onXRangeChange}
onMaxStepChange={onMaxStepChange}
/>
);
const mainChart = [...__lineProps].reverse().find(p =>
p.data.datasets && p.data.datasets.some(d => d.label === 'b')
);
const ds = mainChart.data.datasets.find(d => d.label === 'b');
expect(ds.data).toEqual([
{ x: 0, y: 3 },
{ x: 1, y: 4 }
]);
});
});