Skip to content

Commit

Permalink
added missing test
Browse files Browse the repository at this point in the history
  • Loading branch information
gmmorris committed Nov 24, 2020
1 parent 00f7ef3 commit f9c7560
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 13 deletions.
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();
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);
});

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 @@ -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 @@ -10,9 +10,9 @@ import { ActionParamsProps } from '../../../../types';
import { ServerLogActionParams } from '.././types';
import { TextAreaWithMessageVariables } from '../../text_area_with_message_variables';

export const ServerLogParamsFields: React.FunctionComponent<ActionParamsProps<
ServerLogActionParams
>> = ({ actionParams, editAction, index, errors, messageVariables, defaultMessage }) => {
export const ServerLogParamsFields: React.FunctionComponent<
ActionParamsProps<ServerLogActionParams>
> = ({ actionParams, editAction, index, errors, messageVariables, defaultMessage }) => {
const { message, level } = actionParams;
const levelOptions = [
{ value: 'trace', text: 'Trace' },
Expand All @@ -23,7 +23,7 @@ export const ServerLogParamsFields: React.FunctionComponent<ActionParamsProps<
{ value: 'fatal', text: 'Fatal' },
];
useEffect(() => {
if (!actionParams?.level) {
if (!actionParams.level) {
editAction('level', 'info', index);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
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

0 comments on commit f9c7560

Please sign in to comment.