Skip to content

Commit

Permalink
fix(component-activity-tab): fix commit activity tab in components de…
Browse files Browse the repository at this point in the history
…tails page
  • Loading branch information
karthikjeeyar committed Oct 30, 2023
1 parent aac31b4 commit f9ee28e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
34 changes: 34 additions & 0 deletions src/hooks/__tests__/useComponents.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { useK8sWatchResource } from '@openshift/dynamic-plugin-sdk-utils';
import { renderHook } from '@testing-library/react-hooks';
import { mockComponentsData } from '../../components/ApplicationDetails/__data__/WorkflowComponentsData';
import { useComponents } from '../useComponents';

jest.mock('@openshift/dynamic-plugin-sdk-utils', () => ({
useK8sWatchResource: jest.fn(() => [[], true]),
getActiveWorkspace: jest.fn(),
}));

const useK8sWatchResourceMock = useK8sWatchResource as jest.Mock;

describe('useComponents', () => {
it('should return empty array when call is inflight', () => {
useK8sWatchResourceMock.mockReturnValue([[], false, undefined]);
const { result } = renderHook(() => useComponents('test-ns', 'test-dev-samples'));
expect(result.current).toEqual([[], false, undefined]);
});

it('should return empty array when the namespace is not passed', () => {
const { result } = renderHook(() => useComponents(null, 'test-dev-samples'));
expect(useK8sWatchResourceMock).toHaveBeenCalledWith(null);

expect(result.current).toEqual([[], false, undefined]);
});

it('should return components when namespace is passed', () => {
useK8sWatchResourceMock.mockReturnValue([mockComponentsData, true, undefined]);

const { result } = renderHook(() => useComponents('test-ns', 'test-dev-samples'));
const [components] = result.current;
expect(components).toHaveLength(3);
});
});
14 changes: 9 additions & 5 deletions src/hooks/useComponents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,15 @@ export const useComponents = (
namespace: string,
applicationName: string,
): [ComponentKind[], boolean, unknown] => {
const [components, componentsLoaded, error] = useK8sWatchResource<ComponentKind[]>({
groupVersionKind: ComponentGroupVersionKind,
namespace,
isList: true,
});
const [components, componentsLoaded, error] = useK8sWatchResource<ComponentKind[]>(
namespace
? {
groupVersionKind: ComponentGroupVersionKind,
namespace,
isList: true,
}
: null,
);
const appComponents: ComponentKind[] = React.useMemo(
() =>
componentsLoaded
Expand Down

0 comments on commit f9ee28e

Please sign in to comment.