Skip to content

Commit

Permalink
added test for useAcceleratorProfiles
Browse files Browse the repository at this point in the history
  • Loading branch information
pnaik1 committed Jan 18, 2024
1 parent 8d5d4fe commit 306411e
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 2 deletions.
4 changes: 3 additions & 1 deletion frontend/src/__mocks__/mockAcceleratorProfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { genUID } from './mockUtils';
type MockResourceConfigType = {
name?: string;
namespace?: string;
uid?: string;
displayName?: string;
identifier?: string;
description?: string;
Expand All @@ -15,6 +16,7 @@ type MockResourceConfigType = {
export const mockAcceleratorProfile = ({
name = 'migrated-gpu',
namespace = 'test-project',
uid = genUID('service'),
displayName = 'Nvidia GPU',
identifier = 'nvidia.com/gpu',
description = '',
Expand All @@ -35,7 +37,7 @@ export const mockAcceleratorProfile = ({
name,
namespace,
resourceVersion: '1309350',
uid: genUID('service'),
uid,
},
spec: {
identifier,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { act } from '@testing-library/react';
import { k8sListResource } from '@openshift/dynamic-plugin-sdk-utils';
import { standardUseFetchState, testHook } from '~/__tests__/unit/testUtils/hooks';
import { mockK8sResourceList } from '~/__mocks__/mockK8sResourceList';
import { mockAcceleratorProfile } from '~/__mocks__/mockAcceleratorProfile';
import useAcceleratorProfiles from '~/pages/notebookController/screens/server/useAcceleratorProfiles';

jest.mock('@openshift/dynamic-plugin-sdk-utils', () => ({
k8sListResource: jest.fn(),
}));

const k8sListResourceMock = k8sListResource as jest.Mock;

describe('useGroups', () => {
it('should return successful list of accelerators profiles', async () => {
k8sListResourceMock.mockReturnValue(
Promise.resolve(mockK8sResourceList([mockAcceleratorProfile({ uid: 'test-project-12m' })])),
);

const renderResult = testHook(useAcceleratorProfiles)('test-project');
expect(k8sListResourceMock).toHaveBeenCalledTimes(1);
expect(renderResult).hookToStrictEqual(standardUseFetchState([]));
expect(renderResult).hookToHaveUpdateCount(1);

// wait for update
await renderResult.waitForNextUpdate();
expect(k8sListResourceMock).toHaveBeenCalledTimes(1);
expect(renderResult).hookToStrictEqual(
standardUseFetchState([mockAcceleratorProfile({ uid: 'test-project-12m' })], true),
);
expect(renderResult).hookToHaveUpdateCount(2);
expect(renderResult).hookToBeStable([false, false, true, true]);

// refresh
k8sListResourceMock.mockReturnValue(
Promise.resolve(mockK8sResourceList([mockAcceleratorProfile({})])),
);
await act(() => renderResult.result.current[3]());
expect(k8sListResourceMock).toHaveBeenCalledTimes(2);
expect(renderResult).hookToHaveUpdateCount(3);
expect(renderResult).hookToBeStable([false, true, true, true]);
});
});

it('should handle when accelerator profiles are not found', async () => {
const error = {
statusObject: {
code: 404,
},
};
k8sListResourceMock.mockReturnValue(Promise.reject(error));

const renderResult = testHook(useAcceleratorProfiles)('test-project');
expect(k8sListResourceMock).toHaveBeenCalledTimes(1);
expect(renderResult).hookToStrictEqual(standardUseFetchState([]));
expect(renderResult).hookToHaveUpdateCount(1);

await renderResult.waitForNextUpdate();
expect(k8sListResourceMock).toHaveBeenCalledTimes(1);
expect(renderResult).hookToStrictEqual(
standardUseFetchState([], false, new Error('No accelerators profiles found.')),
);
expect(renderResult).hookToHaveUpdateCount(2);
expect(renderResult).hookToBeStable([true, true, false, true]);

//refresh
await act(() => renderResult.result.current[3]());
// error 404 should cache error and prevent subsequent attempts to fetch
expect(k8sListResourceMock).toHaveBeenCalledTimes(2);
expect(renderResult).hookToStrictEqual(
standardUseFetchState([], false, new Error('No accelerators profiles found.')),
);
expect(renderResult).hookToHaveUpdateCount(3);
expect(renderResult).hookToBeStable([true, true, false, true]);
});

it('should handle other errors and rethrow', async () => {
k8sListResourceMock.mockReturnValue(Promise.reject(new Error('error1')));

const renderResult = testHook(useAcceleratorProfiles)('test-project');
expect(k8sListResourceMock).toHaveBeenCalledTimes(1);
expect(renderResult).hookToStrictEqual(standardUseFetchState([]));
expect(renderResult).hookToHaveUpdateCount(1);

// wait for update
await renderResult.waitForNextUpdate();
expect(k8sListResourceMock).toHaveBeenCalledTimes(1);
expect(renderResult).hookToStrictEqual(standardUseFetchState([], false, new Error('error1')));
expect(renderResult).hookToHaveUpdateCount(2);
expect(renderResult).hookToBeStable([true, true, false, true]);

// refresh
k8sListResourceMock.mockReturnValue(Promise.reject(new Error('error2')));
await act(() => renderResult.result.current[3]());
expect(k8sListResourceMock).toHaveBeenCalledTimes(2);
expect(renderResult).hookToStrictEqual(standardUseFetchState([], false, new Error('error2')));
expect(renderResult).hookToHaveUpdateCount(3);
expect(renderResult).hookToBeStable([true, true, false, true]);
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ import { listAcceleratorProfiles } from '~/api';

const useAcceleratorProfiles = (namespace: string): FetchState<AcceleratorProfileKind[]> => {
const getAcceleratorProfiles = React.useCallback(
() => listAcceleratorProfiles(namespace),
() =>
listAcceleratorProfiles(namespace).catch((e) => {
if (e.statusObject?.code === 404) {
throw new Error('No accelerators profiles found.');
}
throw e;
}),
[namespace],
);
return useFetchState<AcceleratorProfileKind[]>(getAcceleratorProfiles, []);
Expand Down

0 comments on commit 306411e

Please sign in to comment.