-
Notifications
You must be signed in to change notification settings - Fork 906
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Discover] Decouple data$
updates to prevent rows clearing on hook re-render
#8806
Merged
joshuali925
merged 6 commits into
opensearch-project:main
from
joshuali925:fix-discover-clear-rows
Nov 7, 2024
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e68c98f
[Discover] Decouple `data$` updates to prevent rows clearing on hook …
joshuali925 c8dc88e
Changeset file for PR #8806 created/updated
opensearch-changeset-bot[bot] 2327463
re-initialize data$ when the selected dataset changes
joshuali925 dac9fa4
add unit tests
joshuali925 fc4d91f
increase coverage
joshuali925 9dd86ac
fix flaky unit tests
joshuali925 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: | ||
- Decouple data$ updates to prevent rows clearing on hook re-render ([#8806](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/8806)) |
156 changes: 156 additions & 0 deletions
156
src/plugins/discover/public/application/view_components/utils/use_search.test.tsx
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,156 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { act, renderHook } from '@testing-library/react-hooks'; | ||
import { createMemoryHistory } from 'history'; | ||
import React from 'react'; | ||
import { Provider } from 'react-redux'; | ||
import { Router } from 'react-router-dom'; | ||
import { Subject } from 'rxjs'; | ||
import { createDataExplorerServicesMock } from '../../../../../data_explorer/public/utils/mocks'; | ||
import { DiscoverViewServices } from '../../../build_services'; | ||
import { discoverPluginMock } from '../../../mocks'; | ||
import { ResultStatus, useSearch } from './use_search'; | ||
|
||
jest.mock('./use_index_pattern', () => ({ | ||
useIndexPattern: jest.fn(), | ||
})); | ||
|
||
const mockSavedSearch = { | ||
id: 'test-saved-search', | ||
title: 'Test Saved Search', | ||
searchSource: { | ||
setField: jest.fn(), | ||
getField: jest.fn(), | ||
fetch: jest.fn(), | ||
getSearchRequestBody: jest.fn().mockResolvedValue({}), | ||
getOwnField: jest.fn(), | ||
getDataFrame: jest.fn(() => ({ name: 'test-pattern' })), | ||
}, | ||
getFullPath: jest.fn(), | ||
getOpenSearchType: jest.fn(), | ||
}; | ||
|
||
const createMockServices = (): DiscoverViewServices => { | ||
const dataExplorerServicesMock = createDataExplorerServicesMock(); | ||
const discoverServicesMock = discoverPluginMock.createDiscoverServicesMock(); | ||
const services: DiscoverViewServices = { | ||
...dataExplorerServicesMock, | ||
...discoverServicesMock, | ||
}; | ||
|
||
(services.data.query.timefilter.timefilter.getRefreshInterval as jest.Mock).mockReturnValue({ | ||
pause: false, | ||
value: 10, | ||
}); | ||
services.getSavedSearchById = jest.fn().mockResolvedValue(mockSavedSearch); | ||
return services; | ||
}; | ||
|
||
const history = createMemoryHistory(); | ||
const mockStore = { | ||
getState: () => ({ | ||
discover: { | ||
savedSearch: 'test-saved-search', | ||
sort: [], | ||
interval: 'auto', | ||
savedQuery: undefined, | ||
}, | ||
}), | ||
subscribe: jest.fn(), | ||
dispatch: jest.fn(), | ||
}; | ||
const wrapper: React.FC = ({ children }) => { | ||
return ( | ||
<Provider store={mockStore}> | ||
<Router history={history}>{children}</Router> | ||
</Provider> | ||
); | ||
}; | ||
|
||
describe('useSearch', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should initialize with loading state when search on page load is enabled', async () => { | ||
const services = createMockServices(); | ||
(services.uiSettings.get as jest.Mock).mockReturnValueOnce(true); | ||
|
||
const { result } = renderHook(() => useSearch(services), { wrapper }); | ||
|
||
expect(result.current.data$.getValue()).toEqual( | ||
expect.objectContaining({ status: ResultStatus.LOADING }) | ||
); | ||
}); | ||
|
||
it('should initialize with uninitialized state when search on page load is disabled', async () => { | ||
const services = createMockServices(); | ||
(services.uiSettings.get as jest.Mock).mockReturnValueOnce(false); | ||
(services.data.query.timefilter.timefilter.getRefreshInterval as jest.Mock).mockReturnValue({ | ||
pause: true, | ||
value: 10, | ||
}); | ||
|
||
const { result } = renderHook(() => useSearch(services), { wrapper }); | ||
expect(result.current.data$.getValue()).toEqual( | ||
expect.objectContaining({ status: ResultStatus.UNINITIALIZED }) | ||
); | ||
}); | ||
|
||
it('should update startTime when hook rerenders', async () => { | ||
const services = createMockServices(); | ||
|
||
const { result, rerender } = renderHook(() => useSearch(services), { | ||
wrapper, | ||
}); | ||
|
||
const initialStartTime = result.current.data$.getValue().queryStatus?.startTime; | ||
expect(initialStartTime).toBeDefined(); | ||
|
||
rerender(); | ||
const newStartTime = result.current.data$.getValue().queryStatus?.startTime; | ||
expect(newStartTime).toBeDefined(); | ||
expect(newStartTime).not.toEqual(initialStartTime); | ||
}); | ||
|
||
it('should reset data observable when dataset changes', async () => { | ||
const services = createMockServices(); | ||
const mockDatasetUpdates$ = new Subject(); | ||
services.data.query.queryString.getUpdates$ = jest.fn().mockReturnValue(mockDatasetUpdates$); | ||
|
||
const { result, waitForNextUpdate } = renderHook(() => useSearch(services), { | ||
wrapper, | ||
}); | ||
|
||
act(() => { | ||
result.current.data$.next({ status: ResultStatus.READY }); | ||
}); | ||
|
||
expect(result.current.data$.getValue()).toEqual( | ||
expect.objectContaining({ status: ResultStatus.READY }) | ||
); | ||
|
||
act(() => { | ||
mockDatasetUpdates$.next({ | ||
dataset: { id: 'new-dataset-id', title: 'New Dataset', type: 'INDEX_PATTERN' }, | ||
}); | ||
mockDatasetUpdates$.next({ | ||
dataset: { id: 'new-dataset-id', title: 'New Dataset', type: 'INDEX_PATTERN' }, | ||
}); | ||
mockDatasetUpdates$.next({ | ||
dataset: { id: 'new-dataset-id2', title: 'New Dataset', type: 'INDEX_PATTERN' }, | ||
}); | ||
}); | ||
|
||
await act(async () => { | ||
await waitForNextUpdate(); | ||
}); | ||
|
||
expect(result.current.data$.getValue()).toEqual( | ||
expect.objectContaining({ status: ResultStatus.LOADING }) | ||
); | ||
}); | ||
}); |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason why this is separated into two
useEffect()
's? If mimicking the originaluseMemo()
, could be done in a single update and reduce the number ofuseEffects()
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
startTime updates should only update
queryStatus.startTime
and not resetstatus
. it's the bug this PR is trying to fix, we should decouple queryStatus and status updates