Skip to content

Commit 5a07ca7

Browse files
committed
[ML] Fix jest tests.
1 parent 7246934 commit 5a07ca7

20 files changed

Lines changed: 113 additions & 114 deletions
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
7+
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
8+
import { coreMock } from 'src/core/public/mocks';
9+
10+
import { getAppProviders, AppDependencies } from './app_dependencies';
11+
12+
const coreSetup = coreMock.createSetup();
13+
const coreStart = coreMock.createStart();
14+
15+
const appDependencies: AppDependencies = {
16+
chrome: coreStart.chrome,
17+
data: {},
18+
docLinks: coreStart.docLinks,
19+
i18n: coreStart.i18n,
20+
notifications: coreStart.notifications,
21+
uiSettings: coreStart.uiSettings,
22+
savedObjects: coreStart.savedObjects,
23+
overlays: coreStart.overlays,
24+
http: coreSetup.http,
25+
};
26+
27+
export const Providers = getAppProviders(appDependencies);

x-pack/plugins/transform/public/app/app_dependencies.tsx

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,38 @@
77
import React, { createContext, useContext, ReactNode } from 'react';
88
import { HashRouter } from 'react-router-dom';
99

10+
import { CoreSetup, CoreStart } from '../../../../../src/core/public';
11+
1012
import { API_BASE_PATH } from '../../common/constants';
1113

12-
import { PluginsDependencies } from '../plugin';
1314
import { setDependencyCache } from '../shared_imports';
1415

1516
import { AuthorizationProvider } from './lib/authorization';
1617

1718
export interface AppDependencies {
18-
core: any;
19-
plugins: PluginsDependencies;
19+
chrome: CoreStart['chrome'];
20+
data: any;
21+
docLinks: CoreStart['docLinks'];
22+
http: CoreSetup['http'];
23+
i18n: CoreStart['i18n'];
24+
notifications: CoreStart['notifications'];
25+
uiSettings: CoreStart['uiSettings'];
26+
savedObjects: CoreStart['savedObjects'];
27+
overlays: CoreStart['overlays'];
2028
}
2129

2230
let DependenciesContext: React.Context<AppDependencies>;
2331

