[Infra UI] Fixes #37999 - Reset error when IP address changes to valid entry (#38022)

* [Infra UI] Fixes #37999 - Reset error when IP address changes to something valid

* movnig the setError above try/catch
This commit is contained in:
Chris Cowan 2019-06-06 12:49:26 -07:00 committed by GitHub
parent ec55e5199e
commit 7d944a78ba
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 0 deletions

View file

@ -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);
});
});

View file

@ -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', {