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

[Uptime] Tests/uptime testing utils #87650

Merged
Merged
Show file tree
Hide file tree
Changes from 14 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

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,20 @@
*/

import React from 'react';
import { coreMock } from 'src/core/public/mocks';
import { renderWithRouter, shallowWithRouter } from '../../../lib';
import { render } from '../../../lib/helper/rtl_helpers';
import { MLJobLink } from './ml_job_link';
import { KibanaContextProvider } from '../../../../../../../src/plugins/kibana_react/public';

const core = coreMock.createStart();
describe('ML JobLink', () => {
it('shallow renders without errors', () => {
const wrapper = shallowWithRouter(
<MLJobLink dateRange={{ to: '', from: '' }} basePath="" monitorId="testMonitor" />
);
expect(wrapper).toMatchSnapshot();
});

it('renders without errors', () => {
const wrapper = renderWithRouter(
<KibanaContextProvider
services={{ ...core, triggersActionsUi: { getEditAlertFlyout: jest.fn() } }}
>
<MLJobLink dateRange={{ to: '', from: '' }} basePath="" monitorId="testMonitor" />
</KibanaContextProvider>
const expectedHref = `/app/ml#/explorer?_g=(ml:(jobIds:!(testmonitor_high_latency_by_geo)),refreshInterval:(pause:!t,value:0),time:(from:'',to:''))&_a=(mlExplorerFilter:(filterActive:!t,filteredFields:!(monitor.id,testMonitor)),mlExplorerSwimlane:(viewByFieldName:observer.geo.name))`;
const { getByRole, getByText } = render(
<MLJobLink dateRange={{ to: '', from: '' }} basePath="" monitorId="testMonitor">
<div>Test link</div>
</MLJobLink>
);
expect(wrapper).toMatchSnapshot();

const jobLink = getByRole('link');
expect(jobLink.getAttribute('href')).toBe(expectedHref);
expect(getByText('Test link'));
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import React from 'react';
import { ExecutedStep } from './executed_step';
import { Ping } from '../../../../common/runtime_types';
import { mountWithRouter } from '../../../lib';
import { render } from '../../../lib/helper/rtl_helpers';

// FLAKY: https://github.com/elastic/kibana/issues/85899
describe.skip('ExecutedStep', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

We might want to cherry-pick this change to a separate branch and revert it here, since we're going to need to get approval to close the issue before we can remove the skip. I'm fine with keeping the test revision below since they're all skipped in master right now anyway.

Expand Down Expand Up @@ -35,32 +36,9 @@ describe.skip('ExecutedStep', () => {
});

it('renders correct step heading', () => {
expect(
mountWithRouter(<ExecutedStep index={3} step={step} checkGroup={'fake-group'} />).find(
'EuiText'
)
).toMatchInlineSnapshot(`
<EuiText>
<div
className="euiText euiText--medium"
>
<strong>
<FormattedMessage
defaultMessage="{stepNumber}. {stepName}"
id="xpack.uptime.synthetics.executedStep.stepName"
values={
Object {
"stepName": "STEP_NAME",
"stepNumber": 4,
}
}
>
4. STEP_NAME
</FormattedMessage>
</strong>
</div>
</EuiText>
`);
const { getByText } = render(<ExecutedStep index={3} step={step} checkGroup={'fake-group'} />);

expect(getByText(`${step?.synthetics?.step?.index}. ${step?.synthetics?.step?.name}`));
Copy link
Contributor

Choose a reason for hiding this comment

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

This is exactly what I was hoping to achieve when we talked about this test. It tests the same thing in a much cleaner format.

});

it('renders a link to the step detail view', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import { ChromeBreadcrumb } from 'kibana/public';
import React from 'react';
import { Route } from 'react-router-dom';
import { of } from 'rxjs';
import { MountWithReduxProvider, mountWithRouter } from '../../../../lib';
import { KibanaContextProvider } from '../../../../../../../../src/plugins/kibana_react/public';
import { render } from '../../../../lib/helper/rtl_helpers';
import { useMonitorBreadcrumb } from './use_monitor_breadcrumb';
import { OVERVIEW_ROUTE } from '../../../../../common/constants';
import { Ping } from '../../../../../common/runtime_types/ping';
import { JourneyState } from '../../../../state/reducers/journey';
import { chromeServiceMock } from 'src/core/public/mocks';

describe('useMonitorBreadcrumbs', () => {
it('sets the given breadcrumbs', () => {
Expand All @@ -27,14 +27,11 @@ describe('useMonitorBreadcrumbs', () => {
return <>Step Water Fall</>;
};

mountWithRouter(
<MountWithReduxProvider>
<KibanaContextProvider services={{ ...core }}>
<Route path={OVERVIEW_ROUTE}>
<Component />
</Route>
</KibanaContextProvider>
</MountWithReduxProvider>
render(
<Route path={OVERVIEW_ROUTE}>
<Component />
</Route>,
{ kibanaProps: { services: { ...core } } }
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is an example of overwriting the entire Kibana context core. This is useful in this context to get access to a function for getting the breadcrumbs out of core.

);

expect(getBreadcrumbs()).toMatchInlineSnapshot(`
Expand Down Expand Up @@ -68,6 +65,7 @@ const mockCore: () => [() => ChromeBreadcrumb[], any] = () => {
navigateToUrl: jest.fn(),
},
chrome: {
...chromeServiceMock.createStartContract,
setBreadcrumbs: (newBreadcrumbs: ChromeBreadcrumb[]) => {
breadcrumbObj = newBreadcrumbs;
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
*/

import { DYNAMIC_SETTINGS_DEFAULTS } from '../../../common/constants';
import { AppState } from '../../state';

/**
* NOTE: This variable name MUST start with 'mock*' in order for
* Jest to accept its use within a jest.mock()
*/
export const mockStore = {
export const mockState: AppState = {
overviewFilters: {
filters: {
locations: [],
Expand Down Expand Up @@ -111,8 +112,11 @@ export const mockStore = {
alerts: {
alertDeletion: { data: null, loading: false },
anomalyAlert: { data: null, loading: false },
anomalyAlertDeletion: { data: null, loading: false },
alerts: { data: null, loading: false },
connectors: { data: null, loading: false },
newAlert: { data: null, loading: false },
},
journeys: {},
networkEvents: {},
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
*/

import React, { ReactElement } from 'react';

import { Router } from 'react-router-dom';
import { MemoryHistory } from 'history/createMemoryHistory';
import { createMemoryHistory } from 'history';
import { mountWithIntl, renderWithIntl, shallowWithIntl } from '@kbn/test/jest';
import { AppState } from '../../state';
import { MountWithReduxProvider } from './helper_with_redux';
import { AppState } from '../../state';

const helperWithRouter: <R>(
helper: (node: ReactElement) => R,
Expand All @@ -28,7 +27,7 @@ const helperWithRouter: <R>(

if (wrapReduxStore) {
return helper(
<MountWithReduxProvider store={storeState}>{routerWrapper}</MountWithReduxProvider>
<MountWithReduxProvider state={storeState}>{routerWrapper}</MountWithReduxProvider>
);
}

Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/uptime/public/lib/helper/helper_with_redux.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import React from 'react';
import { Provider as ReduxProvider } from 'react-redux';
import { AppState } from '../../state';

export const MountWithReduxProvider: React.FC<{ store?: AppState }> = ({ children, store }) => (
export const MountWithReduxProvider: React.FC<{ state?: AppState }> = ({ children, state }) => (
<ReduxProvider
store={{
dispatch: jest.fn(),
getState: jest.fn().mockReturnValue(store || { selectedFilters: null }),
getState: jest.fn().mockReturnValue(state || { selectedFilters: null }),
subscribe: jest.fn(),
replaceReducer: jest.fn(),
}}
Expand Down
Loading