|
| 1 | +import { expect, test } from '../../../../../../playwright/fixtures/main-fixtures.js'; |
| 2 | +import { complexDataSet } from '../../../resources/DemoProps.js'; |
| 3 | +import { |
| 4 | + testLoadingStates, |
| 5 | + testPassThroughProps, |
| 6 | + testStackAggregateTotals, |
| 7 | + testZoomingTool, |
| 8 | +} from '../../../test-utils/sharedTests.js'; |
| 9 | +import { BarChart } from '../index.js'; |
| 10 | +import { |
| 11 | + BarChartClickTest, |
| 12 | + BarChartDataPointClickTest, |
| 13 | + BarChartHighlightColorTest, |
| 14 | + BarChartLegendConfigTest, |
| 15 | + BarChartSecondYAxisTest, |
| 16 | +} from './BarChartTestComponents.js'; |
| 17 | + |
| 18 | +const dimensions = [{ accessor: 'name', interval: 0 }]; |
| 19 | +const measures = [ |
| 20 | + { accessor: 'users', label: 'Users' }, |
| 21 | + { accessor: 'sessions', label: 'Active Sessions' }, |
| 22 | + { accessor: 'volume', label: 'Vol.' }, |
| 23 | +]; |
| 24 | +const baseProps = { dataset: complexDataSet, dimensions, measures }; |
| 25 | + |
| 26 | +test.describe('BarChart', () => { |
| 27 | + test('Basic', async ({ mount, page }) => { |
| 28 | + await mount(<BarChart {...baseProps} />); |
| 29 | + await expect(page.locator('.recharts-responsive-container')).toBeVisible(); |
| 30 | + await expect(page.locator('.recharts-bar')).toHaveCount(3); |
| 31 | + await expect(page.locator('.recharts-bar-rectangles')).toHaveCount(3); |
| 32 | + }); |
| 33 | + |
| 34 | + test('click handlers', async ({ mount, page }) => { |
| 35 | + await mount(<BarChartClickTest />); |
| 36 | + |
| 37 | + await page.getByText('January').click(); |
| 38 | + await expect(page.getByTestId('click-count')).toHaveText('1'); |
| 39 | + |
| 40 | + await page.locator('[name="January"]').first().click(); |
| 41 | + await expect(page.getByTestId('click-count')).toHaveText('2'); |
| 42 | + await expect(page.getByTestId('last-payload')).toHaveText(JSON.stringify(complexDataSet[0])); |
| 43 | + |
| 44 | + await page.locator('.recharts-legend-item-text').filter({ hasText: 'Users' }).click(); |
| 45 | + await expect(page.getByTestId('legend-click-count')).toHaveText('1'); |
| 46 | + await expect(page.getByTestId('last-legend-value')).toHaveText('Users'); |
| 47 | + |
| 48 | + await page.locator('.recharts-legend-item-text').filter({ hasText: 'Vol.' }).click(); |
| 49 | + await expect(page.getByTestId('last-legend-datakey')).toHaveText('volume'); |
| 50 | + }); |
| 51 | + |
| 52 | + testLoadingStates(BarChart, baseProps, { dimensions: [], measures: [] }, '.recharts-bar'); |
| 53 | + |
| 54 | + test('legendConfig', async ({ mount, page }) => { |
| 55 | + await mount(<BarChartLegendConfigTest />); |
| 56 | + await expect(page.getByTestId('catval').first()).toBeVisible(); |
| 57 | + }); |
| 58 | + |
| 59 | + testZoomingTool(BarChart, baseProps); |
| 60 | + |
| 61 | + testPassThroughProps(BarChart, { dimensions: [], measures: [] }); |
| 62 | + |
| 63 | + testStackAggregateTotals(BarChart, { dataset: complexDataSet.slice(0, 3), dimensions }, [ |
| 64 | + { accessor: 'users', stackId: 'A', label: 'Users' }, |
| 65 | + { accessor: 'sessions', stackId: 'A', label: 'Active Sessions' }, |
| 66 | + ]); |
| 67 | + |
| 68 | + test('onDataPointClick', async ({ mount, page }) => { |
| 69 | + await mount(<BarChartDataPointClickTest />); |
| 70 | + |
| 71 | + await page.locator('[name="January"]').first().click(); |
| 72 | + await expect(page.getByTestId('dp-click-count')).toHaveText('1'); |
| 73 | + await expect(page.getByTestId('dp-last-datakey')).not.toHaveText(''); |
| 74 | + await expect(page.getByTestId('dp-last-value')).not.toHaveText(''); |
| 75 | + await expect(page.getByTestId('dp-last-data-index')).not.toHaveText('-1'); |
| 76 | + await expect(page.getByTestId('dp-last-payload')).toHaveText(JSON.stringify(complexDataSet[0])); |
| 77 | + }); |
| 78 | + |
| 79 | + test('highlightColor', async ({ mount, page }) => { |
| 80 | + await mount(<BarChartHighlightColorTest />); |
| 81 | + |
| 82 | + // January has users=100 (<=200 → green), February has users=230 (>200 → red) |
| 83 | + await expect(page.locator('.recharts-bar-rectangle [fill="green"]').first()).toBeAttached(); |
| 84 | + await expect(page.locator('.recharts-bar-rectangle [fill="red"]').first()).toBeAttached(); |
| 85 | + }); |
| 86 | + |
| 87 | + test('secondYAxis', async ({ mount, page }) => { |
| 88 | + await mount(<BarChartSecondYAxisTest />); |
| 89 | + // BarChart is horizontal so the secondary "Y" axis renders as an additional XAxis |
| 90 | + await expect(page.locator('.recharts-xAxis')).toHaveCount(2); |
| 91 | + }); |
| 92 | +}); |
0 commit comments