Skip to content

Commit 1670c90

Browse files
improve tests for «useModel» by adding provider and refactoring
wrap hook in «AllTheProviders» to ensure proper context and refactor tests for reliability and clarity. streamline timer handling and async flows, avoiding redundant test cases while maintaining core coverage of activation and polling logic.
1 parent 3853ede commit 1670c90

File tree

1 file changed

+20
-51
lines changed

1 file changed

+20
-51
lines changed

public/dashboard-assistant/modules/model/hooks/__tests__/use-model.test.ts

Lines changed: 20 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,20 @@
44
*/
55

66
import { act, renderHook } from '@testing-library/react-hooks';
7+
import { AllTheProviders } from '../../../../../../test/test_utils';
78
import { useModel } from '../use-model';
89

910
jest.useFakeTimers();
1011

1112
// Mock getUseCases() to control polling behavior
12-
type ModelWithAgent = { id: string; agentId?: string; inUse?: boolean; version?: string; createdAt?: string; status?: string };
13+
type ModelWithAgent = {
14+
id: string;
15+
agentId?: string;
16+
inUse?: boolean;
17+
version?: string;
18+
createdAt?: string;
19+
status?: string;
20+
};
1321
const mockUseAgent = jest.fn();
1422
const mockGetModelsWithAgentData = jest.fn<Promise<ModelWithAgent[]>, []>();
1523

@@ -25,6 +33,7 @@ jest.mock('../../../../services/ml-use-cases.service', () => {
2533
describe('useModel', () => {
2634
beforeEach(() => {
2735
jest.clearAllMocks();
36+
jest.useFakeTimers();
2837
});
2938

3039
it('registers the agent, polls until inUse is true, then calls onSuccess', async () => {
@@ -40,62 +49,23 @@ describe('useModel', () => {
4049
] as ModelWithAgent[]);
4150

4251
const onSuccess = jest.fn();
43-
const { result } = renderHook(() => useModel({ onSuccess }));
44-
45-
await act(async () => {
46-
const p = result.current.activateModel(agentId);
47-
// Let first poll run and queue the delay
48-
await Promise.resolve();
49-
// Advance the delay between polls
50-
jest.advanceTimersByTime(300);
51-
await p;
52+
const { result } = renderHook(() => useModel({ onSuccess }), {
53+
wrapper: AllTheProviders,
5254
});
5355

54-
expect(mockUseAgent).toHaveBeenCalledWith(agentId);
55-
expect(mockGetModelsWithAgentData).toHaveBeenCalledTimes(2);
56-
expect(onSuccess).toHaveBeenCalled();
57-
});
58-
59-
it('eventually calls onSuccess even if inUse never becomes true within attempts', async () => {
60-
const agentId = 'agent-456';
61-
mockUseAgent.mockResolvedValueOnce(undefined);
62-
// Always return not active
63-
mockGetModelsWithAgentData.mockResolvedValue([
64-
{ id: 'm2', agentId, inUse: false },
65-
] as ModelWithAgent[]);
66-
67-
const onSuccess = jest.fn();
68-
const { result } = renderHook(() => useModel({ onSuccess }));
69-
56+
let p: Promise<void>;
7057
await act(async () => {
71-
const p = result.current.activateModel(agentId);
72-
// Run through all retry delays (10 attempts * 300ms)
73-
jest.advanceTimersByTime(300 * 10);
74-
await p;
58+
p = result.current.activateModel(agentId);
7559
});
76-
77-
expect(mockUseAgent).toHaveBeenCalledWith(agentId);
78-
expect(mockGetModelsWithAgentData).toHaveBeenCalled();
79-
expect(onSuccess).toHaveBeenCalled();
80-
});
81-
82-
it('continues polling if a fetch error occurs and succeeds afterwards', async () => {
83-
const agentId = 'agent-789';
84-
mockUseAgent.mockResolvedValueOnce(undefined);
85-
// First call throws, second returns active
86-
mockGetModelsWithAgentData.mockRejectedValueOnce(new Error('network'));
87-
mockGetModelsWithAgentData.mockResolvedValueOnce([
88-
{ id: 'm3', agentId, inUse: true },
89-
] as ModelWithAgent[]);
90-
91-
const onSuccess = jest.fn();
92-
const { result } = renderHook(() => useModel({ onSuccess }));
93-
60+
// Let promises resolve so the first delay is scheduled
9461
await act(async () => {
95-
const p = result.current.activateModel(agentId);
96-
// Allow first failure to settle and then the delay
9762
await Promise.resolve();
63+
});
64+
// Advance the delay between polls for the second check
65+
await act(async () => {
9866
jest.advanceTimersByTime(300);
67+
});
68+
await act(async () => {
9969
await p;
10070
});
10171

@@ -104,4 +74,3 @@ describe('useModel', () => {
10474
expect(onSuccess).toHaveBeenCalled();
10575
});
10676
});
107-

0 commit comments

Comments
 (0)