* [Infra UI] Fixes #37999 - Reset error when IP address changes to something valid * movnig the setError above try/catch
This commit is contained in:
parent
ec55e5199e
commit
7d944a78ba
|
@ -27,6 +27,7 @@ describe('useHostIpToName Hook', () => {
|
|||
expect(result.current.loading).toBe(false);
|
||||
expect(result.current.error).toBe(null);
|
||||
});
|
||||
|
||||
it('should handle errors', async () => {
|
||||
const error = new Error('Host not found');
|
||||
mockedFetch.post.mockRejectedValue(error);
|
||||
|
@ -38,4 +39,22 @@ describe('useHostIpToName Hook', () => {
|
|||
expect(result.current.loading).toBe(false);
|
||||
expect(result.current.error).toBe(error);
|
||||
});
|
||||
|
||||
it('should reset errors', async () => {
|
||||
const error = new Error('Host not found');
|
||||
mockedFetch.post.mockRejectedValue(error);
|
||||
const { result, waitForNextUpdate, rerender } = renderUseHostIpToNameHook();
|
||||
expect(result.current.name).toBe(null);
|
||||
expect(result.current.loading).toBe(true);
|
||||
await waitForNextUpdate();
|
||||
expect(result.current.name).toBe(null);
|
||||
expect(result.current.loading).toBe(false);
|
||||
expect(result.current.error).toBe(error);
|
||||
mockedFetch.post.mockResolvedValue({ data: { host: 'example-01' } } as any);
|
||||
rerender({ ipAddress: '192.168.1.2', indexPattern: 'metricbeat-*' });
|
||||
await waitForNextUpdate();
|
||||
expect(result.current.name).toBe('example-01');
|
||||
expect(result.current.loading).toBe(false);
|
||||
expect(result.current.error).toBe(null);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -17,6 +17,7 @@ export const useHostIpToName = (ipAddress: string | null, indexPattern: string |
|
|||
() => {
|
||||
(async () => {
|
||||
setLoadingState(true);
|
||||
setError(null);
|
||||
try {
|
||||
if (ipAddress && indexPattern) {
|
||||
const response = await fetch.post<IpToHostResponse>('../api/infra/ip_to_host', {
|
||||
|
|
Loading…
Reference in a new issue