Skip to content

Commit

Permalink
Merge branch '7.x' into backport/7.x/pr-90482
Browse files Browse the repository at this point in the history
  • Loading branch information
kibanamachine authored Feb 8, 2021
2 parents 30e50c2 + 392ba21 commit 9142bcb
Show file tree
Hide file tree
Showing 14 changed files with 333 additions and 116 deletions.
4 changes: 4 additions & 0 deletions x-pack/plugins/data_enhanced/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import React from 'react';
import moment from 'moment';
import { CoreSetup, CoreStart, Plugin, PluginInitializerContext } from 'src/core/public';
import { DataPublicPluginSetup, DataPublicPluginStart } from '../../../../src/plugins/data/public';
import { BfetchPublicSetup } from '../../../../src/plugins/bfetch/public';
Expand Down Expand Up @@ -86,6 +87,9 @@ export class DataEnhancedPlugin
application: core.application,
timeFilter: plugins.data.query.timefilter.timefilter,
storage: this.storage,
disableSaveAfterSessionCompletesTimeout: moment
.duration(this.config.search.sessions.notTouchedTimeout)
.asMilliseconds(),
})
)
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import React from 'react';
import React, { ReactNode } from 'react';
import { StubBrowserStorage } from '@kbn/test/jest';
import { render, waitFor, screen, act } from '@testing-library/react';
import { Storage } from '../../../../../../../src/plugins/kibana_utils/public/';
Expand All @@ -20,6 +20,8 @@ import {
} from '../../../../../../../src/plugins/data/public';
import { coreMock } from '../../../../../../../src/core/public/mocks';
import { TOUR_RESTORE_STEP_KEY, TOUR_TAKING_TOO_LONG_STEP_KEY } from './search_session_tour';
import userEvent from '@testing-library/user-event';
import { IntlProvider } from 'react-intl';

const coreStart = coreMock.createStart();
const dataStart = dataPluginMock.createStartContract();
Expand All @@ -30,6 +32,12 @@ const timeFilter = dataStart.query.timefilter.timefilter as jest.Mocked<Timefilt
timeFilter.getRefreshIntervalUpdate$.mockImplementation(() => refreshInterval$);
timeFilter.getRefreshInterval.mockImplementation(() => refreshInterval$.getValue());

const disableSaveAfterSessionCompletesTimeout = 5 * 60 * 1000;

function Container({ children }: { children?: ReactNode }) {
return <IntlProvider locale="en">{children}</IntlProvider>;
}

beforeEach(() => {
storage = new Storage(new StubBrowserStorage());
refreshInterval$.next({ value: 0, pause: true });
Expand All @@ -47,8 +55,13 @@ test("shouldn't show indicator in case no active search session", async () => {
application: coreStart.application,
timeFilter,
storage,
disableSaveAfterSessionCompletesTimeout,
});
const { getByTestId, container } = render(<SearchSessionIndicator />);
const { getByTestId, container } = render(
<Container>
<SearchSessionIndicator />
</Container>
);

// make sure `searchSessionIndicator` isn't appearing after some time (lazy-loading)
await expect(
Expand All @@ -69,8 +82,13 @@ test("shouldn't show indicator in case app hasn't opt-in", async () => {
application: coreStart.application,
timeFilter,
storage,
disableSaveAfterSessionCompletesTimeout,
});
const { getByTestId, container } = render(<SearchSessionIndicator />);
const { getByTestId, container } = render(
<Container>
<SearchSessionIndicator />
</Container>
);
sessionService.isSessionStorageReady.mockImplementation(() => false);

// make sure `searchSessionIndicator` isn't appearing after some time (lazy-loading)
Expand All @@ -93,8 +111,13 @@ test('should show indicator in case there is an active search session', async ()
application: coreStart.application,
timeFilter,
storage,
disableSaveAfterSessionCompletesTimeout,
});
const { getByTestId } = render(<SearchSessionIndicator />);
const { getByTestId } = render(
<Container>
<SearchSessionIndicator />
</Container>
);

await waitFor(() => getByTestId('searchSessionIndicator'));
});
Expand All @@ -118,13 +141,20 @@ test('should be disabled in case uiConfig says so ', async () => {
application: coreStart.application,
timeFilter,
storage,
disableSaveAfterSessionCompletesTimeout,
});

render(<SearchSessionIndicator />);
render(
<Container>
<SearchSessionIndicator />
</Container>
);

await waitFor(() => screen.getByTestId('searchSessionIndicator'));

expect(screen.getByTestId('searchSessionIndicator').querySelector('button')).toBeDisabled();
await userEvent.click(screen.getByLabelText('Search session loading'));

expect(screen.getByRole('button', { name: 'Save session' })).toBeDisabled();
});

test('should be disabled during auto-refresh', async () => {
Expand All @@ -135,19 +165,82 @@ test('should be disabled during auto-refresh', async () => {
application: coreStart.application,
timeFilter,
storage,
disableSaveAfterSessionCompletesTimeout,
});

render(<SearchSessionIndicator />);
render(
<Container>
<SearchSessionIndicator />
</Container>
);

await waitFor(() => screen.getByTestId('searchSessionIndicator'));

expect(screen.getByTestId('searchSessionIndicator').querySelector('button')).not.toBeDisabled();
await userEvent.click(screen.getByLabelText('Search session loading'));

expect(screen.getByRole('button', { name: 'Save session' })).not.toBeDisabled();

act(() => {
refreshInterval$.next({ value: 0, pause: false });
});

expect(screen.getByTestId('searchSessionIndicator').querySelector('button')).toBeDisabled();
expect(screen.getByRole('button', { name: 'Save session' })).toBeDisabled();
});

describe('Completed inactivity', () => {
beforeEach(() => {
jest.useFakeTimers();
});
afterEach(() => {
jest.useRealTimers();
});
test('save should be disabled after completed and timeout', async () => {
const state$ = new BehaviorSubject(SearchSessionState.Loading);

const SearchSessionIndicator = createConnectedSearchSessionIndicator({
sessionService: { ...sessionService, state$ },
application: coreStart.application,
timeFilter,
storage,
disableSaveAfterSessionCompletesTimeout,
});

render(
<Container>
<SearchSessionIndicator />
</Container>
);

await waitFor(() => screen.getByTestId('searchSessionIndicator'));

await userEvent.click(screen.getByLabelText('Search session loading'));

expect(screen.getByRole('button', { name: 'Save session' })).not.toBeDisabled();

act(() => {
jest.advanceTimersByTime(5 * 60 * 1000);
});

expect(screen.getByRole('button', { name: 'Save session' })).not.toBeDisabled();

act(() => {
state$.next(SearchSessionState.Completed);
});

expect(screen.getByRole('button', { name: 'Save session' })).not.toBeDisabled();

act(() => {
jest.advanceTimersByTime(2.5 * 60 * 1000);
});

expect(screen.getByRole('button', { name: 'Save session' })).not.toBeDisabled();

act(() => {
jest.advanceTimersByTime(2.5 * 60 * 1000);
});

expect(screen.getByRole('button', { name: 'Save session' })).toBeDisabled();
});
});

describe('tour steps', () => {
Expand All @@ -167,8 +260,13 @@ describe('tour steps', () => {
application: coreStart.application,
timeFilter,
storage,
disableSaveAfterSessionCompletesTimeout,
});
const rendered = render(<SearchSessionIndicator />);
const rendered = render(
<Container>
<SearchSessionIndicator />
</Container>
);

await waitFor(() => rendered.getByTestId('searchSessionIndicator'));

Expand Down Expand Up @@ -199,8 +297,13 @@ describe('tour steps', () => {
application: coreStart.application,
timeFilter,
storage,
disableSaveAfterSessionCompletesTimeout,
});
const rendered = render(<SearchSessionIndicator />);
const rendered = render(
<Container>
<SearchSessionIndicator />
</Container>
);

const searchSessionIndicator = await rendered.findByTestId('searchSessionIndicator');
expect(searchSessionIndicator).toBeTruthy();
Expand All @@ -225,8 +328,13 @@ describe('tour steps', () => {
application: coreStart.application,
timeFilter,
storage,
disableSaveAfterSessionCompletesTimeout,
});
const rendered = render(<SearchSessionIndicator />);
const rendered = render(
<Container>
<SearchSessionIndicator />
</Container>
);

await waitFor(() => rendered.getByTestId('searchSessionIndicator'));
expect(screen.getByTestId('searchSessionIndicatorPopoverContainer')).toBeInTheDocument();
Expand All @@ -242,8 +350,13 @@ describe('tour steps', () => {
application: coreStart.application,
timeFilter,
storage,
disableSaveAfterSessionCompletesTimeout,
});
const rendered = render(<SearchSessionIndicator />);
const rendered = render(
<Container>
<SearchSessionIndicator />
</Container>
);

await waitFor(() => rendered.getByTestId('searchSessionIndicator'));

Expand Down
Loading

0 comments on commit 9142bcb

Please sign in to comment.