UNPKG

useful-custom-react-hooks

Version:

A collection of useful custom React hooks to simplify common tasks and enhance your React applications.

25 lines (24 loc) 830 B
import { renderHook, act } from '@testing-library/react-hooks'; import { useStorage } from '../hooks/useStorage'; describe('useStorage hook', () => { beforeEach(() => { localStorage.clear(); }); it('should set and get data from local storage', () => { const key = 'testKey'; const { result } = renderHook(() => useStorage(key)); expect(result.current[0]).toBeNull(); act(() => { result.current[1]({ foo: 'bar' }); }); expect(result.current[0]).toEqual({ foo: 'bar' }); expect(JSON.parse(localStorage.getItem(key))).toEqual({ foo: 'bar', }); act(() => { result.current[1](null); }); expect(result.current[0]).toBeNull(); expect(localStorage.getItem(key)).toBeNull(); }); });