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

[ML] Data Frame Analytics: Fix scatterplot matrix boilerplate visibility with no fields selected. #96590

Merged
merged 3 commits into from
Apr 12, 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
@@ -0,0 +1,87 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import React from 'react';
import { render, waitFor, screen } from '@testing-library/react';

import { IntlProvider } from 'react-intl';

import euiThemeLight from '@elastic/eui/dist/eui_theme_light.json';

import { ScatterplotMatrix } from './scatterplot_matrix';

const mockEsSearch = jest.fn((body) => ({
hits: { hits: [{ fields: { x: [1], y: [2] } }, { fields: { x: [2], y: [3] } }] },
}));
jest.mock('../../contexts/kibana', () => ({
useMlApiContext: () => ({
esSearch: mockEsSearch,
}),
}));

const mockEuiTheme = euiThemeLight;
jest.mock('../color_range_legend', () => ({
useCurrentEuiTheme: () => ({
euiTheme: mockEuiTheme,
}),
}));

// Mocking VegaChart to avoid a jest/canvas related error
jest.mock('../vega_chart', () => ({
VegaChart: () => <div data-test-subj="mlVegaChart" />,
}));

describe('Data Frame Analytics: <ScatterplotMatrix />', () => {
it('renders the scatterplot matrix wrapper with options but not the chart itself', async () => {
// prepare
render(
<IntlProvider locale="en">
<ScatterplotMatrix
{...{
fields: [],
index: 'the-index-name',
}}
/>
</IntlProvider>
);

// assert
await waitFor(() => {
expect(mockEsSearch).toHaveBeenCalledTimes(0);
// should hide the loading indicator and render the wrapping options boilerplate
expect(screen.queryByTestId('mlScatterplotMatrix loaded')).toBeInTheDocument();
// should not render the scatterplot matrix itself because there's no data items.
expect(screen.queryByTestId('mlVegaChart')).not.toBeInTheDocument();
});
});

it('renders the scatterplot matrix wrapper with options and the chart itself', async () => {
// prepare
render(
<IntlProvider locale="en">
<ScatterplotMatrix
{...{
fields: ['x', 'y'],
index: 'the-index-name',
}}
/>
</IntlProvider>
);

// assert
await waitFor(() => {
expect(mockEsSearch).toHaveBeenCalledWith({
body: { _source: false, fields: ['x', 'y'], from: 0, query: undefined, size: 1000 },
index: 'the-index-name',
});
// should hide the loading indicator and render the wrapping options boilerplate
expect(screen.queryByTestId('mlScatterplotMatrix loaded')).toBeInTheDocument();
// should render the scatterplot matrix.
expect(screen.queryByTestId('mlVegaChart')).toBeInTheDocument();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export const ScatterplotMatrix: FC<ScatterplotMatrixProps> = ({
// are sized according to outlier_score
const [dynamicSize, setDynamicSize] = useState<boolean>(false);

// used to give the use the option to customize the fields used for the matrix axes
// used to give the user the option to customize the fields used for the matrix axes
const [fields, setFields] = useState<string[]>([]);

useEffect(() => {
Expand Down Expand Up @@ -165,7 +165,7 @@ export const ScatterplotMatrix: FC<ScatterplotMatrixProps> = ({

useEffect(() => {
if (fields.length === 0) {
setSplom(undefined);
setSplom({ columns: [], items: [], messages: [] });
setIsLoading(false);
return;
}
Expand Down