-
Notifications
You must be signed in to change notification settings - Fork 959
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Discover] fix: Clean up sync URL subscription in Discover plugin top…
…Nav (#9316) * [Discover] fix: Clean up sync URL subscription in Discover plugin topNav Signed-off-by: Joey Liu <[email protected]> * Changeset file for PR #9316 created/updated * Update unit test Signed-off-by: Joey Liu <[email protected]> * Revert save modal change Signed-off-by: Joey Liu <[email protected]> --------- Signed-off-by: Joey Liu <[email protected]> Co-authored-by: opensearch-changeset-bot[bot] <154024398+opensearch-changeset-bot[bot]@users.noreply.github.com>
- Loading branch information
1 parent
4da33a1
commit 8cc5f84
Showing
9 changed files
with
213 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
fix: | ||
- Clean up sync URL subscription in Discover plugin topNav ([#9316](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/9316)) |
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
139 changes: 139 additions & 0 deletions
139
src/plugins/data/public/query/state_sync/use_sync_state_with_url.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,139 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { Subscription } from 'rxjs'; | ||
import { createBrowserHistory, History } from 'history'; | ||
import { FilterManager } from '../filter_manager'; | ||
import { getFilter } from '../filter_manager/test_helpers/get_stub_filter'; | ||
import { | ||
DataStorage, | ||
Filter, | ||
FilterStateStore, | ||
IndexPatternsService, | ||
UI_SETTINGS, | ||
} from '../../../common'; | ||
import { coreMock } from '../../../../../core/public/mocks'; | ||
import { | ||
createOsdUrlStateStorage, | ||
IOsdUrlStateStorage, | ||
} from '../../../../opensearch_dashboards_utils/public'; | ||
import { QueryService, QueryStart } from '../query_service'; | ||
import { TimefilterContract } from '../timefilter'; | ||
import { ISearchInterceptor } from '../../search'; | ||
import { act, renderHook } from '@testing-library/react-hooks'; | ||
import { useSyncQueryStateWithUrl } from './use_sync_state_with_url'; | ||
import * as SyncQueryStateWithUrl from './sync_state_with_url'; | ||
|
||
const mockStopSync = jest.fn(); | ||
const mockSyncQueryStateWithUrl = jest.fn().mockReturnValue({ | ||
stop: mockStopSync, | ||
}); | ||
|
||
const setupMock = coreMock.createSetup(); | ||
const startMock = coreMock.createStart(); | ||
|
||
setupMock.uiSettings.get.mockImplementation((key: string) => { | ||
switch (key) { | ||
case 'defaultIndex': | ||
return 'logstash-*'; | ||
case UI_SETTINGS.FILTERS_PINNED_BY_DEFAULT: | ||
return true; | ||
case 'timepicker:timeDefaults': | ||
return { from: 'now-15m', to: 'now' }; | ||
case 'search:queryLanguage': | ||
return 'kuery'; | ||
case UI_SETTINGS.TIMEPICKER_REFRESH_INTERVAL_DEFAULTS: | ||
return { pause: false, value: 0 }; | ||
case UI_SETTINGS.QUERY_ENHANCEMENTS_ENABLED: | ||
return false; | ||
case UI_SETTINGS.SEARCH_MAX_RECENT_DATASETS: | ||
return 4; | ||
default: | ||
throw new Error(`sync_query test: not mocked uiSetting: ${key}`); | ||
} | ||
}); | ||
|
||
describe('use_sync_query_state_with_url', () => { | ||
let queryServiceStart: QueryStart; | ||
let filterManager: FilterManager; | ||
let timefilter: TimefilterContract; | ||
let osdUrlStateStorage: IOsdUrlStateStorage; | ||
let history: History; | ||
let indexPatternsMock: IndexPatternsService; | ||
|
||
beforeEach(() => { | ||
indexPatternsMock = ({ | ||
get: jest.fn(), | ||
} as unknown) as IndexPatternsService; | ||
}); | ||
|
||
let filterManagerChangeSub: Subscription; | ||
let filterManagerChangeTriggered = jest.fn(); | ||
let mockSearchInterceptor: jest.Mocked<ISearchInterceptor>; | ||
|
||
let gF: Filter; | ||
let aF: Filter; | ||
|
||
beforeEach(() => { | ||
const queryService = new QueryService(); | ||
queryService.setup({ | ||
uiSettings: setupMock.uiSettings, | ||
storage: new DataStorage(window.localStorage, 'opensearch_dashboards.'), | ||
sessionStorage: new DataStorage(window.sessionStorage, 'opensearch_dashboards.'), | ||
defaultSearchInterceptor: mockSearchInterceptor, | ||
application: setupMock.application, | ||
notifications: setupMock.notifications, | ||
}); | ||
queryServiceStart = queryService.start({ | ||
indexPatterns: indexPatternsMock, | ||
uiSettings: startMock.uiSettings, | ||
storage: new DataStorage(window.localStorage, 'opensearch_dashboards.'), | ||
savedObjectsClient: startMock.savedObjects.client, | ||
application: startMock.application, | ||
notifications: startMock.notifications, | ||
}); | ||
filterManager = queryServiceStart.filterManager; | ||
timefilter = queryServiceStart.timefilter.timefilter; | ||
|
||
filterManagerChangeTriggered = jest.fn(); | ||
filterManagerChangeSub = filterManager.getUpdates$().subscribe(filterManagerChangeTriggered); | ||
|
||
window.location.href = '/'; | ||
history = createBrowserHistory(); | ||
osdUrlStateStorage = createOsdUrlStateStorage({ useHash: false, history }); | ||
|
||
gF = getFilter(FilterStateStore.GLOBAL_STATE, true, true, 'key1', 'value1'); | ||
aF = getFilter(FilterStateStore.APP_STATE, true, true, 'key3', 'value3'); | ||
|
||
jest | ||
.spyOn(SyncQueryStateWithUrl, 'syncQueryStateWithUrl') | ||
.mockImplementation(mockSyncQueryStateWithUrl); | ||
}); | ||
afterEach(() => { | ||
filterManagerChangeSub.unsubscribe(); | ||
}); | ||
|
||
it('Should invoke connectStorageToQueryState', () => { | ||
const { result } = renderHook(() => | ||
useSyncQueryStateWithUrl(queryServiceStart, osdUrlStateStorage) | ||
); | ||
|
||
act(() => { | ||
result.current.startSyncingQueryStateWithUrl(); | ||
}); | ||
|
||
expect(mockSyncQueryStateWithUrl).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('Should call stop callback when hook unmount', () => { | ||
const { unmount } = renderHook(() => | ||
useSyncQueryStateWithUrl(queryServiceStart, osdUrlStateStorage) | ||
); | ||
|
||
unmount(); | ||
|
||
expect(mockStopSync).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |
41 changes: 41 additions & 0 deletions
41
src/plugins/data/public/query/state_sync/use_sync_state_with_url.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,41 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { useEffect, useState } from 'react'; | ||
import { IOsdUrlStateStorage } from '../../../../opensearch_dashboards_utils/public'; | ||
import { QuerySetup, QueryStart } from '../query_service'; | ||
import { syncQueryStateWithUrl } from './sync_state_with_url'; | ||
|
||
/** | ||
* Hook version of syncQueryStateWithUrl, automatically clean up subscriptions | ||
* @param QueryService: either setup or start | ||
* @param osdUrlStateStorage to use for syncing | ||
*/ | ||
export const useSyncQueryStateWithUrl = ( | ||
query: Pick<QueryStart | QuerySetup, 'filterManager' | 'timefilter' | 'queryString' | 'state$'>, | ||
osdUrlStateStorage: IOsdUrlStateStorage | ||
) => { | ||
const [startSync, setStartSync] = useState(false); | ||
useEffect(() => { | ||
let stopSync: () => void; | ||
|
||
if (startSync) { | ||
// starts syncing `_g` portion of url with query services | ||
stopSync = syncQueryStateWithUrl(query, osdUrlStateStorage).stop; | ||
} | ||
|
||
return () => { | ||
if (stopSync) { | ||
stopSync(); | ||
} | ||
}; | ||
}, [osdUrlStateStorage, query, startSync]); | ||
|
||
const startSyncingQueryStateWithUrl = () => { | ||
setStartSync(true); | ||
}; | ||
|
||
return { startSyncingQueryStateWithUrl }; | ||
}; |
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