|
| 1 | +import { describe, expect, it, vi } from 'vitest' |
| 2 | +import { scaleBand, scaleLinear } from 'd3-scale' |
| 3 | +import { barY } from './bar' |
| 4 | +import { motion } from './motion' |
| 5 | +import { createChartScene, defineChart } from './scene' |
| 6 | + |
| 7 | +describe('motion run clock', () => { |
| 8 | + it.each([ |
| 9 | + { firstFrame: 900, delay: 0, observedAt: 1500 }, |
| 10 | + { firstFrame: 1250, delay: 0, observedAt: 1500 }, |
| 11 | + { firstFrame: 900, delay: 100, observedAt: 1600 }, |
| 12 | + ])( |
| 13 | + 'measures elapsed time from scheduling: %j', |
| 14 | + ({ firstFrame, delay, observedAt }) => { |
| 15 | + const callbacks = new Map<number, FrameRequestCallback>() |
| 16 | + let handle = 0 |
| 17 | + const now = vi.spyOn(window.performance, 'now').mockReturnValue(1000) |
| 18 | + const request = vi |
| 19 | + .spyOn(window, 'requestAnimationFrame') |
| 20 | + .mockImplementation(function (this: Window, callback) { |
| 21 | + expect(this).toBe(window) |
| 22 | + callbacks.set(++handle, callback) |
| 23 | + return handle |
| 24 | + }) |
| 25 | + const cancel = vi |
| 26 | + .spyOn(window, 'cancelAnimationFrame') |
| 27 | + .mockImplementation(function (this: Window, id) { |
| 28 | + expect(this).toBe(window) |
| 29 | + if (id !== null && id !== undefined) callbacks.delete(id) |
| 30 | + }) |
| 31 | + const container = document.createElement('div') |
| 32 | + const surface = motion({ |
| 33 | + transition: { type: 'tween', duration: 1000, easing: 'linear' }, |
| 34 | + }).mount(container, () => {}) |
| 35 | + const scene = createChartScene( |
| 36 | + defineChart({ |
| 37 | + marks: [ |
| 38 | + barY([{ category: 'A', value: 100 }], { |
| 39 | + x: 'category', |
| 40 | + y: 'value', |
| 41 | + motion: { delay }, |
| 42 | + }), |
| 43 | + ], |
| 44 | + scales: { |
| 45 | + x: { scale: scaleBand }, |
| 46 | + y: { scale: scaleLinear().domain([0, 100]) }, |
| 47 | + }, |
| 48 | + guides: false, |
| 49 | + margin: 0, |
| 50 | + }), |
| 51 | + { width: 300, height: 200 }, |
| 52 | + ) |
| 53 | + const frame = (time: number) => { |
| 54 | + const pending = [...callbacks.values()] |
| 55 | + callbacks.clear() |
| 56 | + expect(pending).not.toHaveLength(0) |
| 57 | + pending.forEach((callback) => callback(time)) |
| 58 | + } |
| 59 | + try { |
| 60 | + surface.render(scene, { ariaLabel: 'Scheduled motion' }) |
| 61 | + frame(firstFrame) |
| 62 | + const bar = container.querySelector('rect')! |
| 63 | + if (firstFrame < 1000 + delay) |
| 64 | + expect(Number(bar.getAttribute('height'))).toBe(0) |
| 65 | + frame(observedAt) |
| 66 | + expect(Number(bar.getAttribute('height'))).toBeCloseTo(100) |
| 67 | + frame(2000 + delay) |
| 68 | + expect(Number(bar.getAttribute('height'))).toBe(200) |
| 69 | + expect(container.querySelector('svg')!.dataset.tsMotionState).toBe( |
| 70 | + 'finished', |
| 71 | + ) |
| 72 | + } finally { |
| 73 | + surface.destroy() |
| 74 | + now.mockRestore() |
| 75 | + request.mockRestore() |
| 76 | + cancel.mockRestore() |
| 77 | + } |
| 78 | + }, |
| 79 | + ) |
| 80 | +}) |
0 commit comments