Skip to content

Commit 534bd0c

Browse files
fix tests
1 parent e31dba7 commit 534bd0c

File tree

10 files changed

+76
-71
lines changed

10 files changed

+76
-71
lines changed

src/plugins/es_ui_shared/public/request/np_ready_request.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,14 @@ export const sendRequest = async (
5353
try {
5454
const response = await httpClient[method](path, { body });
5555

56-
return { data: response, error: null };
56+
return {
57+
data: response.data ? response.data : response,
58+
error: null,
59+
};
5760
} catch (e) {
5861
return {
5962
data: null,
60-
error: e.response ? e.response : e,
63+
error: e.response && e.response.data ? e.response.data : e.body,
6164
};
6265
}
6366
};

x-pack/legacy/plugins/index_management/__jest__/client_integration/helpers/http_requests.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,30 @@
55
*/
66

77
import sinon, { SinonFakeServer } from 'sinon';
8-
9-
const API_PATH = '/api/index_management';
8+
import { API_BASE_PATH } from '../../../common/constants';
109

1110
type HttpResponse = Record<string, any> | any[];
1211

1312
// Register helpers to mock HTTP Requests
1413
const registerHttpRequestMockHelpers = (server: SinonFakeServer) => {
1514
const setLoadTemplatesResponse = (response: HttpResponse = []) => {
16-
server.respondWith('GET', `${API_PATH}/templates`, [
15+
server.respondWith('GET', `${API_BASE_PATH}/templates`, [
1716
200,
1817
{ 'Content-Type': 'application/json' },
1918
JSON.stringify(response),
2019
]);
2120
};
2221

2322
const setLoadIndicesResponse = (response: HttpResponse = []) => {
24-
server.respondWith('GET', `${API_PATH}/indices`, [
23+
server.respondWith('GET', `${API_BASE_PATH}/indices`, [
2524
200,
2625
{ 'Content-Type': 'application/json' },
2726
JSON.stringify(response),
2827
]);
2928
};
3029

3130
const setDeleteTemplateResponse = (response: HttpResponse = []) => {
32-
server.respondWith('DELETE', `${API_PATH}/templates`, [
31+
server.respondWith('DELETE', `${API_BASE_PATH}/templates`, [
3332
200,
3433
{ 'Content-Type': 'application/json' },
3534
JSON.stringify(response),
@@ -40,18 +39,18 @@ const registerHttpRequestMockHelpers = (server: SinonFakeServer) => {
4039
const status = error ? error.status || 400 : 200;
4140
const body = error ? error.body : response;
4241

43-
server.respondWith('GET', `${API_PATH}/templates/:id`, [
42+
server.respondWith('GET', `${API_BASE_PATH}/templates/:id`, [
4443
status,
4544
{ 'Content-Type': 'application/json' },
4645
JSON.stringify(body),
4746
]);
4847
};
4948

5049
const setCreateTemplateResponse = (response?: HttpResponse, error?: any) => {
51-
const status = error ? error.status || 400 : 200;
50+
const status = error ? error.body.status || 400 : 200;
5251
const body = error ? JSON.stringify(error.body) : JSON.stringify(response);
5352

54-
server.respondWith('PUT', `${API_PATH}/templates`, [
53+
server.respondWith('PUT', `${API_BASE_PATH}/templates`, [
5554
status,
5655
{ 'Content-Type': 'application/json' },
5756
body,
@@ -62,7 +61,7 @@ const registerHttpRequestMockHelpers = (server: SinonFakeServer) => {
6261
const status = error ? error.status || 400 : 200;
6362
const body = error ? JSON.stringify(error.body) : JSON.stringify(response);
6463

65-
server.respondWith('PUT', `${API_PATH}/templates/:name`, [
64+
server.respondWith('PUT', `${API_BASE_PATH}/templates/:name`, [
6665
status,
6766
{ 'Content-Type': 'application/json' },
6867
body,

x-pack/legacy/plugins/index_management/__jest__/client_integration/helpers/setup_environment.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,30 @@
77
import axios from 'axios';
88
import axiosXhrAdapter from 'axios/lib/adapters/xhr';
99
import { init as initHttpRequests } from './http_requests';
10-
import { setHttpClient } from '../../../public/app/services/api';
10+
import { httpService } from '../../../public/app/services/http';
11+
import { breadcrumbService } from '../../../public/app/services/breadcrumbs';
12+
import { documentationService } from '../../../public/app/services/documentation';
13+
import { notificationService } from '../../../public/app/services/notification';
14+
import { uiMetricService } from '../../../public/app/services/ui_metric';
15+
import { createUiStatsReporter } from '../../../../../../../src/legacy/core_plugins/ui_metric/public';
16+
17+
/* eslint-disable @kbn/eslint/no-restricted-paths */
18+
import { notificationServiceMock } from '../../../../../../../src/core/public/notifications/notifications_service.mock';
19+
import { chromeServiceMock } from '../../../../../../../src/core/public/chrome/chrome_service.mock';
20+
import { docLinksServiceMock } from '../../../../../../../src/core/public/doc_links/doc_links_service.mock';
1121

1222
const mockHttpClient = axios.create({ adapter: axiosXhrAdapter });
1323

1424
export const setupEnvironment = () => {
15-
const { server, httpRequestsMockHelpers } = initHttpRequests();
16-
25+
// Mock initialization of services
1726
// @ts-ignore
18-
setHttpClient(mockHttpClient);
27+
httpService.init(mockHttpClient);
28+
breadcrumbService.init(chromeServiceMock.createStartContract(), '');
29+
documentationService.init(docLinksServiceMock.createStartContract());
30+
notificationService.init(notificationServiceMock.createStartContract());
31+
uiMetricService.init(createUiStatsReporter);
32+
33+
const { server, httpRequestsMockHelpers } = initHttpRequests();
1934

2035
return {
2136
server,

x-pack/legacy/plugins/index_management/__jest__/client_integration/home.test.ts

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ import { act } from 'react-dom/test-utils';
88
import * as fixtures from '../../test/fixtures';
99
import { setupEnvironment, pageHelpers, nextTick, getRandomString } from './helpers';
1010
import { IdxMgmtHomeTestBed } from './helpers/home.helpers';
11-
12-
const API_PATH = '/api/index_management';
11+
import { API_BASE_PATH } from '../../common/constants';
1312

1413
const { setup } = pageHelpers.home;
1514

@@ -21,16 +20,7 @@ const removeWhiteSpaceOnArrayValues = (array: any[]) =>
2120
return value.trim();
2221
});
2322

24-
jest.mock('ui/index_patterns', () => ({
25-
ILLEGAL_CHARACTERS: '',
26-
CONTAINS_SPACES: '',
27-
validateIndexPattern: () => {},
28-
}));
29-
30-
jest.mock('ui/chrome', () => ({
31-
breadcrumbs: { set: () => {} },
32-
addBasePath: (path: string) => path || '/api/index_management',
33-
}));
23+
jest.mock('ui/new_platform');
3424

3525
// We need to skip the tests until react 16.9.0 is released
3626
// which supports asynchronous code inside act()
@@ -204,7 +194,9 @@ describe.skip('<IndexManagementHome />', () => {
204194
});
205195

206196
expect(server.requests.length).toBe(totalRequests + 1);
207-
expect(server.requests[server.requests.length - 1].url).toBe(`${API_PATH}/templates`);
197+
expect(server.requests[server.requests.length - 1].url).toBe(
198+
`${API_BASE_PATH}/templates`
199+
);
208200
});
209201

210202
test('should have a button to create a new template', () => {
@@ -346,7 +338,7 @@ describe.skip('<IndexManagementHome />', () => {
346338
const latestRequest = server.requests[server.requests.length - 1];
347339

348340
expect(latestRequest.method).toBe('DELETE');
349-
expect(latestRequest.url).toBe(`${API_PATH}/templates/${template1.name}`);
341+
expect(latestRequest.url).toBe(`${API_BASE_PATH}/templates/${template1.name}`);
350342
});
351343
});
352344

x-pack/legacy/plugins/index_management/__jest__/client_integration/template_clone.test.tsx

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,16 +113,13 @@ describe.skip('<TemplateClone />', () => {
113113

114114
const latestRequest = server.requests[server.requests.length - 1];
115115

116-
const body = JSON.parse(latestRequest.requestBody);
117-
const expected = {
116+
const expected = JSON.stringify({
118117
...templateToClone,
119118
name: `${templateToClone.name}-copy`,
120119
indexPatterns: DEFAULT_INDEX_PATTERNS,
121-
aliases: {},
122-
mappings: {},
123-
settings: {},
124-
};
125-
expect(body).toEqual(expected);
120+
});
121+
122+
expect(JSON.parse(latestRequest.requestBody).body).toEqual(expected);
126123
});
127124
});
128125
});

x-pack/legacy/plugins/index_management/__jest__/client_integration/template_create.test.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -310,15 +310,16 @@ describe.skip('<TemplateCreate />', () => {
310310

311311
const latestRequest = server.requests[server.requests.length - 1];
312312

313-
const expected = {
313+
const expected = JSON.stringify({
314+
isManaged: false,
314315
name: TEMPLATE_NAME,
315316
indexPatterns: DEFAULT_INDEX_PATTERNS,
316317
settings: SETTINGS,
317318
mappings: MAPPINGS,
318319
aliases: ALIASES,
319-
isManaged: false,
320-
};
321-
expect(JSON.parse(latestRequest.requestBody)).toEqual(expected);
320+
});
321+
322+
expect(JSON.parse(latestRequest.requestBody).body).toEqual(expected);
322323
});
323324

324325
it('should surface the API errors from the put HTTP request', async () => {

x-pack/legacy/plugins/index_management/__jest__/client_integration/template_edit.test.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,17 +118,18 @@ describe.skip('<TemplateEdit />', () => {
118118

119119
const { version, order } = templateToEdit;
120120

121-
const expected = {
121+
const expected = JSON.stringify({
122122
name: TEMPLATE_NAME,
123123
version,
124124
order,
125125
indexPatterns: UPDATED_INDEX_PATTERN,
126+
isManaged: false,
126127
settings: SETTINGS,
127128
mappings: MAPPINGS,
128129
aliases: ALIASES,
129-
isManaged: false,
130-
};
131-
expect(JSON.parse(latestRequest.requestBody)).toEqual(expected);
130+
});
131+
132+
expect(JSON.parse(latestRequest.requestBody).body).toEqual(expected);
132133
});
133134
});
134135
});

x-pack/legacy/plugins/index_management/__jest__/components/index_table.test.js

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,31 @@
55
*/
66

77
import React from 'react';
8+
import axios from 'axios';
9+
import axiosXhrAdapter from 'axios/lib/adapters/xhr';
810
import { MemoryRouter } from 'react-router-dom';
911
import { AppWithoutRouter } from '../../public/app/app';
1012
import { Provider } from 'react-redux';
1113
import { loadIndicesSuccess } from '../../public/app/store/actions';
1214
import { breadcrumbService } from '../../public/app/services/breadcrumbs';
1315
import { uiMetricService } from '../../public/app/services/ui_metric';
1416
import { notificationService } from '../../public/app/services/notification';
17+
import { httpService } from '../../public/app/services/http';
1518
import { createUiStatsReporter } from '../../../../../../src/legacy/core_plugins/ui_metric/public';
1619
import { indexManagementStore } from '../../public/app/store';
17-
import { BASE_PATH } from '../../common/constants';
20+
import { BASE_PATH, API_BASE_PATH } from '../../common/constants';
1821
import { mountWithIntl } from '../../../../../test_utils/enzyme_helpers';
1922
import sinon from 'sinon';
2023
import { findTestSubject } from '@elastic/eui/lib/test';
2124

25+
/* eslint-disable @kbn/eslint/no-restricted-paths */
26+
import { notificationServiceMock } from '../../../../../../src/core/public/notifications/notifications_service.mock';
27+
import { chromeServiceMock } from '../../../../../../src/core/public/chrome/chrome_service.mock';
28+
2229
jest.mock('ui/new_platform');
2330

31+
const mockHttpClient = axios.create({ adapter: axiosXhrAdapter });
32+
2433
let server = null;
2534

2635
let store = null;
@@ -104,18 +113,12 @@ const namesText = rendered => {
104113
describe('index table', () => {
105114
beforeEach(() => {
106115
// Mock initialization of services
107-
breadcrumbService.init(
108-
{
109-
setBreadcrumbs() {},
110-
},
111-
{}
112-
);
116+
// @ts-ignore
117+
httpService.init(mockHttpClient);
118+
breadcrumbService.init(chromeServiceMock.createStartContract(), '');
113119
uiMetricService.init(createUiStatsReporter);
114-
notificationService.init({
115-
toasts: {
116-
add() {}
117-
}
118-
});
120+
notificationService.init(notificationServiceMock.createStartContract());
121+
119122

120123
store = indexManagementStore();
121124
component = (
@@ -127,7 +130,7 @@ describe('index table', () => {
127130
);
128131
store.dispatch(loadIndicesSuccess({ indices }));
129132
server = sinon.fakeServer.create();
130-
server.respondWith('/api/index_management/indices', [
133+
server.respondWith(`${API_BASE_PATH}/indices`, [
131134
200,
132135
{ 'Content-Type': 'application/json' },
133136
JSON.stringify(indices)
@@ -137,7 +140,7 @@ describe('index table', () => {
137140
{ 'Content-Type': 'application/json' },
138141
JSON.stringify({ acknowledged: true })
139142
]);
140-
server.respondWith('/api/index_management/indices/reload', [
143+
server.respondWith(`${API_BASE_PATH}/indices/reload`, [
141144
200,
142145
{ 'Content-Type': 'application/json' },
143146
JSON.stringify(indices)
@@ -350,7 +353,8 @@ describe('index table', () => {
350353
status: index.name === 'testy0' ? 'close' : index.status
351354
};
352355
});
353-
server.respondWith('/api/index_management/indices/reload', [
356+
357+
server.respondWith(`${API_BASE_PATH}/indices/reload`, [
354358
200,
355359
{ 'Content-Type': 'application/json' },
356360
JSON.stringify(modifiedIndices)
@@ -364,7 +368,7 @@ describe('index table', () => {
364368
status: index.name === 'testy1' ? 'open' : index.status
365369
};
366370
});
367-
server.respondWith('/api/index_management/indices/reload', [
371+
server.respondWith(`${API_BASE_PATH}/indices/reload`, [
368372
200,
369373
{ 'Content-Type': 'application/json' },
370374
JSON.stringify(modifiedIndices)

x-pack/legacy/plugins/index_management/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { i18n } from '@kbn/i18n';
99
import { Legacy } from 'kibana';
1010
import { createRouter } from '../../server/lib/create_router';
1111
import { registerLicenseChecker } from '../../server/lib/register_license_checker';
12-
import { PLUGIN } from './common/constants';
12+
import { PLUGIN, API_BASE_PATH } from './common/constants';
1313
import { LegacySetup } from './server/plugin';
1414
import { plugin as initServerPlugin } from './server';
1515

@@ -33,7 +33,7 @@ export function indexManagement(kibana: any) {
3333
const pluginsSetup = {};
3434

3535
const __LEGACY: LegacySetup = {
36-
router: createRouter(server, PLUGIN.ID, '/api/index_management/'),
36+
router: createRouter(server, PLUGIN.ID, `${API_BASE_PATH}/`),
3737
plugins: {
3838
license: {
3939
registerLicenseChecker: registerLicenseChecker.bind(

x-pack/legacy/plugins/index_management/public/app/services/api.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@ import { Template } from '../../../common/types';
4141

4242
export async function loadIndices() {
4343
const response = await httpService.httpClient.get(`${API_BASE_PATH}/indices`);
44-
return response;
44+
return response.data ? response.data : response;
4545
}
4646

4747
export async function reloadIndices(indexNames: string[]) {
4848
const body = JSON.stringify({
4949
indexNames,
5050
});
5151
const response = await httpService.httpClient.post(`${API_BASE_PATH}/indices/reload`, { body });
52-
return response;
52+
return response.data ? response.data : response;
5353
}
5454

5555
export async function closeIndices(indices: string[]) {
@@ -244,10 +244,3 @@ export async function updateTemplate(template: Template) {
244244

245245
return result;
246246
}
247-
248-
export async function loadTemplateToClone(name: Template['name']) {
249-
return await sendRequest({
250-
path: `${API_BASE_PATH}/templates/${encodeURIComponent(name)}`,
251-
method: 'get',
252-
});
253-
}

0 commit comments

Comments
 (0)