Skip to content

Commit

Permalink
[Discover] Fix Initialization if No Saved Query (#8930)
Browse files Browse the repository at this point in the history
* replace default query with current query

Signed-off-by: Sean Li <[email protected]>

* Changeset file for PR #8930 created/updated

* adding unit tests

Signed-off-by: Sean Li <[email protected]>

---------

Signed-off-by: Sean Li <[email protected]>
Co-authored-by: opensearch-changeset-bot[bot] <154024398+opensearch-changeset-bot[bot]@users.noreply.github.com>
  • Loading branch information
sejli and opensearch-changeset-bot[bot] authored Dec 3, 2024
1 parent b31206a commit 080b0db
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 4 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/8930.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
fix:
- Update saved search initialization logic to use current query instead of default query ([#8930](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/8930))
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,37 @@ jest.mock('./use_index_pattern', () => ({
useIndexPattern: jest.fn(),
}));

const mockQuery = {
query: 'test query',
language: 'test language',
};

const mockDefaultQuery = {
query: 'default query',
language: 'default language',
};

const mockSavedSearch = {
id: 'test-saved-search',
title: 'Test Saved Search',
searchSource: {
setField: jest.fn(),
getField: jest.fn(),
getField: jest.fn().mockReturnValue(mockQuery),
fetch: jest.fn(),
getSearchRequestBody: jest.fn().mockResolvedValue({}),
getOwnField: jest.fn(),
getDataFrame: jest.fn(() => ({ name: 'test-pattern' })),
},
getFullPath: jest.fn(),
getOpenSearchType: jest.fn(),
};

const mockSavedSearchEmptyQuery = {
id: 'test-saved-search',
title: 'Test Saved Search',
searchSource: {
setField: jest.fn(),
getField: jest.fn().mockReturnValue(undefined),
fetch: jest.fn(),
getSearchRequestBody: jest.fn().mockResolvedValue({}),
getOwnField: jest.fn(),
Expand Down Expand Up @@ -215,4 +240,36 @@ describe('useSearch', () => {
expect.objectContaining({ status: ResultStatus.LOADING, rows: [] })
);
});

it('should load saved search', async () => {
const services = createMockServices();
services.data.query.queryString.setQuery = jest.fn();

const { waitForNextUpdate } = renderHook(() => useSearch(services), {
wrapper,
});

await act(async () => {
await waitForNextUpdate();
});

expect(services.data.query.queryString.setQuery).toBeCalledWith(mockQuery);
});

it('if no saved search, use get query', async () => {
const services = createMockServices();
services.getSavedSearchById = jest.fn().mockResolvedValue(mockSavedSearchEmptyQuery);
services.data.query.queryString.getQuery = jest.fn().mockReturnValue(mockDefaultQuery);
services.data.query.queryString.setQuery = jest.fn();

const { waitForNextUpdate } = renderHook(() => useSearch(services), {
wrapper,
});

await act(async () => {
await waitForNextUpdate();
});

expect(services.data.query.queryString.setQuery).toBeCalledWith(mockDefaultQuery);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -392,8 +392,7 @@ export const useSearch = (services: DiscoverViewServices) => {
const savedSearchInstance = await getSavedSearchById(savedSearchId);

const query =
savedSearchInstance.searchSource.getField('query') ||
data.query.queryString.getDefaultQuery();
savedSearchInstance.searchSource.getField('query') || data.query.queryString.getQuery();

const isEnhancementsEnabled = await uiSettings.get('query:enhancements:enabled');
if (isEnhancementsEnabled && query.dataset) {
Expand Down Expand Up @@ -432,7 +431,7 @@ export const useSearch = (services: DiscoverViewServices) => {
}

filterManager.setAppFilters(actualFilters);
data.query.queryString.setQuery(savedQuery ? data.query.queryString.getQuery() : query);
data.query.queryString.setQuery(query);
setSavedSearch(savedSearchInstance);

if (savedSearchInstance?.id) {
Expand Down

0 comments on commit 080b0db

Please sign in to comment.