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

[8.0] [Exploratory View] series name enter duplicate series (#120528) #120602

Merged
merged 1 commit into from
Dec 7, 2021
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 @@ -112,7 +112,7 @@ const HIDE_SERIES_LABEL = i18n.translate('xpack.observability.seriesEditor.hide'
});

const COPY_SERIES_LABEL = i18n.translate('xpack.observability.seriesEditor.clone', {
defaultMessage: 'Copy series',
defaultMessage: 'Duplicate series',
});

const VIEW_SAMPLE_DOCUMENTS_LABEL = i18n.translate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,17 @@

import React from 'react';
import { fireEvent, screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { mockUxSeries, render } from '../../rtl_helpers';
import { SeriesName } from './series_name';

describe.skip('SeriesChartTypesSelect', function () {
// ensures that fields appropriately match to their label
jest.mock('@elastic/eui/lib/services/accessibility/html_id_generator', () => ({
...jest.requireActual('@elastic/eui/lib/services/accessibility/html_id_generator'),
htmlIdGenerator: () => () => `id-${Math.random()}`,
}));

describe('SeriesName', function () {
it('should render properly', async function () {
render(<SeriesName seriesId={0} series={mockUxSeries} />);

Expand All @@ -20,7 +27,7 @@ describe.skip('SeriesChartTypesSelect', function () {
it('should display input when editing name', async function () {
render(<SeriesName seriesId={0} series={mockUxSeries} />);

let input = screen.queryByLabelText(mockUxSeries.name);
let input = screen.queryByTestId('exploratoryViewSeriesNameInput') as HTMLInputElement;

// read only
expect(input).not.toBeInTheDocument();
Expand All @@ -30,17 +37,52 @@ describe.skip('SeriesChartTypesSelect', function () {
fireEvent.click(editButton);

await waitFor(() => {
input = screen.getByLabelText(mockUxSeries.name);
input = screen.getByTestId('exploratoryViewSeriesNameInput') as HTMLInputElement;

expect(input).toBeInTheDocument();
expect(input.value).toBe(mockUxSeries.name);
});

// toggle readonly
fireEvent.click(editButton);

await waitFor(() => {
input = screen.getByLabelText(mockUxSeries.name);
input = screen.queryByTestId('exploratoryViewSeriesNameInput') as HTMLInputElement;

expect(screen.getByText(mockUxSeries.name)).toBeInTheDocument();
expect(input).not.toBeInTheDocument();
});
});

it('should save name on enter key', async function () {
const newName = '-test-new-name';
render(<SeriesName seriesId={0} series={mockUxSeries} />);

let input = screen.queryByTestId('exploratoryViewSeriesNameInput') as HTMLInputElement;

// read only
expect(input).not.toBeInTheDocument();

const editButton = screen.getByRole('button');
// toggle editing
userEvent.click(editButton);

await waitFor(() => {
input = screen.getByTestId('exploratoryViewSeriesNameInput') as HTMLInputElement;

expect(input).toBeInTheDocument();
});

userEvent.click(input);
userEvent.type(input, newName);

// submit
userEvent.keyboard('{enter}');

await waitFor(() => {
input = screen.queryByTestId('exploratoryViewSeriesNameInput') as HTMLInputElement;

expect(screen.getByText(`${mockUxSeries.name}${newName}`)).toBeInTheDocument();
expect(input).not.toBeInTheDocument();
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import React, { useState, ChangeEvent, useEffect, useRef } from 'react';
import React, { useState, ChangeEvent, useEffect, useRef, KeyboardEventHandler } from 'react';
import styled from 'styled-components';
import { i18n } from '@kbn/i18n';
import {
Expand Down Expand Up @@ -57,6 +57,12 @@ export function SeriesName({ series, seriesId }: Props) {
}
};

const onKeyDown: KeyboardEventHandler<HTMLInputElement> = (event) => {
if (event.key === 'Enter') {
setIsEditingEnabled(false);
}
};

useEffect(() => {
setValue(series.name);
}, [series.name]);
Expand All @@ -75,12 +81,14 @@ export function SeriesName({ series, seriesId }: Props) {
<EuiFieldText
value={value}
onChange={onChange}
fullWidth
onBlur={onSave}
onKeyDown={onKeyDown}
fullWidth
inputRef={inputRef}
aria-label={i18n.translate('xpack.observability.expView.seriesEditor.seriesName', {
defaultMessage: 'Series name',
})}
data-test-subj="exploratoryViewSeriesNameInput"
/>
</EuiOutsideClickDetector>
</EuiFlexItem>
Expand Down