Skip to content

Commit

Permalink
[ML] Fix tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
walterra committed Mar 12, 2020
1 parent c6357c3 commit 9fa3c4b
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -30,6 +25,10 @@ const appDependencies: AppDependencies = {
http: coreSetup.http,
};

export const Providers: FC = ({ children }) => {
return <KibanaContextProvider services={appDependencies}>{children}</KibanaContextProvider>;
export const useAppDependencies = () => {
return appDependencies;
};

export const useToastNotifications = () => {
return coreSetup.notifications;
};
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -20,6 +19,7 @@ import {
import { PivotPreview } from './pivot_preview';

jest.mock('../../../shared_imports');
jest.mock('../../../app/app_dependencies');

describe('Transform: <PivotPreview />', () => {
// Using the async/await wait()/done() pattern to avoid act() errors.
Expand All @@ -44,11 +44,7 @@ describe('Transform: <PivotPreview />', () => {
query: getPivotQuery('the-query'),
};

const { getByText } = render(
<Providers>
<PivotPreview {...props} />
</Providers>
);
const { getByText } = render(<PivotPreview {...props} />);

// Act
// Assert
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
<Providers>
<ToastNotificationText {...props} />
</Providers>
);
const { container } = render(<ToastNotificationText {...props} />);
expect(container.textContent).toBe('a short text message');
});

Expand All @@ -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(
<Providers>
<ToastNotificationText {...props} />
</Providers>
);
const { container } = render(<ToastNotificationText {...props} />);
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'
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: <SourceIndexPreview />', () => {
// Using the async/await wait()/done() pattern to avoid act() errors.
Expand All @@ -27,11 +27,7 @@ describe('Transform: <SourceIndexPreview />', () => {
} as SearchItems['indexPattern'],
query: getPivotQuery('the-query'),
};
const { getByText } = render(
<Providers>
<SourceIndexPreview {...props} />
</Providers>
);
const { getByText } = render(<SourceIndexPreview {...props} />);

// Act
// Assert
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: <StepCreateForm />', () => {
test('Minimal initialization', () => {
Expand All @@ -25,11 +24,7 @@ describe('Transform: <StepCreateForm />', () => {
onChange() {},
};

const { getByText } = render(
<Providers>
<StepCreateForm {...props} />
</Providers>
);
const { getByText } = render(<StepCreateForm {...props} />);

// Act
// Assert
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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: <DefinePivotForm />', () => {
// Using the async/await wait()/done() pattern to avoid act() errors.
Expand All @@ -31,10 +56,21 @@ describe('Transform: <DefinePivotForm />', () => {
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(
<Providers>
<StepDefineForm onChange={jest.fn()} searchItems={searchItems as SearchItems} />
</Providers>
<I18nProvider>
<KibanaContextProvider services={services}>
<StepDefineForm onChange={jest.fn()} searchItems={searchItems as SearchItems} />
</KibanaContextProvider>
</I18nProvider>
);

// Act
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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: <DefinePivotSummary />', () => {
// Using the async/await wait()/done() pattern to avoid act() errors.
Expand Down Expand Up @@ -57,9 +57,7 @@ describe('Transform: <DefinePivotSummary />', () => {
};

const { getByText } = render(
<Providers>
<StepDefineSummary formState={formState} searchItems={searchItems as SearchItems} />
</Providers>
<StepDefineSummary formState={formState} searchItems={searchItems as SearchItems} />
);

// Act
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <DeleteAction />', () => {
test('Minimal initialization', () => {
Expand All @@ -25,14 +24,7 @@ describe('Transform: Transform List Actions <DeleteAction />', () => {
deleteTransform(d: TransformListRow) {},
};

const wrapper = shallow(
<Providers>
<DeleteAction {...props} />
</Providers>
)
.find(DeleteAction)
.shallow();

const wrapper = shallow(<DeleteAction {...props} />);
expect(wrapper).toMatchSnapshot();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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 <StartAction />', () => {
test('Minimal initialization', () => {
Expand All @@ -25,13 +24,7 @@ describe('Transform: Transform List Actions <StartAction />', () => {
startTransform(d: TransformListRow) {},
};

const wrapper = shallow(
<Providers>
<StartAction {...props} />
</Providers>
)
.find(StartAction)
.shallow();
const wrapper = shallow(<StartAction {...props} />);

expect(wrapper).toMatchSnapshot();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <StopAction />', () => {
test('Minimal initialization', () => {
Expand All @@ -25,13 +24,7 @@ describe('Transform: Transform List Actions <StopAction />', () => {
stopTransform(d: TransformListRow) {},
};

const wrapper = shallow(
<Providers>
<StopAction {...props} />
</Providers>
)
.find(StopAction)
.shallow();
const wrapper = shallow(<StopAction {...props} />);

expect(wrapper).toMatchSnapshot();
});
Expand Down

0 comments on commit 9fa3c4b

Please sign in to comment.