diff --git a/docs/useSessionStorage.md b/docs/useSessionStorage.md index 1ac6956d8f..3db117ce18 100644 --- a/docs/useSessionStorage.md +++ b/docs/useSessionStorage.md @@ -2,6 +2,10 @@ React side-effect hook that manages a single `sessionStorage` key. +Each hook instance maintains its own state. When instances share a key, changing one +instance's value persists that value; rerendering an instance whose state is unchanged +leaves the stored value intact. + ## Usage diff --git a/src/useSessionStorage.ts b/src/useSessionStorage.ts index 67e97c8814..13a362890a 100644 --- a/src/useSessionStorage.ts +++ b/src/useSessionStorage.ts @@ -37,7 +37,7 @@ const useSessionStorage = ( // If user is in private mode or has storage restriction // sessionStorage can throw. Also JSON.stringify can throw. } - }); + }, [key, raw, state]); return [state, setState]; }; diff --git a/tests/useSessionStorage.server.test.ts b/tests/useSessionStorage.server.test.ts new file mode 100644 index 0000000000..497655416c --- /dev/null +++ b/tests/useSessionStorage.server.test.ts @@ -0,0 +1,14 @@ +import useSessionStorage from '../src/useSessionStorage'; + +jest.mock('../src/misc/util', () => ({ + isBrowser: false, +})); + +describe('useSessionStorage on the server', () => { + it('returns the initial value and a safe state setter', () => { + const [value, setValue] = useSessionStorage('value', 'initial'); + + expect(value).toBe('initial'); + expect(setValue('updated')).toBeUndefined(); + }); +}); diff --git a/tests/useSessionStorage.test.ts b/tests/useSessionStorage.test.ts new file mode 100644 index 0000000000..3907f25b74 --- /dev/null +++ b/tests/useSessionStorage.test.ts @@ -0,0 +1,89 @@ +import { act, renderHook } from '@testing-library/react-hooks'; +import useSessionStorage from '../src/useSessionStorage'; + +describe('useSessionStorage', () => { + afterEach(() => { + sessionStorage.clear(); + jest.clearAllMocks(); + }); + + it.each([false, true])( + 'preserves updates when multiple instances share a key with raw=%s', + (raw) => { + const { result, rerender } = renderHook(() => ({ + first: useSessionStorage('shared', 'initial', raw), + second: useSessionStorage('shared', 'initial', raw), + })); + + act(() => result.current.first[1]('updated')); + + expect(result.current.first[0]).toBe('updated'); + expect(result.current.second[0]).toBe('initial'); + expect(sessionStorage.getItem('shared')).toBe(raw ? 'updated' : '"updated"'); + + rerender(); + + expect(sessionStorage.getItem('shared')).toBe(raw ? 'updated' : '"updated"'); + } + ); + + it('does not persist unchanged state on unrelated rerenders', () => { + const { rerender } = renderHook(() => useSessionStorage('value', 'initial')); + (sessionStorage.setItem as jest.Mock).mockClear(); + + rerender(); + + expect(sessionStorage.setItem).not.toHaveBeenCalled(); + }); + + it('persists the current state under a new key', () => { + const { result, rerender } = renderHook(({ key }) => useSessionStorage(key, 'initial'), { + initialProps: { key: 'first' }, + }); + + act(() => result.current[1]('updated')); + rerender({ key: 'second' }); + + expect(result.current[0]).toBe('updated'); + expect(sessionStorage.getItem('first')).toBe('"updated"'); + expect(sessionStorage.getItem('second')).toBe('"updated"'); + }); + + it('persists the current state when the serialization mode changes', () => { + const { result, rerender } = renderHook( + ({ raw }) => useSessionStorage('value', 'initial', raw), + { + initialProps: { raw: false }, + } + ); + + act(() => result.current[1]('updated')); + rerender({ raw: true }); + expect(sessionStorage.getItem('value')).toBe('updated'); + + rerender({ raw: false }); + expect(sessionStorage.getItem('value')).toBe('"updated"'); + }); + + it('keeps the initial state when session storage access is restricted', () => { + (sessionStorage.getItem as jest.Mock).mockImplementationOnce(() => { + throw new Error('Storage access denied'); + }); + (sessionStorage.setItem as jest.Mock).mockImplementationOnce(() => { + throw new Error('Storage access denied'); + }); + + const { result } = renderHook(() => useSessionStorage('value', 'initial')); + + expect(result.current[0]).toBe('initial'); + expect(result.error).toBeUndefined(); + }); + + it('treats an empty serialized value as null', () => { + (sessionStorage.getItem as jest.Mock).mockReturnValueOnce(''); + + const { result } = renderHook(() => useSessionStorage('value', 'initial')); + + expect(result.current[0]).toBeNull(); + }); +});