Skip to content

Commit f617af5

Browse files
add unit test for «ModelRegister» component
Introduce a unit test to verify the behavior of the «ModelRegister» component during installation failure. Mock dependencies like «ModelForm» and «useAssistantInstallation» to simulate scenarios, ensuring proper handling of error states and absence of success messages.
1 parent b3e833d commit f617af5

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
* Copyright Wazuh Contributors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
import React from 'react';
7+
import userEvent from '@testing-library/user-event';
8+
import { render, screen, waitFor } from '../../../../test/test_utils';
9+
10+
jest.mock('../model-form', () => {
11+
const React = require('react');
12+
13+
const MockModelForm = ({ onChange, onValidationChange }: any) => {
14+
React.useEffect(() => {
15+
onChange?.({
16+
modelProvider: 'OpenAI',
17+
model: 'gpt-4o',
18+
apiUrl: 'https://api.openai.com/v1/chat/completions',
19+
apiKey: 'mock-key',
20+
});
21+
onValidationChange?.(true);
22+
}, [onChange, onValidationChange]);
23+
24+
return <div data-testid="mock-model-form" />;
25+
};
26+
27+
return {
28+
__esModule: true,
29+
ModelForm: MockModelForm,
30+
};
31+
});
32+
33+
jest.mock('../../modules/installation-manager/hooks/use-assistant-installation', () => {
34+
const React = require('react');
35+
const { ExecutionState } = require('../../modules/installation-manager/domain');
36+
37+
const failedStep = {
38+
stepName: 'Create Model',
39+
state: ExecutionState.FAILED,
40+
error: new Error('Model step failed'),
41+
};
42+
43+
const progress = {
44+
getSteps: () => [
45+
{
46+
stepName: 'Persist ML Commons settings',
47+
state: ExecutionState.FINISHED_SUCCESSFULLY,
48+
},
49+
failedStep,
50+
],
51+
getFailedSteps: () => [failedStep],
52+
isFinished: () => false,
53+
isFinishedSuccessfully: () => false,
54+
isFinishedWithWarnings: () => false,
55+
hasFailed: () => true,
56+
};
57+
58+
return {
59+
__esModule: true,
60+
useAssistantInstallation: () => {
61+
const [error, setError] = React.useState<string | undefined>(undefined);
62+
63+
const install = React.useCallback(async () => {
64+
setError('Steps: "Create Model" has failed');
65+
}, []);
66+
67+
return {
68+
install,
69+
setModel: jest.fn(),
70+
reset: jest.fn(),
71+
isLoading: false,
72+
error,
73+
result: { success: false },
74+
modelData: undefined,
75+
progress,
76+
isSuccess: false,
77+
};
78+
},
79+
};
80+
});
81+
82+
import { ModelRegister } from '../model-register';
83+
84+
describe('ModelRegister', () => {
85+
it('does not show success toast when installation fails', async () => {
86+
const user = userEvent.setup();
87+
88+
render(<ModelRegister />);
89+
90+
const deployButton = await screen.findByRole('button', { name: /Deploy/i });
91+
expect(deployButton).toBeEnabled();
92+
93+
await user.click(deployButton);
94+
95+
await screen.findByText(/Error deploying model/i);
96+
97+
await waitFor(() => {
98+
expect(screen.queryByText('Model deployed successfully.')).not.toBeInTheDocument();
99+
});
100+
});
101+
});

0 commit comments

Comments
 (0)