Brijesh619 commented on code in PR #703:
URL: https://github.com/apache/atlas/pull/703#discussion_r3926943584
##########
dashboard/src/utils/metricsUtils.ts:
##########
@@ -239,3 +239,13 @@ export const getClassificationDistribution = (
.sort((a, b) => b.count - a.count)
.slice(0, topN);
};
+
+export interface RechartsEventPayload<T> {
+ payload?: T;
+}
+
+export const getPayloadFromRechartsEvent = <T>(item: unknown): T | undefined
=> {
Review Comment:
Added `getPayloadFromRechartsEvent` helper function to `metricsUtils.ts` and
added unit test coverage in `metricsUtils.test.ts` for:
- Valid payload object
- `null` / `undefined` inputs
- Primitive inputs
- Object without `payload` key
Also updated `EntityStatusDonut.tsx` (as well as `EntityTypeBarChart.tsx`
and `ClassificationDistributionCard.tsx`) to use `getPayloadFromRechartsEvent`
for consistent event payload extraction across chart components.
##########
dashboard/src/views/DashboardOverview/__tests__/ClassificationDistributionCard.test.tsx:
##########
@@ -110,4 +137,38 @@ describe('ClassificationDistributionCard', () => {
expect(visibleTextNodes?.length).toBe(1);
expect(visibleTextNodes?.[0]?.textContent).toBe(truncatedLongName);
});
+
+ it('navigates to classification search on valid bar click', async () =>
{
+ const user = userEvent.setup();
+ render(
+ <MemoryRouter>
+ <ClassificationDistributionCard tag={{}} />
+ </MemoryRouter>,
+ );
+
+ await user.click(screen.getByTestId('bar'));
+
expect(mockNavigateToClassificationSearch).toHaveBeenCalledWith(expect.anything(),
shortName);
+ });
+
+ it('ignores bar click when payload is invalid/missing', async () => {
+ const user = userEvent.setup();
+ render(
+ <MemoryRouter>
+ <ClassificationDistributionCard tag={{}} />
+ </MemoryRouter>,
+ );
+
+ mockNavigateToClassificationSearch.mockClear();
+ mockBarClickPayload = null;
+ await user.click(screen.getByTestId('bar'));
+
expect(mockNavigateToClassificationSearch).not.toHaveBeenCalled();
+
+ mockBarClickPayload = 'bad' as unknown;
+ await user.click(screen.getByTestId('bar'));
+
expect(mockNavigateToClassificationSearch).not.toHaveBeenCalled();
+
+ mockBarClickPayload = {};
+ await user.click(screen.getByTestId('bar'));
+
expect(mockNavigateToClassificationSearch).not.toHaveBeenCalled();
+ });
});
Review Comment:
Added equivalent unit tests in `ClassificationDistributionCard.test.tsx`
covering Y-axis label click, keyboard navigation (`Enter` and `Space`), and
non-interactive key guards for full feature parity with
`EntityTypeBarChart.test.tsx`.
##########
dashboard/src/views/Statistics/__tests__/EntityStatsChart.test.tsx:
##########
@@ -0,0 +1,104 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import React from "react";
+import { render, screen, fireEvent } from "@testing-library/react";
+import EntityStatsChart from "../EntityStatsChart";
+
+jest.mock("recharts", () => {
+ const OriginalRecharts = jest.requireActual("recharts");
+ return {
+ ...OriginalRecharts,
+ ResponsiveContainer: ({ children }: { children: React.ReactNode
}) => <div>{children}</div>,
+ AreaChart: ({ children }: { children: React.ReactNode }) =>
<div>{children}</div>,
+ Area: () => <div data-testid="area" />,
+ CartesianGrid: () => <div />,
+ XAxis: () => <div />,
+ YAxis: () => <div />,
+ Tooltip: () => <div />,
+ Legend: ({ content }: { content: () => React.ReactNode }) => {
+ const Content = content;
+ return <div data-testid="legend-mock">{Content ?
<Content /> : null}</div>;
+ },
+ };
+});
+
+describe("EntityStatsChart custom legend", () => {
+ const mockOnLegendClick = jest.fn();
+ const mockGetColorForKey = jest.fn((key: string) => {
+ if (key === "Active") return "blue";
+ if (key === "Deleted") return "red";
+ if (key === "Shell") return "orange";
+ return "black";
+ });
+
+ const defaultProps = {
+ chartData: [
+ { timestamp: 1600000000000, Active: 10, Deleted: 2,
Shell: 1 },
+ ],
+ chartMode: "stacked",
+ activeKeys: { Active: true, Deleted: false, Shell: true },
+ onLegendClick: mockOnLegendClick,
+ getColorForKey: mockGetColorForKey,
+ };
+
+ beforeEach(() => {
+ jest.clearAllMocks();
+ });
+
+ it("renders the custom legend with correct aria-labels and handles
click toggles", () => {
+ render(<EntityStatsChart {...defaultProps} />);
+
+ const activeLegend = screen.getByTestId("legend-Active");
+ const deletedLegend = screen.getByTestId("legend-Deleted");
+
+ expect(activeLegend).toHaveAttribute("aria-label", "Active");
+ expect(deletedLegend).toHaveAttribute("aria-label", "Deleted");
+
+ fireEvent.click(activeLegend);
+ expect(mockOnLegendClick).toHaveBeenCalledWith("Active");
+
+ fireEvent.click(deletedLegend);
+ expect(mockOnLegendClick).toHaveBeenCalledWith("Deleted");
+ });
+
+ it("applies active styling when key is active", () => {
+ render(<EntityStatsChart {...defaultProps} />);
+
+ const activeLegend = screen.getByTestId("legend-Active");
+ const typography =
activeLegend.querySelector(".legend-typography");
+ const colorBox =
activeLegend.querySelector(".legend-color-box");
+
+ expect(typography).toHaveClass("legend-active");
+ expect(typography).not.toHaveClass("legend-inactive");
+ expect(colorBox).toHaveStyle("background-color: blue");
+ });
+
+ it("applies inactive styling when key is inactive", () => {
+ render(<EntityStatsChart {...defaultProps} />);
+
+ const deletedLegend = screen.getByTestId("legend-Deleted");
+ const typography =
deletedLegend.querySelector(".legend-typography");
+ const colorBox =
deletedLegend.querySelector(".legend-color-box");
+
+ expect(typography).toHaveClass("legend-inactive");
+ expect(typography).not.toHaveClass("legend-active");
+ // The value is rgb equivalent for '#d3d3d3' or just string
depending on jsdom, but typically testing library normalizes it to rgb(211,
211, 211) or allows matching string.
+ // using string matching for '#d3d3d3'
+ expect(colorBox).toHaveStyle({ backgroundColor: "#d3d3d3" });
+ });
+});
Review Comment:
Added unit tests in `EntityStatsChart.test.tsx` covering keyboard
interactions (`Enter` and `Space` keys) on the custom legend `ButtonBase`
elements, along with negative key press cases to verify `onLegendClick`
behavior and accessibility guards.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]