|
| 1 | +import { fireEvent, render, screen } from '@shared/tests'; |
| 2 | +import * as React from 'react'; |
| 3 | +import { complexDataSet } from '../../resources/DemoProps'; |
| 4 | +import { ColumnChartWithTrend } from './ColumnChartWithTrend'; |
| 5 | +import { createPassThroughPropsTest } from '@shared/tests/utils'; |
| 6 | + |
| 7 | +const dimensions = [ |
| 8 | + { |
| 9 | + accessor: 'name', |
| 10 | + formatter: (d) => `${d} 2019`, |
| 11 | + interval: 0 |
| 12 | + } |
| 13 | +]; |
| 14 | + |
| 15 | +const measures = [ |
| 16 | + { |
| 17 | + accessor: 'users', |
| 18 | + label: 'Users', |
| 19 | + formatter: (val) => val.toLocaleString(), |
| 20 | + type: 'line' |
| 21 | + }, |
| 22 | + { |
| 23 | + accessor: 'sessions', |
| 24 | + label: 'Active Sessions', |
| 25 | + type: 'column' |
| 26 | + } |
| 27 | +]; |
| 28 | + |
| 29 | +describe('ColumnChart', () => { |
| 30 | + it('Renders with data', async () => { |
| 31 | + const { container, asFragment } = render( |
| 32 | + <ColumnChartWithTrend dataset={complexDataSet} dimensions={dimensions} measures={measures} /> |
| 33 | + ); |
| 34 | + |
| 35 | + // Check if two responsive containers are rendered |
| 36 | + const responsiveContainers = container.querySelectorAll('div.recharts-responsive-container'); |
| 37 | + expect(responsiveContainers.length).toBe(2); |
| 38 | + |
| 39 | + // Check if trend line container, trend line and the path of the line is rendered correctly |
| 40 | + const trendLineChartContainer = container.querySelector('g.recharts-line'); |
| 41 | + expect(trendLineChartContainer).toBeInTheDocument(); |
| 42 | + const trendLine = trendLineChartContainer.querySelector('path'); |
| 43 | + expect(trendLine).not.toBeNull(); |
| 44 | + expect(trendLine.getAttribute('d')[0]).toBe('M'); |
| 45 | + |
| 46 | + // Check if column chart container, bars and single bars are rendered |
| 47 | + const columnChartContainer = container.querySelector('g.recharts-bar'); |
| 48 | + expect(columnChartContainer).toBeInTheDocument(); |
| 49 | + const barContainer = columnChartContainer.querySelectorAll('g.recharts-bar-rectangles'); |
| 50 | + expect(barContainer.length).toBeGreaterThanOrEqual(1); |
| 51 | + const singleBars = columnChartContainer.querySelectorAll('g.recharts-bar-rectangle'); |
| 52 | + expect(singleBars.length).toBeGreaterThanOrEqual(1); |
| 53 | + |
| 54 | + // Check if snapshot matches render |
| 55 | + expect(asFragment()).toMatchSnapshot(); |
| 56 | + }); |
| 57 | + |
| 58 | + it('Check onClick events', async () => { |
| 59 | + const onClick = jest.fn(); |
| 60 | + const onLegendClick = jest.fn(); |
| 61 | + const { container, asFragment } = render( |
| 62 | + <ColumnChartWithTrend |
| 63 | + onLegendClick={onLegendClick} |
| 64 | + onClick={onClick} |
| 65 | + dataset={complexDataSet} |
| 66 | + dimensions={dimensions} |
| 67 | + measures={measures} |
| 68 | + /> |
| 69 | + ); |
| 70 | + |
| 71 | + // Check if click on axis label is working |
| 72 | + const firstXAxisLabel = screen.getByText(/January 2.../); |
| 73 | + fireEvent.click(firstXAxisLabel); |
| 74 | + expect(onClick).toBeCalled(); |
| 75 | + |
| 76 | + // Check if click on legend is working |
| 77 | + const legendContainer = screen.getByText(/Active Sessions/); |
| 78 | + fireEvent.click(legendContainer); |
| 79 | + expect(onLegendClick).toBeCalled(); |
| 80 | + |
| 81 | + // Check if click in column chart container is working |
| 82 | + fireEvent.click(container.querySelector('g.recharts-bar')); |
| 83 | + expect(onClick).toBeCalled(); |
| 84 | + |
| 85 | + // Check if click in trend line container is working |
| 86 | + fireEvent.click(container.querySelector('g.recharts-line')); |
| 87 | + expect(onClick).toBeCalled(); |
| 88 | + |
| 89 | + // Check if snapshot matches render |
| 90 | + expect(asFragment()).toMatchSnapshot(); |
| 91 | + }); |
| 92 | + |
| 93 | + it('Loading placeholder', () => { |
| 94 | + const { container, asFragment } = render(<ColumnChartWithTrend dimensions={[]} measures={[]} />); |
| 95 | + |
| 96 | + // Check if no responsive container is rendered |
| 97 | + const responsiveContainers = container.querySelectorAll('div.recharts-responsive-container'); |
| 98 | + expect(responsiveContainers.length).toBe(0); |
| 99 | + |
| 100 | + // Check if no trend line container is rendered |
| 101 | + const trendLineContainer = container.querySelector('g.recharts-line'); |
| 102 | + expect(trendLineContainer).toBeNull(); |
| 103 | + |
| 104 | + // Check if no column chart container is rendered |
| 105 | + const columnChartContainer = container.querySelector('g.recharts-bar'); |
| 106 | + expect(columnChartContainer).toBeNull(); |
| 107 | + |
| 108 | + // Check if snapshot matches render |
| 109 | + expect(asFragment()).toMatchSnapshot(); |
| 110 | + }); |
| 111 | + |
| 112 | + it('onLegendClick should not crash when invalid handler is provided', () => { |
| 113 | + render( |
| 114 | + <ColumnChartWithTrend |
| 115 | + dataset={complexDataSet} |
| 116 | + dimensions={dimensions} |
| 117 | + measures={measures} |
| 118 | + onLegendClick={'123' as any} |
| 119 | + /> |
| 120 | + ); |
| 121 | + |
| 122 | + expect(() => { |
| 123 | + fireEvent.click(screen.getByText(/Active Sessions/)); |
| 124 | + }).not.toThrow(); |
| 125 | + }); |
| 126 | + |
| 127 | + createPassThroughPropsTest(ColumnChartWithTrend, { dimensions: [], measures: [] }); |
| 128 | +}); |
0 commit comments