diff --git a/x-pack/plugins/infra/public/pages/link_to/use_host_ip_to_name.test.ts b/x-pack/plugins/infra/public/pages/link_to/use_host_ip_to_name.test.ts index dd0cffe8f8f8..32f3864bbfe4 100644 --- a/x-pack/plugins/infra/public/pages/link_to/use_host_ip_to_name.test.ts +++ b/x-pack/plugins/infra/public/pages/link_to/use_host_ip_to_name.test.ts @@ -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); + }); }); diff --git a/x-pack/plugins/infra/public/pages/link_to/use_host_ip_to_name.ts b/x-pack/plugins/infra/public/pages/link_to/use_host_ip_to_name.ts index 34f47edfd97f..b1d5bfddfb1b 100644 --- a/x-pack/plugins/infra/public/pages/link_to/use_host_ip_to_name.ts +++ b/x-pack/plugins/infra/public/pages/link_to/use_host_ip_to_name.ts @@ -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('../api/infra/ip_to_host', {