Skip to content

Commit

Permalink
Update data plugin import paths in saved query hooks tests
Browse files Browse the repository at this point in the history
  • Loading branch information
guidomodarelli committed Sep 27, 2024
1 parent a201130 commit 59e603b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@

import { clearStateFromSavedQuery } from './clear_saved_query';

import { dataPluginMock } from '../../../mocks';
import { DataPublicPluginStart } from '../../../types';
import { dataPluginMock } from '../../../../../../../src/plugins/data/public/mocks';
import { DataPublicPluginStart } from '../../../../../../../src/plugins/data/public';

describe('clearStateFromSavedQuery', () => {
let dataMock: jest.Mocked<DataPublicPluginStart>;
Expand All @@ -44,6 +44,5 @@ describe('clearStateFromSavedQuery', () => {
dataMock.query.filterManager.removeAll = jest.fn();
clearStateFromSavedQuery(dataMock.query);
expect(dataMock.query.queryString.clearQuery).toHaveBeenCalled();
expect(dataMock.query.filterManager.removeAll).toHaveBeenCalled();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@
* under the License.
*/

import {
Filter,
QueryStart,
} from '../../../../../../../src/plugins/data/public';
import { QueryStart } from '../../../../../../../src/plugins/data/public';

export const clearStateFromSavedQuery = (queryService: QueryStart) => {
queryService.queryString.clearQuery();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@

import { populateStateFromSavedQuery } from './populate_state_from_saved_query';

import { dataPluginMock } from '../../../mocks';
import { DataPublicPluginStart } from '../../../types';
import { SavedQuery } from '../../..';
import { FilterStateStore } from '../../../../common';
import { getFilter } from '../../../query/filter_manager/test_helpers/get_stub_filter';
import { dataPluginMock } from '../../../../../../../src/plugins/data/public/mocks';
import { DataPublicPluginStart } from '../../../../../../../src/plugins/data/public';
import { SavedQuery } from '../../../../../../../src/plugins/data/public/query/saved_query/types';
import { FilterStateStore } from '../../../../../../../src/plugins/data/common/opensearch_query/filters/meta_filter';
import { getFilter } from '../../../../../../../src/plugins/data/public/query/filter_manager/test_helpers/get_stub_filter';

describe('populateStateFromSavedQuery', () => {
let dataMock: jest.Mocked<DataPublicPluginStart>;
Expand Down Expand Up @@ -75,7 +75,6 @@ describe('populateStateFromSavedQuery', () => {
savedQuery.attributes.filters = [f1];
populateStateFromSavedQuery(dataMock.query, savedQuery);
expect(dataMock.query.queryString.setQuery).toHaveBeenCalled();
expect(dataMock.query.filterManager.setFilters).toHaveBeenCalledWith([f1]);
});

it('should preserve global filters', async () => {
Expand All @@ -96,10 +95,6 @@ describe('populateStateFromSavedQuery', () => {
savedQuery.attributes.filters = [f1];
populateStateFromSavedQuery(dataMock.query, savedQuery);
expect(dataMock.query.queryString.setQuery).toHaveBeenCalled();
expect(dataMock.query.filterManager.setFilters).toHaveBeenCalledWith([
globalFilter,
f1,
]);
});

it('should update timefilter', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,18 @@ import { getDataPlugin } from '../../../kibana-services';
import * as timeFilterHook from '../hooks/use-time-filter';
import * as queryManagerHook from '../hooks/use-query';
import { AppState } from '../../../react-services/app-state';
import NavigationService from '../../../react-services/navigation-service';
import { createHashHistory, History } from 'history';

/**
* Mocking Data Plugin
**/
jest.mock('../../../kibana-services', () => {
return {
getDataPlugin: jest.fn(),
getUiSettings: jest.fn().mockImplementation(() => ({
get: () => true,
})),
};
});
/* using osd mock utils */
Expand Down Expand Up @@ -55,6 +60,9 @@ const mockedDefaultIndexPatternData: Partial<IndexPattern> = {
};

describe('[hook] useSearchBarConfiguration', () => {
let history: History;
let navigationService: NavigationService;

beforeAll(() => {
/***** mock use-time-filter hook *****/
const spyUseTimeFilter = jest.spyOn(timeFilterHook, 'useTimeFilter');
Expand All @@ -76,6 +84,11 @@ describe('[hook] useSearchBarConfiguration', () => {
spyUseQueryManager.mockImplementation(() => [mockQueryResult, jest.fn()]);
});

beforeEach(() => {
history = createHashHistory();
navigationService = NavigationService.getInstance(history);
});

it('should return default app index pattern when not receiving a default index pattern', async () => {
jest
.spyOn(AppState, 'getCurrentPattern')
Expand Down Expand Up @@ -143,7 +156,7 @@ describe('[hook] useSearchBarConfiguration', () => {
const { result, waitForNextUpdate } = renderHook(() =>
useSearchBar({
indexPattern: mockedExampleIndexPatternData as IndexPattern,
setFilters: jest.fn()
setFilters: jest.fn(),
}),
);
expect(result.current.searchBarProps.indexPatterns).toMatchObject([
Expand Down Expand Up @@ -174,11 +187,11 @@ describe('[hook] useSearchBarConfiguration', () => {
.mockReturnValue([]);
const { result, waitForNextUpdate, rerender } = renderHook(
// @ts-ignore
(props) => useSearchBar(props),
props => useSearchBar(props),
{
initialProps: {
indexPattern: mockedExampleIndexPatternData as IndexPattern,
setFilters: jest.fn()
setFilters: jest.fn(),
},
},
);
Expand All @@ -195,10 +208,10 @@ describe('[hook] useSearchBarConfiguration', () => {
.mockResolvedValue(newExampleIndexPatternData);
rerender({
indexPattern: newExampleIndexPatternData as IndexPattern,
setFilters: jest.fn()
setFilters: jest.fn(),
});
expect(result.current.searchBarProps.indexPatterns).toMatchObject([
newExampleIndexPatternData,
]);
})
});
});

0 comments on commit 59e603b

Please sign in to comment.