Skip to content

Commit

Permalink
[Index Management] Add no index name message and prevent an http requ…
Browse files Browse the repository at this point in the history
…est being unnecessarily sent out
  • Loading branch information
yuliacech committed Sep 22, 2023
1 parent 41debe7 commit d3591e6
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ import { WithAppDependencies } from '../helpers';
import { testIndexName } from './mocks';

let routerMock: typeof reactRouterMock;
const testBedConfig: AsyncTestBedConfig = {
const getTestBedConfig = (initialEntry?: string): AsyncTestBedConfig => ({
memoryRouter: {
initialEntries: [`/indices/index_details?indexName=${testIndexName}`],
initialEntries: [initialEntry ?? `/indices/index_details?indexName=${testIndexName}`],
componentRoutePath: `/indices/index_details`,
onRouter: (router) => {
routerMock = router;
},
},
doMountAsync: true,
};
});

export interface IndexDetailsPageTestBed extends TestBed {
routerMock: typeof reactRouterMock;
Expand Down Expand Up @@ -66,6 +66,7 @@ export interface IndexDetailsPageTestBed extends TestBed {
errorSection: {
isDisplayed: () => boolean;
clickReloadButton: () => Promise<void>;
noIndexNameMessageIsDisplayed: () => boolean;
};
stats: {
getCodeBlockContent: () => string;
Expand All @@ -84,13 +85,18 @@ export interface IndexDetailsPageTestBed extends TestBed {
};
}

export const setup = async (
httpSetup: HttpSetup,
overridingDependencies: any = {}
): Promise<IndexDetailsPageTestBed> => {
export const setup = async ({
httpSetup,
dependencies = {},
initialEntry,
}: {
httpSetup: HttpSetup;
dependencies?: any;
initialEntry?: string;
}): Promise<IndexDetailsPageTestBed> => {
const initTestBed = registerTestBed(
WithAppDependencies(IndexDetailsPage, httpSetup, overridingDependencies),
testBedConfig
WithAppDependencies(IndexDetailsPage, httpSetup, dependencies),
getTestBedConfig(initialEntry)
);
const testBed = await initTestBed();
const { find, component, exists } = testBed;
Expand All @@ -105,6 +111,9 @@ export const setup = async (
});
component.update();
},
noIndexNameMessageIsDisplayed: () => {
return exists('indexDetailsNoIndexNameError');
},
};
const getHeader = () => {
return component.find('[data-test-subj="indexDetailsHeader"] h1').text();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,13 @@ describe('<IndexDetailsPage />', () => {
httpRequestsMockHelpers.setLoadIndexSettingsResponse(testIndexName, testIndexSettings);

await act(async () => {
testBed = await setup(httpSetup, {
url: {
locators: {
get: () => ({ navigate: jest.fn() }),
testBed = await setup({
httpSetup,
dependencies: {
url: {
locators: {
get: () => ({ navigate: jest.fn() }),
},
},
},
});
Expand All @@ -76,7 +79,7 @@ describe('<IndexDetailsPage />', () => {
message: `Data for index ${testIndexName} was not found`,
});
await act(async () => {
testBed = await setup(httpSetup);
testBed = await setup({ httpSetup });
});

testBed.component.update();
Expand All @@ -92,6 +95,19 @@ describe('<IndexDetailsPage />', () => {
await testBed.actions.errorSection.clickReloadButton();
expect(httpSetup.get).toHaveBeenCalledTimes(numberOfRequests + 1);
});

it('renders an error section when no index name is provided', async () => {
// already sent 2 requests while setting up the component
const numberOfRequests = 2;
expect(httpSetup.get).toHaveBeenCalledTimes(numberOfRequests);
await act(async () => {
testBed = await setup({ httpSetup, initialEntry: '/indices/index_details' });
});
testBed.component.update();
expect(testBed.actions.errorSection.noIndexNameMessageIsDisplayed()).toBe(true);
// no extra http request was sent
expect(httpSetup.get).toHaveBeenCalledTimes(numberOfRequests);
});
});

describe('Stats tab', () => {
Expand Down Expand Up @@ -139,7 +155,7 @@ describe('<IndexDetailsPage />', () => {
);

await act(async () => {
testBed = await setup(httpSetup);
testBed = await setup({ httpSetup });
});
testBed.component.update();

Expand All @@ -149,8 +165,11 @@ describe('<IndexDetailsPage />', () => {

it('hides index stats tab if enableIndexStats===false', async () => {
await act(async () => {
testBed = await setup(httpSetup, {
config: { enableIndexStats: false },
testBed = await setup({
httpSetup,
dependencies: {
config: { enableIndexStats: false },
},
});
});
testBed.component.update();
Expand All @@ -165,7 +184,7 @@ describe('<IndexDetailsPage />', () => {
message: 'Error',
});
await act(async () => {
testBed = await setup(httpSetup);
testBed = await setup({ httpSetup });
});

testBed.component.update();
Expand Down Expand Up @@ -214,8 +233,11 @@ describe('<IndexDetailsPage />', () => {

it('hides index stats from detail panels if enableIndexStats===false', async () => {
await act(async () => {
testBed = await setup(httpSetup, {
config: { enableIndexStats: false },
testBed = await setup({
httpSetup,
dependencies: {
config: { enableIndexStats: false },
},
});
});
testBed.component.update();
Expand All @@ -227,10 +249,13 @@ describe('<IndexDetailsPage />', () => {
describe('extension service summary', () => {
it('renders all summaries added to the extension service', async () => {
await act(async () => {
testBed = await setup(httpSetup, {
services: {
extensionsService: {
summaries: [() => <span>test</span>, () => <span>test2</span>],
testBed = await setup({
httpSetup,
dependencies: {
services: {
extensionsService: {
summaries: [() => <span>test</span>, () => <span>test2</span>],
},
},
},
});
Expand All @@ -242,10 +267,13 @@ describe('<IndexDetailsPage />', () => {

it(`doesn't render empty panels if the summary renders null`, async () => {
await act(async () => {
testBed = await setup(httpSetup, {
services: {
extensionsService: {
summaries: [() => null],
testBed = await setup({
httpSetup,
dependencies: {
services: {
extensionsService: {
summaries: [() => null],
},
},
},
});
Expand All @@ -256,10 +284,13 @@ describe('<IndexDetailsPage />', () => {

it(`doesn't render anything when no summaries added to the extension service`, async () => {
await act(async () => {
testBed = await setup(httpSetup, {
services: {
extensionsService: {
summaries: [],
testBed = await setup({
httpSetup,
dependencies: {
services: {
extensionsService: {
summaries: [],
},
},
},
});
Expand Down Expand Up @@ -310,7 +341,7 @@ describe('<IndexDetailsPage />', () => {
message: `Was not able to load mappings`,
});
await act(async () => {
testBed = await setup(httpSetup);
testBed = await setup({ httpSetup });
});

testBed.component.update();
Expand Down Expand Up @@ -372,7 +403,7 @@ describe('<IndexDetailsPage />', () => {
message: `Was not able to load settings`,
});
await act(async () => {
testBed = await setup(httpSetup);
testBed = await setup( { httpSetup });
});

testBed.component.update();
Expand Down Expand Up @@ -485,7 +516,7 @@ describe('<IndexDetailsPage />', () => {
});

await act(async () => {
testBed = await setup(httpSetup);
testBed = await setup({ httpSetup });
});
testBed.component.update();

Expand Down Expand Up @@ -578,7 +609,7 @@ describe('<IndexDetailsPage />', () => {
});

await act(async () => {
testBed = await setup(httpSetup);
testBed = await setup({ httpSetup });
});
testBed.component.update();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import {
EuiPageHeaderProps,
EuiPageSection,
EuiButton,
EuiPageTemplate,
EuiText,
EuiCode,
} from '@elastic/eui';
import { SectionLoading } from '@kbn/es-ui-shared-plugin/public';

Expand Down Expand Up @@ -106,15 +109,17 @@ export const DetailsPage: FunctionComponent<
return getSelectedTabContent({ tab: indexDetailsSection, index, indexName });
}, [index, indexDetailsSection, indexName]);
const fetchIndexDetails = useCallback(async () => {
setIsLoading(true);
try {
const { data, error: loadingError } = await loadIndex(indexName);
setIsLoading(false);
setError(loadingError);
setIndex(data);
} catch (e) {
setIsLoading(false);
setError(e);
if (indexName) {
setIsLoading(true);
try {
const { data, error: loadingError } = await loadIndex(indexName);
setIsLoading(false);
setError(loadingError);
setIndex(data);
} catch (e) {
setIsLoading(false);
setError(e);
}
}
}, [indexName]);

Expand Down Expand Up @@ -145,6 +150,34 @@ export const DetailsPage: FunctionComponent<
}));
}, [indexDetailsSection, onSectionChange, config]);

if (!indexName) {
return (
<EuiPageTemplate.EmptyPrompt
data-test-subj="indexDetailsNoIndexNameError"
color="danger"
iconType="warning"
title={
<h2>
<FormattedMessage
id="xpack.idxMgmt.indexDetails.noIndexNameErrorTitle"
defaultMessage="Unable to load index details"
/>
</h2>
}
body={
<EuiText color="subdued">
<FormattedMessage
id="xpack.idxMgmt.indexDetails.noIndexNameErrorDescription"
defaultMessage="An index name is required for this page. Add a query parameter {queryParam} followed by an index name to the url."
values={{
queryParam: <EuiCode>indexName</EuiCode>,
}}
/>
</EuiText>
}
/>
);
}
if (isLoading && !index) {
return (
<SectionLoading>
Expand Down

0 comments on commit d3591e6

Please sign in to comment.