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

Backport/8.0/pr 117941 #118414

Merged
merged 2 commits into from
Nov 11, 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
10 changes: 9 additions & 1 deletion packages/kbn-test/src/jest/utils/testbed/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,12 @@
*/

export { registerTestBed } from './testbed';
export type { TestBed, TestBedConfig, SetupFunc, UnwrapPromise } from './types';
export type {
TestBed,
TestBedConfig,
AsyncTestBedConfig,
SetupFunc,
UnwrapPromise,
SyncSetupFunc,
AsyncSetupFunc,
} from './types';
31 changes: 24 additions & 7 deletions packages/kbn-test/src/jest/utils/testbed/testbed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,14 @@ import {
mountComponentAsync,
getJSXComponentWithProps,
} from './mount_component';
import { TestBedConfig, TestBed, SetupFunc } from './types';
import {
TestBedConfig,
AsyncTestBedConfig,
TestBed,
SetupFunc,
SyncSetupFunc,
AsyncSetupFunc,
} from './types';

const defaultConfig: TestBedConfig = {
defaultProps: {},
Expand Down Expand Up @@ -48,10 +55,18 @@ const defaultConfig: TestBedConfig = {
});
```
*/
export const registerTestBed = <T extends string = string>(
export function registerTestBed<T extends string = string>(
Component: ComponentType<any>,
config: AsyncTestBedConfig
): AsyncSetupFunc<T>;
export function registerTestBed<T extends string = string>(
Component: ComponentType<any>,
config?: TestBedConfig
): SetupFunc<T> => {
): SyncSetupFunc<T>;
export function registerTestBed<T extends string = string>(
Component: ComponentType<any>,
config?: AsyncTestBedConfig | TestBedConfig
): SetupFunc<T> {
const {
defaultProps = defaultConfig.defaultProps,
memoryRouter = defaultConfig.memoryRouter!,
Expand Down Expand Up @@ -188,7 +203,7 @@ export const registerTestBed = <T extends string = string>(
value,
isAsync = false
) => {
const formInput = typeof input === 'string' ? find(input) : (input as ReactWrapper);
const formInput = typeof input === 'string' ? find(input) : input;

if (!formInput.length) {
throw new Error(`Input "${input}" was not found.`);
Expand All @@ -207,7 +222,7 @@ export const registerTestBed = <T extends string = string>(
value,
doUpdateComponent = true
) => {
const formSelect = typeof select === 'string' ? find(select) : (select as ReactWrapper);
const formSelect = typeof select === 'string' ? find(select) : select;

if (!formSelect.length) {
throw new Error(`Select "${select}" was not found.`);
Expand Down Expand Up @@ -314,7 +329,7 @@ export const registerTestBed = <T extends string = string>(
router.history.push(url);
};

return {
const testBed: TestBed<T> = {
component,
exists,
find,
Expand All @@ -336,8 +351,10 @@ export const registerTestBed = <T extends string = string>(
navigateTo,
},
};

return testBed;
}
};

return setup;
};
}
19 changes: 15 additions & 4 deletions packages/kbn-test/src/jest/utils/testbed/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@
*/

import { Store } from 'redux';
import { ReactWrapper } from 'enzyme';
import { ReactWrapper as GenericReactWrapper } from 'enzyme';
import { LocationDescriptor } from 'history';

export type AsyncSetupFunc<T> = (props?: any) => Promise<TestBed<T>>;
export type SyncSetupFunc<T> = (props?: any) => TestBed<T>;
export type SetupFunc<T> = (props?: any) => TestBed<T> | Promise<TestBed<T>>;
export type ReactWrapper = GenericReactWrapper<any>;

export interface EuiTableMetaData {
/** Array of rows of the table. Each row exposes its reactWrapper and its columns */
Expand Down Expand Up @@ -51,7 +54,7 @@ export interface TestBed<T = string> {
find('myForm.nameInput');
```
*/
find: (testSubject: T, reactWrapper?: ReactWrapper) => ReactWrapper<any>;
find: (testSubject: T, reactWrapper?: ReactWrapper) => ReactWrapper;
/**
* Update the props of the mounted component
*
Expand Down Expand Up @@ -147,15 +150,23 @@ export interface TestBed<T = string> {
};
}

export interface TestBedConfig {
export interface BaseTestBedConfig {
/** The default props to pass to the mounted component. */
defaultProps?: Record<string, any>;
/** Configuration object for the react-router `MemoryRouter. */
memoryRouter?: MemoryRouterConfig;
/** An optional redux store. You can also provide a function that returns a store. */
store?: (() => Store) | Store | null;
}

export interface AsyncTestBedConfig extends BaseTestBedConfig {
/* Mount the component asynchronously. When using "hooked" components with _useEffect()_ calls, you need to set this to "true". */
doMountAsync: true;
}

export interface TestBedConfig extends BaseTestBedConfig {
/* Mount the component asynchronously. When using "hooked" components with _useEffect()_ calls, you need to set this to "true". */
doMountAsync?: boolean;
doMountAsync?: false;
}

