Skip to content

Commit d9aa591

Browse files
test(ui): add unit tests for useModelVersionById hook (#2595)
* add unit tests Signed-off-by: Yulia Krimerman <juliapiterova@hotmail.com> * addressed comments Signed-off-by: Yulia Krimerman <juliapiterova@hotmail.com> --------- Signed-off-by: Yulia Krimerman <juliapiterova@hotmail.com>
1 parent 85618f5 commit d9aa591

1 file changed

Lines changed: 104 additions & 0 deletions

File tree

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
2+
import * as React from 'react';
3+
import { useFetchState } from 'mod-arch-core';
4+
import useModelVersionById from '~/app/hooks/useModelVersionById';
5+
import { useModelRegistryAPI } from '~/app/hooks/useModelRegistryAPI';
6+
import { ModelRegistryAPIs } from '~/app/types';
7+
import { mockModelVersion } from '~/__mocks__/mockModelVersion';
8+
import { testHook } from '~/__tests__/unit/testUtils/hooks';
9+
10+
jest.mock('mod-arch-core', () => ({
11+
useFetchState: jest.fn(),
12+
NotReadyError: class NotReadyError extends Error {
13+
constructor(message: string) {
14+
super(message);
15+
this.name = 'NotReadyError';
16+
}
17+
},
18+
}));
19+
20+
global.fetch = jest.fn();
21+
22+
jest.mock('~/app/hooks/useModelRegistryAPI', () => ({
23+
useModelRegistryAPI: jest.fn(),
24+
}));
25+
26+
const mockUseModelRegistryAPI = jest.mocked(useModelRegistryAPI);
27+
const mockUseFetchState = jest.mocked(useFetchState);
28+
29+
const mockModelRegistryAPIs: ModelRegistryAPIs = {
30+
createRegisteredModel: jest.fn(),
31+
createModelVersionForRegisteredModel: jest.fn(),
32+
createModelArtifactForModelVersion: jest.fn(),
33+
getRegisteredModel: jest.fn(),
34+
getModelVersion: jest.fn(),
35+
listModelVersions: jest.fn(),
36+
listRegisteredModels: jest.fn(),
37+
getModelVersionsByRegisteredModel: jest.fn(),
38+
getModelArtifactsByModelVersion: jest.fn(),
39+
patchRegisteredModel: jest.fn(),
40+
patchModelVersion: jest.fn(),
41+
patchModelArtifact: jest.fn(),
42+
listModelTransferJobs: jest.fn(),
43+
getModelTransferJobByName: jest.fn(),
44+
createModelTransferJob: jest.fn(),
45+
updateModelTransferJob: jest.fn(),
46+
deleteModelTransferJob: jest.fn(),
47+
getModelTransferJobEvents: jest.fn(),
48+
};
49+
50+
const captureCallback = (): ((opts: unknown) => Promise<unknown>) => {
51+
mockUseFetchState.mockReturnValue([null, false, undefined, jest.fn()]);
52+
return mockUseFetchState.mock.calls[0][0] as (opts: unknown) => Promise<unknown>;
53+
};
54+
55+
describe('useModelVersionById', () => {
56+
beforeEach(() => {
57+
jest.clearAllMocks();
58+
});
59+
60+
it('should reject with an error when the API is not available', async () => {
61+
mockUseModelRegistryAPI.mockReturnValue({
62+
api: mockModelRegistryAPIs,
63+
apiAvailable: false,
64+
refreshAllAPI: jest.fn(),
65+
});
66+
67+
testHook(useModelVersionById)('version-1');
68+
const callback = captureCallback();
69+
70+
await expect(callback({})).rejects.toThrow('API not yet available');
71+
});
72+
73+
it('should reject with NotReadyError when modelVersionId is not provided', async () => {
74+
mockUseModelRegistryAPI.mockReturnValue({
75+
api: mockModelRegistryAPIs,
76+
apiAvailable: true,
77+
refreshAllAPI: jest.fn(),
78+
});
79+
80+
testHook(useModelVersionById)();
81+
const callback = captureCallback();
82+
83+
await expect(callback({})).rejects.toThrow('No model version id');
84+
await expect(callback({})).rejects.toMatchObject({ name: 'NotReadyError' });
85+
});
86+
87+
it('should call api.getModelVersion with the correct modelVersionId', async () => {
88+
const getModelVersionMock = jest.fn().mockResolvedValue(mockModelVersion({ id: 'v-42' }));
89+
90+
mockUseModelRegistryAPI.mockReturnValue({
91+
api: { ...mockModelRegistryAPIs, getModelVersion: getModelVersionMock },
92+
apiAvailable: true,
93+
refreshAllAPI: jest.fn(),
94+
});
95+
96+
testHook(useModelVersionById)('v-42');
97+
const callback = captureCallback();
98+
99+
const result = await callback({});
100+
101+
expect(getModelVersionMock).toHaveBeenCalledWith({}, 'v-42');
102+
expect(result).toEqual(mockModelVersion({ id: 'v-42' }));
103+
});
104+
});

0 commit comments

Comments
 (0)