mirror of
https://github.com/elastic/kibana.git
synced 2025-04-25 02:09:32 -04:00
Removing axios from data_view_field_editor test (#129116)
* test bed fixes Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
This commit is contained in:
parent
6542dc194a
commit
ae39e1bae7
5 changed files with 29 additions and 55 deletions
|
@ -19,7 +19,7 @@ import {
|
||||||
} from './field_editor.helpers';
|
} from './field_editor.helpers';
|
||||||
|
|
||||||
describe('<FieldEditor />', () => {
|
describe('<FieldEditor />', () => {
|
||||||
const { server, httpRequestsMockHelpers } = setupEnvironment();
|
const { httpRequestsMockHelpers } = setupEnvironment();
|
||||||
|
|
||||||
let testBed: FieldEditorTestBed;
|
let testBed: FieldEditorTestBed;
|
||||||
let onChange: jest.Mock<Props['onChange']> = jest.fn();
|
let onChange: jest.Mock<Props['onChange']> = jest.fn();
|
||||||
|
@ -70,7 +70,6 @@ describe('<FieldEditor />', () => {
|
||||||
|
|
||||||
afterAll(() => {
|
afterAll(() => {
|
||||||
jest.useRealTimers();
|
jest.useRealTimers();
|
||||||
server.restore();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
|
|
|
@ -15,7 +15,7 @@ import { setup } from './field_editor_flyout_content.helpers';
|
||||||
import { mockDocuments, createPreviewError } from './helpers/mocks';
|
import { mockDocuments, createPreviewError } from './helpers/mocks';
|
||||||
|
|
||||||
describe('<FieldEditorFlyoutContent />', () => {
|
describe('<FieldEditorFlyoutContent />', () => {
|
||||||
const { server, httpRequestsMockHelpers } = setupEnvironment();
|
const { httpRequestsMockHelpers } = setupEnvironment();
|
||||||
|
|
||||||
beforeAll(() => {
|
beforeAll(() => {
|
||||||
jest.useFakeTimers();
|
jest.useFakeTimers();
|
||||||
|
@ -23,7 +23,6 @@ describe('<FieldEditorFlyoutContent />', () => {
|
||||||
|
|
||||||
afterAll(() => {
|
afterAll(() => {
|
||||||
jest.useRealTimers();
|
jest.useRealTimers();
|
||||||
server.restore();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
|
|
|
@ -32,7 +32,6 @@ describe('Field editor Preview panel', () => {
|
||||||
|
|
||||||
afterAll(() => {
|
afterAll(() => {
|
||||||
jest.useRealTimers();
|
jest.useRealTimers();
|
||||||
server.restore();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
let testBed: FieldEditorFlyoutContentTestBed;
|
let testBed: FieldEditorFlyoutContentTestBed;
|
||||||
|
@ -55,9 +54,6 @@ describe('Field editor Preview panel', () => {
|
||||||
];
|
];
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
server.respondImmediately = true;
|
|
||||||
server.autoRespond = true;
|
|
||||||
|
|
||||||
httpRequestsMockHelpers.setFieldPreviewResponse({ values: ['mockedScriptValue'] });
|
httpRequestsMockHelpers.setFieldPreviewResponse({ values: ['mockedScriptValue'] });
|
||||||
setIndexPatternFields(indexPatternFields);
|
setIndexPatternFields(indexPatternFields);
|
||||||
setSearchResponse(mockDocuments);
|
setSearchResponse(mockDocuments);
|
||||||
|
@ -278,23 +274,18 @@ describe('Field editor Preview panel', () => {
|
||||||
httpRequestsMockHelpers.setFieldPreviewResponse({ values: [scriptEmitResponse] });
|
httpRequestsMockHelpers.setFieldPreviewResponse({ values: [scriptEmitResponse] });
|
||||||
|
|
||||||
const {
|
const {
|
||||||
actions: {
|
actions: { toggleFormRow, fields, waitForUpdates, getRenderedFieldsPreview },
|
||||||
toggleFormRow,
|
|
||||||
fields,
|
|
||||||
waitForUpdates,
|
|
||||||
getLatestPreviewHttpRequest,
|
|
||||||
getRenderedFieldsPreview,
|
|
||||||
},
|
|
||||||
} = testBed;
|
} = testBed;
|
||||||
|
|
||||||
await toggleFormRow('value');
|
await toggleFormRow('value');
|
||||||
await fields.updateName('myRuntimeField');
|
await fields.updateName('myRuntimeField');
|
||||||
await fields.updateScript('echo("hello")');
|
await fields.updateScript('echo("hello")');
|
||||||
await waitForUpdates(); // Run validations
|
await waitForUpdates(); // Run validations
|
||||||
const request = getLatestPreviewHttpRequest(server);
|
|
||||||
|
|
||||||
// Make sure the payload sent is correct
|
// 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',
|
context: 'keyword_field',
|
||||||
document: {
|
document: {
|
||||||
description: 'First doc - description',
|
description: 'First doc - description',
|
||||||
|
@ -373,10 +364,8 @@ describe('Field editor Preview panel', () => {
|
||||||
test('should display an updating indicator while fetching the docs and the preview', async () => {
|
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
|
// 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.
|
// respond immediately. We'll manualy send the response.
|
||||||
server.respondImmediately = false;
|
|
||||||
server.autoRespond = false;
|
|
||||||
|
|
||||||
httpRequestsMockHelpers.setFieldPreviewResponse({ values: ['ok'] });
|
httpRequestsMockHelpers.setFieldPreviewResponse({ values: ['ok'] }, undefined, true);
|
||||||
|
|
||||||
const {
|
const {
|
||||||
exists,
|
exists,
|
||||||
|
@ -394,17 +383,14 @@ describe('Field editor Preview panel', () => {
|
||||||
await fields.updateScript('echo("hello")');
|
await fields.updateScript('echo("hello")');
|
||||||
expect(exists('isUpdatingIndicator')).toBe(true); // indicator while getting preview
|
expect(exists('isUpdatingIndicator')).toBe(true); // indicator while getting preview
|
||||||
|
|
||||||
server.respond();
|
|
||||||
await waitForUpdates();
|
await waitForUpdates();
|
||||||
expect(exists('isUpdatingIndicator')).toBe(false);
|
expect(exists('isUpdatingIndicator')).toBe(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should not display the updating indicator when neither the type nor the script has changed', async () => {
|
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);
|
||||||
// We want to test if the loading indicator is in the DOM, for that we need to manually
|
// We want to test if the loading indicator is in the DOM, for that we need to manually
|
||||||
// send the response from the server
|
// send the response from the server
|
||||||
server.respondImmediately = false;
|
|
||||||
server.autoRespond = false;
|
|
||||||
|
|
||||||
const {
|
const {
|
||||||
exists,
|
exists,
|
||||||
|
@ -417,7 +403,6 @@ describe('Field editor Preview panel', () => {
|
||||||
await fields.updateScript('echo("hello")');
|
await fields.updateScript('echo("hello")');
|
||||||
expect(exists('isUpdatingIndicator')).toBe(true);
|
expect(exists('isUpdatingIndicator')).toBe(true);
|
||||||
|
|
||||||
server.respond();
|
|
||||||
await waitForDocumentsAndPreviewUpdate();
|
await waitForDocumentsAndPreviewUpdate();
|
||||||
|
|
||||||
expect(exists('isUpdatingIndicator')).toBe(false);
|
expect(exists('isUpdatingIndicator')).toBe(false);
|
||||||
|
|
|
@ -6,42 +6,37 @@
|
||||||
* Side Public License, v 1.
|
* Side Public License, v 1.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import sinon, { SinonFakeServer } from 'sinon';
|
import { httpServiceMock } from '../../../../../../src/core/public/mocks';
|
||||||
import { API_BASE_PATH } from '../../../common/constants';
|
|
||||||
|
|
||||||
type HttpResponse = Record<string, any> | any[];
|
type HttpResponse = Record<string, any> | any[];
|
||||||
|
|
||||||
// Register helpers to mock HTTP Requests
|
const registerHttpRequestMockHelpers = (
|
||||||
const registerHttpRequestMockHelpers = (server: SinonFakeServer) => {
|
httpSetup: ReturnType<typeof httpServiceMock.createStartContract>
|
||||||
const setFieldPreviewResponse = (response?: HttpResponse, error?: any) => {
|
) => {
|
||||||
const status = error ? error.body.status || 400 : 200;
|
const setFieldPreviewResponse = (response?: HttpResponse, error?: any, delayResponse = false) => {
|
||||||
const body = error ? JSON.stringify(error.body) : JSON.stringify(response);
|
const body = error ? JSON.stringify(error.body) : response;
|
||||||
|
|
||||||
server.respondWith('POST', `${API_BASE_PATH}/field_preview`, [
|
httpSetup.post.mockImplementation(() => {
|
||||||
status,
|
if (delayResponse) {
|
||||||
{ 'Content-Type': 'application/json' },
|
return new Promise((resolve) => {
|
||||||
body,
|
setTimeout(() => resolve({ data: body }), 1000);
|
||||||
]);
|
});
|
||||||
|
} else {
|
||||||
|
return Promise.resolve({ data: body });
|
||||||
|
}
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
return {
|
return {
|
||||||
setFieldPreviewResponse,
|
setFieldPreviewResponse,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
export const init = () => {
|
export const init = () => {
|
||||||
const server = sinon.fakeServer.create();
|
const httpSetup = httpServiceMock.createSetupContract();
|
||||||
server.respondImmediately = true;
|
const httpRequestsMockHelpers = registerHttpRequestMockHelpers(httpSetup);
|
||||||
|
|
||||||
// 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);
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
server,
|
httpSetup,
|
||||||
httpRequestsMockHelpers,
|
httpRequestsMockHelpers,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
|
@ -10,8 +10,6 @@
|
||||||
import './jest.mocks';
|
import './jest.mocks';
|
||||||
|
|
||||||
import React, { FunctionComponent } from 'react';
|
import React, { FunctionComponent } from 'react';
|
||||||
import axios from 'axios';
|
|
||||||
import axiosXhrAdapter from 'axios/lib/adapters/xhr';
|
|
||||||
import { merge } from 'lodash';
|
import { merge } from 'lodash';
|
||||||
|
|
||||||
import { notificationServiceMock, uiSettingsServiceMock } from '../../../../../core/public/mocks';
|
import { notificationServiceMock, uiSettingsServiceMock } from '../../../../../core/public/mocks';
|
||||||
|
@ -23,7 +21,6 @@ import { init as initHttpRequests } from './http_requests';
|
||||||
import { fieldFormatsMock as fieldFormats } from '../../../../field_formats/common/mocks';
|
import { fieldFormatsMock as fieldFormats } from '../../../../field_formats/common/mocks';
|
||||||
import { FieldFormat } from '../../../../field_formats/common';
|
import { FieldFormat } from '../../../../field_formats/common';
|
||||||
|
|
||||||
const mockHttpClient = axios.create({ adapter: axiosXhrAdapter });
|
|
||||||
const dataStart = dataPluginMock.createStartContract();
|
const dataStart = dataPluginMock.createStartContract();
|
||||||
const { search } = dataStart;
|
const { search } = dataStart;
|
||||||
|
|
||||||
|
@ -61,12 +58,11 @@ search.search = spySearchQuery;
|
||||||
let apiService: ApiService;
|
let apiService: ApiService;
|
||||||
|
|
||||||
export const setupEnvironment = () => {
|
export const setupEnvironment = () => {
|
||||||
// @ts-expect-error Axios does not fullfill HttpSetupn from core but enough for our tests
|
const { httpSetup, httpRequestsMockHelpers } = initHttpRequests();
|
||||||
apiService = initApi(mockHttpClient);
|
apiService = initApi(httpSetup);
|
||||||
const { server, httpRequestsMockHelpers } = initHttpRequests();
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
server,
|
server: httpSetup,
|
||||||
httpRequestsMockHelpers,
|
httpRequestsMockHelpers,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue