-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added test for useAcceleratorProfiles
- Loading branch information
Showing
3 changed files
with
109 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
...tend/src/pages/notebookController/screens/server/__tests__/useAcceleratorProfiles.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters