| import React, { ReactNode, useState } from 'react'; |
| import { describe, it, expect, beforeAll, beforeEach } from 'vitest'; |
| import { act } from '@testing-library/react'; |
| import { createSelectorTestCase } from '../helper/createSelectorTestCase'; |
| import { Legend, Line, LineChart, YAxis } from '../../src'; |
| import { PageData } from '../_data'; |
| import { mockGetTotalLength } from '../helper/mockGetTotalLength'; |
| import { ExpectedLabel, expectLabels } from '../helper/expectLabel'; |
| import { mockSequenceOfGetBoundingClientRect } from '../helper/mockGetBoundingClientRect'; |
| import { MockProgressAnimationManager } from '../animation/MockProgressAnimationManager'; |
| import { expectDots } from '../helper/expectDots'; |
| import { expectLines } from '../helper/expectLine'; |
|
|
| function getLine(container: HTMLElement) { |
| return container.querySelector('.recharts-line-curve'); |
| } |
|
|
| describe('Line animation', () => { |
| beforeEach(() => { |
| mockSequenceOfGetBoundingClientRect([ |
| { width: 0, height: 0, left: 0, top: 50 }, |
| { width: 50, height: 50, left: 0, top: 50 }, |
| ]); |
| }); |
|
|
| beforeAll(() => { |
| mockGetTotalLength(100); |
| }); |
|
|
| const expectedUvLabels: ReadonlyArray<ExpectedLabel> = [ |
| { |
| height: null, |
| offset: '5', |
| textContent: '400', |
| width: null, |
| x: '5', |
| y: '5', |
| }, |
| { |
| height: null, |
| offset: '5', |
| textContent: '300', |
| width: null, |
| x: '23', |
| y: '27.5', |
| }, |
| { |
| height: null, |
| offset: '5', |
| textContent: '300', |
| width: null, |
| x: '41', |
| y: '27.5', |
| }, |
| { |
| height: null, |
| offset: '5', |
| textContent: '200', |
| width: null, |
| x: '59', |
| y: '50', |
| }, |
| { |
| height: null, |
| offset: '5', |
| textContent: '278', |
| width: null, |
| x: '77', |
| y: '32.45', |
| }, |
| { |
| height: null, |
| offset: '5', |
| textContent: '189', |
| width: null, |
| x: '95', |
| y: '52.475', |
| }, |
| ]; |
|
|
| const expectedPvLabels: ReadonlyArray<ExpectedLabel> = [ |
| { |
| height: null, |
| offset: '5', |
| textContent: '2400', |
| width: null, |
| x: '5', |
| y: '73.4', |
| }, |
| { |
| height: null, |
| offset: '5', |
| textContent: '4567', |
| width: null, |
| x: '23', |
| y: '53.897000000000006', |
| }, |
| { |
| height: null, |
| offset: '5', |
| textContent: '1398', |
| width: null, |
| x: '41', |
| y: '82.41799999999999', |
| }, |
| { |
| height: null, |
| offset: '5', |
| textContent: '9800', |
| width: null, |
| x: '59', |
| y: '6.8000000000000025', |
| }, |
| { |
| height: null, |
| offset: '5', |
| textContent: '3908', |
| width: null, |
| x: '77', |
| y: '59.827999999999996', |
| }, |
| { |
| height: null, |
| offset: '5', |
| textContent: '4800', |
| width: null, |
| x: '95', |
| y: '51.8', |
| }, |
| ]; |
|
|
| |
| |
| |
| |
| |
| const pvLabelsWithUvPositions: ReadonlyArray<ExpectedLabel> = [ |
| { |
| height: null, |
| offset: '5', |
| textContent: '2400', |
| width: null, |
| x: '5', |
| y: '5', |
| }, |
| { |
| height: null, |
| offset: '5', |
| textContent: '4567', |
| width: null, |
| x: '23', |
| y: '27.5', |
| }, |
| { |
| height: null, |
| offset: '5', |
| textContent: '1398', |
| width: null, |
| x: '41', |
| y: '27.5', |
| }, |
| { |
| height: null, |
| offset: '5', |
| textContent: '9800', |
| width: null, |
| x: '59', |
| y: '50', |
| }, |
| { |
| height: null, |
| offset: '5', |
| textContent: '3908', |
| width: null, |
| x: '77', |
| y: '32.45', |
| }, |
| { |
| height: null, |
| offset: '5', |
| textContent: '4800', |
| width: null, |
| x: '95', |
| y: '52.475', |
| }, |
| ]; |
|
|
| describe('with isAnimationActive={false}', () => { |
| const renderTestCase = createSelectorTestCase(({ children }) => ( |
| <LineChart data={PageData} width={100} height={100}> |
| <Line dataKey="uv" isAnimationActive={false} label /> |
| {children} |
| </LineChart> |
| )); |
|
|
| it('should render the line without stroke-dasharray', () => { |
| const { container } = renderTestCase(); |
|
|
| const line = getLine(container); |
| expect(line).toBeInTheDocument(); |
| |
| expect(line).toHaveAttribute('d', 'M5,5L23,27.5L41,27.5L59,50L77,32.45L95,52.475'); |
| |
| expect(line).not.toHaveAttribute('stroke-dasharray'); |
| }); |
|
|
| it('should render all the dots without animation', () => { |
| const { container } = renderTestCase(); |
|
|
| expectDots(container, [ |
| { |
| cx: '5', |
| cy: '5', |
| }, |
| { |
| cx: '23', |
| cy: '27.5', |
| }, |
| { |
| cx: '41', |
| cy: '27.5', |
| }, |
| { |
| cx: '59', |
| cy: '50', |
| }, |
| { |
| cx: '77', |
| cy: '32.45', |
| }, |
| { |
| cx: '95', |
| cy: '52.475', |
| }, |
| ]); |
| }); |
|
|
| it('should render all labels without animation', () => { |
| const { container } = renderTestCase(); |
|
|
| expectLabels(container, expectedUvLabels); |
| }); |
| }); |
|
|
| describe('with simple props', () => { |
| const renderTestCase = createSelectorTestCase(({ children }) => ( |
| <LineChart data={PageData} width={100} height={100}> |
| <Line dataKey="uv" animationEasing="linear" label /> |
| {children} |
| </LineChart> |
| )); |
|
|
| it('should start the line with fully generated path but 0 stroke-dasharray', () => { |
| const { container } = renderTestCase(); |
|
|
| const line = getLine(container); |
| expect(line).toBeInTheDocument(); |
| |
| expect(line).toHaveAttribute('d', 'M5,5L23,27.5L41,27.5L59,50L77,32.45L95,52.475'); |
| |
| expect(line).toHaveAttribute('stroke-dasharray', '0px 0px'); |
| }); |
|
|
| it('should animate line left-to-right by continually extending the stroke-dasharray', async () => { |
| const { container, animationManager } = renderTestCase(); |
|
|
| await animationManager.setAnimationProgress(0.1); |
|
|
| const line = getLine(container); |
| |
| |
| expect(line).toHaveAttribute('stroke-dasharray', '10.000000000000002px 90px'); |
|
|
| await animationManager.setAnimationProgress(0.2); |
|
|
| |
| expect(line).toHaveAttribute('stroke-dasharray', '20px 80px'); |
|
|
| await animationManager.setAnimationProgress(1); |
| |
| expect(line).toHaveAttribute('stroke-dasharray', '100px 0px'); |
|
|
| await animationManager.completeAnimation(); |
| |
| |
| |
| |
| expect(line).toHaveAttribute('stroke-dasharray', '100px 0px'); |
| }); |
|
|
| it('should set the stroke-dasharray to 100, 0 when the animation is completed', async () => { |
| const { container, animationManager } = renderTestCase(); |
|
|
| await animationManager.setAnimationProgress(1); |
|
|
| const line = getLine(container); |
| expect(line).toHaveAttribute('stroke-dasharray', '100px 0px'); |
| }); |
|
|
| it('should render all the dots without animation', () => { |
| const { container } = renderTestCase(); |
|
|
| expectDots(container, [ |
| { |
| cx: '5', |
| cy: '5', |
| }, |
| { |
| cx: '23', |
| cy: '27.5', |
| }, |
| { |
| cx: '41', |
| cy: '27.5', |
| }, |
| { |
| cx: '59', |
| cy: '50', |
| }, |
| { |
| cx: '77', |
| cy: '32.45', |
| }, |
| { |
| cx: '95', |
| cy: '52.475', |
| }, |
| ]); |
| }); |
|
|
| it('should hide all labels until the animation is completed', async () => { |
| const { container, animationManager } = renderTestCase(); |
|
|
| |
| |
| |
| |
| expectLabels(container, expectedUvLabels); |
|
|
| |
| await animationManager.setAnimationProgress(0.1); |
|
|
| expectLabels(container, []); |
|
|
| |
| await animationManager.setAnimationProgress(0.9); |
| expectLabels(container, []); |
|
|
| |
| await animationManager.completeAnimation(); |
| expectLabels(container, expectedUvLabels); |
| }); |
|
|
| it('should not move the path itself during the animation', async () => { |
| const { container, animationManager } = renderTestCase(); |
|
|
| const line = getLine(container); |
| expect(line).toBeInTheDocument(); |
| |
| const initialPath = line.getAttribute('d'); |
| expect(initialPath).toBe('M5,5L23,27.5L41,27.5L59,50L77,32.45L95,52.475'); |
| expect(line.getAttribute('d')).toBe(initialPath); |
|
|
| |
|
|
| await animationManager.setAnimationProgress(0.1); |
| expect(line.getAttribute('d')).toBe(initialPath); |
|
|
| await animationManager.setAnimationProgress(0.9); |
| expect(line.getAttribute('d')).toBe(initialPath); |
|
|
| await animationManager.completeAnimation(); |
| expect(line.getAttribute('d')).toBe(initialPath); |
| }); |
| }); |
|
|
| describe('with stroke-dasharray prop', () => { |
| describe('with isAnimationActive={false}', () => { |
| const renderTestCase = createSelectorTestCase(({ children }) => ( |
| <LineChart data={PageData} width={100} height={100}> |
| <Line dataKey="uv" strokeDasharray="5 5" isAnimationActive={false} /> |
| {children} |
| </LineChart> |
| )); |
|
|
| it('should render the line with stroke-dasharray', () => { |
| const { container } = renderTestCase(); |
|
|
| const line = getLine(container); |
| expect(line).toBeInTheDocument(); |
| |
| expect(line).toHaveAttribute('d', 'M5,5L23,27.5L41,27.5L59,50L77,32.45L95,52.475'); |
| |
| expect(line).toHaveAttribute('stroke-dasharray', '5 5'); |
| }); |
| }); |
|
|
| describe('with isAnimationActive={true}', () => { |
| const renderTestCase = createSelectorTestCase(({ children }) => ( |
| <LineChart data={PageData} width={100} height={100}> |
| <Line dataKey="uv" animationEasing="linear" strokeDasharray="7 3" /> |
| {children} |
| </LineChart> |
| )); |
|
|
| it('should start the line with fully generated path but 0 stroke-dasharray', () => { |
| const { container } = renderTestCase(); |
|
|
| const line = getLine(container); |
| expect(line).toBeInTheDocument(); |
| |
| expect(line).toHaveAttribute('d', 'M5,5L23,27.5L41,27.5L59,50L77,32.45L95,52.475'); |
| |
| expect(line).toHaveAttribute('stroke-dasharray', '0px, 0px'); |
| }); |
|
|
| it('should animate line left-to-right by continually extending the stroke-dasharray', async () => { |
| const { container, animationManager } = renderTestCase(); |
|
|
| await animationManager.setAnimationProgress(0.1); |
|
|
| const line = getLine(container); |
| |
| |
| expect(line).toHaveAttribute('stroke-dasharray', '7px, 3px, 1.7763568394002505e-15px, 90px'); |
|
|
| await animationManager.setAnimationProgress(0.2); |
|
|
| |
| expect(line).toHaveAttribute('stroke-dasharray', '7px, 3px, 7px, 3px, 0px, 80px'); |
|
|
| await animationManager.setAnimationProgress(1); |
| |
| expect(line).toHaveAttribute( |
| 'stroke-dasharray', |
| '7px, 3px, 7px, 3px, 7px, 3px, 7px, 3px, 7px, 3px, 7px, 3px, 7px, 3px, 7px, 3px, 7px, 3px, 7px, 3px, 0px, 0px', |
| ); |
|
|
| await animationManager.completeAnimation(); |
| |
| |
| |
| |
| expect(line).toHaveAttribute( |
| 'stroke-dasharray', |
| '7px, 3px, 7px, 3px, 7px, 3px, 7px, 3px, 7px, 3px, 7px, 3px, 7px, 3px, 7px, 3px, 7px, 3px, 7px, 3px, 0px, 0px', |
| ); |
| }); |
| }); |
| }); |
|
|
| describe('with <Legend /> sibling', () => { |
| const renderTestCase = createSelectorTestCase(({ children }) => ( |
| <LineChart data={PageData} width={100} height={100}> |
| <Legend /> |
| <Line dataKey="uv" animationEasing="linear" label /> |
| {children} |
| </LineChart> |
| )); |
|
|
| it('should not move the path during the animation', async () => { |
| const { container, animationManager } = renderTestCase(); |
|
|
| const line = getLine(container); |
| expect(line).toBeInTheDocument(); |
| |
| const initialPath = line.getAttribute('d'); |
| expect(initialPath).toBe('M5,5L23,15L41,15L59,25L77,17.2L95,26.1'); |
| expect(line.getAttribute('d')).toBe(initialPath); |
|
|
| |
| await animationManager.setAnimationProgress(0.1); |
| expect(line.getAttribute('d')).toBe(initialPath); |
|
|
| await animationManager.setAnimationProgress(0.9); |
| expect(line.getAttribute('d')).toBe(initialPath); |
|
|
| await animationManager.completeAnimation(); |
| expect(line.getAttribute('d')).toBe(initialPath); |
| }); |
| }); |
|
|
| describe('with <YAxis width="auto" /> sibling', () => { |
| const renderTestCase = createSelectorTestCase(({ children }) => ( |
| <LineChart data={PageData} width={100} height={100}> |
| <YAxis width="auto" /> |
| <Line dataKey="uv" animationEasing="linear" label /> |
| {children} |
| </LineChart> |
| )); |
|
|
| it('should not move the path during the animation', async () => { |
| const { container, animationManager } = renderTestCase(); |
|
|
| const line = getLine(container); |
| expect(line).toBeInTheDocument(); |
| |
| const initialPath = line.getAttribute('d'); |
| expect(initialPath).toBe('M63,5L69.4,27.5L75.8,27.5L82.2,50L88.6,32.45L95,52.475'); |
| expect(line.getAttribute('d')).toBe(initialPath); |
|
|
| |
| await animationManager.setAnimationProgress(0.1); |
| expect(line.getAttribute('d')).toBe(initialPath); |
|
|
| await animationManager.setAnimationProgress(0.9); |
| expect(line.getAttribute('d')).toBe(initialPath); |
|
|
| await animationManager.completeAnimation(); |
| expect(line.getAttribute('d')).toBe(initialPath); |
| }); |
| }); |
|
|
| describe('when changing dataKey prop', () => { |
| const MyTestCase = ({ children }: { children: ReactNode }) => { |
| const [dataKey, setDataKey] = useState('uv'); |
| const changeDataKey = () => { |
| setDataKey(prev => (prev === 'uv' ? 'pv' : 'uv')); |
| }; |
| return ( |
| <div> |
| <button type="button" onClick={changeDataKey}> |
| Change dataKey |
| </button> |
| <LineChart data={PageData} width={100} height={100}> |
| <Line dataKey={dataKey} animationEasing="linear" label /> |
| {children} |
| </LineChart> |
| </div> |
| ); |
| }; |
|
|
| const renderTestCase = createSelectorTestCase(MyTestCase); |
|
|
| describe('interrupting the initial animation', () => { |
| async function prime(container: HTMLElement, animationManager: MockProgressAnimationManager) { |
| |
| |
| |
| |
| await animationManager.setAnimationProgress(0.3); |
|
|
| |
| const button = container.querySelector('button'); |
| expect(button).toBeInTheDocument(); |
| act(() => { |
| button.click(); |
| }); |
|
|
| |
| } |
|
|
| it('should continue growing the line where it left off', async () => { |
| const { container, animationManager } = renderTestCase(); |
| await prime(container, animationManager); |
| const fullyVisibleLine = '100px 0px'; |
|
|
| |
| |
| |
| |
| expect(getLine(container)).toHaveAttribute('stroke-dasharray', '30px 70px'); |
|
|
| |
| |
| |
| |
| |
| await animationManager.setAnimationProgress(0.1); |
| expect(getLine(container)).toHaveAttribute('stroke-dasharray', '40px 60px'); |
|
|
| await animationManager.setAnimationProgress(0.2); |
| expect(getLine(container)).toHaveAttribute('stroke-dasharray', '50px 50px'); |
|
|
| await animationManager.setAnimationProgress(0.3); |
| expect(getLine(container)).toHaveAttribute('stroke-dasharray', '60px 40px'); |
|
|
| |
| await animationManager.setAnimationProgress(0.7); |
| expect(getLine(container)).toHaveAttribute('stroke-dasharray', fullyVisibleLine); |
|
|
| await animationManager.completeAnimation(); |
| expect(getLine(container)).toHaveAttribute('stroke-dasharray', fullyVisibleLine); |
| }); |
|
|
| it('should hide labels during the animation', async () => { |
| const { container, animationManager } = renderTestCase(); |
|
|
| await prime(container, animationManager); |
|
|
| |
| |
| |
| |
| |
| |
| expectLabels(container, pvLabelsWithUvPositions); |
| await animationManager.setAnimationProgress(0.2); |
| |
| expectLabels(container, []); |
|
|
| await animationManager.setAnimationProgress(0.5); |
| |
| expectLabels(container, []); |
|
|
| await animationManager.setAnimationProgress(1); |
| |
| expectLabels(container, []); |
|
|
| await animationManager.completeAnimation(); |
|
|
| |
| expectLabels(container, expectedPvLabels); |
| }); |
|
|
| it('should animate the line path', async () => { |
| const { container, animationManager } = renderTestCase(); |
|
|
| await prime(container, animationManager); |
|
|
| const initialPath = 'M5,5L23,27.5L41,27.5L59,50L77,32.45L95,52.475'; |
| |
| |
| |
| |
| expect(getLine(container).getAttribute('d')).toBe(initialPath); |
|
|
| |
| await animationManager.setAnimationProgress(0.2); |
| expect(getLine(container).getAttribute('d')).toBe('M5,18.68L23,32.779L41,38.484L59,41.36L77,37.926L95,52.34'); |
| expect(getLine(container).getAttribute('d')).not.toBe(initialPath); |
|
|
| await animationManager.setAnimationProgress(0.5); |
| expect(getLine(container).getAttribute('d')).toBe('M5,39.2L23,40.699L41,54.959L59,28.4L77,46.139L95,52.138'); |
|
|
| await animationManager.setAnimationProgress(1); |
| expect(getLine(container).getAttribute('d')).toBe('M5,73.4L23,53.897L41,82.418L59,6.8L77,59.828L95,51.8'); |
|
|
| |
| await animationManager.completeAnimation(); |
| expect(getLine(container).getAttribute('d')).toBe('M5,73.4L23,53.897L41,82.418L59,6.8L77,59.828L95,51.8'); |
| }); |
|
|
| it('should animate the dots', async () => { |
| const { container, animationManager } = renderTestCase(); |
|
|
| await prime(container, animationManager); |
|
|
| |
| expectDots(container, [ |
| { |
| cx: '5', |
| cy: '5', |
| }, |
| { |
| cx: '23', |
| cy: '27.5', |
| }, |
| { |
| cx: '41', |
| cy: '27.5', |
| }, |
| { |
| cx: '59', |
| cy: '50', |
| }, |
| { |
| cx: '77', |
| cy: '32.45', |
| }, |
| { |
| cx: '95', |
| cy: '52.475', |
| }, |
| ]); |
|
|
| |
| await animationManager.setAnimationProgress(0.2); |
| expectDots(container, [ |
| { |
| cx: '5', |
| cy: '18.68', |
| }, |
| { |
| cx: '23', |
| cy: '32.7794', |
| }, |
| { |
| cx: '41', |
| cy: '38.483599999999996', |
| }, |
| { |
| cx: '59', |
| cy: '41.36', |
| }, |
| { |
| cx: '77', |
| cy: '37.9256', |
| }, |
| { |
| cx: '95', |
| cy: '52.34', |
| }, |
| ]); |
|
|
| await animationManager.setAnimationProgress(0.5); |
| expectDots(container, [ |
| { |
| cx: '5', |
| cy: '39.2', |
| }, |
| { |
| cx: '23', |
| cy: '40.6985', |
| }, |
| { |
| cx: '41', |
| cy: '54.958999999999996', |
| }, |
| { |
| cx: '59', |
| cy: '28.400000000000002', |
| }, |
| { |
| cx: '77', |
| cy: '46.138999999999996', |
| }, |
| { |
| cx: '95', |
| cy: '52.1375', |
| }, |
| ]); |
|
|
| await animationManager.setAnimationProgress(1); |
| const finalDotPositions = [ |
| { |
| cx: '5', |
| cy: '73.4', |
| }, |
| { |
| cx: '23', |
| cy: '53.897000000000006', |
| }, |
| { |
| cx: '41', |
| cy: '82.41799999999999', |
| }, |
| { |
| cx: '59', |
| cy: '6.8000000000000025', |
| }, |
| { |
| cx: '77', |
| cy: '59.827999999999996', |
| }, |
| { |
| cx: '95', |
| cy: '51.8', |
| }, |
| ]; |
| expectDots(container, finalDotPositions); |
|
|
| |
| await animationManager.completeAnimation(); |
| expectDots(container, finalDotPositions); |
| }); |
| }); |
|
|
| describe('interaction after initial animation completes', () => { |
| async function prime(container: HTMLElement, animationManager: MockProgressAnimationManager) { |
| |
| await animationManager.completeAnimation(); |
|
|
| |
| const button = container.querySelector('button'); |
| expect(button).toBeInTheDocument(); |
| act(() => { |
| button.click(); |
| }); |
|
|
| |
| } |
|
|
| it('should keep the whole line visible during the animation', async () => { |
| const { container, animationManager } = renderTestCase(); |
| await prime(container, animationManager); |
| const fullyVisibleLine = '100px 0px'; |
|
|
| |
| |
| |
| expect(getLine(container)).toHaveAttribute('stroke-dasharray', '100px 0px'); |
|
|
| await animationManager.setAnimationProgress(0.1); |
| expect(getLine(container)).toHaveAttribute('stroke-dasharray', fullyVisibleLine); |
|
|
| await animationManager.setAnimationProgress(0.5); |
| expect(getLine(container)).toHaveAttribute('stroke-dasharray', fullyVisibleLine); |
|
|
| await animationManager.setAnimationProgress(1); |
| expect(getLine(container)).toHaveAttribute('stroke-dasharray', fullyVisibleLine); |
|
|
| await animationManager.completeAnimation(); |
| expect(getLine(container)).toHaveAttribute('stroke-dasharray', fullyVisibleLine); |
| }); |
|
|
| it('should hide labels during the animation', async () => { |
| const { container, animationManager } = renderTestCase(); |
|
|
| await prime(container, animationManager); |
|
|
| |
| |
| |
| |
| |
| |
| expectLabels(container, pvLabelsWithUvPositions); |
| await animationManager.setAnimationProgress(0.2); |
| |
| expectLabels(container, []); |
|
|
| await animationManager.setAnimationProgress(0.5); |
| |
| expectLabels(container, []); |
|
|
| await animationManager.setAnimationProgress(1); |
| |
| expectLabels(container, []); |
|
|
| await animationManager.completeAnimation(); |
|
|
| |
| expectLabels(container, expectedPvLabels); |
| }); |
|
|
| it('should animate the line path', async () => { |
| const { container, animationManager } = renderTestCase(); |
|
|
| await prime(container, animationManager); |
|
|
| const initialPath = 'M5,5L23,27.5L41,27.5L59,50L77,32.45L95,52.475'; |
| |
| |
| |
| |
| expect(getLine(container).getAttribute('d')).toBe(initialPath); |
|
|
| |
| await animationManager.setAnimationProgress(0.2); |
| expect(getLine(container).getAttribute('d')).toBe('M5,18.68L23,32.779L41,38.484L59,41.36L77,37.926L95,52.34'); |
| expect(getLine(container).getAttribute('d')).not.toBe(initialPath); |
|
|
| await animationManager.setAnimationProgress(0.5); |
| expect(getLine(container).getAttribute('d')).toBe('M5,39.2L23,40.699L41,54.959L59,28.4L77,46.139L95,52.138'); |
|
|
| await animationManager.setAnimationProgress(1); |
| expect(getLine(container).getAttribute('d')).toBe('M5,73.4L23,53.897L41,82.418L59,6.8L77,59.828L95,51.8'); |
|
|
| |
| await animationManager.completeAnimation(); |
| expect(getLine(container).getAttribute('d')).toBe('M5,73.4L23,53.897L41,82.418L59,6.8L77,59.828L95,51.8'); |
| }); |
|
|
| it('should animate the dots', async () => { |
| const { container, animationManager } = renderTestCase(); |
|
|
| await prime(container, animationManager); |
|
|
| |
| expectDots(container, [ |
| { |
| cx: '5', |
| cy: '5', |
| }, |
| { |
| cx: '23', |
| cy: '27.5', |
| }, |
| { |
| cx: '41', |
| cy: '27.5', |
| }, |
| { |
| cx: '59', |
| cy: '50', |
| }, |
| { |
| cx: '77', |
| cy: '32.45', |
| }, |
| { |
| cx: '95', |
| cy: '52.475', |
| }, |
| ]); |
|
|
| |
| await animationManager.setAnimationProgress(0.2); |
| expectDots(container, [ |
| { |
| cx: '5', |
| cy: '18.68', |
| }, |
| { |
| cx: '23', |
| cy: '32.7794', |
| }, |
| { |
| cx: '41', |
| cy: '38.483599999999996', |
| }, |
| { |
| cx: '59', |
| cy: '41.36', |
| }, |
| { |
| cx: '77', |
| cy: '37.9256', |
| }, |
| { |
| cx: '95', |
| cy: '52.34', |
| }, |
| ]); |
|
|
| await animationManager.setAnimationProgress(0.5); |
| expectDots(container, [ |
| { |
| cx: '5', |
| cy: '39.2', |
| }, |
| { |
| cx: '23', |
| cy: '40.6985', |
| }, |
| { |
| cx: '41', |
| cy: '54.958999999999996', |
| }, |
| { |
| cx: '59', |
| cy: '28.400000000000002', |
| }, |
| { |
| cx: '77', |
| cy: '46.138999999999996', |
| }, |
| { |
| cx: '95', |
| cy: '52.1375', |
| }, |
| ]); |
|
|
| await animationManager.setAnimationProgress(1); |
| const finalDotPositions = [ |
| { |
| cx: '5', |
| cy: '73.4', |
| }, |
| { |
| cx: '23', |
| cy: '53.897000000000006', |
| }, |
| { |
| cx: '41', |
| cy: '82.41799999999999', |
| }, |
| { |
| cx: '59', |
| cy: '6.8000000000000025', |
| }, |
| { |
| cx: '77', |
| cy: '59.827999999999996', |
| }, |
| { |
| cx: '95', |
| cy: '51.8', |
| }, |
| ]; |
| expectDots(container, finalDotPositions); |
|
|
| |
| await animationManager.completeAnimation(); |
| expectDots(container, finalDotPositions); |
| }); |
| }); |
| }); |
|
|
| describe('when the Line has a key prop to force re-animation', () => { |
| const MyTestCase = ({ children }: { children: ReactNode }) => { |
| const [dataKey, setDataKey] = useState('uv'); |
| const changeDataKey = () => { |
| setDataKey(prev => (prev === 'uv' ? 'pv' : 'uv')); |
| }; |
| return ( |
| <div> |
| <button type="button" onClick={changeDataKey}> |
| Change dataKey |
| </button> |
| <LineChart data={PageData} width={100} height={100}> |
| {/* Thanks to the key prop, React will force re-render. |
| * This effectively makes it so that the Line always does the initial animation |
| * even if it has already been rendered before. |
| */} |
| <Line dataKey={dataKey} animationEasing="linear" label key={dataKey} /> |
| {children} |
| </LineChart> |
| </div> |
| ); |
| }; |
|
|
| const renderTestCase = createSelectorTestCase(MyTestCase); |
|
|
| async function prime(container: HTMLElement, animationManager: MockProgressAnimationManager) { |
| |
| await animationManager.completeAnimation(); |
|
|
| |
| const button = container.querySelector('button'); |
| expect(button).toBeInTheDocument(); |
| act(() => { |
| button.click(); |
| }); |
|
|
| |
| } |
|
|
| it('should animate the line length from 0 to full length', async () => { |
| const { container, animationManager } = renderTestCase(); |
| await prime(container, animationManager); |
|
|
| const initialPath = 'M5,73.4L23,53.897L41,82.418L59,6.8L77,59.828L95,51.8'; |
| |
| |
| |
| |
| expect(getLine(container).getAttribute('d')).toBe(initialPath); |
| |
| expect(getLine(container)).toHaveAttribute('stroke-dasharray', '0px 0px'); |
|
|
| |
| await animationManager.setAnimationProgress(0.2); |
| expect(getLine(container).getAttribute('d')).toBe(initialPath); |
| expect(getLine(container)).toHaveAttribute('stroke-dasharray', '20px 80px'); |
|
|
| await animationManager.setAnimationProgress(0.5); |
| expect(getLine(container).getAttribute('d')).toBe(initialPath); |
| expect(getLine(container)).toHaveAttribute('stroke-dasharray', '50px 50px'); |
|
|
| await animationManager.setAnimationProgress(1); |
| expect(getLine(container).getAttribute('d')).toBe(initialPath); |
| expect(getLine(container)).toHaveAttribute('stroke-dasharray', '100px 0px'); |
|
|
| |
| await animationManager.completeAnimation(); |
| expect(getLine(container).getAttribute('d')).toBe(initialPath); |
| expect(getLine(container)).toHaveAttribute('stroke-dasharray', '100px 0px'); |
| }); |
| }); |
|
|
| describe('tests that change data array', () => { |
| type DataType = Array<{ |
| name: string; |
| val: number; |
| }>; |
| const uvData: DataType = PageData.map(d => ({ name: d.name, val: d.uv })); |
| const pvData: DataType = PageData.map(d => ({ name: d.name, val: d.pv })); |
|
|
| const MyTestCase = ({ children }: { children: ReactNode }) => { |
| const [data, setData] = useState(uvData); |
| const changeData = () => { |
| setData(prevData => { |
| |
| if (prevData === uvData) { |
| return pvData; |
| } |
| return uvData; |
| }); |
| }; |
| return ( |
| <div> |
| <button type="button" onClick={changeData}> |
| Add data |
| </button> |
| <LineChart data={data} width={100} height={100}> |
| <Line dataKey="val" animationEasing="linear" label /> |
| {children} |
| </LineChart> |
| </div> |
| ); |
| }; |
|
|
| const renderTestCase = createSelectorTestCase(MyTestCase); |
|
|
| describe('interrupting the initial animation', () => { |
| async function prime(container: HTMLElement, animationManager: MockProgressAnimationManager) { |
| |
| |
| |
| |
| await animationManager.setAnimationProgress(0.3); |
|
|
| |
| const button = container.querySelector('button'); |
| expect(button).toBeInTheDocument(); |
| act(() => { |
| button.click(); |
| }); |
|
|
| |
| } |
|
|
| it('should continue growing the line where it left off', async () => { |
| const { container, animationManager } = renderTestCase(); |
| await prime(container, animationManager); |
| const fullyVisibleLine = '100px 0px'; |
|
|
| |
| |
| |
| |
| expect(getLine(container)).toHaveAttribute('stroke-dasharray', '30px 70px'); |
|
|
| |
| |
| |
| |
| |
| await animationManager.setAnimationProgress(0.1); |
| expect(getLine(container)).toHaveAttribute('stroke-dasharray', '40px 60px'); |
|
|
| await animationManager.setAnimationProgress(0.2); |
| expect(getLine(container)).toHaveAttribute('stroke-dasharray', '50px 50px'); |
|
|
| await animationManager.setAnimationProgress(0.3); |
| expect(getLine(container)).toHaveAttribute('stroke-dasharray', '60px 40px'); |
|
|
| |
| await animationManager.setAnimationProgress(0.7); |
| expect(getLine(container)).toHaveAttribute('stroke-dasharray', fullyVisibleLine); |
|
|
| await animationManager.completeAnimation(); |
| expect(getLine(container)).toHaveAttribute('stroke-dasharray', fullyVisibleLine); |
| }); |
|
|
| it('should hide labels during the animation', async () => { |
| const { container, animationManager } = renderTestCase(); |
|
|
| await prime(container, animationManager); |
|
|
| |
| |
| |
| |
| |
| |
| expectLabels(container, pvLabelsWithUvPositions); |
| await animationManager.setAnimationProgress(0.2); |
| |
| expectLabels(container, []); |
|
|
| await animationManager.setAnimationProgress(0.5); |
| |
| expectLabels(container, []); |
|
|
| await animationManager.setAnimationProgress(1); |
| |
| expectLabels(container, []); |
|
|
| await animationManager.completeAnimation(); |
|
|
| |
| expectLabels(container, expectedPvLabels); |
| }); |
| }); |
|
|
| describe('interaction after initial animation completes', () => { |
| async function prime(container: HTMLElement, animationManager: MockProgressAnimationManager) { |
| |
| await animationManager.completeAnimation(); |
|
|
| |
| const button = container.querySelector('button'); |
| expect(button).toBeInTheDocument(); |
| act(() => { |
| button.click(); |
| }); |
|
|
| |
| } |
|
|
| it('should keep the whole line visible during the animation', async () => { |
| const { container, animationManager } = renderTestCase(); |
| await prime(container, animationManager); |
| const fullyVisibleLine = '100px 0px'; |
|
|
| |
| |
| |
| expect(getLine(container)).toHaveAttribute('stroke-dasharray', fullyVisibleLine); |
|
|
| await animationManager.setAnimationProgress(0.1); |
| expect(getLine(container)).toHaveAttribute('stroke-dasharray', fullyVisibleLine); |
|
|
| await animationManager.setAnimationProgress(0.5); |
| expect(getLine(container)).toHaveAttribute('stroke-dasharray', fullyVisibleLine); |
|
|
| await animationManager.setAnimationProgress(1); |
| expect(getLine(container)).toHaveAttribute('stroke-dasharray', fullyVisibleLine); |
|
|
| await animationManager.completeAnimation(); |
| expect(getLine(container)).toHaveAttribute('stroke-dasharray', fullyVisibleLine); |
| }); |
|
|
| it('should hide labels during the animation', async () => { |
| const { container, animationManager } = renderTestCase(); |
|
|
| await prime(container, animationManager); |
|
|
| |
| |
| |
| |
| |
| |
| expectLabels(container, pvLabelsWithUvPositions); |
|
|
| await animationManager.setAnimationProgress(0.2); |
| |
| expectLabels(container, []); |
|
|
| await animationManager.setAnimationProgress(0.5); |
| |
| expectLabels(container, []); |
|
|
| await animationManager.setAnimationProgress(1); |
| |
| expectLabels(container, []); |
|
|
| await animationManager.completeAnimation(); |
|
|
| |
| expectLabels(container, expectedPvLabels); |
| }); |
|
|
| it('should animate the line path', async () => { |
| const { container, animationManager } = renderTestCase(); |
|
|
| await prime(container, animationManager); |
|
|
| const initialPath = 'M5,5L23,27.5L41,27.5L59,50L77,32.45L95,52.475'; |
| |
| |
| |
| |
| expect(getLine(container).getAttribute('d')).toBe(initialPath); |
|
|
| |
| await animationManager.setAnimationProgress(0.2); |
| expect(getLine(container).getAttribute('d')).toBe('M5,18.68L23,32.779L41,38.484L59,41.36L77,37.926L95,52.34'); |
| expect(getLine(container).getAttribute('d')).not.toBe(initialPath); |
|
|
| await animationManager.setAnimationProgress(0.5); |
| expect(getLine(container).getAttribute('d')).toBe('M5,39.2L23,40.699L41,54.959L59,28.4L77,46.139L95,52.138'); |
|
|
| await animationManager.setAnimationProgress(1); |
| expect(getLine(container).getAttribute('d')).toBe('M5,73.4L23,53.897L41,82.418L59,6.8L77,59.828L95,51.8'); |
|
|
| |
| await animationManager.completeAnimation(); |
| expect(getLine(container).getAttribute('d')).toBe('M5,73.4L23,53.897L41,82.418L59,6.8L77,59.828L95,51.8'); |
| }); |
|
|
| it('should animate the dots', async () => { |
| const { container, animationManager } = renderTestCase(); |
|
|
| await prime(container, animationManager); |
|
|
| |
| expectDots(container, [ |
| { |
| cx: '5', |
| cy: '5', |
| }, |
| { |
| cx: '23', |
| cy: '27.5', |
| }, |
| { |
| cx: '41', |
| cy: '27.5', |
| }, |
| { |
| cx: '59', |
| cy: '50', |
| }, |
| { |
| cx: '77', |
| cy: '32.45', |
| }, |
| { |
| cx: '95', |
| cy: '52.475', |
| }, |
| ]); |
|
|
| |
| await animationManager.setAnimationProgress(0.2); |
| expectDots(container, [ |
| { |
| cx: '5', |
| cy: '18.68', |
| }, |
| { |
| cx: '23', |
| cy: '32.7794', |
| }, |
| { |
| cx: '41', |
| cy: '38.483599999999996', |
| }, |
| { |
| cx: '59', |
| cy: '41.36', |
| }, |
| { |
| cx: '77', |
| cy: '37.9256', |
| }, |
| { |
| cx: '95', |
| cy: '52.34', |
| }, |
| ]); |
|
|
| await animationManager.setAnimationProgress(0.5); |
| expectDots(container, [ |
| { |
| cx: '5', |
| cy: '39.2', |
| }, |
| { |
| cx: '23', |
| cy: '40.6985', |
| }, |
| { |
| cx: '41', |
| cy: '54.958999999999996', |
| }, |
| { |
| cx: '59', |
| cy: '28.400000000000002', |
| }, |
| { |
| cx: '77', |
| cy: '46.138999999999996', |
| }, |
| { |
| cx: '95', |
| cy: '52.1375', |
| }, |
| ]); |
|
|
| await animationManager.setAnimationProgress(1); |
| const finalDotPositions = [ |
| { |
| cx: '5', |
| cy: '73.4', |
| }, |
| { |
| cx: '23', |
| cy: '53.897000000000006', |
| }, |
| { |
| cx: '41', |
| cy: '82.41799999999999', |
| }, |
| { |
| cx: '59', |
| cy: '6.8000000000000025', |
| }, |
| { |
| cx: '77', |
| cy: '59.827999999999996', |
| }, |
| { |
| cx: '95', |
| cy: '51.8', |
| }, |
| ]; |
| expectDots(container, finalDotPositions); |
|
|
| |
| await animationManager.completeAnimation(); |
| expectDots(container, finalDotPositions); |
| }); |
| }); |
| }); |
|
|
| describe('tests that add more elements to the data array', () => { |
| const data1 = PageData.slice(0, 2); |
| const data2 = PageData.slice(0, 4); |
|
|
| const renderTestCase = createSelectorTestCase(({ children }) => { |
| const [data, setData] = useState(data1); |
| const addMoreData = () => { |
| setData(prevData => { |
| if (prevData === data1) { |
| return data2; |
| } |
| return data1; |
| }); |
| }; |
| return ( |
| <div> |
| <button type="button" onClick={addMoreData}> |
| Add more data |
| </button> |
| <LineChart data={data} width={100} height={100}> |
| <Line dataKey="uv" animationEasing="linear" label /> |
| {children} |
| </LineChart> |
| </div> |
| ); |
| }); |
|
|
| async function prime(container: HTMLElement, animationManager: MockProgressAnimationManager) { |
| |
| await animationManager.completeAnimation(); |
|
|
| |
| const button = container.querySelector('button'); |
| expect(button).toBeInTheDocument(); |
| act(() => { |
| button.click(); |
| }); |
|
|
| |
| } |
|
|
| it('should keep the whole line visible during the animation', async () => { |
| const { container, animationManager } = renderTestCase(); |
| await prime(container, animationManager); |
| const fullyVisibleLine = '100px 0px'; |
|
|
| |
| |
| |
| expect(getLine(container)).toHaveAttribute('stroke-dasharray', fullyVisibleLine); |
|
|
| await animationManager.setAnimationProgress(0.1); |
| expect(getLine(container)).toHaveAttribute('stroke-dasharray', fullyVisibleLine); |
|
|
| await animationManager.setAnimationProgress(0.5); |
| expect(getLine(container)).toHaveAttribute('stroke-dasharray', fullyVisibleLine); |
|
|
| await animationManager.setAnimationProgress(1); |
| expect(getLine(container)).toHaveAttribute('stroke-dasharray', fullyVisibleLine); |
|
|
| await animationManager.completeAnimation(); |
| expect(getLine(container)).toHaveAttribute('stroke-dasharray', fullyVisibleLine); |
| }); |
|
|
| it('should hide labels during the animation', async () => { |
| const { container, animationManager } = renderTestCase(); |
|
|
| await prime(container, animationManager); |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| expectLabels(container, [ |
| { |
| height: null, |
| offset: '5', |
| textContent: '400', |
| width: null, |
| x: '5', |
| y: '5', |
| }, |
| { |
| height: null, |
| offset: '5', |
| textContent: '300', |
| width: null, |
| x: '5', |
| y: '5', |
| }, |
| { |
| height: null, |
| offset: '5', |
| textContent: '300', |
| width: null, |
| x: '95', |
| y: '27.5', |
| }, |
| { |
| height: null, |
| offset: '5', |
| textContent: '200', |
| width: null, |
| x: '95', |
| y: '27.5', |
| }, |
| ]); |
|
|
| await animationManager.setAnimationProgress(0.2); |
| |
| expectLabels(container, []); |
|
|
| await animationManager.setAnimationProgress(0.5); |
| |
| expectLabels(container, []); |
|
|
| await animationManager.setAnimationProgress(1); |
| |
| expectLabels(container, []); |
|
|
| await animationManager.completeAnimation(); |
|
|
| |
| expectLabels(container, [ |
| { |
| height: null, |
| offset: '5', |
| textContent: '400', |
| width: null, |
| x: '5', |
| y: '5', |
| }, |
| { |
| height: null, |
| offset: '5', |
| textContent: '300', |
| width: null, |
| x: '35', |
| y: '27.5', |
| }, |
| { |
| height: null, |
| offset: '5', |
| textContent: '300', |
| width: null, |
| x: '65', |
| y: '27.5', |
| }, |
| { |
| height: null, |
| offset: '5', |
| textContent: '200', |
| width: null, |
| x: '95', |
| y: '50', |
| }, |
| ]); |
| }); |
|
|
| it('should animate the line path', async () => { |
| const { container, animationManager } = renderTestCase(); |
|
|
| await prime(container, animationManager); |
|
|
| const initialPath = 'M5,5L5,5L95,27.5L95,27.5'; |
| |
| |
| |
| |
| expect(getLine(container).getAttribute('d')).toBe(initialPath); |
|
|
| |
| await animationManager.setAnimationProgress(0.2); |
| expect(getLine(container).getAttribute('d')).toBe('M5,5L11,9.5L89,27.5L95,32'); |
| expect(getLine(container).getAttribute('d')).not.toBe(initialPath); |
|
|
| await animationManager.setAnimationProgress(0.5); |
| expect(getLine(container).getAttribute('d')).toBe('M5,5L20,16.25L80,27.5L95,38.75'); |
|
|
| await animationManager.setAnimationProgress(1); |
| expect(getLine(container).getAttribute('d')).toBe('M5,5L35,27.5L65,27.5L95,50'); |
|
|
| |
| await animationManager.completeAnimation(); |
| expect(getLine(container).getAttribute('d')).toBe('M5,5L35,27.5L65,27.5L95,50'); |
| }); |
|
|
| it('should add more dots and animate them', async () => { |
| const { container, animationManager } = renderTestCase(); |
|
|
| |
| expectDots(container, [ |
| { |
| cx: '5', |
| cy: '5', |
| }, |
| { |
| cx: '95', |
| cy: '27.5', |
| }, |
| ]); |
|
|
| await prime(container, animationManager); |
|
|
| |
| |
| |
| |
| expectDots(container, [ |
| { |
| cx: '5', |
| cy: '5', |
| }, |
| { |
| cx: '5', |
| cy: '5', |
| }, |
| { |
| cx: '95', |
| cy: '27.5', |
| }, |
| { |
| cx: '95', |
| cy: '27.5', |
| }, |
| ]); |
|
|
| |
| await animationManager.setAnimationProgress(0.2); |
| expectDots(container, [ |
| { |
| cx: '5', |
| cy: '5', |
| }, |
| { |
| cx: '11', |
| cy: '9.5', |
| }, |
| { |
| cx: '89', |
| cy: '27.5', |
| }, |
| { |
| cx: '95', |
| cy: '32', |
| }, |
| ]); |
|
|
| await animationManager.setAnimationProgress(0.5); |
| expectDots(container, [ |
| { |
| cx: '5', |
| cy: '5', |
| }, |
| { |
| cx: '20', |
| cy: '16.25', |
| }, |
| { |
| cx: '80', |
| cy: '27.5', |
| }, |
| { |
| cx: '95', |
| cy: '38.75', |
| }, |
| ]); |
|
|
| await animationManager.setAnimationProgress(1); |
| const finalDotPositions = [ |
| { |
| cx: '5', |
| cy: '5', |
| }, |
| { |
| cx: '35', |
| cy: '27.5', |
| }, |
| { |
| cx: '65', |
| cy: '27.5', |
| }, |
| { |
| cx: '95', |
| cy: '50', |
| }, |
| ]; |
| expectDots(container, finalDotPositions); |
|
|
| |
| await animationManager.completeAnimation(); |
| expectDots(container, finalDotPositions); |
| }); |
| }); |
|
|
| describe('when the line element hides during the animation', () => { |
| const renderTestCase = createSelectorTestCase(({ children }) => { |
| const [isVisible, setIsVisible] = useState(true); |
| const toggleVisibility = () => { |
| setIsVisible(prev => !prev); |
| }; |
| return ( |
| <div> |
| <button type="button" onClick={toggleVisibility}> |
| Toggle visibility |
| </button> |
| <LineChart data={PageData} width={100} height={100}> |
| <Line dataKey="uv" animationEasing="linear" label hide={!isVisible} /> |
| {children} |
| </LineChart> |
| </div> |
| ); |
| }); |
|
|
| it('should not crash when the line hides during the animation', async () => { |
| const { container, animationManager } = renderTestCase(); |
|
|
| |
| await animationManager.setAnimationProgress(0.3); |
|
|
| |
| const button = container.querySelector('button'); |
| expect(button).toBeInTheDocument(); |
| act(() => { |
| button.click(); |
| }); |
|
|
| |
| expectLines(container, []); |
| }); |
|
|
| it('should restart the animation from the beginning when the line element appears again', async () => { |
| const { container, animationManager } = renderTestCase(); |
|
|
| |
| await animationManager.setAnimationProgress(0.3); |
|
|
| |
| const button = container.querySelector('button'); |
| expect(button).toBeInTheDocument(); |
| act(() => { |
| button.click(); |
| }); |
|
|
| expectLines(container, []); |
|
|
| |
| act(() => { |
| button.click(); |
| }); |
|
|
| |
| expect(getLine(container)).toHaveAttribute('stroke-dasharray', '0px 0px'); |
| expect(getLine(container).getAttribute('d')).toBe('M5,5L23,27.5L41,27.5L59,50L77,32.45L95,52.475'); |
|
|
| await animationManager.setAnimationProgress(0.3); |
| |
| expect(getLine(container)).toHaveAttribute('stroke-dasharray', '30px 70px'); |
|
|
| |
| await animationManager.completeAnimation(); |
|
|
| |
| expectLines(container, [{ d: 'M5,5L23,27.5L41,27.5L59,50L77,32.45L95,52.475' }]); |
| }); |
| }); |
| }); |
|
|