-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathBarChartPro.zoom.test.tsx
212 lines (186 loc) · 6.07 KB
/
BarChartPro.zoom.test.tsx
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
import * as React from 'react';
import { expect } from 'chai';
import { createRenderer, screen, fireEvent } from '@mui/internal-test-utils';
import { describeSkipIf, isJSDOM, testSkipIf } from 'test/utils/skipIf';
import { BarChartPro } from './BarChartPro';
describeSkipIf(isJSDOM)('<BarChartPro /> - Zoom', () => {
const { render } = createRenderer();
const barChartProps = {
series: [
{
data: [10, 20, 30, 40],
},
],
xAxis: [
{
scaleType: 'band',
data: ['A', 'B', 'C', 'D'],
zoom: true,
height: 30,
id: 'x',
},
],
yAxis: [{ position: 'none' }],
width: 100,
height: 130,
margin: 0,
slotProps: { tooltip: { trigger: 'none' } },
} as const;
const options = {
wrapper: ({ children }: { children?: React.ReactNode }) => (
<div style={{ width: 100, height: 130 }}>{children}</div>
),
};
// eslint-disable-next-line mocha/no-top-level-hooks
beforeEach(() => {
// TODO: Remove beforeEach/afterEach after vitest becomes our main runner
if (window?.document?.body?.style) {
window.document.body.style.margin = '0';
}
});
// eslint-disable-next-line mocha/no-top-level-hooks
afterEach(() => {
if (window?.document?.body?.style) {
window.document.body.style.margin = '8px';
}
});
it('should zoom on wheel', async () => {
const { user } = render(<BarChartPro {...barChartProps} />, options);
expect(screen.queryByText('A')).not.to.equal(null);
expect(screen.queryByText('B')).not.to.equal(null);
expect(screen.queryByText('C')).not.to.equal(null);
expect(screen.queryByText('D')).not.to.equal(null);
const svg = document.querySelector('svg')!;
await user.pointer([
{
target: svg,
coords: { x: 50, y: 50 },
},
]);
// scroll, we scroll exactly in the center of the svg
// And we do it 200 times which is the lowest number to trigger a zoom where both A and D are not visible
for (let i = 0; i < 200; i += 1) {
fireEvent.wheel(svg, { deltaY: -1, clientX: 50, clientY: 50 });
}
expect(screen.queryByText('A')).to.equal(null);
expect(screen.queryByText('B')).not.to.equal(null);
expect(screen.queryByText('C')).not.to.equal(null);
expect(screen.queryByText('D')).to.equal(null);
// scroll back
for (let i = 0; i < 200; i += 1) {
fireEvent.wheel(svg, { deltaY: 1, clientX: 50, clientY: 50 });
}
expect(screen.queryByText('A')).not.to.equal(null);
expect(screen.queryByText('B')).not.to.equal(null);
expect(screen.queryByText('C')).not.to.equal(null);
expect(screen.queryByText('D')).not.to.equal(null);
});
['MouseLeft', 'TouchA'].forEach((pointerName) => {
it(`should pan on ${pointerName} drag`, async () => {
const { user } = render(
<BarChartPro {...barChartProps} initialZoom={[{ axisId: 'x', start: 75, end: 100 }]} />,
options,
);
expect(screen.queryByText('A')).to.equal(null);
expect(screen.queryByText('B')).to.equal(null);
expect(screen.queryByText('C')).to.equal(null);
expect(screen.queryByText('D')).not.to.equal(null);
const svg = document.querySelector('svg')!;
// we drag one position so C should be visible
await user.pointer([
{
keys: `[${pointerName}>]`,
target: svg,
coords: { x: 5, y: 20 },
},
{
pointerName: pointerName === 'MouseLeft' ? undefined : pointerName,
target: svg,
coords: { x: 100, y: 20 },
},
{
keys: `[/${pointerName}]`,
target: svg,
coords: { x: 100, y: 20 },
},
]);
expect(screen.queryByText('A')).to.equal(null);
expect(screen.queryByText('B')).to.equal(null);
expect(screen.queryByText('C')).not.to.equal(null);
expect(screen.queryByText('D')).to.equal(null);
// we drag all the way to the left so A should be visible
await user.pointer([
{
keys: `[${pointerName}>]`,
target: svg,
coords: { x: 5, y: 20 },
},
{
pointerName: pointerName === 'MouseLeft' ? undefined : pointerName,
target: svg,
coords: { x: 300, y: 20 },
},
{
keys: `[/${pointerName}]`,
target: svg,
coords: { x: 300, y: 20 },
},
]);
expect(screen.queryByText('A')).not.to.equal(null);
expect(screen.queryByText('B')).to.equal(null);
expect(screen.queryByText('C')).to.equal(null);
expect(screen.queryByText('D')).to.equal(null);
});
});
// Technically it should work, but it's not working in the test environment
// https://github.com/pmndrs/use-gesture/discussions/430
testSkipIf(true)('should zoom on pinch', async () => {
const { user } = render(<BarChartPro {...barChartProps} />, options);
expect(screen.queryByText('A')).not.to.equal(null);
expect(screen.queryByText('B')).not.to.equal(null);
expect(screen.queryByText('C')).not.to.equal(null);
expect(screen.queryByText('D')).not.to.equal(null);
const svg = document.querySelector('svg')!;
await user.pointer({
keys: '[TouchA]',
target: svg,
coords: { x: 50, y: 50 },
});
await user.pointer([
{
keys: '[TouchA>]',
target: svg,
coords: { x: 55, y: 45 },
},
{
keys: '[TouchB>]',
target: svg,
coords: { x: 45, y: 55 },
},
{
pointerName: 'TouchA',
target: svg,
coords: { x: 75, y: 25 },
},
{
pointerName: 'TouchB',
target: svg,
coords: { x: 25, y: 75 },
},
{
keys: '[/TouchA]',
target: svg,
coords: { x: 75, y: 25 },
},
{
keys: '[/TouchB]',
target: svg,
coords: { x: 25, y: 75 },
},
]);
expect(screen.queryByText('A')?.textContent).to.equal(null);
expect(screen.queryByText('B')).not.to.equal(null);
expect(screen.queryByText('C')).not.to.equal(null);
expect(screen.queryByText('D')).to.equal(null);
});
});