Skip to content

Commit 77bfeb2

Browse files
Adding loading when form is submitted and added unit tests
1 parent 75d8d13 commit 77bfeb2

3 files changed

Lines changed: 49 additions & 19 deletions

File tree

x-pack/plugins/search_inference_endpoints/public/components/add_inference_endpoints/inference_form.tsx

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77

88
import { Form, useForm } from '@kbn/es-ui-shared-plugin/static/forms/hook_form_lib';
9-
import React, { useCallback } from 'react';
9+
import React, { useCallback, useState } from 'react';
1010
import { InferenceServices } from '@kbn/inference-endpoint-ui-common';
1111
import { EuiButton, EuiFlexGroup, EuiFlexItem, EuiSpacer } from '@elastic/eui';
1212
import { useProviders } from '../../hooks/use_providers';
@@ -18,17 +18,30 @@ interface InferenceFormProps {
1818
onSubmitSuccess: (state: boolean) => void;
1919
}
2020
export const InferenceForm: React.FC<InferenceFormProps> = ({ onSubmitSuccess }) => {
21-
const { mutate: addEndpoint } = useAddEndpoint(() => onSubmitSuccess(false));
21+
const [isLoading, setIsLoading] = useState<boolean>(false);
22+
const onSuccess = useCallback(() => {
23+
setIsLoading(false);
24+
onSubmitSuccess(false);
25+
}, [onSubmitSuccess]);
26+
const onError = useCallback(() => {
27+
setIsLoading(false);
28+
}, []);
29+
const { mutate: addEndpoint } = useAddEndpoint(
30+
() => onSuccess(),
31+
() => onError()
32+
);
2233
const { data: providers } = useProviders();
2334
const { form } = useForm();
2435
const handleSubmit = useCallback(async () => {
36+
setIsLoading(true);
2537
const { isValid, data } = await form.submit();
2638

2739
if (isValid) {
2840
addEndpoint({
2941
inferenceEndpoint: data as InferenceEndpoint,
3042
});
31-
return;
43+
} else {
44+
setIsLoading(false);
3245
}
3346
}, [addEndpoint, form]);
3447

@@ -42,8 +55,8 @@ export const InferenceForm: React.FC<InferenceFormProps> = ({ onSubmitSuccess })
4255
fill
4356
color="success"
4457
size="m"
45-
isLoading={form.isSubmitting}
46-
disabled={!form.isValid && form.isSubmitted}
58+
isLoading={form.isSubmitting || isLoading}
59+
disabled={(!form.isValid && form.isSubmitted) || isLoading}
4760
data-test-subj="add-inference-endpoint-submit-button"
4861
onClick={handleSubmit}
4962
>

x-pack/plugins/search_inference_endpoints/public/hooks/use_add_endpoint.test.tsx

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,24 +41,32 @@ const mockUseKibana = useKibana as jest.Mock;
4141
const mockAdd = jest.fn();
4242
const mockAddSuccess = jest.fn();
4343
const mockAddError = jest.fn();
44+
const mockOnSuccess = jest.fn();
45+
const mockOnError = jest.fn();
4446

4547
describe('useAddEndpoint', () => {
46-
mockUseKibana.mockReturnValue({
47-
services: {
48-
http: {
49-
put: mockAdd,
50-
},
51-
notifications: {
52-
toasts: {
53-
addSuccess: mockAddSuccess,
54-
addError: mockAddError,
48+
beforeEach(() => {
49+
mockUseKibana.mockReturnValue({
50+
services: {
51+
http: {
52+
put: mockAdd,
53+
},
54+
notifications: {
55+
toasts: {
56+
addSuccess: mockAddSuccess,
57+
addError: mockAddError,
58+
},
5559
},
5660
},
57-
},
61+
});
62+
});
63+
64+
afterEach(() => {
65+
jest.clearAllMocks();
5866
});
5967

6068
it('show call add inference endpoint and show success toast', async () => {
61-
const { result } = renderHook(() => useAddEndpoint(), { wrapper });
69+
const { result } = renderHook(() => useAddEndpoint(mockOnSuccess, mockOnError), { wrapper });
6270

6371
result.current.mutate({ inferenceEndpoint: mockInferenceEndpoint });
6472

@@ -73,15 +81,21 @@ describe('useAddEndpoint', () => {
7381
expect(mockAddSuccess).toHaveBeenCalledWith({
7482
title: i18n.ENDPOINT_ADDED_SUCCESS,
7583
});
84+
expect(mockOnSuccess).toHaveBeenCalled();
85+
expect(mockOnError).not.toHaveBeenCalled();
7686
});
7787

7888
it('should show error toast on failure', async () => {
7989
const error = { body: { message: 'error' } };
8090
mockAdd.mockRejectedValue(error);
81-
const { result } = renderHook(() => useAddEndpoint(), { wrapper });
91+
const { result } = renderHook(() => useAddEndpoint(mockOnSuccess, mockOnError), { wrapper });
8292

8393
result.current.mutate({ inferenceEndpoint: mockInferenceEndpoint });
8494

85-
await waitFor(() => expect(mockAddError).toHaveBeenCalled());
95+
await waitFor(() => {
96+
expect(mockAddError).toHaveBeenCalled();
97+
expect(mockOnSuccess).not.toHaveBeenCalled();
98+
expect(mockOnError).toHaveBeenCalled();
99+
});
86100
});
87101
});

x-pack/plugins/search_inference_endpoints/public/hooks/use_add_endpoint.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ interface MutationArgs {
1717
inferenceEndpoint: InferenceEndpoint;
1818
}
1919

20-
export const useAddEndpoint = (onSuccess?: () => void) => {
20+
export const useAddEndpoint = (onSuccess?: () => void, onError?: () => void) => {
2121
const queryClient = useQueryClient();
2222
const { services } = useKibana();
2323
const toasts = services.notifications?.toasts;
@@ -46,6 +46,9 @@ export const useAddEndpoint = (onSuccess?: () => void) => {
4646
title: i18n.ENDPOINT_CREATION_FAILED,
4747
toastMessage: error.body.message,
4848
});
49+
if (onError) {
50+
onError();
51+
}
4952
},
5053
}
5154
);

0 commit comments

Comments
 (0)