From 3985c9ed4c1539d56eb56109f081338d01165bc0 Mon Sep 17 00:00:00 2001 From: Msarawan Date: Thu, 12 Sep 2024 14:05:17 +0530 Subject: [PATCH] [#218] fix: Broker page refreshes automatically upon broker creation fixed the bug where the broker page refreshes automatically upon broker creation. and updated the unit tests. --- .../BrokersList.container.test.tsx | 24 +++++---- .../view-brokers/BrokersList.container.tsx | 51 +++++++++---------- src/k8s/models.ts | 2 +- 3 files changed, 39 insertions(+), 38 deletions(-) diff --git a/src/brokers/view-brokers/BrokersList.container.test.tsx b/src/brokers/view-brokers/BrokersList.container.test.tsx index 0e63bcf3..28d0aa53 100644 --- a/src/brokers/view-brokers/BrokersList.container.test.tsx +++ b/src/brokers/view-brokers/BrokersList.container.test.tsx @@ -2,10 +2,10 @@ import { render, screen, waitFor } from '@app/test-utils'; import { AMQBrokerModel } from '@app/k8s/models'; import { BrokersContainer } from './BrokersList.container'; import { useParams, useNavigate } from 'react-router-dom-v5-compat'; -import { k8sListItems } from '@openshift-console/dynamic-plugin-sdk'; +import { useK8sWatchResource } from '@openshift-console/dynamic-plugin-sdk'; jest.mock('@openshift-console/dynamic-plugin-sdk', () => ({ - k8sListItems: jest.fn(), + useK8sWatchResource: jest.fn(), k8sDelete: jest.fn(), })); @@ -45,12 +45,12 @@ describe('BrokersContainer', () => { const mockUseNavigate = useNavigate as jest.Mock; const mockUseParams = useParams as jest.Mock; const mockNamespace = 'test-namespace'; - const mockK8sListItems = k8sListItems as jest.Mock; + const mockK8sWatchResource = useK8sWatchResource as jest.Mock; beforeEach(() => { mockUseNavigate.mockReturnValue(jest.fn()); mockUseParams.mockReturnValue({ ns: mockNamespace }); - mockK8sListItems.mockResolvedValue([]); + mockK8sWatchResource.mockReturnValue([[], true, null]); }); afterEach(() => { @@ -61,9 +61,14 @@ describe('BrokersContainer', () => { render(); await waitFor(() => { - expect(k8sListItems).toHaveBeenCalledWith({ - model: AMQBrokerModel, - queryParams: { ns: mockNamespace }, + expect(useK8sWatchResource).toHaveBeenCalledWith({ + namespace: mockNamespace, + groupVersionKind: { + kind: AMQBrokerModel.kind, + version: AMQBrokerModel.apiVersion, + group: AMQBrokerModel.apiGroup, + }, + isList: true, }); }); expect(screen.getByText('BrokersList Component')).toBeInTheDocument(); @@ -72,9 +77,10 @@ describe('BrokersContainer', () => { ).toBeInTheDocument(); }); - it('should handle API error on fetchK8sListItems', async () => { + it('should handle API error on fetching the ListItems', async () => { const errorMessage = 'Failed to load brokers'; - mockK8sListItems.mockRejectedValue(new Error(errorMessage)); + + mockK8sWatchResource.mockReturnValue([[], false, errorMessage]); render(); diff --git a/src/brokers/view-brokers/BrokersList.container.tsx b/src/brokers/view-brokers/BrokersList.container.tsx index 1df19de2..3ad64a49 100644 --- a/src/brokers/view-brokers/BrokersList.container.tsx +++ b/src/brokers/view-brokers/BrokersList.container.tsx @@ -1,5 +1,8 @@ -import { useEffect, useState, FC } from 'react'; -import { k8sListItems, k8sDelete } from '@openshift-console/dynamic-plugin-sdk'; +import { useState, FC } from 'react'; +import { + k8sDelete, + useK8sWatchResource, +} from '@openshift-console/dynamic-plugin-sdk'; import { AMQBrokerModel } from '@app/k8s/models'; import { K8sResourceCommonWithData, BrokerCR } from '@app/k8s/types'; import { BrokersList } from './components/BrokersList/BrokersList'; @@ -11,33 +14,23 @@ export const BrokersContainer: FC = () => { const { ns: namespace } = useParams<{ ns?: string }>(); //states - const [brokers, setBrokers] = useState(); - const [loading, setLoading] = useState(true); - const [loadError, setLoadError] = useState(); const [isModalOpen, setIsModalOpen] = useState(false); const [selectedBroker, setSelectedBroker] = useState(); + const [_deleteError, setDeleteError] = useState(null); + const [_deleteSuccess, setDeleteSuccess] = useState(false); - const fetchK8sListItems = () => { - setLoading(false); - k8sListItems({ - model: AMQBrokerModel, - queryParams: { ns: namespace }, - }) - .then((brokers) => { - setBrokers(brokers); - }) - .catch((e) => { - setLoadError(e.message); - }) - .finally(() => { - setLoading(true); - }); - }; - - useEffect(() => { - fetchK8sListItems(); - }, [namespace]); + const [brokers, loaded, loadError] = useK8sWatchResource< + K8sResourceCommonWithData[] + >({ + namespace, + groupVersionKind: { + kind: AMQBrokerModel.kind, + version: AMQBrokerModel.apiVersion, + group: AMQBrokerModel.apiGroup, + }, + isList: true, + }); const onEditBroker = (broker: BrokerCR) => { const namespace = broker.metadata.namespace; @@ -51,10 +44,12 @@ export const BrokersContainer: FC = () => { resource: { ...selectedBroker }, }) .then(() => { - fetchK8sListItems(); + setDeleteSuccess(true); + setDeleteError(null); }) .catch((e) => { - setLoadError(e.message); + setDeleteError(`Failed to delete broker: ${e.message}`); + setDeleteSuccess(false); }) .finally(() => { setIsModalOpen(false); @@ -77,7 +72,7 @@ export const BrokersContainer: FC = () => {