forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ML] Fix unnecessary trigger of wildcard field type search for ML plu…
…gin routes. (elastic#84605) Passing in an empty string '' to useResolver() would trigger a wild card search across all indices and fields, potentially causing a timeout and the page would fail to load. The following pages were affected: Single Metric Viewer, Data frame analytics models list, Data frame analytics jobs list, Data frame analytics exploration page, File Data Visualizer (Data visualizer - Import data from a log file). This PR fixes it by passing undefined instead of '' to useResolver to avoid calling _fields_for_wildcard with an empty pattern. Jest tests were added to cover the two parameter scenarios empty string/undefined.
- Loading branch information
Showing
10 changed files
with
150 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
x-pack/plugins/ml/public/application/routing/use_resolver.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* 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 { renderHook, act } from '@testing-library/react-hooks'; | ||
|
||
import { IUiSettingsClient } from 'kibana/public'; | ||
|
||
import { useCreateAndNavigateToMlLink } from '../contexts/kibana/use_create_url'; | ||
import { useNotifications } from '../contexts/kibana'; | ||
|
||
import { useResolver } from './use_resolver'; | ||
|
||
jest.mock('../contexts/kibana/use_create_url', () => { | ||
return { | ||
useCreateAndNavigateToMlLink: jest.fn(), | ||
}; | ||
}); | ||
|
||
jest.mock('../contexts/kibana', () => { | ||
return { | ||
useMlUrlGenerator: () => ({ | ||
createUrl: jest.fn(), | ||
}), | ||
useNavigateToPath: () => jest.fn(), | ||
useNotifications: jest.fn(), | ||
}; | ||
}); | ||
|
||
const addError = jest.fn(); | ||
(useNotifications as jest.Mock).mockImplementation(() => ({ | ||
toasts: { addSuccess: jest.fn(), addDanger: jest.fn(), addError }, | ||
})); | ||
|
||
const redirectToJobsManagementPage = jest.fn(() => Promise.resolve()); | ||
(useCreateAndNavigateToMlLink as jest.Mock).mockImplementation(() => redirectToJobsManagementPage); | ||
|
||
describe('useResolver', () => { | ||
afterEach(() => { | ||
jest.useFakeTimers(); | ||
}); | ||
afterEach(() => { | ||
jest.advanceTimersByTime(0); | ||
jest.useRealTimers(); | ||
}); | ||
|
||
it('should accept undefined as indexPatternId and savedSearchId.', async () => { | ||
const { result, waitForNextUpdate } = renderHook(() => | ||
useResolver(undefined, undefined, {} as IUiSettingsClient, {}) | ||
); | ||
|
||
await act(async () => { | ||
await waitForNextUpdate(); | ||
}); | ||
|
||
expect(result.current).toStrictEqual({ | ||
context: { | ||
combinedQuery: { | ||
bool: { | ||
must: [ | ||
{ | ||
match_all: {}, | ||
}, | ||
], | ||
}, | ||
}, | ||
currentIndexPattern: null, | ||
currentSavedSearch: null, | ||
indexPatterns: null, | ||
kibanaConfig: {}, | ||
}, | ||
results: {}, | ||
}); | ||
expect(addError).toHaveBeenCalledTimes(0); | ||
expect(redirectToJobsManagementPage).toHaveBeenCalledTimes(0); | ||
}); | ||
|
||
it('should add an error toast and redirect if indexPatternId is an empty string.', async () => { | ||
const { result } = renderHook(() => useResolver('', undefined, {} as IUiSettingsClient, {})); | ||
|
||
await act(async () => {}); | ||
|
||
expect(result.current).toStrictEqual({ context: null, results: {} }); | ||
expect(addError).toHaveBeenCalledTimes(1); | ||
expect(redirectToJobsManagementPage).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters