From ce900bc1d6c6a3065f5f47023b8389451aa737e9 Mon Sep 17 00:00:00 2001 From: Matt Kime Date: Wed, 6 Apr 2022 23:05:56 -0500 Subject: [PATCH] partial progress --- .../field_editor_flyout_content.test.ts | 3 +- .../field_editor_flyout_preview.test.ts | 25 ++++---- .../helpers/http_requests.ts | 58 ++++++++++++++++++- .../helpers/setup_environment.tsx | 4 +- 4 files changed, 72 insertions(+), 18 deletions(-) diff --git a/src/plugins/data_view_field_editor/__jest__/client_integration/field_editor_flyout_content.test.ts b/src/plugins/data_view_field_editor/__jest__/client_integration/field_editor_flyout_content.test.ts index 1730593dbda20..a3ae323a1c3db 100644 --- a/src/plugins/data_view_field_editor/__jest__/client_integration/field_editor_flyout_content.test.ts +++ b/src/plugins/data_view_field_editor/__jest__/client_integration/field_editor_flyout_content.test.ts @@ -15,7 +15,7 @@ import { setup } from './field_editor_flyout_content.helpers'; import { mockDocuments, createPreviewError } from './helpers/mocks'; describe('', () => { - const { server, httpRequestsMockHelpers } = setupEnvironment(); + const { httpRequestsMockHelpers } = setupEnvironment(); beforeAll(() => { jest.useFakeTimers(); @@ -23,7 +23,6 @@ describe('', () => { afterAll(() => { jest.useRealTimers(); - server.restore(); }); beforeEach(async () => { diff --git a/src/plugins/data_view_field_editor/__jest__/client_integration/field_editor_flyout_preview.test.ts b/src/plugins/data_view_field_editor/__jest__/client_integration/field_editor_flyout_preview.test.ts index c3175d7097a36..5d4d318e4c35d 100644 --- a/src/plugins/data_view_field_editor/__jest__/client_integration/field_editor_flyout_preview.test.ts +++ b/src/plugins/data_view_field_editor/__jest__/client_integration/field_editor_flyout_preview.test.ts @@ -32,7 +32,6 @@ describe('Field editor Preview panel', () => { afterAll(() => { jest.useRealTimers(); - server.restore(); }); let testBed: FieldEditorFlyoutContentTestBed; @@ -55,8 +54,8 @@ describe('Field editor Preview panel', () => { ]; beforeEach(async () => { - server.respondImmediately = true; - server.autoRespond = true; + // server.respondImmediately = true; + // server.autoRespond = true; httpRequestsMockHelpers.setFieldPreviewResponse({ values: ['mockedScriptValue'] }); setIndexPatternFields(indexPatternFields); @@ -282,7 +281,7 @@ describe('Field editor Preview panel', () => { toggleFormRow, fields, waitForUpdates, - getLatestPreviewHttpRequest, + // getLatestPreviewHttpRequest, getRenderedFieldsPreview, }, } = testBed; @@ -291,10 +290,12 @@ describe('Field editor Preview panel', () => { await fields.updateName('myRuntimeField'); await fields.updateScript('echo("hello")'); await waitForUpdates(); // Run validations - const request = getLatestPreviewHttpRequest(server); + // todo might not need anymore + // const request = getLatestPreviewHttpRequest(server); // Make sure the payload sent is correct - expect(request.requestBody).toEqual({ + console.log('checking calls'); + expect(server.post.mock.calls[0]).toEqual({ context: 'keyword_field', document: { description: 'First doc - description', @@ -373,8 +374,8 @@ describe('Field editor Preview panel', () => { test('should display an updating indicator while fetching the docs and the preview', async () => { // We want to test if the loading indicator is in the DOM, for that we don't want the server to // respond immediately. We'll manualy send the response. - server.respondImmediately = false; - server.autoRespond = false; + // server.respondImmediately = false; + // server.autoRespond = false; httpRequestsMockHelpers.setFieldPreviewResponse({ values: ['ok'] }); @@ -394,7 +395,7 @@ describe('Field editor Preview panel', () => { await fields.updateScript('echo("hello")'); expect(exists('isUpdatingIndicator')).toBe(true); // indicator while getting preview - server.respond(); + // server.respond(); await waitForUpdates(); expect(exists('isUpdatingIndicator')).toBe(false); }); @@ -403,8 +404,8 @@ describe('Field editor Preview panel', () => { httpRequestsMockHelpers.setFieldPreviewResponse({ values: ['ok'] }); // We want to test if the loading indicator is in the DOM, for that we need to manually // send the response from the server - server.respondImmediately = false; - server.autoRespond = false; + // server.respondImmediately = false; + // server.autoRespond = false; const { exists, @@ -417,7 +418,7 @@ describe('Field editor Preview panel', () => { await fields.updateScript('echo("hello")'); expect(exists('isUpdatingIndicator')).toBe(true); - server.respond(); + // server.respond(); await waitForDocumentsAndPreviewUpdate(); expect(exists('isUpdatingIndicator')).toBe(false); diff --git a/src/plugins/data_view_field_editor/__jest__/client_integration/helpers/http_requests.ts b/src/plugins/data_view_field_editor/__jest__/client_integration/helpers/http_requests.ts index e83048f107f9c..d0a4660272d42 100644 --- a/src/plugins/data_view_field_editor/__jest__/client_integration/helpers/http_requests.ts +++ b/src/plugins/data_view_field_editor/__jest__/client_integration/helpers/http_requests.ts @@ -9,11 +9,52 @@ import { httpServiceMock } from '../../../../../../src/core/public/mocks'; import { API_BASE_PATH } from '../../../common/constants'; +type HttpMethod = 'GET' | 'PUT' | 'DELETE' | 'POST'; + type HttpResponse = Record | any[]; +const registerHttpRequestMockHelpers = ( + httpSetup: ReturnType, + shouldDelayResponse: () => boolean +) => { + const mockResponses = new Map>>( + ['GET', 'PUT', 'DELETE', 'POST'].map( + (method) => [method, new Map()] as [HttpMethod, Map>] + ) + ); + + /* + const mockMethodImplementation = (method: HttpMethod, path: string) => { + const responsePromise = mockResponses.get(method)?.get(path) ?? Promise.resolve({}); + if (shouldDelayResponse()) { + return new Promise((resolve) => { + setTimeout(() => resolve(responsePromise), 1000); + }); + } + return responsePromise; + }; + */ + + const setFieldPreviewResponse = (response?: HttpResponse, error?: any) => { + const status = error ? error.body.status || 400 : 200; + const body = error ? JSON.stringify(error.body) : JSON.stringify(response); + + console.log('settingFieldPreviewResponse'); + httpSetup.post.mockImplementation(() => { + console.log('calling mocked post'); + return Promise.resolve([status, { 'Content-Type': 'application/json' }, body]); + }); + }; + return { + setFieldPreviewResponse, + }; +}; + // Register helpers to mock HTTP Requests +/* const registerHttpRequestMockHelpers = ( - httpSetup: ReturnType + httpSetup: ReturnType, + shouldDelayResponse: () => boolean ) => { const setFieldPreviewResponse = (response?: HttpResponse, error?: any) => { const status = error ? error.body.status || 400 : 200; @@ -30,8 +71,10 @@ const registerHttpRequestMockHelpers = ( setFieldPreviewResponse, }; }; +*/ export const init = () => { + /* const server = sinon.fakeServer.create(); server.respondImmediately = true; @@ -41,9 +84,20 @@ export const init = () => { server.respondWith([200, {}, 'DefaultSinonMockServerResponse']); const httpRequestsMockHelpers = registerHttpRequestMockHelpers(server); + */ + + let isResponseDelayed = false; + const getDelayResponse = () => isResponseDelayed; + const setDelayResponse = (shouldDelayResponse: boolean) => { + isResponseDelayed = shouldDelayResponse; + }; + + const httpSetup = httpServiceMock.createSetupContract(); + const httpRequestsMockHelpers = registerHttpRequestMockHelpers(httpSetup, getDelayResponse); return { - server, + httpSetup, + setDelayResponse, httpRequestsMockHelpers, }; }; diff --git a/src/plugins/data_view_field_editor/__jest__/client_integration/helpers/setup_environment.tsx b/src/plugins/data_view_field_editor/__jest__/client_integration/helpers/setup_environment.tsx index 91a2585ebf7bf..04beff5d249e0 100644 --- a/src/plugins/data_view_field_editor/__jest__/client_integration/helpers/setup_environment.tsx +++ b/src/plugins/data_view_field_editor/__jest__/client_integration/helpers/setup_environment.tsx @@ -64,10 +64,10 @@ let apiService: ApiService; export const setupEnvironment = () => { apiService = initApi(mockHttpClient); - const { server, httpRequestsMockHelpers } = initHttpRequests(); + const { httpSetup, httpRequestsMockHelpers } = initHttpRequests(); return { - server, + server: httpSetup, httpRequestsMockHelpers, }; };