-
-
Notifications
You must be signed in to change notification settings - Fork 666
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Persist navigation settings (#7144)
- Loading branch information
Showing
4 changed files
with
86 additions
and
9 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,61 @@ | ||
import { useLocalStorageState } from './useLocalStorageState'; | ||
import { createLocalStorage } from '../utils/createLocalStorage'; | ||
import { render } from 'utils/testRenderer'; | ||
import { screen, waitFor } from '@testing-library/react'; | ||
import type { FC } from 'react'; | ||
|
||
const TestComponent: FC<{ | ||
keyName: string; | ||
initialValue: any; | ||
}> = ({ keyName, initialValue }) => { | ||
const [value, setValue] = useLocalStorageState(keyName, initialValue); | ||
|
||
return ( | ||
<div> | ||
<span data-testid='storedValue'>{value}</span> | ||
<button | ||
type='submit' | ||
onClick={() => setValue('updatedValue')} | ||
data-testid='updateButton' | ||
/> | ||
</div> | ||
); | ||
}; | ||
|
||
describe('useLocalStorageState', () => { | ||
beforeEach(() => { | ||
window.localStorage.clear(); | ||
}); | ||
|
||
it('should initialize with the initial value if local storage is empty', () => { | ||
render(<TestComponent keyName='testKey' initialValue='initialValue' />); | ||
|
||
expect(screen.getByTestId('storedValue').textContent).toBe( | ||
'initialValue', | ||
); | ||
}); | ||
|
||
it('updates the local storage and state when value changes', async () => { | ||
render(<TestComponent keyName='testKey' initialValue='initialValue' />); | ||
|
||
screen.getByTestId('updateButton').click(); | ||
|
||
expect(screen.getByTestId('storedValue').textContent).toBe( | ||
'updatedValue', | ||
); | ||
await waitFor(() => { | ||
const { value } = createLocalStorage('testKey', {}); | ||
expect(value).toStrictEqual('updatedValue'); | ||
}); | ||
}); | ||
|
||
it('initializes with the value from local storage if available', async () => { | ||
createLocalStorage('testKey', {}).setValue('storedValue'); | ||
|
||
render(<TestComponent keyName='testKey' initialValue='initialValue' />); | ||
|
||
expect(screen.getByTestId('storedValue').textContent).toBe( | ||
'storedValue', | ||
); | ||
}); | ||
}); |
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,18 @@ | ||
import { useEffect, useState } from 'react'; | ||
import { createLocalStorage } from '../utils/createLocalStorage'; | ||
|
||
export const useLocalStorageState = <T extends object | string>( | ||
key: string, | ||
initialValue: T, | ||
) => { | ||
const { value: initialStoredValue, setValue: setStoredValue } = | ||
createLocalStorage<T>(key, initialValue); | ||
|
||
const [localValue, setLocalValue] = useState<T>(initialStoredValue); | ||
|
||
useEffect(() => { | ||
setStoredValue(localValue); | ||
}, [localValue]); | ||
|
||
return [localValue, setLocalValue] as const; | ||
}; |
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