From e5fea46dac09658ef81c761e72828b4623160aab Mon Sep 17 00:00:00 2001 From: Walter Rafelsberger Date: Thu, 5 Mar 2020 12:13:51 +0100 Subject: [PATCH] [ML] Migrate tests to react-testing-library. --- .../public/__mocks__/shared_imports.ts | 10 +- .../source_index_preview.test.tsx.snap | 584 ----------------- .../source_index_preview.test.tsx | 17 +- .../step_create_form.test.tsx.snap | 581 ----------------- .../step_create/step_create_form.test.tsx | 16 +- .../__snapshots__/pivot_preview.test.tsx.snap | 604 ------------------ .../step_define_form.test.tsx.snap | 572 ----------------- .../step_define_summary.test.tsx.snap | 99 --- .../step_define/pivot_preview.test.tsx | 16 +- .../step_define/step_define_form.test.tsx | 24 +- .../step_define/step_define_summary.test.tsx | 31 +- 11 files changed, 62 insertions(+), 2492 deletions(-) delete mode 100644 x-pack/legacy/plugins/transform/public/app/sections/create_transform/components/source_index_preview/__snapshots__/source_index_preview.test.tsx.snap delete mode 100644 x-pack/legacy/plugins/transform/public/app/sections/create_transform/components/step_create/__snapshots__/step_create_form.test.tsx.snap delete mode 100644 x-pack/legacy/plugins/transform/public/app/sections/create_transform/components/step_define/__snapshots__/pivot_preview.test.tsx.snap delete mode 100644 x-pack/legacy/plugins/transform/public/app/sections/create_transform/components/step_define/__snapshots__/step_define_form.test.tsx.snap delete mode 100644 x-pack/legacy/plugins/transform/public/app/sections/create_transform/components/step_define/__snapshots__/step_define_summary.test.tsx.snap diff --git a/x-pack/legacy/plugins/transform/public/__mocks__/shared_imports.ts b/x-pack/legacy/plugins/transform/public/__mocks__/shared_imports.ts index aa130b5030fc7..d7fca9820e614 100644 --- a/x-pack/legacy/plugins/transform/public/__mocks__/shared_imports.ts +++ b/x-pack/legacy/plugins/transform/public/__mocks__/shared_imports.ts @@ -6,12 +6,14 @@ jest.mock('ui/new_platform'); -export function XJsonMode() {} -export function setDependencyCache() {} -export const useRequest = () => ({ +export const expandLiteralStrings = jest.fn(); +export const XJsonMode = jest.fn(); +export const setDependencyCache = jest.fn(); +export const useRequest = jest.fn(() => ({ isLoading: false, error: null, data: undefined, -}); +})); export { mlInMemoryTableBasicFactory } from '../../../ml/public/application/components/ml_in_memory_table'; export const SORT_DIRECTION = { ASC: 'asc' }; +export const KqlFilterBar = jest.fn(() => null); diff --git a/x-pack/legacy/plugins/transform/public/app/sections/create_transform/components/source_index_preview/__snapshots__/source_index_preview.test.tsx.snap b/x-pack/legacy/plugins/transform/public/app/sections/create_transform/components/source_index_preview/__snapshots__/source_index_preview.test.tsx.snap deleted file mode 100644 index 6d2d3d5c4a6a5..0000000000000 --- a/x-pack/legacy/plugins/transform/public/app/sections/create_transform/components/source_index_preview/__snapshots__/source_index_preview.test.tsx.snap +++ /dev/null @@ -1,584 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`Transform: Minimal initialization 1`] = ` - - - - - - - - - -`; diff --git a/x-pack/legacy/plugins/transform/public/app/sections/create_transform/components/source_index_preview/source_index_preview.test.tsx b/x-pack/legacy/plugins/transform/public/app/sections/create_transform/components/source_index_preview/source_index_preview.test.tsx index ec79735741427..48eff132cd753 100644 --- a/x-pack/legacy/plugins/transform/public/app/sections/create_transform/components/source_index_preview/source_index_preview.test.tsx +++ b/x-pack/legacy/plugins/transform/public/app/sections/create_transform/components/source_index_preview/source_index_preview.test.tsx @@ -4,8 +4,9 @@ * you may not use this file except in compliance with the Elastic License. */ -import { shallow } from 'enzyme'; import React from 'react'; +import { render } from '@testing-library/react'; +import '@testing-library/jest-dom/extend-expect'; import { createPublicShim } from '../../../../../shim'; import { getAppProviders } from '../../../../app_dependencies'; @@ -14,17 +15,12 @@ import { SearchItems } from '../../../../hooks/use_search_items'; import { SourceIndexPreview } from './source_index_preview'; -// workaround to make React.memo() work with enzyme -jest.mock('react', () => { - const r = jest.requireActual('react'); - return { ...r, memo: (x: any) => x }; -}); - jest.mock('ui/new_platform'); jest.mock('../../../../../shared_imports'); describe('Transform: ', () => { test('Minimal initialization', () => { + // Arrange const props = { indexPattern: { title: 'the-index-pattern-title', @@ -32,14 +28,15 @@ describe('Transform: ', () => { } as SearchItems['indexPattern'], query: getPivotQuery('the-query'), }; - const Providers = getAppProviders(createPublicShim()); - const wrapper = shallow( + const { getByText } = render( ); - expect(wrapper).toMatchSnapshot(); + // Act + // Assert + expect(getByText(`Source index ${props.indexPattern.title}`)).toBeInTheDocument(); }); }); diff --git a/x-pack/legacy/plugins/transform/public/app/sections/create_transform/components/step_create/__snapshots__/step_create_form.test.tsx.snap b/x-pack/legacy/plugins/transform/public/app/sections/create_transform/components/step_create/__snapshots__/step_create_form.test.tsx.snap deleted file mode 100644 index db4ff0c1a99ae..0000000000000 --- a/x-pack/legacy/plugins/transform/public/app/sections/create_transform/components/step_create/__snapshots__/step_create_form.test.tsx.snap +++ /dev/null @@ -1,581 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`Transform: Minimal initialization 1`] = ` - - - - - - - - - -`; diff --git a/x-pack/legacy/plugins/transform/public/app/sections/create_transform/components/step_create/step_create_form.test.tsx b/x-pack/legacy/plugins/transform/public/app/sections/create_transform/components/step_create/step_create_form.test.tsx index 80968fd6e2887..7a22af492e36e 100644 --- a/x-pack/legacy/plugins/transform/public/app/sections/create_transform/components/step_create/step_create_form.test.tsx +++ b/x-pack/legacy/plugins/transform/public/app/sections/create_transform/components/step_create/step_create_form.test.tsx @@ -4,25 +4,21 @@ * you may not use this file except in compliance with the Elastic License. */ -import { shallow } from 'enzyme'; import React from 'react'; +import { render } from '@testing-library/react'; +import '@testing-library/jest-dom/extend-expect'; import { createPublicShim } from '../../../../../shim'; import { getAppProviders } from '../../../../app_dependencies'; import { StepCreateForm } from './step_create_form'; -// workaround to make React.memo() work with enzyme -jest.mock('react', () => { - const r = jest.requireActual('react'); - return { ...r, memo: (x: any) => x }; -}); - jest.mock('ui/new_platform'); jest.mock('../../../../../shared_imports'); describe('Transform: ', () => { test('Minimal initialization', () => { + // Arrange const props = { createIndexPattern: false, transformId: 'the-transform-id', @@ -32,12 +28,14 @@ describe('Transform: ', () => { }; const Providers = getAppProviders(createPublicShim()); - const wrapper = shallow( + const { getByText } = render( ); - expect(wrapper).toMatchSnapshot(); + // Act + // Assert + expect(getByText('Create and start')).toBeInTheDocument(); }); }); diff --git a/x-pack/legacy/plugins/transform/public/app/sections/create_transform/components/step_define/__snapshots__/pivot_preview.test.tsx.snap b/x-pack/legacy/plugins/transform/public/app/sections/create_transform/components/step_define/__snapshots__/pivot_preview.test.tsx.snap deleted file mode 100644 index bc0d983c6e022..0000000000000 --- a/x-pack/legacy/plugins/transform/public/app/sections/create_transform/components/step_define/__snapshots__/pivot_preview.test.tsx.snap +++ /dev/null @@ -1,604 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`Transform: Minimal initialization 1`] = ` - - - - - - - - - -`; diff --git a/x-pack/legacy/plugins/transform/public/app/sections/create_transform/components/step_define/__snapshots__/step_define_form.test.tsx.snap b/x-pack/legacy/plugins/transform/public/app/sections/create_transform/components/step_define/__snapshots__/step_define_form.test.tsx.snap deleted file mode 100644 index 30c57a9f3f4ae..0000000000000 --- a/x-pack/legacy/plugins/transform/public/app/sections/create_transform/components/step_define/__snapshots__/step_define_form.test.tsx.snap +++ /dev/null @@ -1,572 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`Transform: Minimal initialization 1`] = ` - - - - - - - - - -`; diff --git a/x-pack/legacy/plugins/transform/public/app/sections/create_transform/components/step_define/__snapshots__/step_define_summary.test.tsx.snap b/x-pack/legacy/plugins/transform/public/app/sections/create_transform/components/step_define/__snapshots__/step_define_summary.test.tsx.snap deleted file mode 100644 index 4955a0a95b7e9..0000000000000 --- a/x-pack/legacy/plugins/transform/public/app/sections/create_transform/components/step_define/__snapshots__/step_define_summary.test.tsx.snap +++ /dev/null @@ -1,99 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`Transform: Minimal initialization 1`] = ` - - -
- - - - - - - - -
-
- - - - - -
-`; diff --git a/x-pack/legacy/plugins/transform/public/app/sections/create_transform/components/step_define/pivot_preview.test.tsx b/x-pack/legacy/plugins/transform/public/app/sections/create_transform/components/step_define/pivot_preview.test.tsx index 6b49a305e515b..464b6e1fd9fe3 100644 --- a/x-pack/legacy/plugins/transform/public/app/sections/create_transform/components/step_define/pivot_preview.test.tsx +++ b/x-pack/legacy/plugins/transform/public/app/sections/create_transform/components/step_define/pivot_preview.test.tsx @@ -4,8 +4,9 @@ * you may not use this file except in compliance with the Elastic License. */ -import { shallow } from 'enzyme'; import React from 'react'; +import { render } from '@testing-library/react'; +import '@testing-library/jest-dom/extend-expect'; import { createPublicShim } from '../../../../../shim'; import { getAppProviders } from '../../../../app_dependencies'; @@ -20,17 +21,12 @@ import { SearchItems } from '../../../../hooks/use_search_items'; import { PivotPreview } from './pivot_preview'; -// workaround to make React.memo() work with enzyme -jest.mock('react', () => { - const r = jest.requireActual('react'); - return { ...r, memo: (x: any) => x }; -}); - jest.mock('ui/new_platform'); jest.mock('../../../../../shared_imports'); describe('Transform: ', () => { test('Minimal initialization', () => { + // Arrange const groupBy: PivotGroupByConfig = { agg: PIVOT_SUPPORTED_GROUP_BY_AGGS.TERMS, field: 'the-group-by-field', @@ -54,12 +50,14 @@ describe('Transform: ', () => { }; const Providers = getAppProviders(createPublicShim()); - const wrapper = shallow( + const { getByText } = render( ); - expect(wrapper).toMatchSnapshot(); + // Act + // Assert + expect(getByText('Transform pivot preview')).toBeInTheDocument(); }); }); diff --git a/x-pack/legacy/plugins/transform/public/app/sections/create_transform/components/step_define/step_define_form.test.tsx b/x-pack/legacy/plugins/transform/public/app/sections/create_transform/components/step_define/step_define_form.test.tsx index f31af733fa3ee..f45ef7cfddbf9 100644 --- a/x-pack/legacy/plugins/transform/public/app/sections/create_transform/components/step_define/step_define_form.test.tsx +++ b/x-pack/legacy/plugins/transform/public/app/sections/create_transform/components/step_define/step_define_form.test.tsx @@ -4,8 +4,9 @@ * you may not use this file except in compliance with the Elastic License. */ -import { shallow } from 'enzyme'; import React from 'react'; +import { render } from '@testing-library/react'; +import '@testing-library/jest-dom/extend-expect'; import { createPublicShim } from '../../../../../shim'; import { getAppProviders } from '../../../../app_dependencies'; @@ -19,25 +20,28 @@ import { SearchItems } from '../../../../hooks/use_search_items'; import { StepDefineForm, getAggNameConflictToastMessages } from './step_define_form'; -// workaround to make React.memo() work with enzyme -jest.mock('react', () => { - const r = jest.requireActual('react'); - return { ...r, memo: (x: any) => x }; -}); - jest.mock('ui/new_platform'); jest.mock('../../../../../shared_imports'); describe('Transform: ', () => { test('Minimal initialization', () => { + // Arrange + const searchItems = { + indexPattern: { + title: 'the-index-pattern-title', + fields: [] as any[], + } as SearchItems['indexPattern'], + }; const Providers = getAppProviders(createPublicShim()); - const wrapper = shallow( + const { getByLabelText } = render( - {}} searchItems={{} as SearchItems} /> + ); - expect(wrapper).toMatchSnapshot(); + // Act + // Assert + expect(getByLabelText('Index pattern')).toBeInTheDocument(); }); }); diff --git a/x-pack/legacy/plugins/transform/public/app/sections/create_transform/components/step_define/step_define_summary.test.tsx b/x-pack/legacy/plugins/transform/public/app/sections/create_transform/components/step_define/step_define_summary.test.tsx index e3a9830ea1904..0f7da50bbbade 100644 --- a/x-pack/legacy/plugins/transform/public/app/sections/create_transform/components/step_define/step_define_summary.test.tsx +++ b/x-pack/legacy/plugins/transform/public/app/sections/create_transform/components/step_define/step_define_summary.test.tsx @@ -4,9 +4,12 @@ * you may not use this file except in compliance with the Elastic License. */ -import { shallow } from 'enzyme'; import React from 'react'; +import { render } from '@testing-library/react'; +import '@testing-library/jest-dom/extend-expect'; +import { createPublicShim } from '../../../../../shim'; +import { getAppProviders } from '../../../../app_dependencies'; import { PivotAggsConfig, PivotGroupByConfig, @@ -18,16 +21,18 @@ import { SearchItems } from '../../../../hooks/use_search_items'; import { StepDefineExposedState } from './step_define_form'; import { StepDefineSummary } from './step_define_summary'; -// workaround to make React.memo() work with enzyme -jest.mock('react', () => { - const r = jest.requireActual('react'); - return { ...r, memo: (x: any) => x }; -}); - +jest.mock('ui/new_platform'); jest.mock('../../../../../shared_imports'); describe('Transform: ', () => { test('Minimal initialization', () => { + // Arrange + const searchItems = { + indexPattern: { + title: 'the-index-pattern-title', + fields: [] as any[], + } as SearchItems['indexPattern'], + }; const groupBy: PivotGroupByConfig = { agg: PIVOT_SUPPORTED_GROUP_BY_AGGS.TERMS, field: 'the-group-by-field', @@ -51,10 +56,16 @@ describe('Transform: ', () => { valid: true, }; - const wrapper = shallow( - + const Providers = getAppProviders(createPublicShim()); + const { getByText } = render( + + + ); - expect(wrapper).toMatchSnapshot(); + // Act + // Assert + expect(getByText('Group by')).toBeInTheDocument(); + expect(getByText('Aggregations')).toBeInTheDocument(); }); });