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

[Alerting] fixes buggy default message behaviour #84202

Merged
merged 6 commits into from
Nov 30, 2020
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
Expand Up @@ -33,4 +33,97 @@ describe('EmailParamsFields renders', () => {
expect(wrapper.find('[data-test-subj="subjectInput"]').length > 0).toBeTruthy();
expect(wrapper.find('[data-test-subj="messageTextArea"]').length > 0).toBeTruthy();
});

test('message param field is rendered with default value if not set', () => {
const actionParams = {
cc: [],
bcc: [],
to: ['test@test.com'],
subject: 'test',
};

const editAction = jest.fn();
mountWithIntl(
<EmailParamsFields
actionParams={actionParams}
errors={{ to: [], cc: [], bcc: [], subject: [], message: [] }}
editAction={editAction}
defaultMessage={'Some default message'}
index={0}
/>
);

expect(editAction).toHaveBeenCalledWith('message', 'Some default message', 0);
});

test('when the default message changes, so is the underlying message if it was set by the previous default', () => {
const actionParams = {
cc: [],
bcc: [],
to: ['test@test.com'],
subject: 'test',
};

const editAction = jest.fn();
const wrapper = mountWithIntl(
<EmailParamsFields
actionParams={actionParams}
errors={{ to: [], cc: [], bcc: [], subject: [], message: [] }}
editAction={editAction}
defaultMessage={'Some default message'}
index={0}
/>
);

expect(editAction).toHaveBeenCalledWith('message', 'Some default message', 0);

wrapper.setProps({
defaultMessage: 'Some different default message',
});

expect(editAction).toHaveBeenCalledWith('message', 'Some different default message', 0);
});

test('when the default message changes, it doesnt change the underlying message if it wasnt set by a previous default', () => {
const actionParams = {
cc: [],
bcc: [],
to: ['test@test.com'],
subject: 'test',
};

const editAction = jest.fn();
const wrapper = mountWithIntl(
<EmailParamsFields
actionParams={actionParams}
errors={{ to: [], cc: [], bcc: [], subject: [], message: [] }}
editAction={editAction}
defaultMessage={'Some default message'}
index={0}
/>
);

expect(editAction).toHaveBeenCalledWith('message', 'Some default message', 0);

// simulate value being updated
const valueToSimulate = 'some new value';
wrapper
.find('[data-test-subj="messageTextArea"]')
.first()
.simulate('change', { target: { value: valueToSimulate } });
expect(editAction).toHaveBeenCalledWith('message', valueToSimulate, 0);
wrapper.setProps({
actionParams: {
...actionParams,
message: valueToSimulate,
},
});

// simulate default changing
wrapper.setProps({
defaultMessage: 'Some different default message',
});

expect(editAction).not.toHaveBeenCalledWith('message', 'Some different default message', 0);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { ActionParamsProps } from '../../../../types';
import { EmailActionParams } from '../types';
import { TextFieldWithMessageVariables } from '../../text_field_with_message_variables';
import { TextAreaWithMessageVariables } from '../../text_area_with_message_variables';
import { resolvedActionGroupMessage } from '../../../constants';

export const EmailParamsFields = ({
actionParams,
Expand All @@ -28,17 +27,19 @@ export const EmailParamsFields = ({
const [addCC, setAddCC] = useState<boolean>(false);
const [addBCC, setAddBCC] = useState<boolean>(false);

const [[isUsingDefault, defaultMessageUsed], setDefaultMessageUsage] = useState<
[boolean, string | undefined]
>([false, defaultMessage]);
useEffect(() => {
if (defaultMessage === resolvedActionGroupMessage) {
editAction('message', defaultMessage, index);
} else if (
(!message || message === resolvedActionGroupMessage) &&
defaultMessage &&
defaultMessage.length > 0
if (
!actionParams?.message ||
(isUsingDefault &&
actionParams?.message === defaultMessageUsed &&
defaultMessageUsed !== defaultMessage)
) {
setDefaultMessageUsage([true, defaultMessage]);
editAction('message', defaultMessage, index);
}

// eslint-disable-next-line react-hooks/exhaustive-deps
}, [defaultMessage]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { useGetSingleIssue } from './use_get_single_issue';
import * as i18n from './translations';

interface Props {
selectedValue: string | null;
selectedValue?: string | null;
http: HttpSetup;
toastNotifications: Pick<
ToastsApi,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ interface Props {
ToastsApi,
'get$' | 'add' | 'remove' | 'addSuccess' | 'addWarning' | 'addDanger' | 'addError'
>;
issueType: string;
issueType: string | undefined;
actionConnector?: ActionConnector;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ interface Props {
ToastsApi,
'get$' | 'add' | 'remove' | 'addSuccess' | 'addWarning' | 'addDanger' | 'addError'
>;
id: string | null;
id?: string | null;
actionConnector?: ActionConnector;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import { ServerLogLevelOptions } from '.././types';
import ServerLogParamsFields from './server_log_params';

describe('ServerLogParamsFields renders', () => {
const editAction = jest.fn();
test('all params fields is rendered', () => {
const editAction = jest.fn();
const actionParams = {
level: ServerLogLevelOptions.TRACE,
message: 'test',
Expand All @@ -35,20 +35,103 @@ describe('ServerLogParamsFields renders', () => {
test('level param field is rendered with default value if not selected', () => {
const actionParams = {
message: 'test message',
level: ServerLogLevelOptions.INFO,
};
const editAction = jest.fn();

mountWithIntl(
<ServerLogParamsFields
actionParams={actionParams}
errors={{ message: [] }}
editAction={editAction}
index={0}
/>
);

expect(editAction).toHaveBeenCalledWith('level', 'info', 0);
});

test('message param field is rendered with default value if not set', () => {
const actionParams = {
level: ServerLogLevelOptions.TRACE,
};

const editAction = jest.fn();

mountWithIntl(
<ServerLogParamsFields
actionParams={actionParams}
defaultMessage={'Some default message'}
errors={{ message: [] }}
editAction={editAction}
index={0}
/>
);

expect(editAction).toHaveBeenCalledWith('message', 'Some default message', 0);
});

test('when the default message changes, so is the underlying message if it was set by the previous default', () => {
const actionParams = {
level: ServerLogLevelOptions.TRACE,
};

const editAction = jest.fn();
const wrapper = mountWithIntl(
<ServerLogParamsFields
actionParams={actionParams}
defaultMessage={'Some default message'}
errors={{ message: [] }}
editAction={() => {}}
editAction={editAction}
index={0}
/>
);
expect(wrapper.find('[data-test-subj="loggingLevelSelect"]').length > 0).toBeTruthy();
expect(
wrapper.find('[data-test-subj="loggingLevelSelect"]').first().prop('value')
).toStrictEqual('info');
expect(wrapper.find('[data-test-subj="messageTextArea"]').length > 0).toBeTruthy();

expect(editAction).toHaveBeenCalledWith('message', 'Some default message', 0);

wrapper.setProps({
defaultMessage: 'Some different default message',
});

expect(editAction).toHaveBeenCalledWith('message', 'Some different default message', 0);
});

test('when the default message changes, it doesnt change the underlying message if it wasnt set by a previous default', () => {
const actionParams = {
level: ServerLogLevelOptions.TRACE,
};

const editAction = jest.fn();
const wrapper = mountWithIntl(
<ServerLogParamsFields
actionParams={actionParams}
defaultMessage={'Some default message'}
errors={{ message: [] }}
editAction={editAction}
index={0}
/>
);

expect(editAction).toHaveBeenCalledWith('message', 'Some default message', 0);

// simulate value being updated
const valueToSimulate = 'some new value';
wrapper
.find('[data-test-subj="messageTextArea"]')
.first()
.simulate('change', { target: { value: valueToSimulate } });
expect(editAction).toHaveBeenCalledWith('message', valueToSimulate, 0);
wrapper.setProps({
actionParams: {
...actionParams,
message: valueToSimulate,
},
});

// simulate default changing
wrapper.setProps({
defaultMessage: 'Some different default message',
});

expect(editAction).not.toHaveBeenCalledWith('message', 'Some different default message', 0);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import React, { Fragment, useEffect } from 'react';
import React, { Fragment, useEffect, useState } from 'react';
import { i18n } from '@kbn/i18n';
import { EuiSelect, EuiFormRow } from '@elastic/eui';
import { ActionParamsProps } from '../../../../types';
import { ServerLogActionParams } from '.././types';
import { TextAreaWithMessageVariables } from '../../text_area_with_message_variables';
import { resolvedActionGroupMessage } from '../../../constants';

export const ServerLogParamsFields: React.FunctionComponent<
ActionParamsProps<ServerLogActionParams>
Expand All @@ -23,25 +22,26 @@ export const ServerLogParamsFields: React.FunctionComponent<
{ value: 'error', text: 'Error' },
{ value: 'fatal', text: 'Fatal' },
];

useEffect(() => {
if (!actionParams?.level) {
if (!actionParams.level) {
editAction('level', 'info', index);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

const [[isUsingDefault, defaultMessageUsed], setDefaultMessageUsage] = useState<
[boolean, string | undefined]
>([false, defaultMessage]);
useEffect(() => {
if (defaultMessage === resolvedActionGroupMessage) {
editAction('message', defaultMessage, index);
} else if (
(!message || message === resolvedActionGroupMessage) &&
defaultMessage &&
defaultMessage.length > 0
if (
!actionParams?.message ||
(isUsingDefault &&
actionParams?.message === defaultMessageUsed &&
defaultMessageUsed !== defaultMessage)
) {
setDefaultMessageUsage([true, defaultMessage]);
editAction('message', defaultMessage, index);
}

// eslint-disable-next-line react-hooks/exhaustive-deps
}, [defaultMessage]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import React, { useEffect } from 'react';
import React, { useEffect, useState } from 'react';
import { i18n } from '@kbn/i18n';
import { ActionParamsProps } from '../../../../types';
import { SlackActionParams } from '../types';
import { TextAreaWithMessageVariables } from '../../text_area_with_message_variables';
import { resolvedActionGroupMessage } from '../../../constants';

const SlackParamsFields: React.FunctionComponent<ActionParamsProps<SlackActionParams>> = ({
actionParams,
Expand All @@ -19,17 +18,19 @@ const SlackParamsFields: React.FunctionComponent<ActionParamsProps<SlackActionPa
defaultMessage,
}) => {
const { message } = actionParams;
const [[isUsingDefault, defaultMessageUsed], setDefaultMessageUsage] = useState<
[boolean, string | undefined]
>([false, defaultMessage]);
useEffect(() => {
if (defaultMessage === resolvedActionGroupMessage) {
editAction('message', defaultMessage, index);
} else if (
(!message || message === resolvedActionGroupMessage) &&
defaultMessage &&
defaultMessage.length > 0
if (
!actionParams?.message ||
(isUsingDefault &&
actionParams?.message === defaultMessageUsed &&
defaultMessageUsed !== defaultMessage)
) {
setDefaultMessageUsage([true, defaultMessage]);
editAction('message', defaultMessage, index);
}

// eslint-disable-next-line react-hooks/exhaustive-deps
}, [defaultMessage]);

Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/triggers_actions_ui/public/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export interface ActionConnectorFieldsProps<TActionConnector> {
}

export interface ActionParamsProps<TParams> {
actionParams: TParams;
actionParams: Partial<TParams>;
index: number;
editAction: (key: string, value: AlertActionParam, index: number) => void;
errors: IErrorObject;
Expand Down