Skip to content

Commit

Permalink
[ML] Transforms: Preserves the field for unsupported aggs (#142106)
Browse files Browse the repository at this point in the history
* preserve field for unsupported agg

* add unit test
  • Loading branch information
darnautov authored Sep 30, 2022
1 parent 68bc381 commit 2d8eb0e
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 8 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@
*/

import { shallow } from 'enzyme';
import { fireEvent, render } from '@testing-library/react';
import React from 'react';

import { AggName } from '../../../../../../common/types/aggregations';
import { PIVOT_SUPPORTED_AGGS } from '../../../../../../common/types/pivot_aggs';

import { PivotAggsConfig } from '../../../../common';

import { PopoverForm } from './popover_form';
import { I18nProvider } from '@kbn/i18n-react';

describe('Transform: Aggregation <PopoverForm />', () => {
afterEach(() => {
jest.clearAllMocks();
});

test('Minimal initialization', () => {
const defaultData: PivotAggsConfig = {
agg: PIVOT_SUPPORTED_AGGS.CARDINALITY,
Expand All @@ -37,4 +40,67 @@ describe('Transform: Aggregation <PopoverForm />', () => {

expect(wrapper).toMatchSnapshot();
});

test('preserves the field for unsupported aggs', async () => {
const mockOnChange = jest.fn();
const { getByTestId } = render(
<I18nProvider>
<PopoverForm
defaultData={{
field: 'AvgTicketPrice',
keyed: true,
ranges: [
{
to: 500,
},
{
from: 500,
to: 700,
},
{
from: 700,
},
],
// @ts-ignore
agg: 'range',
aggName: 'AvgTicketPrice.ranges',
dropDownName: 'AvgTicketPrice.ranges',
}}
otherAggNames={[]}
options={{}}
onChange={mockOnChange}
/>
</I18nProvider>
);

const aggNameInput = getByTestId('transformAggName');
fireEvent.change(aggNameInput, {
target: { value: 'betterName' },
});

const applyButton = getByTestId('transformApplyAggChanges');
fireEvent.click(applyButton);

expect(mockOnChange).toHaveBeenCalledTimes(1);
expect(mockOnChange).toHaveBeenCalledWith({
field: 'AvgTicketPrice',
keyed: true,
ranges: [
{
to: 500,
},
{
from: 500,
to: 700,
},
{
from: 700,
},
],
// @ts-ignore
agg: 'range',
aggName: 'betterName',
dropDownName: 'AvgTicketPrice.ranges',
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ export const PopoverForm: React.FC<Props> = ({ defaultData, otherAggNames, onCha
...aggConfigDef,
agg,
aggName,
field: resultField,
dropDownName: defaultData.dropDownName,
...(isUnsupportedAgg ? {} : { field: resultField }),
};

return updatedItem;
Expand Down Expand Up @@ -153,7 +153,7 @@ export const PopoverForm: React.FC<Props> = ({ defaultData, otherAggNames, onCha
}

return (
<EuiForm style={{ width: '300px' }} data-test-subj={'transformAggPopoverForm_' + aggName}>
<EuiForm css={{ width: '300px' }} data-test-subj={'transformAggPopoverForm_' + aggName}>
<EuiFormRow
error={!validAggName && [aggNameError]}
isInvalid={!validAggName}
Expand Down Expand Up @@ -257,7 +257,7 @@ export const PopoverForm: React.FC<Props> = ({ defaultData, otherAggNames, onCha
fontSize="s"
language="json"
paddingSize="s"
style={{ width: '100%', height: '200px' }}
css={{ width: '100%', height: '200px' }}
>
{JSON.stringify(getEsAggFromAggConfig(defaultData), null, 2)}
</EuiCodeBlock>
Expand Down

0 comments on commit 2d8eb0e

Please sign in to comment.