From 9d47441c28053e8cbe2338b39ee0de2414f68902 Mon Sep 17 00:00:00 2001 From: Alison Goryachev Date: Thu, 7 Nov 2019 23:23:53 -0500 Subject: [PATCH] fix tests --- .../public/request/np_ready_request.ts | 7 ++++-- .../helpers/http_requests.ts | 2 +- .../helpers/setup_environment.ts | 23 +++++++++++++++---- .../template_clone.test.tsx | 11 ++++----- .../template_create.test.tsx | 9 ++++---- .../client_integration/template_edit.test.tsx | 9 ++++---- .../public/app/services/api.ts | 7 ------ 7 files changed, 39 insertions(+), 29 deletions(-) diff --git a/src/plugins/es_ui_shared/public/request/np_ready_request.ts b/src/plugins/es_ui_shared/public/request/np_ready_request.ts index ec0db8c8e969e38..48c7904661e5156 100644 --- a/src/plugins/es_ui_shared/public/request/np_ready_request.ts +++ b/src/plugins/es_ui_shared/public/request/np_ready_request.ts @@ -53,11 +53,14 @@ export const sendRequest = async ( try { const response = await httpClient[method](path, { body }); - return { data: response, error: null }; + return { + data: response.data ? response.data : response, + error: null, + }; } catch (e) { return { data: null, - error: e.response ? e.response : e, + error: e.response && e.response.data ? e.response.data : e.body, }; } }; diff --git a/x-pack/legacy/plugins/index_management/__jest__/client_integration/helpers/http_requests.ts b/x-pack/legacy/plugins/index_management/__jest__/client_integration/helpers/http_requests.ts index b49736cb1e9ba6c..55496f1c79412b1 100644 --- a/x-pack/legacy/plugins/index_management/__jest__/client_integration/helpers/http_requests.ts +++ b/x-pack/legacy/plugins/index_management/__jest__/client_integration/helpers/http_requests.ts @@ -48,7 +48,7 @@ const registerHttpRequestMockHelpers = (server: SinonFakeServer) => { }; const setCreateTemplateResponse = (response?: HttpResponse, error?: any) => { - const status = error ? error.status || 400 : 200; + const status = error ? error.body.status || 400 : 200; const body = error ? JSON.stringify(error.body) : JSON.stringify(response); server.respondWith('PUT', `${API_PATH}/templates`, [ diff --git a/x-pack/legacy/plugins/index_management/__jest__/client_integration/helpers/setup_environment.ts b/x-pack/legacy/plugins/index_management/__jest__/client_integration/helpers/setup_environment.ts index 2a2b1a2bae50b78..d5ac185a63ccd77 100644 --- a/x-pack/legacy/plugins/index_management/__jest__/client_integration/helpers/setup_environment.ts +++ b/x-pack/legacy/plugins/index_management/__jest__/client_integration/helpers/setup_environment.ts @@ -7,15 +7,30 @@ import axios from 'axios'; import axiosXhrAdapter from 'axios/lib/adapters/xhr'; import { init as initHttpRequests } from './http_requests'; -import { setHttpClient } from '../../../public/app/services/api'; +import { httpService } from '../../../public/app/services/http'; +import { breadcrumbService } from '../../../public/app/services/breadcrumbs'; +import { documentationService } from '../../../public/app/services/documentation'; +import { notificationService } from '../../../public/app/services/notification'; +import { uiMetricService } from '../../../public/app/services/ui_metric'; +import { createUiStatsReporter } from '../../../../../../../src/legacy/core_plugins/ui_metric/public'; + +/* eslint-disable @kbn/eslint/no-restricted-paths */ +import { notificationServiceMock } from '../../../../../../../src/core/public/notifications/notifications_service.mock'; +import { chromeServiceMock } from '../../../../../../../src/core/public/chrome/chrome_service.mock'; +import { docLinksServiceMock } from '../../../../../../../src/core/public/doc_links/doc_links_service.mock'; const mockHttpClient = axios.create({ adapter: axiosXhrAdapter }); export const setupEnvironment = () => { - const { server, httpRequestsMockHelpers } = initHttpRequests(); - + // Mock initialization of services // @ts-ignore - setHttpClient(mockHttpClient); + httpService.init(mockHttpClient); + breadcrumbService.init(chromeServiceMock.createStartContract(), ''); + documentationService.init(docLinksServiceMock.createStartContract()); + notificationService.init(notificationServiceMock.createStartContract()); + uiMetricService.init(createUiStatsReporter); + + const { server, httpRequestsMockHelpers } = initHttpRequests(); return { server, diff --git a/x-pack/legacy/plugins/index_management/__jest__/client_integration/template_clone.test.tsx b/x-pack/legacy/plugins/index_management/__jest__/client_integration/template_clone.test.tsx index 00c56ec9dd3d627..bd8d9b8e3567547 100644 --- a/x-pack/legacy/plugins/index_management/__jest__/client_integration/template_clone.test.tsx +++ b/x-pack/legacy/plugins/index_management/__jest__/client_integration/template_clone.test.tsx @@ -113,16 +113,13 @@ describe.skip('', () => { const latestRequest = server.requests[server.requests.length - 1]; - const body = JSON.parse(latestRequest.requestBody); - const expected = { + const expected = JSON.stringify({ ...templateToClone, name: `${templateToClone.name}-copy`, indexPatterns: DEFAULT_INDEX_PATTERNS, - aliases: {}, - mappings: {}, - settings: {}, - }; - expect(body).toEqual(expected); + }); + + expect(JSON.parse(latestRequest.requestBody).body).toEqual(expected); }); }); }); diff --git a/x-pack/legacy/plugins/index_management/__jest__/client_integration/template_create.test.tsx b/x-pack/legacy/plugins/index_management/__jest__/client_integration/template_create.test.tsx index 4a1d207755859d8..a391811257a9f3e 100644 --- a/x-pack/legacy/plugins/index_management/__jest__/client_integration/template_create.test.tsx +++ b/x-pack/legacy/plugins/index_management/__jest__/client_integration/template_create.test.tsx @@ -310,15 +310,16 @@ describe.skip('', () => { const latestRequest = server.requests[server.requests.length - 1]; - const expected = { + const expected = JSON.stringify({ + isManaged: false, name: TEMPLATE_NAME, indexPatterns: DEFAULT_INDEX_PATTERNS, settings: SETTINGS, mappings: MAPPINGS, aliases: ALIASES, - isManaged: false, - }; - expect(JSON.parse(latestRequest.requestBody)).toEqual(expected); + }); + + expect(JSON.parse(latestRequest.requestBody).body).toEqual(expected); }); it('should surface the API errors from the put HTTP request', async () => { diff --git a/x-pack/legacy/plugins/index_management/__jest__/client_integration/template_edit.test.tsx b/x-pack/legacy/plugins/index_management/__jest__/client_integration/template_edit.test.tsx index a2abf075d89032a..4056bd2ad63e7c7 100644 --- a/x-pack/legacy/plugins/index_management/__jest__/client_integration/template_edit.test.tsx +++ b/x-pack/legacy/plugins/index_management/__jest__/client_integration/template_edit.test.tsx @@ -118,17 +118,18 @@ describe.skip('', () => { const { version, order } = templateToEdit; - const expected = { + const expected = JSON.stringify({ name: TEMPLATE_NAME, version, order, indexPatterns: UPDATED_INDEX_PATTERN, + isManaged: false, settings: SETTINGS, mappings: MAPPINGS, aliases: ALIASES, - isManaged: false, - }; - expect(JSON.parse(latestRequest.requestBody)).toEqual(expected); + }); + + expect(JSON.parse(latestRequest.requestBody).body).toEqual(expected); }); }); }); diff --git a/x-pack/legacy/plugins/index_management/public/app/services/api.ts b/x-pack/legacy/plugins/index_management/public/app/services/api.ts index 1b6c604d6c71f61..b0ff53e76591017 100644 --- a/x-pack/legacy/plugins/index_management/public/app/services/api.ts +++ b/x-pack/legacy/plugins/index_management/public/app/services/api.ts @@ -244,10 +244,3 @@ export async function updateTemplate(template: Template) { return result; } - -export async function loadTemplateToClone(name: Template['name']) { - return await sendRequest({ - path: `${API_BASE_PATH}/templates/${encodeURIComponent(name)}`, - method: 'get', - }); -}