From a94e9f119f741ab675f5d801a6985597520c782a Mon Sep 17 00:00:00 2001 From: srmukher <120183316+srmukher@users.noreply.github.com> Date: Mon, 23 Dec 2024 12:07:07 +0000 Subject: [PATCH] Adding and updating tests --- .../components/DonutChart/DonutChart.base.tsx | 28 +- .../DonutChart/DonutChartRTL.test.tsx | 42 +- .../__snapshots__/DonutChart.test.tsx.snap | 130 +++-- .../__snapshots__/DonutChartRTL.test.tsx.snap | 496 +++++++++++++++++- 4 files changed, 642 insertions(+), 54 deletions(-) diff --git a/packages/charts/react-charting/src/components/DonutChart/DonutChart.base.tsx b/packages/charts/react-charting/src/components/DonutChart/DonutChart.base.tsx index f02cd6f6270bff..0eda3dfe5293cb 100644 --- a/packages/charts/react-charting/src/components/DonutChart/DonutChart.base.tsx +++ b/packages/charts/react-charting/src/components/DonutChart/DonutChart.base.tsx @@ -270,7 +270,7 @@ export class DonutChartBase extends React.Component, currentLegend?: ILegend, ): void { - if (this.props.legendProps?.canSelectMultipleLegends) { + if (this.props.legendProps && this.props.legendProps?.canSelectMultipleLegends) { this.setState({ selectedLegends }); } else { this.setState({ selectedLegends: selectedLegends.slice(-1) }); @@ -280,14 +280,6 @@ export class DonutChartBase extends React.Component { - return this._getHighlightedLegend().includes(legend!); - }; - - private _noLegendsHighlighted = (): boolean => { - return this._getHighlightedLegend().length === 0; - }; - private _focusCallback = (data: IChartDataPoint, id: string, element: SVGPathElement): void => { this._currentHoverElement = element; this.setState({ @@ -344,11 +336,11 @@ export class DonutChartBase extends React.Component point.legend === this.state.legend); - return legendValue - ? legendValue.yAxisCalloutData - ? legendValue.yAxisCalloutData - : legendValue.data! + const pointValue = data.find(point => point.legend === this.state.legend); + return pointValue + ? pointValue.yAxisCalloutData + ? pointValue.yAxisCalloutData + : pointValue.data! : valueInsideDonut; } else if (highlightedLegends.length > 0) { let totalValue = 0; @@ -384,6 +376,14 @@ export class DonutChartBase extends React.Component { + return this._getHighlightedLegend().includes(legend!); + }; + + private _noLegendsHighlighted = (): boolean => { + return this._getHighlightedLegend().length === 0; + }; + private _isChartEmpty(): boolean { return !( this.props.data && diff --git a/packages/charts/react-charting/src/components/DonutChart/DonutChartRTL.test.tsx b/packages/charts/react-charting/src/components/DonutChart/DonutChartRTL.test.tsx index db7dcb00b7e582..cd362b95cc47bb 100644 --- a/packages/charts/react-charting/src/components/DonutChart/DonutChartRTL.test.tsx +++ b/packages/charts/react-charting/src/components/DonutChart/DonutChartRTL.test.tsx @@ -18,6 +18,9 @@ describe('Donut chart interactions', () => { beforeEach(() => { sharedBeforeEach(); jest.spyOn(global.Math, 'random').mockReturnValue(0.1); + // Mock the implementation of wrapTextInsideDonut as it internally calls a Browser Function like + // getComputedTextLength() which will otherwise lead to a crash if mounted + jest.spyOn(utils, 'wrapTextInsideDonut').mockImplementation(() => '20000'); }); afterEach(() => { jest.spyOn(global.Math, 'random').mockRestore(); @@ -70,7 +73,7 @@ describe('Donut chart interactions', () => { test('Should highlight the corresponding Pie on mouse over on legends', () => { // Arrange - const { container } = render(); + const { container } = render(); // Act const legend = screen.queryByText('first'); @@ -157,9 +160,6 @@ describe('Donut chart interactions', () => { }); test('Should change value inside donut with the legend value on mouseOver legend ', () => { - // Mock the implementation of wrapTextInsideDonut as it internally calls a Browser Function like - // getComputedTextLength() which will otherwise lead to a crash if mounted - jest.spyOn(utils, 'wrapTextInsideDonut').mockImplementation(() => '1000'); // Arrange const { container } = render( , @@ -170,7 +170,7 @@ describe('Donut chart interactions', () => { fireEvent.mouseOver(screen.getByText('first')); // Assert - expect(getByClass(container, /insideDonutString.*?/)[0].textContent).toBe('20,000'); + expect(getByClass(container, /insideDonutString.*?/)[0].textContent).toBe('1,000'); }); test('Should reflect theme change', () => { @@ -184,6 +184,38 @@ describe('Donut chart interactions', () => { // Assert expect(container).toMatchSnapshot(); }); + + // add test for legend multi select + test('Should select multiple legends on click', () => { + // Arrange + const { container } = render( + , + ); + + // Act + const firstLegend = screen.queryByText('first')?.closest('button'); + const secondLegend = screen.queryByText('second')?.closest('button'); + expect(firstLegend).toBeDefined(); + expect(secondLegend).toBeDefined(); + fireEvent.click(firstLegend!); + fireEvent.click(secondLegend!); + + // Assert + expect(firstLegend).toHaveAttribute('aria-selected', 'true'); + expect(secondLegend).toHaveAttribute('aria-selected', 'true'); + + const getById = queryAllByAttribute.bind(null, 'id'); + expect(getById(container, /Pie.*?first/i)[0]).toHaveStyle('opacity: 1.0'); + expect(getById(container, /Pie.*?second/i)[0]).toHaveStyle('opacity: 1.0'); + expect(getById(container, /Pie.*?third/i)[0]).toHaveStyle('opacity: 0.1'); + }); }); describe('Donut Chart - axe-core', () => { diff --git a/packages/charts/react-charting/src/components/DonutChart/__snapshots__/DonutChart.test.tsx.snap b/packages/charts/react-charting/src/components/DonutChart/__snapshots__/DonutChart.test.tsx.snap index 376f2500053757..1daf5c828b2547 100644 --- a/packages/charts/react-charting/src/components/DonutChart/__snapshots__/DonutChart.test.tsx.snap +++ b/packages/charts/react-charting/src/components/DonutChart/__snapshots__/DonutChart.test.tsx.snap @@ -125,23 +125,6 @@ exports[`DonutChart - mouse events Should render callout correctly on mouseover role="img" /> - - 20,000 - @@ -831,23 +814,6 @@ exports[`DonutChart - mouse events Should render customized callout on mouseover role="img" /> - - 20,000 - @@ -1911,6 +1877,22 @@ exports[`DonutChart snapShot testing Should render arc labels 1`] = ` onMouseOver={[Function]} role="img" /> + + 20.0k + + + 39.0k + + + 45.0k + @@ -2429,6 +2443,22 @@ exports[`DonutChart snapShot testing Should render arc labels in percentage form onMouseOver={[Function]} role="img" /> + + 19% + + + 38% + + + 43% + diff --git a/packages/charts/react-charting/src/components/DonutChart/__snapshots__/DonutChartRTL.test.tsx.snap b/packages/charts/react-charting/src/components/DonutChart/__snapshots__/DonutChartRTL.test.tsx.snap index 64796dabf922a9..5dab0bf43a88d3 100644 --- a/packages/charts/react-charting/src/components/DonutChart/__snapshots__/DonutChartRTL.test.tsx.snap +++ b/packages/charts/react-charting/src/components/DonutChart/__snapshots__/DonutChartRTL.test.tsx.snap @@ -636,4 +636,498 @@ exports[`Donut chart interactions Should hide callout on mouse leave 1`] = ` `; -exports[`Donut chart interactions Should reflect theme change 1`] = `
`; +exports[`Donut chart interactions Should reflect theme change 1`] = ` +
+
+
+
+
+ + + + + + + + + + + + + +
+
+ +
+
+
+
+
+
+
+
+ +
+
+ +
+
+ +
+
+
+
+
+
+
+
+
+
+
+`;