Skip to content

Commit

Permalink
split enzyme helpers and rtl helpers into different files and adjust …
Browse files Browse the repository at this point in the history
…types
  • Loading branch information
dominiqueclarke committed Jan 11, 2021
1 parent 48f1536 commit 8f8af46
Show file tree
Hide file tree
Showing 10 changed files with 199 additions and 218 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,26 @@
*/

import React from 'react';
import { render } from '../../../lib';
import { render } from '../../../lib/helper/rtl_helpers';
import { TriggersAndActionsUIPublicPluginStart } from '../../../../../../plugins/triggers_actions_ui/public';
import { MLJobLink } from './ml_job_link';

describe('ML JobLink', () => {
it('renders without errors', () => {
const { asFragment } = render(
<MLJobLink dateRange={{ to: '', from: '' }} basePath="" monitorId="testMonitor" />,
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<{
triggersActionsUi: Partial<TriggersAndActionsUIPublicPluginStart>;
}>(
<MLJobLink dateRange={{ to: '', from: '' }} basePath="" monitorId="testMonitor">
<div>Test link</div>
</MLJobLink>,
{
coreOptions: { triggersActionsUi: { getEditAlertFlyout: jest.fn() } },
core: { triggersActionsUi: { getEditAlertFlyout: jest.fn() } },
}
);
expect(asFragment()).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 @@ -7,7 +7,8 @@
import React from 'react';
import { ExecutedStep } from './executed_step';
import { Ping } from '../../../../common/runtime_types';
import { mountWithRouter, render } from '../../../lib';
import { mountWithRouter } from '../../../lib';
import { render } from '../../../lib/helper/rtl_helpers';

// FLAKY: https://github.com/elastic/kibana/issues/85899
describe.skip('ExecutedStep', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { ChromeBreadcrumb } from 'kibana/public';
import React from 'react';
import { Route } from 'react-router-dom';
import { of } from 'rxjs';
import { render } from '../../../../lib';
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';
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
187 changes: 0 additions & 187 deletions x-pack/plugins/uptime/public/lib/helper/component_test_helpers.tsx

This file was deleted.

68 changes: 68 additions & 0 deletions x-pack/plugins/uptime/public/lib/helper/enzyme_helpers.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* 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, { 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 { MountWithReduxProvider } from './helper_with_redux';
import { AppState } from '../../state';

const helperWithRouter: <R>(
helper: (node: ReactElement) => R,
component: ReactElement,
customHistory?: MemoryHistory,
wrapReduxStore?: boolean,
storeState?: AppState
) => R = (helper, component, customHistory, wrapReduxStore, storeState) => {
const history = customHistory ?? createMemoryHistory();

history.location.key = 'TestKeyForTesting';

const routerWrapper = <Router history={history}>{component}</Router>;

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

return helper(routerWrapper);
};

export const renderWithRouter = (component: ReactElement, customHistory?: MemoryHistory) => {
return helperWithRouter(renderWithIntl, component, customHistory);
};

export const shallowWithRouter = (component: ReactElement, customHistory?: MemoryHistory) => {
return helperWithRouter(shallowWithIntl, component, customHistory);
};

export const mountWithRouter = (component: ReactElement, customHistory?: MemoryHistory) => {
return helperWithRouter(mountWithIntl, component, customHistory);
};

export const renderWithRouterRedux = (component: ReactElement, customHistory?: MemoryHistory) => {
return helperWithRouter(renderWithIntl, component, customHistory, true);
};

export const shallowWithRouterRedux = (component: ReactElement, customHistory?: MemoryHistory) => {
return helperWithRouter(shallowWithIntl, component, customHistory, true);
};

export const mountWithRouterRedux = (
component: ReactElement,
options?: { customHistory?: MemoryHistory; storeState?: AppState }
) => {
return helperWithRouter(
mountWithIntl,
component,
options?.customHistory,
true,
options?.storeState
);
};
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

0 comments on commit 8f8af46

Please sign in to comment.