Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { render, wait } from '@testing-library/react';
import React from 'react';
import { ApmIndices } from '.';
import { MockApmPluginContextWrapper } from '../../../../utils/testHelpers';
import * as hooks from '../../../../hooks/useFetcher';

describe('ApmIndices', () => {
it('should not get stuck in infinite loop', async () => {
spyOn(hooks, 'useFetcher').and.returnValue({
data: undefined,
status: 'loading'
});
const { getByText } = render(
<MockApmPluginContextWrapper>
<ApmIndices />
</MockApmPluginContextWrapper>
);

expect(getByText('Indices')).toMatchInlineSnapshot(`
<h2
class="euiTitle euiTitle--medium"
>
Indices
</h2>
`);

await wait();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ async function saveApmIndices({
clearCache();
}

// avoid infinite loop by initializing the state outside the component
const INITIAL_STATE = [] as [];

export function ApmIndices() {
const { toasts } = useApmPluginContext().core.notifications;

Expand All @@ -93,7 +96,7 @@ export function ApmIndices() {

const callApmApiFromHook = useCallApmApi();

const { data = [], status, refetch } = useFetcher(
const { data = INITIAL_STATE, status, refetch } = useFetcher(
callApmApi =>
callApmApi({ pathname: `/api/apm/settings/apm-index-settings` }),
[]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { saveApmIndices } from './save_apm_indices';

describe('saveApmIndices', () => {
it('should trim and strip empty settings', async () => {
const context = {
core: {
savedObjects: {
client: {
create: jest.fn()
}
}
}
} as any;

const apmIndices = {
settingA: 'aa',
settingB: '',
settingC: undefined,
settingD: null,
settingE: ' ',
settingF: 'ff',
settingG: ' gg '
} as any;
await saveApmIndices(context, apmIndices);
expect(context.core.savedObjects.client.create).toHaveBeenCalledWith(
expect.any(String),
{ settingA: 'aa', settingF: 'ff', settingG: 'gg' },
expect.any(Object)
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,23 @@ import { APMRequestHandlerContext } from '../../../routes/typings';

export async function saveApmIndices(
context: APMRequestHandlerContext,
apmIndicesSavedObject: Partial<ApmIndicesConfig>
apmIndices: Partial<ApmIndicesConfig>
) {
return await context.core.savedObjects.client.create(
APM_INDICES_SAVED_OBJECT_TYPE,
apmIndicesSavedObject,
removeEmpty(apmIndices),
{
id: APM_INDICES_SAVED_OBJECT_ID,
overwrite: true
}
);
}

// remove empty/undefined values
function removeEmpty(apmIndices: Partial<ApmIndicesConfig>) {
return Object.fromEntries(
Object.entries(apmIndices)
.map(([key, value]) => [key, value?.trim()])
.filter(([key, value]) => !!value)
);
}