Skip to content

Commit

Permalink
clean up tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alisonelizabeth committed Aug 29, 2023
1 parent 6f28be4 commit 944fb87
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,11 @@ export interface IndexDetailsPageTestBed extends TestBed {
isDisplayed: () => boolean;
clickReloadButton: () => Promise<void>;
};
statsTab: {
indexStatsContentExists: () => boolean;
stats: {
getCodeBlockContent: () => string;
getDocsLinkHref: () => string;
isErrorDisplayed: () => boolean;
clickErrorReloadButton: () => Promise<void>;
indexStatsTabExists: () => boolean;
};
};
Expand Down Expand Up @@ -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,
Expand All @@ -159,7 +174,7 @@ export const setup = async (
discoverLinkExists,
contextMenu,
errorSection,
statsTab,
stats,
},
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -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('<IndexDetailsPage />', () => {
Expand All @@ -22,6 +22,7 @@ describe('<IndexDetailsPage />', () => {
({ 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, {
Expand Down Expand Up @@ -62,23 +63,28 @@ describe('<IndexDetailsPage />', () => {

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,
body: undefined,
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 () => {
Expand All @@ -89,7 +95,34 @@ describe('<IndexDetailsPage />', () => {
});
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);
});
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export const DetailsPageStats: FunctionComponent<RouteComponentProps<{ indexName
if (error) {
return (
<EuiPageTemplate.EmptyPrompt
data-test-subj="indexStatsErrorLoadingDetails"
data-test-subj="indexDetailsStatsError"
color="danger"
iconType="warning"
title={
Expand Down Expand Up @@ -162,6 +162,7 @@ export const DetailsPageStats: FunctionComponent<RouteComponentProps<{ indexName
>
<EuiPanel>
<EuiCodeBlock
data-test-subj="indexDetailsStatsCodeBlock"
isCopyable
language="json"
paddingSize="m"
Expand Down

0 comments on commit 944fb87

Please sign in to comment.