diff --git a/x-pack/plugins/index_management/__jest__/client_integration/helpers/http_requests.ts b/x-pack/plugins/index_management/__jest__/client_integration/helpers/http_requests.ts index 0a054fccdfcc15..9824e614742f32 100644 --- a/x-pack/plugins/index_management/__jest__/client_integration/helpers/http_requests.ts +++ b/x-pack/plugins/index_management/__jest__/client_integration/helpers/http_requests.ts @@ -100,8 +100,11 @@ const registerHttpRequestMockHelpers = ( const setLoadIndexMappingResponse = (response?: HttpResponse, error?: ResponseError) => mockResponse('GET', `${API_BASE_PATH}/mapping/:name`, response, error); - const setLoadIndexStatsResponse = (response?: HttpResponse, error?: ResponseError) => - mockResponse('GET', `${API_BASE_PATH}/stats/:name`, response, error); + const setLoadIndexStatsResponse = ( + indexName: string, + response?: HttpResponse, + error?: ResponseError + ) => mockResponse('GET', `${API_BASE_PATH}/stats/${indexName}`, response, error); const setUpdateIndexSettingsResponse = ( indexName: string, diff --git a/x-pack/plugins/index_management/__jest__/client_integration/index_details_page/index_details_page.helpers.ts b/x-pack/plugins/index_management/__jest__/client_integration/index_details_page/index_details_page.helpers.ts index 9844d2ccdbf66a..3f247144abf392 100644 --- a/x-pack/plugins/index_management/__jest__/client_integration/index_details_page/index_details_page.helpers.ts +++ b/x-pack/plugins/index_management/__jest__/client_integration/index_details_page/index_details_page.helpers.ts @@ -51,8 +51,11 @@ export interface IndexDetailsPageTestBed extends TestBed { isDisplayed: () => boolean; clickReloadButton: () => Promise; }; - statsTab: { - indexStatsContentExists: () => boolean; + stats: { + getCodeBlockContent: () => string; + getDocsLinkHref: () => string; + isErrorDisplayed: () => boolean; + clickErrorReloadButton: () => Promise; indexStatsTabExists: () => boolean; }; }; @@ -140,13 +143,25 @@ export const setup = async ( }, }; - const statsTab = { - indexStatsContentExists: () => { - return exists('statsTabContent'); - }, + const stats = { indexStatsTabExists: () => { return exists('indexDetailsTab-stats'); }, + getCodeBlockContent: () => { + return find('indexDetailsStatsCodeBlock').text(); + }, + getDocsLinkHref: () => { + return find('indexDetailsStatsDocsLink').prop('href'); + }, + isErrorDisplayed: () => { + return exists('indexDetailsStatsError'); + }, + clickErrorReloadButton: async () => { + await act(async () => { + find('reloadIndexStatsButton').simulate('click'); + }); + component.update(); + }, }; return { ...testBed, @@ -159,7 +174,7 @@ export const setup = async ( discoverLinkExists, contextMenu, errorSection, - statsTab, + stats, }, }; }; diff --git a/x-pack/plugins/index_management/__jest__/client_integration/index_details_page/index_details_page.test.ts b/x-pack/plugins/index_management/__jest__/client_integration/index_details_page/index_details_page.test.ts index e0ad2444b69fbc..357ec6fbe160da 100644 --- a/x-pack/plugins/index_management/__jest__/client_integration/index_details_page/index_details_page.test.ts +++ b/x-pack/plugins/index_management/__jest__/client_integration/index_details_page/index_details_page.test.ts @@ -9,7 +9,7 @@ import { setupEnvironment } from '../helpers'; import { IndexDetailsPageTestBed, setup } from './index_details_page.helpers'; import { act } from 'react-dom/test-utils'; import { IndexDetailsSection } from '../../../public/application/sections/home/index_list/details_page'; -import { testIndexMock, testIndexName } from './mocks'; +import { testIndexMock, testIndexName, testIndexStats } from './mocks'; import { API_BASE_PATH, INTERNAL_API_BASE_PATH } from '../../../common'; describe('', () => { @@ -22,6 +22,7 @@ describe('', () => { ({ httpSetup, httpRequestsMockHelpers } = mockEnvironment); // testIndexName is configured in initialEntries of the memory router httpRequestsMockHelpers.setLoadIndexDetailsResponse(testIndexName, testIndexMock); + httpRequestsMockHelpers.setLoadIndexStatsResponse(testIndexName, testIndexStats); await act(async () => { testBed = await setup(httpSetup, { @@ -62,10 +63,6 @@ describe('', () => { describe('Stats tab', () => { it('loads index stats from the API', async () => { - const numberOfRequests = 1; - // Expect initial request to fetch index details - expect(httpSetup.get).toHaveBeenCalledTimes(numberOfRequests); - await testBed.actions.clickIndexDetailsTab(IndexDetailsSection.Stats); expect(httpSetup.get).toHaveBeenLastCalledWith(`${API_BASE_PATH}/stats/${testIndexName}`, { asSystemRequest: undefined, @@ -73,12 +70,21 @@ describe('', () => { query: undefined, version: undefined, }); - expect(httpSetup.get).toHaveBeenCalledTimes(numberOfRequests + 1); }); it('renders index stats', async () => { await testBed.actions.clickIndexDetailsTab(IndexDetailsSection.Stats); - expect(testBed.actions.statsTab.indexStatsContentExists()).toBe(true); + const tabContent = testBed.actions.stats.getCodeBlockContent(); + expect(tabContent).toEqual(JSON.stringify(testIndexStats, null, 2)); + }); + + it('sets the docs link href from the documenation service', async () => { + await testBed.actions.clickIndexDetailsTab(IndexDetailsSection.Stats); + const docsLinkHref = testBed.actions.stats.getDocsLinkHref(); + // the url from the mocked docs mock + expect(docsLinkHref).toEqual( + 'https://www.elastic.co/guide/en/elasticsearch/reference/mocked-test-branch/indices-stats.html' + ); }); it('hides index stats tab if enableIndexStats===false', async () => { @@ -89,7 +95,34 @@ describe('', () => { }); testBed.component.update(); - expect(testBed.actions.statsTab.indexStatsTabExists()).toBe(false); + expect(testBed.actions.stats.indexStatsTabExists()).toBe(false); + }); + + describe('Error handling', () => { + beforeEach(async () => { + httpRequestsMockHelpers.setLoadIndexStatsResponse(testIndexName, undefined, { + statusCode: 500, + message: 'Error', + }); + await act(async () => { + testBed = await setup(httpSetup); + }); + + testBed.component.update(); + await testBed.actions.clickIndexDetailsTab(IndexDetailsSection.Stats); + }); + + it('there is an error prompt', async () => { + expect(testBed.actions.stats.isErrorDisplayed()).toBe(true); + }); + + it('resends a request when reload button is clicked', async () => { + // already sent 3 requests while setting up the component + const numberOfRequests = 3; + expect(httpSetup.get).toHaveBeenCalledTimes(numberOfRequests); + await testBed.actions.stats.clickErrorReloadButton(); + expect(httpSetup.get).toHaveBeenCalledTimes(numberOfRequests + 1); + }); }); }); diff --git a/x-pack/plugins/index_management/__jest__/client_integration/index_details_page/mocks.ts b/x-pack/plugins/index_management/__jest__/client_integration/index_details_page/mocks.ts index 5e165fe0702e63..2dd93156a76ee4 100644 --- a/x-pack/plugins/index_management/__jest__/client_integration/index_details_page/mocks.ts +++ b/x-pack/plugins/index_management/__jest__/client_integration/index_details_page/mocks.ts @@ -29,3 +29,29 @@ export const testIndexMock: Index = { }, isFollowerIndex: false, }; + +// Mocking partial index stats response +export const testIndexStats = { + _shards: { + total: 1, + successful: 1, + failed: 0, + }, + stats: { + uuid: 'tQ-n6sriQzC84xn58VYONQ', + health: 'green', + status: 'open', + primaries: { + docs: { + count: 1000, + deleted: 0, + }, + }, + total: { + docs: { + count: 1000, + deleted: 0, + }, + }, + }, +}; diff --git a/x-pack/plugins/index_management/public/application/sections/home/index_list/details_page/tabs/stats.tsx b/x-pack/plugins/index_management/public/application/sections/home/index_list/details_page/tabs/stats.tsx index ac48ace6b85df5..ab8aa3e89d2b8a 100644 --- a/x-pack/plugins/index_management/public/application/sections/home/index_list/details_page/tabs/stats.tsx +++ b/x-pack/plugins/index_management/public/application/sections/home/index_list/details_page/tabs/stats.tsx @@ -48,7 +48,7 @@ export const DetailsPageStats: FunctionComponent