forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Console] Fixes for console error handling and loading of autocomplete (
elastic#58587) (elastic#59178) * Fix console error handling when offline In cases when the client cannot connect to server the UI would get stuck in a loading state. We need to handle that case explicitly to stop the progress spinner and report the error correctly. * Fix editor request cycle. Request should always complete The bug was that the request could error in such a way that the requestFail dispatch was not being called. Leaving the loading spinner running and an unhelpful error message would appear. Also partly fixed the loading of autocomplete data and cleaned up a legacy import. * Fixed loading of mappings in as they were updated from settings modal. * Fix the mappings update logic TODO, this function needs to be revisited, but for now it is convenient to have the Settings service passed in every time so that the poller can be updated. * Fix poll interval * Address PR feedback Rename variable (instance -> editorRegistry) and remove unused file Co-authored-by: Elastic Machine <[email protected]> Co-authored-by: Elastic Machine <[email protected]>
- Loading branch information
1 parent
a181529
commit 8fd6f43
Showing
14 changed files
with
292 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/plugins/console/public/application/contexts/services_context.mock.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
import { notificationServiceMock } from '../../../../../core/public/mocks'; | ||
import { HistoryMock } from '../../services/history.mock'; | ||
import { SettingsMock } from '../../services/settings.mock'; | ||
import { StorageMock } from '../../services/storage.mock'; | ||
|
||
import { ContextValue } from './services_context'; | ||
|
||
export const serviceContextMock = { | ||
create: (): ContextValue => { | ||
const storage = new StorageMock({} as any, 'test'); | ||
(storage.keys as jest.Mock).mockImplementation(() => []); | ||
return { | ||
elasticsearchUrl: 'test', | ||
services: { | ||
trackUiMetric: { count: () => {}, load: () => {} }, | ||
storage, | ||
settings: new SettingsMock(storage), | ||
history: new HistoryMock(storage), | ||
notifications: notificationServiceMock.createSetupContract(), | ||
objectStorageClient: {} as any, | ||
}, | ||
docLinkVersion: 'NA', | ||
}; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
.../application/hooks/use_send_current_request_to_es/use_send_current_request_to_es.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
jest.mock('./send_request_to_es', () => ({ sendRequestToES: jest.fn() })); | ||
jest.mock('../../contexts/editor_context/editor_registry', () => ({ | ||
instance: { getInputEditor: jest.fn() }, | ||
})); | ||
jest.mock('./track', () => ({ track: jest.fn() })); | ||
jest.mock('../../contexts/request_context', () => ({ useRequestActionContext: jest.fn() })); | ||
|
||
import React from 'react'; | ||
import { renderHook, act } from '@testing-library/react-hooks'; | ||
|
||
import { ContextValue, ServicesContextProvider } from '../../contexts'; | ||
import { serviceContextMock } from '../../contexts/services_context.mock'; | ||
import { useRequestActionContext } from '../../contexts/request_context'; | ||
import { instance as editorRegistry } from '../../contexts/editor_context/editor_registry'; | ||
|
||
import { sendRequestToES } from './send_request_to_es'; | ||
import { useSendCurrentRequestToES } from './use_send_current_request_to_es'; | ||
|
||
describe('useSendCurrentRequestToES', () => { | ||
let mockContextValue: ContextValue; | ||
let dispatch: (...args: any[]) => void; | ||
const contexts = ({ children }: { children?: any }) => ( | ||
<ServicesContextProvider value={mockContextValue}>{children}</ServicesContextProvider> | ||
); | ||
|
||
beforeEach(() => { | ||
mockContextValue = serviceContextMock.create(); | ||
dispatch = jest.fn(); | ||
(useRequestActionContext as jest.Mock).mockReturnValue(dispatch); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
it('calls send request to ES', async () => { | ||
// Set up mocks | ||
(mockContextValue.services.settings.toJSON as jest.Mock).mockReturnValue({}); | ||
// This request should succeed | ||
(sendRequestToES as jest.Mock).mockResolvedValue([]); | ||
(editorRegistry.getInputEditor as jest.Mock).mockImplementation(() => ({ | ||
getRequestsInRange: () => ['test'], | ||
})); | ||
|
||
const { result } = renderHook(() => useSendCurrentRequestToES(), { wrapper: contexts }); | ||
await act(() => result.current()); | ||
expect(sendRequestToES).toHaveBeenCalledWith({ requests: ['test'] }); | ||
|
||
// Second call should be the request success | ||
const [, [requestSucceededCall]] = (dispatch as jest.Mock).mock.calls; | ||
expect(requestSucceededCall).toEqual({ type: 'requestSuccess', payload: { data: [] } }); | ||
}); | ||
|
||
it('handles known errors', async () => { | ||
// Set up mocks | ||
(sendRequestToES as jest.Mock).mockRejectedValue({ response: 'nada' }); | ||
(editorRegistry.getInputEditor as jest.Mock).mockImplementation(() => ({ | ||
getRequestsInRange: () => ['test'], | ||
})); | ||
|
||
const { result } = renderHook(() => useSendCurrentRequestToES(), { wrapper: contexts }); | ||
await act(() => result.current()); | ||
// Second call should be the request failure | ||
const [, [requestFailedCall]] = (dispatch as jest.Mock).mock.calls; | ||
|
||
// The request must have concluded | ||
expect(requestFailedCall).toEqual({ type: 'requestFail', payload: { response: 'nada' } }); | ||
}); | ||
|
||
it('handles unknown errors', async () => { | ||
// Set up mocks | ||
(sendRequestToES as jest.Mock).mockRejectedValue(NaN /* unexpected error value */); | ||
(editorRegistry.getInputEditor as jest.Mock).mockImplementation(() => ({ | ||
getRequestsInRange: () => ['test'], | ||
})); | ||
|
||
const { result } = renderHook(() => useSendCurrentRequestToES(), { wrapper: contexts }); | ||
await act(() => result.current()); | ||
// Second call should be the request failure | ||
const [, [requestFailedCall]] = (dispatch as jest.Mock).mock.calls; | ||
|
||
// The request must have concluded | ||
expect(requestFailedCall).toEqual({ type: 'requestFail', payload: undefined }); | ||
// It also notified the user | ||
expect(mockContextValue.services.notifications.toasts.addError).toHaveBeenCalledWith(NaN, { | ||
title: 'Unknown Request Error', | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.