Skip to content

Commit 5a34b7e

Browse files
test(charts): cover crosshair repaints during data motion (#223)
* test(charts): cover crosshair repaints during data motion * chore(charts): refresh crosshair test preview provenance
1 parent ba6b647 commit 5a34b7e

2 files changed

Lines changed: 69 additions & 1 deletion

File tree

‎benchmarks/conformance/previews/manifest.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"schemaVersion": 1,
33
"width": 288,
44
"height": 192,
5-
"sourceHash": "5cb9f7c0512bf98f18bb88dbb7c2ec524b0239530a64d5a9b1d5a00ab5f9b3d3",
5+
"sourceHash": "82eecd6c1fbdca5e857b965e09bb53c550520b2e596fbc4e9850266f03fdb9b4",
66
"assets": [
77
{
88
"id": "01-line-gaps",

‎packages/charts-core/src/motion.test.ts‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2398,6 +2398,74 @@ describe('SVG motion', () => {
23982398
frames.restore()
23992399
})
24002400

2401+
it('advances the crosshair spring during repeated data-frame repaints', () => {
2402+
const frames = installManagedFrames()
2403+
const makeScene = (offset: number) =>
2404+
createChartScene(
2405+
defineChart({
2406+
marks: [
2407+
lineY(
2408+
Array.from({ length: 20 }, (_, x) => ({ x, y: x + offset })),
2409+
{ x: 'x', y: 'y', key: 'x' },
2410+
),
2411+
crosshair({ x: true, y: false }),
2412+
],
2413+
scales: {
2414+
x: { scale: scaleLinear().domain([0, 19]) },
2415+
y: { scale: scaleLinear().domain([0, 40]) },
2416+
},
2417+
guides: false,
2418+
}),
2419+
{ width: 400, height: 200 },
2420+
)
2421+
const container = document.createElement('div')
2422+
const surface = motion({
2423+
initial: false,
2424+
transition: { type: 'spring', stiffness: 170, damping: 18 },
2425+
}).mount(container, () => {})
2426+
let unsubscribe = () => {}
2427+
try {
2428+
surface.render(makeScene(0), { ariaLabel: 'Crosshair repaint' })
2429+
const next = makeScene(5)
2430+
surface.render(next, { ariaLabel: 'Crosshair repaint' })
2431+
const paint = (index: number) => {
2432+
const point = next.points[index]!
2433+
surface.paintFocus({
2434+
primary: point,
2435+
group: [point],
2436+
source: 'pointer',
2437+
pinned: false,
2438+
})
2439+
}
2440+
paint(2)
2441+
paint(15)
2442+
const line = container.querySelector<SVGLineElement>(
2443+
'.ts-chart__focus-guide-layer line',
2444+
)!
2445+
const start = Number(line.getAttribute('x1'))
2446+
let repaints = 0
2447+
if (!surface.subscribePresentationPoints) {
2448+
throw new Error('Expected motion presentation subscriptions')
2449+
}
2450+
unsubscribe = surface.subscribePresentationPoints(() => {
2451+
repaints += 1
2452+
paint(15)
2453+
})
2454+
for (let time = 16; time <= 128; time += 16) frames.runAll(time)
2455+
expect(repaints).toBe(8)
2456+
expect(Number(line.getAttribute('x1'))).toBeGreaterThan(start + 100)
2457+
expect(Number(line.getAttribute('x1'))).toBeLessThan(next.points[15]!.x)
2458+
expect(container.querySelector('.ts-chart__focus-guide-layer line')).toBe(
2459+
line,
2460+
)
2461+
} finally {
2462+
unsubscribe()
2463+
surface.destroy()
2464+
expect(frames.pending()).toBe(0)
2465+
frames.restore()
2466+
}
2467+
})
2468+
24012469
it('retargets a persistent crosshair spring without replacing guide elements', () => {
24022470
const transition = {
24032471
type: 'spring' as const,

0 commit comments

Comments
 (0)