export interface MemoryRouterConfig {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { act } from 'react-dom/test-utils';
import { ReactWrapper } from 'enzyme';

import { EuiDescriptionListDescription } from '@elastic/eui';
import { registerTestBed, TestBed, TestBedConfig, findTestSubject } from '@kbn/test/jest';
import { registerTestBed, TestBed, AsyncTestBedConfig, findTestSubject } from '@kbn/test/jest';
import { DataStream } from '../../../common';
import { IndexManagementHome } from '../../../public/application/sections/home';
import { indexManagementStore } from '../../../public/application/store';
Expand Down Expand Up @@ -42,7 +42,7 @@ export interface DataStreamsTabTestBed extends TestBed<TestSubjects> {
}

export const setup = async (overridingDependencies: any = {}): Promise<DataStreamsTabTestBed> => {
const testBedConfig: TestBedConfig = {
const testBedConfig: AsyncTestBedConfig = {
store: () => indexManagementStore(services as any),
memoryRouter: {
initialEntries: [`/indices`],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
* 2.0.
*/

import { registerTestBed, TestBed, TestBedConfig } from '@kbn/test/jest';
import { registerTestBed, TestBed, AsyncTestBedConfig } from '@kbn/test/jest';
import { IndexManagementHome } from '../../../public/application/sections/home';
import { indexManagementStore } from '../../../public/application/store';
import { WithAppDependencies, services, TestSubjects } from '../helpers';

const testBedConfig: TestBedConfig = {
const testBedConfig: AsyncTestBedConfig = {
store: () => indexManagementStore(services as any),
memoryRouter: {
initialEntries: [`/indices?includeHidden=true`],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@

import { act } from 'react-dom/test-utils';

import { registerTestBed, TestBed, TestBedConfig, findTestSubject } from '@kbn/test/jest';
import { registerTestBed, TestBed, AsyncTestBedConfig, findTestSubject } from '@kbn/test/jest';
import { TemplateList } from '../../../public/application/sections/home/template_list';
import { TemplateDeserialized } from '../../../common';
import { WithAppDependencies, TestSubjects } from '../helpers';

const testBedConfig: TestBedConfig = {
const testBedConfig: AsyncTestBedConfig = {
memoryRouter: {
initialEntries: [`/templates`],
componentRoutePath: `/templates/:templateName?`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
import { act } from 'react-dom/test-utils';
import { ReactWrapper } from 'enzyme';

import { registerTestBed, TestBed, TestBedConfig, findTestSubject } from '@kbn/test/jest';
import { registerTestBed, TestBed, AsyncTestBedConfig, findTestSubject } from '@kbn/test/jest';
import { IndexManagementHome } from '../../../public/application/sections/home';
import { indexManagementStore } from '../../../public/application/store';
import { WithAppDependencies, services, TestSubjects } from '../helpers';

const testBedConfig: TestBedConfig = {
const testBedConfig: AsyncTestBedConfig = {
store: () => indexManagementStore(services as any),
memoryRouter: {
initialEntries: [`/indices?includeHiddenIndices=true`],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
* 2.0.
*/

import { registerTestBed, TestBedConfig } from '@kbn/test/jest';
import { registerTestBed, AsyncTestBedConfig } from '@kbn/test/jest';
import { TemplateClone } from '../../../public/application/sections/template_clone';
import { WithAppDependencies } from '../helpers';

import { formSetup } from './template_form.helpers';
import { TEMPLATE_NAME } from './constants';

const testBedConfig: TestBedConfig = {
const testBedConfig: AsyncTestBedConfig = {
memoryRouter: {
initialEntries: [`/clone_template/${TEMPLATE_NAME}`],
componentRoutePath: `/clone_template/:name`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { registerTestBed, TestBedConfig } from '@kbn/test/jest';
import { registerTestBed, AsyncTestBedConfig } from '@kbn/test/jest';
import { TemplateCreate } from '../../../public/application/sections/template_create';
import { WithAppDependencies } from '../helpers';

Expand All @@ -16,7 +16,7 @@ export const setup: any = (isLegacy: boolean = false) => {
? { pathname: '/create_template', search: '?legacy=true' }
: { pathname: '/create_template' };

const testBedConfig: TestBedConfig = {
const testBedConfig: AsyncTestBedConfig = {
memoryRouter: {
initialEntries: [route],
componentRoutePath: route,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
* 2.0.
*/

import { registerTestBed, TestBedConfig } from '@kbn/test/jest';
import { registerTestBed, AsyncTestBedConfig } from '@kbn/test/jest';
import { TemplateEdit } from '../../../public/application/sections/template_edit';
import { WithAppDependencies } from '../helpers';

import { formSetup, TestSubjects } from './template_form.helpers';
import { TEMPLATE_NAME } from './constants';

const testBedConfig: TestBedConfig = {
const testBedConfig: AsyncTestBedConfig = {
memoryRouter: {
initialEntries: [`/edit_template/${TEMPLATE_NAME}`],
componentRoutePath: `/edit_template/:name`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { registerTestBed, TestBed, TestBedConfig } from '@kbn/test/jest';
import { registerTestBed, TestBed, AsyncTestBedConfig } from '@kbn/test/jest';
import { BASE_PATH } from '../../../../../../../common';
import { ComponentTemplateCreate } from '../../../component_template_wizard';

Expand All @@ -19,7 +19,7 @@ export type ComponentTemplateCreateTestBed = TestBed<ComponentTemplateFormTestSu
actions: ReturnType<typeof getFormActions>;
};

const testBedConfig: TestBedConfig = {
const testBedConfig: AsyncTestBedConfig = {
memoryRouter: {
initialEntries: [`${BASE_PATH}/create_component_template`],
componentRoutePath: `${BASE_PATH}/create_component_template`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { registerTestBed, TestBed, TestBedConfig } from '@kbn/test/jest';
import { registerTestBed, TestBed, AsyncTestBedConfig } from '@kbn/test/jest';
import { BASE_PATH } from '../../../../../../../common';
import { ComponentTemplateEdit } from '../../../component_template_wizard';

Expand All @@ -19,7 +19,7 @@ export type ComponentTemplateEditTestBed = TestBed<ComponentTemplateFormTestSubj
actions: ReturnType<typeof getFormActions>;
};

const testBedConfig: TestBedConfig = {
const testBedConfig: AsyncTestBedConfig = {
memoryRouter: {
initialEntries: [`${BASE_PATH}/edit_component_template/comp-1`],
componentRoutePath: `${BASE_PATH}/edit_component_template/:name`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@

import { act } from 'react-dom/test-utils';

import { registerTestBed, TestBed, TestBedConfig, findTestSubject, nextTick } from '@kbn/test/jest';
import {
registerTestBed,
TestBed,
AsyncTestBedConfig,
findTestSubject,
nextTick,
} from '@kbn/test/jest';
import { BASE_PATH } from '../../../../../../../common';
import { WithAppDependencies } from './setup_environment';
import { ComponentTemplateList } from '../../../component_template_list/component_template_list';

const testBedConfig: TestBedConfig = {
const testBedConfig: AsyncTestBedConfig = {
memoryRouter: {
initialEntries: [`${BASE_PATH}component_templates`],
componentRoutePath: `${BASE_PATH}component_templates`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { registerTestBed, TestBedConfig, TestBed } from '@kbn/test/jest';
import { registerTestBed, AsyncTestBedConfig, TestBed } from '@kbn/test/jest';
import { PipelinesClone } from '../../../public/application/sections/pipelines_clone';
import { getFormActions, PipelineFormTestSubjects } from './pipeline_form.helpers';
import { WithAppDependencies } from './setup_environment';
Expand All @@ -28,7 +28,7 @@ export const PIPELINE_TO_CLONE = {
],
};

const testBedConfig: TestBedConfig = {
const testBedConfig: AsyncTestBedConfig = {
memoryRouter: {
initialEntries: [getClonePath({ clonedPipelineName: PIPELINE_TO_CLONE.name })],
componentRoutePath: ROUTES.clone,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { registerTestBed, TestBedConfig, TestBed } from '@kbn/test/jest';
import { registerTestBed, AsyncTestBedConfig, TestBed } from '@kbn/test/jest';
import { PipelinesCreate } from '../../../public/application/sections/pipelines_create';
import { getFormActions, PipelineFormTestSubjects } from './pipeline_form.helpers';
import { WithAppDependencies } from './setup_environment';
Expand All @@ -15,7 +15,7 @@ export type PipelinesCreateTestBed = TestBed<PipelineFormTestSubjects> & {
actions: ReturnType<typeof getFormActions>;
};

const testBedConfig: TestBedConfig = {
const testBedConfig: AsyncTestBedConfig = {
memoryRouter: {
initialEntries: [getCreatePath()],
componentRoutePath: ROUTES.create,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { registerTestBed, TestBedConfig, TestBed } from '@kbn/test/jest';
import { registerTestBed, AsyncTestBedConfig, TestBed } from '@kbn/test/jest';
import { PipelinesEdit } from '../../../public/application/sections/pipelines_edit';
import { getFormActions, PipelineFormTestSubjects } from './pipeline_form.helpers';
import { WithAppDependencies } from './setup_environment';
Expand All @@ -28,7 +28,7 @@ export const PIPELINE_TO_EDIT = {
],
};

const testBedConfig: TestBedConfig = {
const testBedConfig: AsyncTestBedConfig = {
memoryRouter: {
initialEntries: [getEditPath({ pipelineName: PIPELINE_TO_EDIT.name })],
componentRoutePath: ROUTES.edit,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@

import { act } from 'react-dom/test-utils';

import { registerTestBed, TestBed, TestBedConfig, findTestSubject } from '@kbn/test/jest';
import { registerTestBed, TestBed, AsyncTestBedConfig, findTestSubject } from '@kbn/test/jest';
import { PipelinesList } from '../../../public/application/sections/pipelines_list';
import { WithAppDependencies } from './setup_environment';
import { getListPath, ROUTES } from '../../../public/application/services/navigation';

const testBedConfig: TestBedConfig = {
const testBedConfig: AsyncTestBedConfig = {
memoryRouter: {
initialEntries: [getListPath()],
componentRoutePath: ROUTES.list,
Expand Down
Loading