Skip to content

Commit

Permalink
[WATCHER] Allow user to set a threshold value of 0 (#44810)
Browse files Browse the repository at this point in the history
  • Loading branch information
alisonelizabeth authored Sep 5, 2019
1 parent 8c44e89 commit e383069
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ export type TestSubjects =
| 'watchActionAccordion'
| 'watchActionAccordion.mockComboBox'
| 'watchActionsPanel'
| 'watchThresholdButton'
| 'watchThresholdInput'
| 'watchConditionTitle'
| 'watchTimeFieldSelect'
| 'watchVisualizationChart'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,45 @@ describe.skip('<ThresholdWatchEdit /> create route', () => {
expect(exists('watchVisualizationChart')).toBe(true);
expect(exists('watchActionsPanel')).toBe(true);
});

describe('watch conditions', () => {
beforeEach(async () => {
const { form, find, component } = testBed;

// Name, index and time fields are required before the watch condition expression renders
form.setInputValue('nameInput', 'my_test_watch');
find('mockComboBox').simulate('change', [{ label: 'index1', value: 'index1' }]); // Using mocked EuiComboBox
form.setInputValue('watchTimeFieldSelect', '@timestamp');

// @ts-ignore (remove when react 16.9.0 is released)
await act(async () => {
await nextTick();
component.update();
});
});

test('should require a threshold value', async () => {
const { form, find, component } = testBed;

find('watchThresholdButton').simulate('click');

// Provide invalid value
form.setInputValue('watchThresholdInput', '');

expect(form.getErrorsMessages()).toContain('A value is required.');

// Provide valid value
form.setInputValue('watchThresholdInput', '0');

// @ts-ignore (remove when react 16.9.0 is released)
await act(async () => {
await nextTick();
component.update();
});

expect(form.getErrorsMessages().length).toEqual(0);
});
});
});

describe('actions', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ export class ThresholdWatch extends BaseWatch {
Array.from(Array(comparators[this.thresholdComparator].requiredValues)).forEach((value, i) => {
const key = `threshold${i}`;
errors[key] = [];
if (!this.threshold[i]) {
if (this.threshold[i] == null || this.threshold[i] === '') {
errors[key].push(i18n.translate(
'xpack.watcher.thresholdWatchExpression.thresholdLevel.valueIsRequiredValidationMessage',
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,7 @@ export const ThresholdWatchEdit = ({ pageTitle }: { pageTitle: string }) => {
id="watchThresholdPopover"
button={
<EuiExpression
data-test-subj="watchThresholdButton"
description={comparators[watch.thresholdComparator].text}
value={watch.threshold
.slice(0, comparators[watch.thresholdComparator].requiredValues)
Expand Down Expand Up @@ -759,6 +760,7 @@ export const ThresholdWatchEdit = ({ pageTitle }: { pageTitle: string }) => {
errors={errors}
>
<EuiFieldNumber
data-test-subj="watchThresholdInput"
value={watch.threshold[i] == null ? '' : watch.threshold[i]}
min={0}
step={0.1}
Expand Down

0 comments on commit e383069

Please sign in to comment.