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

[App Search] Added the log retention confirmation modal to the Settings page #83009

Merged
merged 8 commits into from
Nov 11, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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,70 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* 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 from 'react';
import { shallow } from 'enzyme';

import { GenericConfirmationModal } from './generic_confirmation_modal';

describe('GenericConfirmationModal', () => {
let wrapper: any;
const onClose = jest.fn();
const onSave = jest.fn();

beforeEach(() => {
jest.clearAllMocks();
wrapper = shallow(
<GenericConfirmationModal
title="A title"
subheading="A subheading"
description="A description"
target="DISABLE"
onClose={onClose}
onSave={onSave}
/>
);
});

it('calls onSave callback when save is pressed', () => {
const button = wrapper.find('[data-test-subj="GenericConfirmationModalSave"]');
button.simulate('click');
expect(onSave).toHaveBeenCalled();
});

it('calls onClose callback when Cancel is pressed', () => {
const button = wrapper.find('[data-test-subj="GenericConfirmationModalCancel"]');
button.simulate('click');
expect(onClose).toHaveBeenCalled();
});

it('disables the Save button when the input is empty', () => {
const button = wrapper.find('[data-test-subj="GenericConfirmationModalSave"]');
expect(button.prop('disabled')).toEqual(true);
});

it('disables the Save button when the input is not equal to the target', () => {
const input = wrapper.find('[data-test-subj="GenericConfirmationModalInput"]');
input.prop('onChange')({
target: {
value: 'NOT_GOOD',
},
});

const button = wrapper.find('[data-test-subj="GenericConfirmationModalSave"]');
expect(button.prop('disabled')).toEqual(true);
});

it('enables the Save button when the current input equals the target prop', () => {
const input = wrapper.find('[data-test-subj="GenericConfirmationModalInput"]');
input.prop('onChange')({
target: {
value: 'DISABLE',
},
});
const button = wrapper.find('[data-test-subj="GenericConfirmationModalSave"]');
expect(button.prop('disabled')).toEqual(false);
});
});
JasonStoltz marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* 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, { ReactNode, useState } from 'react';
import { i18n } from '@kbn/i18n';

import {
EuiButton,
EuiButtonEmpty,
EuiFieldText,
EuiFormRow,
EuiModal,
EuiModalBody,
EuiModalFooter,
EuiModalHeader,
EuiModalHeaderTitle,
EuiSpacer,
EuiText,
} from '@elastic/eui';

interface GenericConfirmationModalProps {
description: ReactNode;
subheading: ReactNode;
target: string;
title: ReactNode;
onClose(): void;
onSave(): void;
}

