ArafatKhan2198 commented on code in PR #7626:
URL: https://github.com/apache/ozone/pull/7626#discussion_r1900558001


##########
hadoop-ozone/recon/src/main/resources/webapps/recon/ozone-recon-web/src/__tests__/datanodes/Datanodes.test.tsx:
##########
@@ -0,0 +1,166 @@
+/*
+ * 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,
+  waitFor
+} from '@testing-library/react';
+import userEvent from '@testing-library/user-event';
+import { rest } from "msw";
+import { vi } from 'vitest';
+
+import Datanodes from '@/v2/pages/datanodes/datanodes';
+import * as commonUtils from '@/utils/common';
+import { datanodeServer } from 
'@/__tests__/mocks/datanodeMocks/datanodeServer';
+import { datanodeLocators } from '@/__tests__/locators/locators';
+
+// Mock utility functions
+vi.spyOn(commonUtils, 'showDataFetchError');
+
+vi.mock('@/components/autoReloadPanel/autoReloadPanel', () => ({
+  default: () => <div data-testid="auto-reload-panel" />,
+}));
+vi.mock('@/v2/components/select/multiSelect.tsx', () => ({
+  default: ({ onChange }: { onChange: Function }) => (
+    <select data-testid="multi-select" onChange={(e) => 
onChange(e.target.value)}>
+      <option value="hostname">Hostname</option>
+      <option value="uuid">UUID</option>
+    </select>
+  ),
+}));
+
+describe('Datanodes Component', () => {
+  // Start and stop MSW server before and after all tests
+  beforeAll(() => datanodeServer.listen());
+  afterEach(() => datanodeServer.resetHandlers());
+  afterAll(() => datanodeServer.close());
+
+  test('renders component correctly', () => {
+    render(<Datanodes />);
+
+    expect(screen.getByText(/Datanodes/)).toBeInTheDocument();
+    expect(screen.getByTestId('auto-reload-panel')).toBeInTheDocument();
+    expect(screen.getByTestId('multi-select')).toBeInTheDocument();
+    expect(screen.getByTestId('search-input')).toBeInTheDocument();
+  });
+
+  test('renders table with correct number of rows', async () => {
+    render(<Datanodes />);
+
+    // Wait for the data to load
+    const rows = await waitFor(() => screen.getAllByTestId(/dntable-/));

Review Comment:
   Not sure if this will help much, but for improving readability and 
reusability, this suggestion is optional. Apply it only if you feel it would be 
beneficial.
   
   ### Suggestion: Create Helper Functions
   
   Extract reusable logic (e.g., row selection, waiting for API data) into 
helper functions. This can simplify tests and make them more maintainable.
   
   #### Example Helper Functions:
   
   ```javascript
   const waitForTableData = async () => {
     return await waitFor(() => screen.getAllByTestId(/dntable-/));
   };
   
   const selectRow = (rowId: string) => {
     const checkbox = screen.getByTestId(`checkbox-${rowId}`);
     fireEvent.click(checkbox);
   };
   ```
   
   #### Usage Example:
   
   ```javascript
   test('selects a row correctly', async () => {
     await waitForTableData();
     selectRow('1');
     
expect(screen.getByTestId('dntable-1')).toHaveClass('ant-table-row-selected');
   });
   ```
   
   ---
   
   



-- 
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]

Reply via email to