2432
const setAppDependencies = (deps: AppDependencies) => {
2533
const legacyBasePath = {
26-
prepend: deps.core.http.basePath.prepend,
27-
get: deps.core.http.basePath.get,
34+
prepend: deps.http.basePath.prepend,
35+
get: deps.http.basePath.get,
2836
remove: () => {},
2937
};
3038

3139
setDependencyCache({
32-
autocomplete: deps.plugins.data.autocomplete,
33-
docLinks: deps.core.docLinks,
40+
autocomplete: deps.data.autocomplete,
41+
docLinks: deps.docLinks,
3442
basePath: legacyBasePath as any,
3543
});
3644
DependenciesContext = createContext<AppDependencies>(deps);
@@ -47,15 +55,13 @@ export const useAppDependencies = () => {
4755

4856
export const useToastNotifications = () => {
4957
const {
50-
core: {
51-
notifications: { toasts: toastNotifications },
52-
},
58+
notifications: { toasts: toastNotifications },
5359
} = useAppDependencies();
5460
return toastNotifications;
5561
};
5662

5763
export const getAppProviders = (deps: AppDependencies) => {
58-
const I18nContext = deps.core.i18n.Context;
64+
const I18nContext = deps.i18n.Context;
5965

6066
// Create App dependencies context and get its provider
6167
const AppDependenciesProvider = setAppDependencies(deps);

x-pack/plugins/transform/public/app/components/toast_notification_text.test.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import React from 'react';
88
import { render } from '@testing-library/react';
99

10-
import { getAppProviders } from '../app_dependencies';
10+
import { Providers } from '../app_dependencies.mock';
1111

1212
import { ToastNotificationText } from './toast_notification_text';
1313

@@ -16,7 +16,6 @@ jest.mock('ui/new_platform');
1616

1717
describe('ToastNotificationText', () => {
1818
test('should render the text as plain text', () => {
19-
const Providers = getAppProviders({});
2019
const props = {
2120
text: 'a short text message',
2221
};
@@ -29,7 +28,6 @@ describe('ToastNotificationText', () => {
2928
});
3029

3130
test('should render the text within a modal', () => {
32-
const Providers = getAppProviders({});
3331
const props = {
3432
text:
3533
'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. ',

x-pack/plugins/transform/public/app/components/toast_notification_text.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ import { useAppDependencies } from '../app_dependencies';
2525
const MAX_SIMPLE_MESSAGE_LENGTH = 140;
2626

2727
export const ToastNotificationText: FC<{ text: any }> = ({ text }) => {
28-
const {
29-
core: { overlays },
30-
} = useAppDependencies();
28+
const { overlays } = useAppDependencies();
3129

3230
if (typeof text === 'string' && text.length <= MAX_SIMPLE_MESSAGE_LENGTH) {
3331
return text;

x-pack/plugins/transform/public/app/hooks/use_api.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ import { PreviewRequestBody } from '../common';
1212
import { EsIndex } from './use_api_types';
1313

1414
export const useApi = () => {
15-
const {
16-
core: { http },
17-
} = useAppDependencies();
15+
const { http } = useAppDependencies();
1816

1917
const basePath = http.basePath.prepend('/api/transform');
2018
const indicesBasePath = http.basePath.prepend('/api');

x-pack/plugins/transform/public/app/hooks/use_documentation_links.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { TRANSFORM_DOC_PATHS } from '../constants';
1010

1111
export const useDocumentationLinks = () => {
1212
const deps = useAppDependencies();
13-
const { ELASTIC_WEBSITE_URL, DOC_LINK_VERSION } = deps.core.docLinks;
13+
const { ELASTIC_WEBSITE_URL, DOC_LINK_VERSION } = deps.docLinks;
1414
return {
1515
esDocBasePath: `${ELASTIC_WEBSITE_URL}guide/en/elasticsearch/reference/${DOC_LINK_VERSION}/`,
1616
esIndicesCreateIndex: `${ELASTIC_WEBSITE_URL}guide/en/elasticsearch/reference/${DOC_LINK_VERSION}/indices-create-index.html#indices-create-index`,

x-pack/plugins/transform/public/app/hooks/use_request.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ import { UseRequestConfig, useRequest as _useRequest } from '../../shared_import
99
import { useAppDependencies } from '../app_dependencies';
1010

1111
export const useRequest = (config: UseRequestConfig) => {
12-
const {
13-
core: { http },
14-
} = useAppDependencies();
12+
const { http } = useAppDependencies();
1513
return _useRequest(http, config);
1614
};

x-pack/plugins/transform/public/app/hooks/use_search_items/use_search_items.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ export const useSearchItems = (defaultSavedObjectId: string | undefined) => {
2323
const [savedObjectId, setSavedObjectId] = useState(defaultSavedObjectId);
2424

2525
const appDeps = useAppDependencies();
26-
const indexPatterns = appDeps.plugins.data.indexPatterns;
27-
const uiSettings = appDeps.core.uiSettings;
28-
const savedObjectsClient = appDeps.core.savedObjects.client;
26+
const indexPatterns = appDeps.data.indexPatterns;
27+
const uiSettings = appDeps.uiSettings;
28+
const savedObjectsClient = appDeps.savedObjects.client;
2929
const savedSearches = createSavedSearchesLoader({
3030
savedObjectsClient,
3131
indexPatterns,
32-
chrome: appDeps.core.chrome,
33-
overlays: appDeps.core.overlays,
32+
chrome: appDeps.chrome,
33+
overlays: appDeps.overlays,
3434
});
3535

3636
const [searchItems, setSearchItems] = useState<SearchItems | undefined>(undefined);

x-pack/plugins/transform/public/app/sections/create_transform/components/source_index_preview/source_index_preview.test.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
*/
66

77
import React from 'react';
8-
import { render } from '@testing-library/react';
8+
import { render, wait } from '@testing-library/react';
99
import '@testing-library/jest-dom/extend-expect';
1010

11-
import { getAppProviders } from '../../../../app_dependencies';
11+
import { Providers } from '../../../../app_dependencies.mock';
1212
import { getPivotQuery } from '../../../../common';
1313
import { SearchItems } from '../../../../hooks/use_search_items';
1414

@@ -18,7 +18,8 @@ jest.mock('ui/new_platform');
1818
jest.mock('../../../../../shared_imports');
1919

2020
describe('Transform: <SourceIndexPreview />', () => {
21-
test('Minimal initialization', () => {
21+
// Using the async/await wait()/done() pattern to avoid act() errors.
22+
test('Minimal initialization', async done => {
2223
// Arrange
2324
const props = {
2425
indexPattern: {
@@ -27,7 +28,6 @@ describe('Transform: <SourceIndexPreview />', () => {
2728
} as SearchItems['indexPattern'],
2829
query: getPivotQuery('the-query'),
2930
};
30-
const Providers = getAppProviders({});
3131
const { getByText } = render(
3232
<Providers>
3333
<SourceIndexPreview {...props} />
@@ -37,5 +37,7 @@ describe('Transform: <SourceIndexPreview />', () => {
3737
// Act
3838
// Assert
3939
expect(getByText(`Source index ${props.indexPattern.title}`)).toBeInTheDocument();
40+
await wait();
41+
done();
4042
});
4143
});

x-pack/plugins/transform/public/app/sections/create_transform/components/source_index_preview/use_source_index_data.test.tsx

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7-
import React, { FC } from 'react';
8-
import ReactDOM from 'react-dom';
9-
import { act } from 'react-dom/test-utils';
7+
import { renderHook } from '@testing-library/react-hooks';
8+
import '@testing-library/jest-dom/extend-expect';
109

1110
import { SimpleQuery } from '../../../../common';
1211
import {
@@ -17,50 +16,31 @@ import {
1716

1817
jest.mock('../../../../hooks/use_api');
1918

20-
type Callback = () => void;
21-
interface TestHookProps {
22-
callback: Callback;
23-
}
24-
25-
const TestHook: FC<TestHookProps> = ({ callback }) => {
26-
callback();
27-
return null;
28-
};
29-
30-
const testHook = (callback: Callback) => {
31-
const container = document.createElement('div');
32-
document.body.appendChild(container);
33-
act(() => {
34-
ReactDOM.render(<TestHook callback={callback} />, container);
35-
});
36-
};
37-
3819
const query: SimpleQuery = {
3920
query_string: {
4021
query: '*',
4122
default_operator: 'AND',
4223
},
4324
};
4425

45-
let sourceIndexObj: UseSourceIndexDataReturnType;
46-
4726
describe('useSourceIndexData', () => {
48-
test('indexPattern set triggers loading', () => {
49-
testHook(() => {
50-
act(() => {
51-
sourceIndexObj = useSourceIndexData(
52-
{ id: 'the-id', title: 'the-title', fields: [] },
53-
query,
54-
{ pageIndex: 0, pageSize: 10 }
55-
);
56-
});
57-
});
27+
test('indexPattern set triggers loading', async done => {
28+
const { result, waitForNextUpdate } = renderHook(() =>
29+
useSourceIndexData({ id: 'the-id', title: 'the-title', fields: [] }, query, {
30+
pageIndex: 0,
31+
pageSize: 10,
32+
})
33+
);
34+
const sourceIndexObj: UseSourceIndexDataReturnType = result.current;
35+
36+
await waitForNextUpdate();
5837

5938
expect(sourceIndexObj.errorMessage).toBe('');
6039
expect(sourceIndexObj.status).toBe(SOURCE_INDEX_STATUS.LOADING);
6140
expect(sourceIndexObj.tableItems).toEqual([]);
41+
done();
6242
});
6343

6444
// TODO add more tests to check data retrieved via `api.esSearch()`.
65-
// This needs more investigation in regards to jest/enzyme's React Hooks support.
45+
// This needs more investigation in regards to jest's React Hooks support.
6646
});

0 commit comments

Comments
 (0)