github.com/thanos-io/thanos@v0.32.5/pkg/ui/react-app/src/hooks/useLocalStorage.test.tsx (about) 1 import { useLocalStorage } from './useLocalStorage'; 2 import { renderHook, act } from '@testing-library/react-hooks'; 3 4 describe('useLocalStorage', () => { 5 it('returns the initialState', () => { 6 const initialState = { a: 1, b: 2 }; 7 const { result } = renderHook(() => useLocalStorage('mystorage', initialState)); 8 expect(result.current[0]).toEqual(initialState); 9 }); 10 it('stores the initialState as serialized json in localstorage', () => { 11 const key = 'mystorage'; 12 const initialState = { a: 1, b: 2 }; 13 renderHook(() => useLocalStorage(key, initialState)); 14 expect(localStorage.getItem(key)).toEqual(JSON.stringify(initialState)); 15 }); 16 it('returns a setValue function that can reset local storage', () => { 17 const key = 'mystorage'; 18 const initialState = { a: 1, b: 2 }; 19 const { result } = renderHook(() => useLocalStorage(key, initialState)); 20 const newValue = { a: 2, b: 5 }; 21 act(() => { 22 result.current[1](newValue); 23 }); 24 expect(result.current[0]).toEqual(newValue); 25 expect(localStorage.getItem(key)).toEqual(JSON.stringify(newValue)); 26 }); 27 });