From 59826c9497408f47d13b39e67a9395b1f91f3f21 Mon Sep 17 00:00:00 2001 From: Walter Rafelsberger Date: Wed, 23 Sep 2020 14:07:00 +0200 Subject: [PATCH] [ML] Fix tests. --- x-pack/plugins/ml/public/index.ts | 4 ++ .../public/app/__mocks__/app_dependencies.tsx | 7 ++- .../public/app/__mocks__/shared_context.ts | 13 ++++++ .../transform/public/app/app_dependencies.tsx | 6 +-- .../public/app/hooks/use_index_data.test.tsx | 45 +++++++++++++------ .../step_define/step_define_form.test.tsx | 9 +++- .../step_define/step_define_summary.test.tsx | 9 +++- .../transform_list/expanded_row.test.tsx | 16 +++++-- .../transform/public/shared_imports.ts | 8 +++- 9 files changed, 92 insertions(+), 25 deletions(-) create mode 100644 x-pack/plugins/transform/public/app/__mocks__/shared_context.ts diff --git a/x-pack/plugins/ml/public/index.ts b/x-pack/plugins/ml/public/index.ts index 6896e30a7576f..9dbc6c60ac03a 100755 --- a/x-pack/plugins/ml/public/index.ts +++ b/x-pack/plugins/ml/public/index.ts @@ -43,3 +43,7 @@ export { ANOMALY_SEVERITY } from '../common'; export const getShared = async () => { return await import('./shared'); }; + +// Helper to get Type returned by getShared. +type AwaitReturnType = T extends PromiseLike ? U : T; +export type GetSharedReturnType = AwaitReturnType>; diff --git a/x-pack/plugins/transform/public/app/__mocks__/app_dependencies.tsx b/x-pack/plugins/transform/public/app/__mocks__/app_dependencies.tsx index 75fefc99b5458..b4de5ff145a59 100644 --- a/x-pack/plugins/transform/public/app/__mocks__/app_dependencies.tsx +++ b/x-pack/plugins/transform/public/app/__mocks__/app_dependencies.tsx @@ -4,10 +4,14 @@ * you may not use this file except in compliance with the Elastic License. */ +import { useContext } from 'react'; + import { coreMock } from '../../../../../../src/core/public/mocks'; import { dataPluginMock } from '../../../../../../src/plugins/data/public/mocks'; import { Storage } from '../../../../../../src/plugins/kibana_utils/public'; +import { MlSharedContext } from './shared_context'; + const coreSetup = coreMock.createSetup(); const coreStart = coreMock.createStart(); const dataStart = dataPluginMock.createStartContract(); @@ -26,7 +30,8 @@ const appDependencies = { }; export const useAppDependencies = () => { - return appDependencies; + const ml = useContext(MlSharedContext); + return { ...appDependencies, ml }; }; export const useToastNotifications = () => { diff --git a/x-pack/plugins/transform/public/app/__mocks__/shared_context.ts b/x-pack/plugins/transform/public/app/__mocks__/shared_context.ts new file mode 100644 index 0000000000000..edf75112b266c --- /dev/null +++ b/x-pack/plugins/transform/public/app/__mocks__/shared_context.ts @@ -0,0 +1,13 @@ +/* + * 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 { createContext } from 'react'; + +import { GetSharedReturnType } from '../../shared_imports'; + +// This code is a workaround to provide dependencies that are +// loaded dynamically during runtime. +export const MlSharedContext = createContext({} as GetSharedReturnType); diff --git a/x-pack/plugins/transform/public/app/app_dependencies.tsx b/x-pack/plugins/transform/public/app/app_dependencies.tsx index eb3376fd2ff99..3b06af70d96d4 100644 --- a/x-pack/plugins/transform/public/app/app_dependencies.tsx +++ b/x-pack/plugins/transform/public/app/app_dependencies.tsx @@ -11,9 +11,7 @@ import { ScopedHistory } from 'kibana/public'; import { useKibana } from '../../../../../src/plugins/kibana_react/public'; import { Storage } from '../../../../../src/plugins/kibana_utils/public'; -import { getShared } from '../shared_imports'; - -type ThenArg = T extends PromiseLike ? U : T; +import type { GetSharedReturnType } from '../shared_imports'; export interface AppDependencies { chrome: CoreStart['chrome']; @@ -27,7 +25,7 @@ export interface AppDependencies { storage: Storage; overlays: CoreStart['overlays']; history: ScopedHistory; - ml: ThenArg>; + ml: GetSharedReturnType; } export const useAppDependencies = () => { diff --git a/x-pack/plugins/transform/public/app/hooks/use_index_data.test.tsx b/x-pack/plugins/transform/public/app/hooks/use_index_data.test.tsx index db9ac1e93633f..a0c30f0539a31 100644 --- a/x-pack/plugins/transform/public/app/hooks/use_index_data.test.tsx +++ b/x-pack/plugins/transform/public/app/hooks/use_index_data.test.tsx @@ -4,14 +4,14 @@ * you may not use this file except in compliance with the Elastic License. */ -import React from 'react'; +import React, { FC } from 'react'; import { render, wait } from '@testing-library/react'; import { renderHook } from '@testing-library/react-hooks'; import { CoreSetup } from 'src/core/public'; -import { DataGrid, UseIndexDataReturnType, INDEX_STATUS } from '../../shared_imports'; +import { getShared, UseIndexDataReturnType } from '../../shared_imports'; import { SimpleQuery } from '../common'; @@ -22,6 +22,9 @@ jest.mock('../../shared_imports'); jest.mock('../app_dependencies'); jest.mock('./use_api'); +import { useAppDependencies } from '../__mocks__/app_dependencies'; +import { MlSharedContext } from '../__mocks__/shared_context'; + const query: SimpleQuery = { query_string: { query: '*', @@ -31,22 +34,29 @@ const query: SimpleQuery = { describe('Transform: useIndexData()', () => { test('indexPattern set triggers loading', async (done) => { - const { result, waitForNextUpdate } = renderHook(() => - useIndexData( - ({ - id: 'the-id', - title: 'the-title', - fields: [], - } as unknown) as SearchItems['indexPattern'], - query - ) + const mlShared = await getShared(); + const wrapper: FC = ({ children }) => ( + {children} + ); + + const { result, waitForNextUpdate } = renderHook( + () => + useIndexData( + ({ + id: 'the-id', + title: 'the-title', + fields: [], + } as unknown) as SearchItems['indexPattern'], + query + ), + { wrapper } ); const IndexObj: UseIndexDataReturnType = result.current; await waitForNextUpdate(); expect(IndexObj.errorMessage).toBe(''); - expect(IndexObj.status).toBe(INDEX_STATUS.LOADING); + expect(IndexObj.status).toBe(1); expect(IndexObj.tableItems).toEqual([]); done(); }); @@ -61,7 +71,12 @@ describe('Transform: with useIndexData()', () => { fields: [] as any[], } as SearchItems['indexPattern']; + const mlShared = await getShared(); + const Wrapper = () => { + const { + ml: { DataGrid }, + } = useAppDependencies(); const props = { ...useIndexData(indexPattern, { match_all: {} }), copyToClipboard: 'the-copy-to-clipboard-code', @@ -73,7 +88,11 @@ describe('Transform: with useIndexData()', () => { return ; }; - const { getByText } = render(); + const { getByText } = render( + + + + ); // Act // Assert diff --git a/x-pack/plugins/transform/public/app/sections/create_transform/components/step_define/step_define_form.test.tsx b/x-pack/plugins/transform/public/app/sections/create_transform/components/step_define/step_define_form.test.tsx index 986ac0a212e8a..331312c1a63b6 100644 --- a/x-pack/plugins/transform/public/app/sections/create_transform/components/step_define/step_define_form.test.tsx +++ b/x-pack/plugins/transform/public/app/sections/create_transform/components/step_define/step_define_form.test.tsx @@ -30,6 +30,9 @@ import { StepDefineForm } from './step_define_form'; jest.mock('../../../../../shared_imports'); jest.mock('../../../../../app/app_dependencies'); +import { MlSharedContext } from '../../../../../app/__mocks__/shared_context'; +import { getShared } from '../../../../../shared_imports'; + const createMockWebStorage = () => ({ clear: jest.fn(), getItem: jest.fn(), @@ -51,6 +54,8 @@ describe('Transform: ', () => { // Using the async/await wait()/done() pattern to avoid act() errors. test('Minimal initialization', async (done) => { // Arrange + const mlShared = await getShared(); + const searchItems = { indexPattern: { title: 'the-index-pattern-title', @@ -69,7 +74,9 @@ describe('Transform: ', () => { const { getByLabelText } = render( - + + + ); diff --git a/x-pack/plugins/transform/public/app/sections/create_transform/components/step_define/step_define_summary.test.tsx b/x-pack/plugins/transform/public/app/sections/create_transform/components/step_define/step_define_summary.test.tsx index f8a060e0007b8..d2c4caa88d267 100644 --- a/x-pack/plugins/transform/public/app/sections/create_transform/components/step_define/step_define_summary.test.tsx +++ b/x-pack/plugins/transform/public/app/sections/create_transform/components/step_define/step_define_summary.test.tsx @@ -22,10 +22,15 @@ import { StepDefineSummary } from './step_define_summary'; jest.mock('../../../../../shared_imports'); jest.mock('../../../../../app/app_dependencies'); +import { MlSharedContext } from '../../../../../app/__mocks__/shared_context'; +import { getShared } from '../../../../../shared_imports'; + describe('Transform: ', () => { // Using the async/await wait()/done() pattern to avoid act() errors. test('Minimal initialization', async (done) => { // Arrange + const mlShared = await getShared(); + const searchItems = { indexPattern: { title: 'the-index-pattern-title', @@ -57,7 +62,9 @@ describe('Transform: ', () => { }; const { getByText } = render( - + + + ); // Act diff --git a/x-pack/plugins/transform/public/app/sections/transform_management/components/transform_list/expanded_row.test.tsx b/x-pack/plugins/transform/public/app/sections/transform_management/components/transform_list/expanded_row.test.tsx index 846d8a8ccd200..07cc31a03a152 100644 --- a/x-pack/plugins/transform/public/app/sections/transform_management/components/transform_list/expanded_row.test.tsx +++ b/x-pack/plugins/transform/public/app/sections/transform_management/components/transform_list/expanded_row.test.tsx @@ -13,9 +13,12 @@ import { ExpandedRow } from './expanded_row'; import transformListRow from '../../../../common/__mocks__/transform_list_row.json'; import { within } from '@testing-library/dom'; -jest.mock('../../../../../shared_imports', () => ({ - formatHumanReadableDateTimeSeconds: jest.fn(), -})); +jest.mock('../../../../../shared_imports'); +jest.mock('../../../../../app/app_dependencies'); + +import { MlSharedContext } from '../../../../../app/__mocks__/shared_context'; +import { getShared } from '../../../../../shared_imports'; + describe('Transform: Transform List ', () => { // Set timezone to US/Eastern for consistent test results. beforeEach(() => { @@ -27,9 +30,14 @@ describe('Transform: Transform List ', () => { }); test('Minimal initialization', async () => { + const mlShared = await getShared(); const item: TransformListRow = transformListRow; - const { getByText, getByTestId } = render(); + const { getByText, getByTestId } = render( + + + + ); expect(getByText('Details')).toBeInTheDocument(); expect(getByText('Stats')).toBeInTheDocument(); diff --git a/x-pack/plugins/transform/public/shared_imports.ts b/x-pack/plugins/transform/public/shared_imports.ts index 561718cf6d65a..5cbc0d8fea14f 100644 --- a/x-pack/plugins/transform/public/shared_imports.ts +++ b/x-pack/plugins/transform/public/shared_imports.ts @@ -13,4 +13,10 @@ export { useRequest, } from '../../../../src/plugins/es_ui_shared/public'; -export { getShared, UseIndexDataReturnType, EsSorting, RenderCellValue } from '../../ml/public'; +export { + getShared, + GetSharedReturnType, + UseIndexDataReturnType, + EsSorting, + RenderCellValue, +} from '../../ml/public';