Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport 2.x] Fix add filter for numeric scripted field #7462

Closed
wants to merge 1 commit into from
Closed
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,80 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import React from 'react';
import { mountWithIntl } from 'test_utils/enzyme_helpers';
import { ValueInputType } from './value_input_type';

let onChangeMock: any;

describe('Value input type', () => {
beforeAll(() => {
onChangeMock = jest.fn();
});
it('is number', async () => {
const valueInputProps = {
value: 1,
type: 'number',
onChange: onChangeMock,
onBlur: () => {},
placeholder: '',
};
const component = mountWithIntl(<ValueInputType {...valueInputProps} />);
expect(component.find('EuiFieldNumber').exists()).toBeTruthy();
expect(component.find('EuiFieldNumber').prop('value')).toBe(1);
});

it('is string', async () => {
const valueInputProps = {
value: 'value',
type: 'string',
onChange: () => {},
onBlur: () => {},
placeholder: '',
};
const component = mountWithIntl(<ValueInputType {...valueInputProps} />);
expect(component.find('EuiFieldText').exists()).toBeTruthy();
expect(component.find('EuiFieldText').prop('value')).toBe('value');
});

it('is boolean', async () => {
const valueInputProps = {
value: 'true',
type: 'boolean',
onChange: () => {},
onBlur: () => {},
placeholder: '',
};
const component = mountWithIntl(<ValueInputType {...valueInputProps} />);
expect(component.find('EuiSelect').exists()).toBeTruthy();
expect(component.find('EuiSelect').prop('value')).toBe('true');
});

it('is date', async () => {
const valueInputProps = {
value: 'Jun 18, 2024 @ 12:01:55.000',
type: 'date',
onChange: () => {},
onBlur: () => {},
placeholder: '',
};
const component = mountWithIntl(<ValueInputType {...valueInputProps} />);
expect(component.find('EuiFieldText').exists()).toBeTruthy();
expect(component.find('EuiFieldText').prop('value')).toBe('Jun 18, 2024 @ 12:01:55.000');
});

it('is ip', async () => {
const valueInputProps = {
value: '127.0.0.1',
type: 'ip',
onChange: () => {},
onBlur: () => {},
placeholder: '',
};
const component = mountWithIntl(<ValueInputType {...valueInputProps} />);
expect(component.find('EuiFieldText').exists()).toBeTruthy();
expect(component.find('EuiFieldText').prop('value')).toBe('127.0.0.1');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
? (value as BigInt).toString()
: value
}
onChange={this.onChange}
onChange={this.onNumberChange}
controlOnly={this.props.controlOnly}
className={this.props.className}
/>
Expand Down Expand Up @@ -151,6 +151,17 @@
this.props.onChange(boolValue);
};

private onNumberChange = (event: React.ChangeEvent<HTMLInputElement>) => {
const params = event.target.value;

Check warning on line 155 in src/plugins/data/public/ui/filter_bar/filter_editor/value_input_type.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/ui/filter_bar/filter_editor/value_input_type.tsx#L155

Added line #L155 was not covered by tests
let numValue;
if (typeof params === 'string') {
numValue = parseFloat(params);

Check warning on line 158 in src/plugins/data/public/ui/filter_bar/filter_editor/value_input_type.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/ui/filter_bar/filter_editor/value_input_type.tsx#L158

Added line #L158 was not covered by tests
} else if (typeof params === 'bigint') {
numValue = (params as BigInt).toString();

Check warning on line 160 in src/plugins/data/public/ui/filter_bar/filter_editor/value_input_type.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/ui/filter_bar/filter_editor/value_input_type.tsx#L160

Added line #L160 was not covered by tests
}
this.props.onChange(numValue ?? params);
};

private onChange = (event: React.ChangeEvent<HTMLInputElement>) => {
const params = event.target.value;
this.props.onChange(params);
Expand Down
Loading