-
-
Notifications
You must be signed in to change notification settings - Fork 725
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: persistent search queries (#4624)
- Loading branch information
Showing
7 changed files
with
145 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { createLocalStorage } from 'utils/createLocalStorage'; | ||
import { render } from 'utils/testRenderer'; | ||
import { fireEvent, screen } from '@testing-library/react'; | ||
import { UIProviderContainer } from '../../providers/UIProvider/UIProviderContainer'; | ||
import { Search } from './Search'; | ||
import { SEARCH_INPUT } from 'utils/testIds'; | ||
|
||
const testDisplayComponent = ( | ||
<UIProviderContainer> | ||
<Search | ||
hasFilters | ||
onChange={() => {}} | ||
id="localStorageId" | ||
getSearchContext={() => ({ | ||
data: [], | ||
columns: [], | ||
searchValue: '', | ||
})} | ||
/> | ||
</UIProviderContainer> | ||
); | ||
|
||
test('should read saved query from local storage', async () => { | ||
const { value, setValue } = createLocalStorage( | ||
'Search:localStorageId:v1', | ||
{} | ||
); | ||
setValue({ | ||
query: 'oldquery', | ||
}); | ||
|
||
render(testDisplayComponent); | ||
|
||
const input = screen.getByTestId(SEARCH_INPUT); | ||
|
||
input.focus(); | ||
|
||
await screen.findByText('oldquery'); // local storage saved search query | ||
|
||
screen.getByText('oldquery').click(); // click history hint | ||
|
||
expect(screen.getByDisplayValue('oldquery')).toBeInTheDocument(); // check if input updates | ||
|
||
fireEvent.change(input, { target: { value: 'newquery' } }); | ||
|
||
expect(screen.getByText('newquery')).toBeInTheDocument(); // new saved query updated | ||
}); | ||
|
||
test('should update saved query without local storage', async () => { | ||
render(testDisplayComponent); | ||
|
||
const input = screen.getByTestId(SEARCH_INPUT); | ||
|
||
input.focus(); | ||
|
||
fireEvent.change(input, { target: { value: 'newquery' } }); | ||
|
||
expect(screen.getByText('newquery')).toBeInTheDocument(); // new saved query updated | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { createLocalStorage } from 'utils/createLocalStorage'; | ||
import { useEffect, useState } from 'react'; | ||
|
||
// if you provided persistent id the query will be persisted in local storage | ||
export const useSavedQuery = (id?: string) => { | ||
const { value, setValue } = createLocalStorage( | ||
`Search:${id || 'default'}:v1`, | ||
{ | ||
query: '', | ||
} | ||
); | ||
const [savedQuery, setSavedQuery] = useState(value.query); | ||
|
||
useEffect(() => { | ||
if (id && savedQuery.trim().length > 0) { | ||
setValue({ query: savedQuery }); | ||
} | ||
}, [id, savedQuery]); | ||
|
||
return { | ||
savedQuery, | ||
setSavedQuery: (newValue: string) => { | ||
if (newValue.trim().length > 0) { | ||
setSavedQuery(newValue); | ||
} | ||
}, | ||
}; | ||
}; |
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