From 9fa3c4b5172338f853f1c765adfffefc27fe1aa6 Mon Sep 17 00:00:00 2001 From: Walter Rafelsberger Date: Thu, 12 Mar 2020 08:32:23 +0100 Subject: [PATCH] [ML] Fix tests. --- .../app_dependencies.tsx} | 21 +++++---- .../pivot_preview/pivot_preview.test.tsx | 8 +--- .../toast_notification_text.test.tsx | 15 ++----- .../source_index_preview.test.tsx | 8 +--- .../step_create/step_create_form.test.tsx | 9 +--- .../step_define/step_define_form.test.tsx | 44 +++++++++++++++++-- .../step_define/step_define_summary.test.tsx | 6 +-- .../transform_list/action_delete.test.tsx | 12 +---- .../transform_list/action_start.test.tsx | 11 +---- .../transform_list/action_stop.test.tsx | 11 +---- 10 files changed, 67 insertions(+), 78 deletions(-) rename x-pack/plugins/transform/public/app/{app_dependencies.mock.tsx => __mocks__/app_dependencies.tsx} (55%) diff --git a/x-pack/plugins/transform/public/app/app_dependencies.mock.tsx b/x-pack/plugins/transform/public/app/__mocks__/app_dependencies.tsx similarity index 55% rename from x-pack/plugins/transform/public/app/app_dependencies.mock.tsx rename to x-pack/plugins/transform/public/app/__mocks__/app_dependencies.tsx index 2158dc9315fa50..75fefc99b54580 100644 --- a/x-pack/plugins/transform/public/app/app_dependencies.mock.tsx +++ b/x-pack/plugins/transform/public/app/__mocks__/app_dependencies.tsx @@ -4,20 +4,15 @@ * you may not use this file except in compliance with the Elastic License. */ -import React, { FC } from 'react'; - -import { coreMock } from '../../../../../src/core/public/mocks'; -import { dataPluginMock } from '../../../../../src/plugins/data/public/mocks'; -import { KibanaContextProvider } from '../../../../../src/plugins/kibana_react/public'; -import { Storage } from '../../../../../src/plugins/kibana_utils/public'; - -import { AppDependencies } from './app_dependencies'; +import { coreMock } from '../../../../../../src/core/public/mocks'; +import { dataPluginMock } from '../../../../../../src/plugins/data/public/mocks'; +import { Storage } from '../../../../../../src/plugins/kibana_utils/public'; const coreSetup = coreMock.createSetup(); const coreStart = coreMock.createStart(); const dataStart = dataPluginMock.createStartContract(); -const appDependencies: AppDependencies = { +const appDependencies = { chrome: coreStart.chrome, data: dataStart, docLinks: coreStart.docLinks, @@ -30,6 +25,10 @@ const appDependencies: AppDependencies = { http: coreSetup.http, }; -export const Providers: FC = ({ children }) => { - return {children}; +export const useAppDependencies = () => { + return appDependencies; +}; + +export const useToastNotifications = () => { + return coreSetup.notifications; }; diff --git a/x-pack/plugins/transform/public/app/components/pivot_preview/pivot_preview.test.tsx b/x-pack/plugins/transform/public/app/components/pivot_preview/pivot_preview.test.tsx index 69ac46b8807e39..5ed50eaab46ba7 100644 --- a/x-pack/plugins/transform/public/app/components/pivot_preview/pivot_preview.test.tsx +++ b/x-pack/plugins/transform/public/app/components/pivot_preview/pivot_preview.test.tsx @@ -8,7 +8,6 @@ import React from 'react'; import { render, wait } from '@testing-library/react'; import '@testing-library/jest-dom/extend-expect'; -import { Providers } from '../../app_dependencies.mock'; import { getPivotQuery, PivotAggsConfig, @@ -20,6 +19,7 @@ import { import { PivotPreview } from './pivot_preview'; jest.mock('../../../shared_imports'); +jest.mock('../../../app/app_dependencies'); describe('Transform: ', () => { // Using the async/await wait()/done() pattern to avoid act() errors. @@ -44,11 +44,7 @@ describe('Transform: ', () => { query: getPivotQuery('the-query'), }; - const { getByText } = render( - - - - ); + const { getByText } = render(); // Act // Assert diff --git a/x-pack/plugins/transform/public/app/components/toast_notification_text.test.tsx b/x-pack/plugins/transform/public/app/components/toast_notification_text.test.tsx index 01d7aca05e88f6..e51119d67d5678 100644 --- a/x-pack/plugins/transform/public/app/components/toast_notification_text.test.tsx +++ b/x-pack/plugins/transform/public/app/components/toast_notification_text.test.tsx @@ -7,22 +7,17 @@ import React from 'react'; import { render } from '@testing-library/react'; -import { Providers } from '../app_dependencies.mock'; - import { ToastNotificationText } from './toast_notification_text'; jest.mock('../../shared_imports'); +jest.mock('../../app/app_dependencies'); describe('ToastNotificationText', () => { test('should render the text as plain text', () => { const props = { text: 'a short text message', }; - const { container } = render( - - - - ); + const { container } = render(); expect(container.textContent).toBe('a short text message'); }); @@ -31,11 +26,7 @@ describe('ToastNotificationText', () => { text: 'a text message that is longer than 140 characters. a text message that is longer than 140 characters. a text message that is longer than 140 characters. ', }; - const { container } = render( - - - - ); + const { container } = render(); expect(container.textContent).toBe( 'a text message that is longer than 140 characters. a text message that is longer than 140 characters. a text message that is longer than 140 ...View details' ); diff --git a/x-pack/plugins/transform/public/app/sections/create_transform/components/source_index_preview/source_index_preview.test.tsx b/x-pack/plugins/transform/public/app/sections/create_transform/components/source_index_preview/source_index_preview.test.tsx index 89d9f22a159546..32f6ff9490a0f4 100644 --- a/x-pack/plugins/transform/public/app/sections/create_transform/components/source_index_preview/source_index_preview.test.tsx +++ b/x-pack/plugins/transform/public/app/sections/create_transform/components/source_index_preview/source_index_preview.test.tsx @@ -8,13 +8,13 @@ import React from 'react'; import { render, wait } from '@testing-library/react'; import '@testing-library/jest-dom/extend-expect'; -import { Providers } from '../../../../app_dependencies.mock'; import { getPivotQuery } from '../../../../common'; import { SearchItems } from '../../../../hooks/use_search_items'; import { SourceIndexPreview } from './source_index_preview'; jest.mock('../../../../../shared_imports'); +jest.mock('../../../../../app/app_dependencies'); describe('Transform: ', () => { // Using the async/await wait()/done() pattern to avoid act() errors. @@ -27,11 +27,7 @@ describe('Transform: ', () => { } as SearchItems['indexPattern'], query: getPivotQuery('the-query'), }; - const { getByText } = render( - - - - ); + const { getByText } = render(); // Act // Assert diff --git a/x-pack/plugins/transform/public/app/sections/create_transform/components/step_create/step_create_form.test.tsx b/x-pack/plugins/transform/public/app/sections/create_transform/components/step_create/step_create_form.test.tsx index 4a698dca980ff3..f2837d30402def 100644 --- a/x-pack/plugins/transform/public/app/sections/create_transform/components/step_create/step_create_form.test.tsx +++ b/x-pack/plugins/transform/public/app/sections/create_transform/components/step_create/step_create_form.test.tsx @@ -8,11 +8,10 @@ import React from 'react'; import { render } from '@testing-library/react'; import '@testing-library/jest-dom/extend-expect'; -import { Providers } from '../../../../app_dependencies.mock'; - import { StepCreateForm } from './step_create_form'; jest.mock('../../../../../shared_imports'); +jest.mock('../../../../../app/app_dependencies'); describe('Transform: ', () => { test('Minimal initialization', () => { @@ -25,11 +24,7 @@ describe('Transform: ', () => { onChange() {}, }; - 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 dbc8398da47723..a15e958c16b73e 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 @@ -8,7 +8,14 @@ import React from 'react'; import { render, wait } from '@testing-library/react'; import '@testing-library/jest-dom/extend-expect'; -import { Providers } from '../../../../app_dependencies.mock'; +import { I18nProvider } from '@kbn/i18n/react'; + +import { KibanaContextProvider } from '../../../../../../../../../src/plugins/kibana_react/public'; + +import { coreMock } from '../../../../../../../../../src/core/public/mocks'; +import { dataPluginMock } from '../../../../../../../../../src/plugins/data/public/mocks'; +const startMock = coreMock.createStart(); + import { PivotAggsConfigDict, PivotGroupByConfigDict, @@ -20,6 +27,24 @@ import { SearchItems } from '../../../../hooks/use_search_items'; import { StepDefineForm, getAggNameConflictToastMessages } from './step_define_form'; jest.mock('../../../../../shared_imports'); +jest.mock('../../../../../app/app_dependencies'); + +const createMockWebStorage = () => ({ + clear: jest.fn(), + getItem: jest.fn(), + key: jest.fn(), + removeItem: jest.fn(), + setItem: jest.fn(), + length: 0, +}); + +const createMockStorage = () => ({ + storage: createMockWebStorage(), + get: jest.fn(), + set: jest.fn(), + remove: jest.fn(), + clear: jest.fn(), +}); describe('Transform: ', () => { // Using the async/await wait()/done() pattern to avoid act() errors. @@ -31,10 +56,21 @@ describe('Transform: ', () => { fields: [] as any[], } as SearchItems['indexPattern'], }; + + // mock services for QueryStringInput + const services = { + ...startMock, + data: dataPluginMock.createStartContract(), + appName: 'the-test-app', + storage: createMockStorage(), + }; + const { getByLabelText } = render( - - - + + + + + ); // Act 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 11570a6ef53a59..f2e5d30b0601fd 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 @@ -8,7 +8,6 @@ import React from 'react'; import { render, wait } from '@testing-library/react'; import '@testing-library/jest-dom/extend-expect'; -import { Providers } from '../../../../app_dependencies.mock'; import { PivotAggsConfig, PivotGroupByConfig, @@ -21,6 +20,7 @@ import { StepDefineExposedState } from './step_define_form'; import { StepDefineSummary } from './step_define_summary'; jest.mock('../../../../../shared_imports'); +jest.mock('../../../../../app/app_dependencies'); describe('Transform: ', () => { // Using the async/await wait()/done() pattern to avoid act() errors. @@ -57,9 +57,7 @@ describe('Transform: ', () => { }; const { getByText } = render( - - - + ); // Act diff --git a/x-pack/plugins/transform/public/app/sections/transform_management/components/transform_list/action_delete.test.tsx b/x-pack/plugins/transform/public/app/sections/transform_management/components/transform_list/action_delete.test.tsx index ca83d379bf3db7..fdd0b821f54fd2 100644 --- a/x-pack/plugins/transform/public/app/sections/transform_management/components/transform_list/action_delete.test.tsx +++ b/x-pack/plugins/transform/public/app/sections/transform_management/components/transform_list/action_delete.test.tsx @@ -7,14 +7,13 @@ import { shallow } from 'enzyme'; import React from 'react'; -import { Providers } from '../../../../app_dependencies.mock'; - import { TransformListRow } from '../../../../common'; import { DeleteAction } from './action_delete'; import transformListRow from '../../../../common/__mocks__/transform_list_row.json'; jest.mock('../../../../../shared_imports'); +jest.mock('../../../../../app/app_dependencies'); describe('Transform: Transform List Actions ', () => { test('Minimal initialization', () => { @@ -25,14 +24,7 @@ describe('Transform: Transform List Actions ', () => { deleteTransform(d: TransformListRow) {}, }; - const wrapper = shallow( - - - - ) - .find(DeleteAction) - .shallow(); - + const wrapper = shallow(); expect(wrapper).toMatchSnapshot(); }); }); diff --git a/x-pack/plugins/transform/public/app/sections/transform_management/components/transform_list/action_start.test.tsx b/x-pack/plugins/transform/public/app/sections/transform_management/components/transform_list/action_start.test.tsx index dc45dcccc94959..2de115236c4dc0 100644 --- a/x-pack/plugins/transform/public/app/sections/transform_management/components/transform_list/action_start.test.tsx +++ b/x-pack/plugins/transform/public/app/sections/transform_management/components/transform_list/action_start.test.tsx @@ -7,14 +7,13 @@ import { shallow } from 'enzyme'; import React from 'react'; -import { Providers } from '../../../../app_dependencies.mock'; - import { TransformListRow } from '../../../../common'; import { StartAction } from './action_start'; import transformListRow from '../../../../common/__mocks__/transform_list_row.json'; jest.mock('../../../../../shared_imports'); +jest.mock('../../../../../app/app_dependencies'); describe('Transform: Transform List Actions ', () => { test('Minimal initialization', () => { @@ -25,13 +24,7 @@ describe('Transform: Transform List Actions ', () => { startTransform(d: TransformListRow) {}, }; - const wrapper = shallow( - - - - ) - .find(StartAction) - .shallow(); + const wrapper = shallow(); expect(wrapper).toMatchSnapshot(); }); diff --git a/x-pack/plugins/transform/public/app/sections/transform_management/components/transform_list/action_stop.test.tsx b/x-pack/plugins/transform/public/app/sections/transform_management/components/transform_list/action_stop.test.tsx index dc9ca2fbfff520..a97097d9098481 100644 --- a/x-pack/plugins/transform/public/app/sections/transform_management/components/transform_list/action_stop.test.tsx +++ b/x-pack/plugins/transform/public/app/sections/transform_management/components/transform_list/action_stop.test.tsx @@ -7,14 +7,13 @@ import { shallow } from 'enzyme'; import React from 'react'; -import { Providers } from '../../../../app_dependencies.mock'; - import { TransformListRow } from '../../../../common'; import { StopAction } from './action_stop'; import transformListRow from '../../../../common/__mocks__/transform_list_row.json'; jest.mock('../../../../../shared_imports'); +jest.mock('../../../../../app/app_dependencies'); describe('Transform: Transform List Actions ', () => { test('Minimal initialization', () => { @@ -25,13 +24,7 @@ describe('Transform: Transform List Actions ', () => { stopTransform(d: TransformListRow) {}, }; - const wrapper = shallow( - - - - ) - .find(StopAction) - .shallow(); + const wrapper = shallow(); expect(wrapper).toMatchSnapshot(); });