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

[SIEM][Case] Merge header components #57816

Merged
merged 23 commits into from
Feb 19, 2020
Merged
Show file tree
Hide file tree
Changes from 22 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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
/*
* 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 { shallow } from 'enzyme';
import React from 'react';

import { TestProviders } from '../../mock';
import { EditableTitle } from './editable_title';
import { useMountAppended } from '../../utils/use_mount_appended';

describe('EditableTitle', () => {
const mount = useMountAppended();
const submitTitle = jest.fn();

test('it renders', () => {
const wrapper = shallow(
<EditableTitle title="Test title" onSubmit={submitTitle} isLoading={false} />
);

expect(wrapper).toMatchSnapshot();
});

test('it shows the edit title input field', () => {
const wrapper = mount(
<TestProviders>
<EditableTitle title="Test title" onSubmit={submitTitle} isLoading={false} />
</TestProviders>
);

wrapper.find('button[data-test-subj="editable-title-edit-icon"]').simulate('click');
wrapper.update();

expect(
wrapper
.find('[data-test-subj="editable-title-input-field"]')
.first()
.exists()
).toBe(true);
});

test('it shows the submit button', () => {
const wrapper = mount(
<TestProviders>
<EditableTitle title="Test title" onSubmit={submitTitle} isLoading={false} />
</TestProviders>
);

wrapper.find('button[data-test-subj="editable-title-edit-icon"]').simulate('click');
wrapper.update();

expect(
wrapper
.find('[data-test-subj="editable-title-submit-btn"]')
.first()
.exists()
).toBe(true);
});

test('it shows the cancel button', () => {
const wrapper = mount(
<TestProviders>
<EditableTitle title="Test title" onSubmit={submitTitle} isLoading={false} />
</TestProviders>
);

wrapper.find('button[data-test-subj="editable-title-edit-icon"]').simulate('click');
wrapper.update();

expect(
wrapper
.find('[data-test-subj="editable-title-cancel-btn"]')
.first()
.exists()
).toBe(true);
});

test('it DOES NOT shows the edit icon when in edit mode', () => {
const wrapper = mount(
<TestProviders>
<EditableTitle title="Test title" onSubmit={submitTitle} isLoading={false} />
</TestProviders>
);

wrapper.find('button[data-test-subj="editable-title-edit-icon"]').simulate('click');
wrapper.update();

expect(
wrapper
.find('[data-test-subj="editable-title-edit-icon"]')
.first()
.exists()
).toBe(false);
});

test('it switch to non edit mode when canceled', () => {
const wrapper = mount(
<TestProviders>
<EditableTitle title="Test title" onSubmit={submitTitle} isLoading={false} />
</TestProviders>
);

wrapper.find('button[data-test-subj="editable-title-edit-icon"]').simulate('click');
wrapper.update();
wrapper.find('button[data-test-subj="editable-title-cancel-btn"]').simulate('click');

expect(
wrapper
.find('[data-test-subj="editable-title-edit-icon"]')
.first()
.exists()
).toBe(true);
});

test('it should change the title', () => {
const newTitle = 'new test title';

const wrapper = mount(
<TestProviders>
<EditableTitle title="Test title" onSubmit={submitTitle} isLoading={false} />
</TestProviders>
);

wrapper.find('button[data-test-subj="editable-title-edit-icon"]').simulate('click');
wrapper.update();

wrapper
.find('input[data-test-subj="editable-title-input-field"]')
.simulate('change', { target: { value: newTitle } });

wrapper.update();

expect(
wrapper.find('input[data-test-subj="editable-title-input-field"]').prop('value')
).toEqual(newTitle);
});

cnasikas marked this conversation as resolved.
Show resolved Hide resolved
test('it should NOT change the title when cancel', () => {
const title = 'Test title';
const newTitle = 'new test title';

const wrapper = mount(
<TestProviders>
<EditableTitle title={title} onSubmit={submitTitle} isLoading={false} />
</TestProviders>
);

wrapper.find('button[data-test-subj="editable-title-edit-icon"]').simulate('click');
wrapper.update();

wrapper
.find('input[data-test-subj="editable-title-input-field"]')
.simulate('change', { target: { value: newTitle } });
wrapper.update();

wrapper.find('button[data-test-subj="editable-title-cancel-btn"]').simulate('click');
wrapper.update();

expect(wrapper.find('h1[data-test-subj="header-page-title"]').text()).toEqual(title);
});

test('it submits the title', () => {
const newTitle = 'new test title';

const wrapper = mount(
<TestProviders>
<EditableTitle title="Test title" onSubmit={submitTitle} isLoading={false} />
</TestProviders>
);

wrapper.find('button[data-test-subj="editable-title-edit-icon"]').simulate('click');
wrapper.update();

wrapper
.find('input[data-test-subj="editable-title-input-field"]')
.simulate('change', { target: { value: newTitle } });

wrapper.find('button[data-test-subj="editable-title-submit-btn"]').simulate('click');
wrapper.update();

expect(submitTitle).toHaveBeenCalled();
expect(submitTitle.mock.calls[0][0]).toEqual(newTitle);
cnasikas marked this conversation as resolved.
Show resolved Hide resolved
expect(
wrapper
.find('[data-test-subj="editable-title-edit-icon"]')
.first()
.exists()
).toBe(true);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* 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, { useState, useCallback } from 'react';
import styled, { css } from 'styled-components';

import {
EuiButton,
EuiButtonEmpty,
EuiFlexGroup,
EuiFlexItem,
EuiFieldText,
EuiButtonIcon,
} from '@elastic/eui';

import * as i18n from './translations';

import { Title } from './title';

const StyledEuiButtonIcon = styled(EuiButtonIcon)`
${({ theme }) => css`
margin-left: ${theme.eui.euiSize};
`}
`;

StyledEuiButtonIcon.displayName = 'StyledEuiButtonIcon';

interface Props {
isLoading: boolean;
title: string | React.ReactNode;
onSubmit: (title: string) => void;
}

const EditableTitleComponent: React.FC<Props> = ({ onSubmit, isLoading, title }) => {
const [editMode, setEditMode] = useState(false);
const [changedTitle, onTitleChange] = useState(title);

const onCancel = useCallback(() => setEditMode(false), []);
const onClickEditIcon = useCallback(() => setEditMode(true), []);

const onClickSubmit = useCallback(
(newTitle: string): void => {
onSubmit(newTitle);
setEditMode(false);
},
[changedTitle]
);

return editMode ? (
<EuiFlexGroup alignItems="center" gutterSize="m" justifyContent="spaceBetween">
<EuiFlexItem grow={false}>
<EuiFieldText
onChange={e => onTitleChange(e.target.value)}
value={`${changedTitle}`}
data-test-subj="editable-title-input-field"
/>
</EuiFlexItem>
<EuiFlexGroup gutterSize="none" responsive={false} wrap={true}>
<EuiFlexItem grow={false}>
<EuiButton
fill
isDisabled={isLoading}
isLoading={isLoading}
onClick={() => onClickSubmit(changedTitle as string)}
data-test-subj="editable-title-submit-btn"
>
{i18n.SUBMIT}
</EuiButton>
</EuiFlexItem>
<EuiFlexItem grow={false}>
<EuiButtonEmpty onClick={onCancel} data-test-subj="editable-title-cancel-btn">
{i18n.CANCEL}
</EuiButtonEmpty>
</EuiFlexItem>
</EuiFlexGroup>
<EuiFlexItem />
</EuiFlexGroup>
) : (
<EuiFlexGroup alignItems="center" gutterSize="none">
<EuiFlexItem grow={false}>
<Title title={title} />
</EuiFlexItem>
<EuiFlexItem grow={false}>
<StyledEuiButtonIcon
aria-label={i18n.EDIT_TITLE_ARIA(title as string)}
iconType="pencil"
onClick={onClickEditIcon}
data-test-subj="editable-title-edit-icon"
/>
</EuiFlexItem>
</EuiFlexGroup>
);
};

export const EditableTitle = React.memo(EditableTitleComponent);
Loading