chihsuan commented on code in PR #11825: URL: https://github.com/apache/ozone/pull/11825#discussion_r4453122716
########## hadoop-ozone/recon/src/main/resources/webapps/recon/ozone-recon-web/src/__tests__/heatmap/Heatmap.test.tsx: ########## @@ -0,0 +1,234 @@ +/* + * 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 { fireEvent, render, screen, waitFor } from '@testing-library/react'; +import userEvent from '@testing-library/user-event'; +import { vi } from 'vitest'; + +import Heatmap from '@/v2/pages/heatmap/heatmap'; +import * as commonUtils from '@/utils/common'; +import { + heatmapRequests, + heatmapServer, + heatmapServerErrorHandler, + heatmapUnavailableHandler, + resetHeatmapRequestTracking +} from '@tests/mocks/heatmapMocks/heatmapServer'; + +vi.spyOn(commonUtils, 'showDataFetchError'); + +const plotOnClickRef = vi.hoisted(() => ({ + current: null as ((path: string) => void) | null, +})); + +vi.mock('@/v2/components/plots/heatmapPlot', () => ({ + default: ({ data, onClick, entityType }: { + data: { label?: string | null }; + onClick: (path: string) => void; + entityType: string; + }) => { + plotOnClickRef.current = onClick; + return ( + <div data-testid="heatmap-plot" data-label={data.label ?? ''} data-entity-type={entityType} /> + ); + }, +})); + +vi.mock('@/v2/hooks/useAPIData.hook', async (importOriginal) => { + const actual = await importOriginal<typeof import('@/v2/hooks/useAPIData.hook')>(); + return { + ...actual, + useApiData: <T,>(url: string, defaultValue: T, options = {}) => { + // The hook's own initial GET does not handle a rejected request, so a 404/500 on mount surfaces as an + // unhandled rejection in tests. Skip it and issue the initial request with a handled promise instead. + const api = actual.useApiData(url, defaultValue, { + ...options, + initialFetch: false, + retryAttempts: 0, + retryDelay: 0, + }); + React.useEffect(() => { + if (url) { + api.execute().catch(() => undefined); + } + }, []); // eslint-disable-line react-hooks/exhaustive-deps + return api; + }, + }; +}); + +const waitForPlot = (label: string) => waitFor(() => { + expect(screen.getByTestId('heatmap-plot')).toHaveAttribute('data-label', label); +}); + +const searchPath = async (path: string) => { + const pathInput = screen.getByPlaceholderText('/'); + await userEvent.clear(pathInput); + if (path) { + await userEvent.type(pathInput, path); + } + fireEvent.click(screen.getByRole('button', { name: /search/i })); +}; + +// antd Dropdown opens its overlay on hover after a short delay. +const selectFromDropdown = async (trigger: RegExp, item: string) => { + fireEvent.mouseOver(screen.getByRole('button', { name: trigger })); + fireEvent.click(await screen.findByText(item)); +}; + +// Give any request fired on click a chance to reach the mock server before asserting none was sent. +const flushPendingRequests = () => new Promise((resolve) => setTimeout(resolve, 50)); + +describe('Heatmap page', () => { + beforeAll(() => heatmapServer.listen()); + + beforeEach(() => { + resetHeatmapRequestTracking(); + vi.mocked(commonUtils.showDataFetchError).mockClear(); + }); + + afterEach(() => heatmapServer.resetHandlers()); + afterAll(() => heatmapServer.close()); + + test('renders header, filters, legend, and heatmap plot with data', async () => { + render(<Heatmap />); + + expect(screen.getByText('Heatmap')).toBeInTheDocument(); + expect(screen.getByText('Path')).toBeInTheDocument(); + expect(screen.getByPlaceholderText('/')).toHaveValue('/'); + expect(screen.getByText(/Entity Type:\s*key/)).toBeInTheDocument(); + expect(screen.getByText(/Last\s+24H/)).toBeInTheDocument(); + expect(screen.getByText('Less Accessed')).toBeInTheDocument(); + expect(screen.getByText('Moderate Accessed')).toBeInTheDocument(); + expect(screen.getByText('Most Accessed')).toBeInTheDocument(); + + await waitForPlot('root'); + expect(screen.getByTestId('heatmap-plot')).toHaveAttribute('data-entity-type', 'key'); + expect(heatmapRequests).toEqual([{ path: '/', entityType: 'key', startDate: '24H' }]); + }); + + test('shows validation error for invalid path characters', async () => { + render(<Heatmap />); + await waitForPlot('root'); + + const pathInput = screen.getByPlaceholderText('/'); + await userEvent.clear(pathInput); + await userEvent.type(pathInput, 'bad@path'); + + expect(screen.getByText('Please enter valid path')).toBeInTheDocument(); + }); + + test('fetches heatmap for a new path when the user searches', async () => { + render(<Heatmap />); + await waitForPlot('root'); + + await searchPath('/vol1'); + + await waitForPlot('vol1'); + expect(heatmapRequests.map((request) => request.path)).toEqual(['/', '/vol1']); + }); + + test('does not fetch when searching with an empty path', async () => { + render(<Heatmap />); + await waitForPlot('root'); + + await searchPath(''); + await flushPendingRequests(); + + expect(heatmapRequests).toHaveLength(1); + expect(screen.getByTestId('heatmap-plot')).toHaveAttribute('data-label', 'root'); Review Comment: Could we also switch a filter after the empty search? I noticed this test still passes if the empty-path check in `handleSubmit` is removed, because an empty path never builds a URL anyway. Without the check, the next **Entity Type** change doesn't fetch, and HDDS-17692 will touch that same check. ```suggestion expect(heatmapRequests).toHaveLength(1); expect(screen.getByTestId('heatmap-plot')).toHaveAttribute('data-label', 'root'); await selectFromDropdown(/Entity Type/, 'Bucket'); await waitFor(() => { expect(heatmapRequests).toContainEqual({ path: '/', entityType: 'bucket', startDate: '24H' }); }); ``` -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
