diff --git a/src/legacy/core_plugins/console/np_ready/public/application/containers/editor/legacy/console_editor/editor.test.mock.tsx b/src/legacy/core_plugins/console/np_ready/public/application/containers/editor/legacy/console_editor/editor.test.mock.tsx new file mode 100644 index 00000000000000..a9db5cfcac3ebe --- /dev/null +++ b/src/legacy/core_plugins/console/np_ready/public/application/containers/editor/legacy/console_editor/editor.test.mock.tsx @@ -0,0 +1,51 @@ +/* + * 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('../../../../contexts/editor_context/editor_registry.ts', () => ({ + instance: { + setInputEditor: () => {}, + getInputEditor: () => ({ + getRequestsInRange: (cb: any) => cb([{ test: 'test' }]), + }), + }, +})); +jest.mock('../../../../components/editor_example.tsx', () => {}); +jest.mock('../../../../../../../public/quarantined/src/mappings.js', () => ({ + retrieveAutoCompleteInfo: () => {}, + clearSubscriptions: () => {}, +})); +jest.mock('../../../../../../../public/quarantined/src/input.ts', () => { + return { + initializeEditor: () => ({ + $el: { + css: () => {}, + }, + focus: () => {}, + update: () => {}, + getSession: () => ({ on: () => {}, setUseWrapMode: () => {} }), + commands: { + addCommand: () => {}, + }, + }), + }; +}); + +jest.mock('../../../../hooks/use_send_current_request_to_es/send_request_to_es', () => ({ + sendRequestToES: jest.fn(), +})); diff --git a/src/legacy/core_plugins/console/np_ready/public/application/containers/editor/legacy/console_editor/editor.test.tsx b/src/legacy/core_plugins/console/np_ready/public/application/containers/editor/legacy/console_editor/editor.test.tsx index cb5559edfb2490..6162397ce0650e 100644 --- a/src/legacy/core_plugins/console/np_ready/public/application/containers/editor/legacy/console_editor/editor.test.tsx +++ b/src/legacy/core_plugins/console/np_ready/public/application/containers/editor/legacy/console_editor/editor.test.tsx @@ -17,68 +17,33 @@ * under the License. */ +import './editor.test.mock'; + import React from 'react'; -import { ReactWrapper, mount } from 'enzyme'; +import { mount } from 'enzyme'; import { I18nProvider } from '@kbn/i18n/react'; import { act } from 'react-dom/test-utils'; import * as sinon from 'sinon'; +import { notificationServiceMock } from '../../../../../../../../../../../src/core/public/mocks'; + +import { nextTick } from 'test_utils/enzyme_helpers'; import { ServicesContextProvider, EditorContextProvider, RequestContextProvider, } from '../../../../contexts'; -import { Editor } from './editor'; - -jest.mock('../../../../contexts/editor_context/editor_registry.ts', () => ({ - instance: { - setInputEditor: () => {}, - getInputEditor: () => ({ - getRequestsInRange: (cb: any) => cb([{ test: 'test' }]), - }), - }, -})); -jest.mock('../../../../components/editor_example.tsx', () => {}); -jest.mock('../../../../../../../public/quarantined/src/mappings.js', () => ({ - retrieveAutoCompleteInfo: () => {}, - clearSubscriptions: () => {}, -})); -jest.mock('../../../../../../../public/quarantined/src/input.ts', () => { - return { - initializeEditor: () => ({ - $el: { - css: () => {}, - }, - focus: () => {}, - update: () => {}, - getSession: () => ({ on: () => {}, setUseWrapMode: () => {} }), - commands: { - addCommand: () => {}, - }, - }), - }; -}); - -import * as sendRequestModule from '../../../../hooks/use_send_current_request_to_es/send_request_to_es'; +import { sendRequestToES } from '../../../../hooks/use_send_current_request_to_es/send_request_to_es'; import * as consoleMenuActions from '../console_menu_actions'; +import { Editor } from './editor'; describe('Legacy (Ace) Console Editor Component Smoke Test', () => { let mockedAppContextValue: any; - let editor: ReactWrapper; + const sandbox = sinon.createSandbox(); - beforeEach(() => { - document.queryCommandSupported = sinon.fake(() => true); - mockedAppContextValue = { - services: { - history: { - getSavedEditorState: () => null, - updateCurrentState: () => {}, - }, - }, - docLinkVersion: 'NA', - }; - editor = mount( + const doMount = () => + mount( @@ -89,51 +54,56 @@ describe('Legacy (Ace) Console Editor Component Smoke Test', () => { ); + + beforeEach(() => { + document.queryCommandSupported = sinon.fake(() => true); + mockedAppContextValue = { + services: { + history: { + getSavedEditorState: () => null, + updateCurrentState: jest.fn(), + }, + notifications: notificationServiceMock.createSetupContract(), + }, + docLinkVersion: 'NA', + }; + }); + + afterEach(() => { + sandbox.restore(); }); - // TODO: Re-enable when React ^16.9 is available - it.skip('calls send current request to ES', () => { - const stub = sinon.stub(sendRequestModule, 'sendRequestToES'); - try { - act(() => { - editor.find('[data-test-subj~="sendRequestButton"]').simulate('click'); - }); - expect(stub.called).toBe(true); - expect(stub.callCount).toBe(1); - } finally { - stub.restore(); - } + it('calls send current request to ES', async () => { + (sendRequestToES as jest.Mock).mockRejectedValue({}); + const editor = doMount(); + act(() => { + editor.find('[data-test-subj~="sendRequestButton"]').simulate('click'); + }); + await nextTick(); + expect(sendRequestToES).toBeCalledTimes(1); }); it('opens docs', () => { - const stub = sinon.stub(consoleMenuActions, 'getDocumentation'); - try { - const consoleMenuToggle = editor.find('[data-test-subj~="toggleConsoleMenu"]').last(); - consoleMenuToggle.simulate('click'); - - const docsButton = editor.find('[data-test-subj~="consoleMenuOpenDocs"]').last(); - docsButton.simulate('click'); - - expect(stub.called).toBe(true); - expect(stub.callCount).toBe(1); - } finally { - stub.restore(); - } + const stub = sandbox.stub(consoleMenuActions, 'getDocumentation'); + const editor = doMount(); + const consoleMenuToggle = editor.find('[data-test-subj~="toggleConsoleMenu"]').last(); + consoleMenuToggle.simulate('click'); + + const docsButton = editor.find('[data-test-subj~="consoleMenuOpenDocs"]').last(); + docsButton.simulate('click'); + + expect(stub.callCount).toBe(1); }); it('prompts auto-indent', () => { - const stub = sinon.stub(consoleMenuActions, 'autoIndent'); - try { - const consoleMenuToggle = editor.find('[data-test-subj~="toggleConsoleMenu"]').last(); - consoleMenuToggle.simulate('click'); - - const autoIndentButton = editor.find('[data-test-subj~="consoleMenuAutoIndent"]').last(); - autoIndentButton.simulate('click'); - - expect(stub.called).toBe(true); - expect(stub.callCount).toBe(1); - } finally { - stub.restore(); - } + const stub = sandbox.stub(consoleMenuActions, 'autoIndent'); + const editor = doMount(); + const consoleMenuToggle = editor.find('[data-test-subj~="toggleConsoleMenu"]').last(); + consoleMenuToggle.simulate('click'); + + const autoIndentButton = editor.find('[data-test-subj~="consoleMenuAutoIndent"]').last(); + autoIndentButton.simulate('click'); + + expect(stub.callCount).toBe(1); }); }); diff --git a/src/legacy/core_plugins/console/np_ready/public/application/hooks/use_send_current_request_to_es/use_send_current_request_to_es.ts b/src/legacy/core_plugins/console/np_ready/public/application/hooks/use_send_current_request_to_es/use_send_current_request_to_es.ts index 63d1120808e028..52661103b2d13e 100644 --- a/src/legacy/core_plugins/console/np_ready/public/application/hooks/use_send_current_request_to_es/use_send_current_request_to_es.ts +++ b/src/legacy/core_plugins/console/np_ready/public/application/hooks/use_send_current_request_to_es/use_send_current_request_to_es.ts @@ -44,6 +44,7 @@ export const useSendCurrentRequestToES = () => { }); return; } + const results = await sendRequestToES({ requests, });