export const GenericConfirmationModal: React.FC<GenericConfirmationModalProps> = ({
description,
onClose,
onSave,
subheading,
target,
title,
}) => {
const [inputValue, setInputValue] = useState('');

const onConfirm = () => {
setInputValue('');
onSave();
};

return (
<EuiModal onClose={onClose}>
cee-chen marked this conversation as resolved.
Show resolved Hide resolved
<EuiModalHeader>
<EuiModalHeaderTitle>{title}</EuiModalHeaderTitle>
</EuiModalHeader>
<EuiModalBody>
<EuiText>
<p>
<strong>{subheading}</strong>
</p>
<p>{description}</p>
</EuiText>
<EuiSpacer />
<EuiFormRow
label={i18n.translate(
'xpack.enterpriseSearch.appSearch.settings.logRetention.modal.prompt',
{
defaultMessage: 'Type "{target}" to confirm.',
cee-chen marked this conversation as resolved.
Show resolved Hide resolved
values: { target },
}
)}
>
<EuiFieldText
data-test-subj="GenericConfirmationModalInput"
name="engineName"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this name doesn't seem right? do we even need a name prop

Suggested change
name="engineName"

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

value={inputValue}
onChange={(e) => setInputValue(e.target.value)}
/>
</EuiFormRow>
</EuiModalBody>
<EuiModalFooter>
<EuiButtonEmpty data-test-subj="GenericConfirmationModalCancel" onClick={onClose}>
{i18n.translate('xpack.enterpriseSearch.appSearch.settings.logRetention.modal.cancel', {
defaultMessage: 'Cancel',
Comment on lines +79 to +80
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[not a change request, just me thinking out loud]
I wonder if we should have a library of common UI strings like 'Cancel', 'Save', 'Edit', 'Delete' etc., to lower the number of i18n strings we have and the number of translations the team has to do

Happy to do this is a tech debt refactor in a separate PR later if you think it's a good idea 🤷

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that's a good point!

})}
</EuiButtonEmpty>
<EuiButton
data-test-subj="GenericConfirmationModalSave"
cee-chen marked this conversation as resolved.
Show resolved Hide resolved
onClick={onConfirm}
disabled={inputValue !== target}
cee-chen marked this conversation as resolved.
Show resolved Hide resolved
>
{i18n.translate('xpack.enterpriseSearch.appSearch.settings.logRetention.modal.save', {
defaultMessage: 'Save setting',
})}
</EuiButton>
</EuiModalFooter>
</EuiModal>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import '../../../../__mocks__/kea.mock';
import { setMockActions, setMockValues } from '../../../../__mocks__';

import React from 'react';
import ReactDOM from 'react-dom';
import { shallow } from 'enzyme';

import { LogRetentionConfirmationModal } from './log_retention_confirmation_modal';
import { ELogRetentionOptions } from './types';
import { GenericConfirmationModal } from './generic_confirmation_modal';

describe('<LogRetentionConfirmationModal />', () => {
beforeAll(() => {
// LogRetentionConfirmationModal contains EuiModals, which utilize React Portals,
// so we must mock `createPortal` to get the rendered element directly,
// instead of letting it be placed normally elsewhere in DOM (outside of jest's domain)
// @ts-ignore
ReactDOM.createPortal = jest.fn((element) => {
return element;
});
cee-chen marked this conversation as resolved.
Show resolved Hide resolved
});

const actions = {
closeModals: jest.fn(),
saveLogRetention: jest.fn(),
};

const values = {
openedModal: null,
logRetention: {
analytics: {
enabled: true,
retentionPolicy: {
isDefault: true,
minAgeDays: 180,
},
},
api: {
enabled: true,
retentionPolicy: {
isDefault: true,
minAgeDays: 7,
},
},
},
};

beforeEach(() => {
jest.clearAllMocks();
setMockActions(actions);
setMockValues(values);
});

afterEach(() => {
// Remove the jest mock on createPortal
// @ts-ignore
ReactDOM.createPortal.mockClear();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this ts-ignore and comment here? Do we need it, or can we do

Suggested change
// Remove the jest mock on createPortal
// @ts-ignore
ReactDOM.createPortal.mockClear();
(ReactDOM.createPortal as jest.Mock).mockClear();

or just jest.clearAllMocks 🤷
edit: which we're already doing in beforeEach - can we lose this block entirely?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deleted per previous comment.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

});

it('renders nothing by default', () => {
const logRetentionPanel = shallow(<LogRetentionConfirmationModal />);
expect(logRetentionPanel.isEmptyRender()).toBe(true);
});

describe('analytics', () => {
it('renders the Analytics panel when openedModal is set to Analytics', () => {
setMockValues({
...values,
openedModal: ELogRetentionOptions.Analytics,
});

const logRetentionPanel = shallow(<LogRetentionConfirmationModal />);
expect(
logRetentionPanel.find('[data-test-subj="AnalyticsLogRetentionConfirmationModal"]').length
).toBe(1);
});

it('calls saveLogRetention on save when showing analytics', () => {
setMockValues({
...values,
openedModal: ELogRetentionOptions.Analytics,
});

const logRetentionPanel = shallow(<LogRetentionConfirmationModal />);
const genericConfirmationModal = logRetentionPanel.find(GenericConfirmationModal);
genericConfirmationModal.prop('onSave')();
expect(actions.saveLogRetention).toHaveBeenCalledWith(ELogRetentionOptions.Analytics, false);
});

it('calls closeModals on close', () => {
setMockValues({
...values,
openedModal: ELogRetentionOptions.Analytics,
});

const logRetentionPanel = shallow(<LogRetentionConfirmationModal />);
const genericConfirmationModal = logRetentionPanel.find(GenericConfirmationModal);
genericConfirmationModal.prop('onClose')();
expect(actions.closeModals).toHaveBeenCalled();
});
});

describe('api', () => {
it('renders the API panel when openedModal is set to API', () => {
setMockValues({
...values,
openedModal: ELogRetentionOptions.API,
});

const logRetentionPanel = shallow(<LogRetentionConfirmationModal />);
expect(
logRetentionPanel.find('[data-test-subj="APILogRetentionConfirmationModal"]').length
).toBe(1);
});

it('calls saveLogRetention on save when showing api', () => {
setMockValues({
...values,
openedModal: ELogRetentionOptions.API,
});

const logRetentionPanel = shallow(<LogRetentionConfirmationModal />);
const genericConfirmationModal = logRetentionPanel.find(GenericConfirmationModal);
genericConfirmationModal.prop('onSave')();
expect(actions.saveLogRetention).toHaveBeenCalledWith(ELogRetentionOptions.API, false);
});

it('calls closeModals on close', () => {
setMockValues({
...values,
openedModal: ELogRetentionOptions.API,
});

const logRetentionPanel = shallow(<LogRetentionConfirmationModal />);
const genericConfirmationModal = logRetentionPanel.find(GenericConfirmationModal);
genericConfirmationModal.prop('onClose')();
expect(actions.closeModals).toHaveBeenCalled();
});
});
});
Loading