From a80fc36c4e9030dcd8da81556d0b592ce91c7663 Mon Sep 17 00:00:00 2001 From: Matthew Kime Date: Wed, 13 Apr 2022 00:29:57 -0500 Subject: [PATCH] [7.17] Removing axios from data_view_field_editor test (#129116) (#129952) * Removing axios from data_view_field_editor test (#129116) * test bed fixes Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> (cherry picked from commit ae39e1bae70e26a490f2344bc78dfdc85843c593) # Conflicts: # src/plugins/data_view_field_editor/__jest__/client_integration/field_editor.test.tsx # src/plugins/data_view_field_editor/__jest__/client_integration/field_editor_flyout_content.test.ts # src/plugins/data_view_field_editor/__jest__/client_integration/field_editor_flyout_preview.test.ts # src/plugins/data_view_field_editor/__jest__/client_integration/helpers/setup_environment.tsx * better match main pr * fix integration test --- .../client_integration/field_editor.test.tsx | 3 +- .../field_editor_flyout_content.test.ts | 3 +- .../field_editor_flyout_preview.test.ts | 13 +++--- .../helpers/http_requests.ts | 43 ++++++++----------- .../helpers/setup_environment.tsx | 10 ++--- 5 files changed, 30 insertions(+), 42 deletions(-) diff --git a/src/plugins/index_pattern_field_editor/__jest__/client_integration/field_editor.test.tsx b/src/plugins/index_pattern_field_editor/__jest__/client_integration/field_editor.test.tsx index 4a4c42f69fc8e..ba8547dcf85f2 100644 --- a/src/plugins/index_pattern_field_editor/__jest__/client_integration/field_editor.test.tsx +++ b/src/plugins/index_pattern_field_editor/__jest__/client_integration/field_editor.test.tsx @@ -21,7 +21,7 @@ import type { RuntimeFieldPainlessError } from '../../public/lib'; import { setup, FieldEditorTestBed, defaultProps } from './field_editor.helpers'; describe('', () => { - const { server, httpRequestsMockHelpers } = setupEnvironment(); + const { httpRequestsMockHelpers } = setupEnvironment(); let testBed: FieldEditorTestBed; let onChange: jest.Mock = jest.fn(); @@ -70,7 +70,6 @@ describe('', () => { afterAll(() => { jest.useRealTimers(); - server.restore(); }); beforeEach(async () => { diff --git a/src/plugins/index_pattern_field_editor/__jest__/client_integration/field_editor_flyout_content.test.ts b/src/plugins/index_pattern_field_editor/__jest__/client_integration/field_editor_flyout_content.test.ts index 9b00ff762fe8f..c5a2594fb4050 100644 --- a/src/plugins/index_pattern_field_editor/__jest__/client_integration/field_editor_flyout_content.test.ts +++ b/src/plugins/index_pattern_field_editor/__jest__/client_integration/field_editor_flyout_content.test.ts @@ -12,7 +12,7 @@ import { setupEnvironment } from './helpers'; import { setup } from './field_editor_flyout_content.helpers'; describe('', () => { - const { server, httpRequestsMockHelpers } = setupEnvironment(); + const { httpRequestsMockHelpers } = setupEnvironment(); beforeAll(() => { httpRequestsMockHelpers.setFieldPreviewResponse({ values: ['foo'] }); @@ -21,7 +21,6 @@ describe('', () => { afterAll(() => { jest.useRealTimers(); - server.restore(); }); test('should have the correct title', async () => { diff --git a/src/plugins/index_pattern_field_editor/__jest__/client_integration/field_editor_flyout_preview.test.ts b/src/plugins/index_pattern_field_editor/__jest__/client_integration/field_editor_flyout_preview.test.ts index c6d14984463eb..527b0a09f46c2 100644 --- a/src/plugins/index_pattern_field_editor/__jest__/client_integration/field_editor_flyout_preview.test.ts +++ b/src/plugins/index_pattern_field_editor/__jest__/client_integration/field_editor_flyout_preview.test.ts @@ -33,7 +33,6 @@ describe('Field editor Preview panel', () => { afterAll(() => { jest.useRealTimers(); - server.restore(); }); let testBed: FieldEditorFlyoutContentTestBed; @@ -346,9 +345,8 @@ describe('Field editor Preview panel', () => { actions: { toggleFormRow, fields, - waitForDocumentsAndPreviewUpdate, - getLatestPreviewHttpRequest, getRenderedFieldsPreview, + waitForDocumentsAndPreviewUpdate, }, } = testBed; @@ -356,10 +354,11 @@ describe('Field editor Preview panel', () => { await fields.updateName('myRuntimeField'); await fields.updateScript('echo("hello")'); await waitForDocumentsAndPreviewUpdate(); - const request = getLatestPreviewHttpRequest(server); // Make sure the payload sent is correct - expect(request.requestBody).toEqual({ + const firstCall = server.post.mock.calls[0] as Array<{ body: any }>; + const payload = JSON.parse(firstCall[1]?.body); + expect(payload).toEqual({ context: 'keyword_field', document: { description: 'First doc - description', @@ -380,7 +379,7 @@ describe('Field editor Preview panel', () => { }); test('should display an updating indicator while fetching the preview', async () => { - httpRequestsMockHelpers.setFieldPreviewResponse({ values: ['ok'] }); + httpRequestsMockHelpers.setFieldPreviewResponse({ values: ['ok'] }, undefined, true); const { exists, @@ -399,7 +398,7 @@ describe('Field editor Preview panel', () => { }); test('should not display the updating indicator when neither the type nor the script has changed', async () => { - httpRequestsMockHelpers.setFieldPreviewResponse({ values: ['ok'] }); + httpRequestsMockHelpers.setFieldPreviewResponse({ values: ['ok'] }, undefined, true); const { exists, diff --git a/src/plugins/index_pattern_field_editor/__jest__/client_integration/helpers/http_requests.ts b/src/plugins/index_pattern_field_editor/__jest__/client_integration/helpers/http_requests.ts index 4b03db247bad1..4c7a5832f037c 100644 --- a/src/plugins/index_pattern_field_editor/__jest__/client_integration/helpers/http_requests.ts +++ b/src/plugins/index_pattern_field_editor/__jest__/client_integration/helpers/http_requests.ts @@ -6,42 +6,37 @@ * Side Public License, v 1. */ -import sinon, { SinonFakeServer } from 'sinon'; -import { API_BASE_PATH } from '../../../common/constants'; +import { httpServiceMock } from '../../../../../../src/core/public/mocks'; type HttpResponse = Record | any[]; -// Register helpers to mock HTTP Requests -const registerHttpRequestMockHelpers = (server: SinonFakeServer) => { - const setFieldPreviewResponse = (response?: HttpResponse, error?: any) => { - const status = error ? error.body.status || 400 : 200; - const body = error ? JSON.stringify(error.body) : JSON.stringify(response); - - server.respondWith('POST', `${API_BASE_PATH}/field_preview`, [ - status, - { 'Content-Type': 'application/json' }, - body, - ]); +const registerHttpRequestMockHelpers = ( + httpSetup: ReturnType +) => { + const setFieldPreviewResponse = (response?: HttpResponse, error?: any, delayResponse = false) => { + const body = error ? JSON.stringify(error.body) : response; + + httpSetup.post.mockImplementation(() => { + if (delayResponse) { + return new Promise((resolve) => { + setTimeout(() => resolve({ data: body }), 1000); + }); + } else { + return Promise.resolve({ data: body }); + } + }); }; - return { setFieldPreviewResponse, }; }; export const init = () => { - const server = sinon.fakeServer.create(); - server.respondImmediately = true; - - // Define default response for unhandled requests. - // We make requests to APIs which don't impact the component under test, e.g. UI metric telemetry, - // and we can mock them all with a 200 instead of mocking each one individually. - server.respondWith([200, {}, 'DefaultSinonMockServerResponse']); - - const httpRequestsMockHelpers = registerHttpRequestMockHelpers(server); + const httpSetup = httpServiceMock.createSetupContract(); + const httpRequestsMockHelpers = registerHttpRequestMockHelpers(httpSetup); return { - server, + httpSetup, httpRequestsMockHelpers, }; }; diff --git a/src/plugins/index_pattern_field_editor/__jest__/client_integration/helpers/setup_environment.tsx b/src/plugins/index_pattern_field_editor/__jest__/client_integration/helpers/setup_environment.tsx index a996a4ae512bc..f952293e2c9e0 100644 --- a/src/plugins/index_pattern_field_editor/__jest__/client_integration/helpers/setup_environment.tsx +++ b/src/plugins/index_pattern_field_editor/__jest__/client_integration/helpers/setup_environment.tsx @@ -9,8 +9,6 @@ import './jest.mocks'; import React, { FunctionComponent } from 'react'; -import axios from 'axios'; -import axiosXhrAdapter from 'axios/lib/adapters/xhr'; import { merge } from 'lodash'; import { notificationServiceMock, uiSettingsServiceMock } from '../../../../../core/public/mocks'; @@ -20,7 +18,6 @@ import { FieldPreviewProvider } from '../../../public/components/preview'; import { initApi, ApiService } from '../../../public/lib'; import { init as initHttpRequests } from './http_requests'; -const mockHttpClient = axios.create({ adapter: axiosXhrAdapter }); const dataStart = dataPluginMock.createStartContract(); const { search, fieldFormats } = dataStart; @@ -44,12 +41,11 @@ search.search = spySearchQuery; let apiService: ApiService; export const setupEnvironment = () => { - // @ts-expect-error Axios does not fullfill HttpSetupn from core but enough for our tests - apiService = initApi(mockHttpClient); - const { server, httpRequestsMockHelpers } = initHttpRequests(); + const { httpSetup, httpRequestsMockHelpers } = initHttpRequests(); + apiService = initApi(httpSetup); return { - server, + server: httpSetup, httpRequestsMockHelpers, }; };