Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/wojtekmaj/react-hooks. A collection of React Hooks.
hooks react react-hooks
Something went wrong. Try again.
3.5 kB · 133 lines
TypeScript
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';import { act, renderHook } from '@testing-library/react';
import useNetworkInformation from './useNetworkInformation.js';
const itIfWindowDefined = it.runIf(typeof window !== 'undefined');const itIfWindowUndefined = it.runIf(typeof window === 'undefined');
describe('useNetworkInformation()', () => { beforeEach(() => { if (typeof window !== 'undefined') { Object.defineProperty(navigator, 'connection', { configurable: true, enumerable: true, get: () => undefined, }); } });
afterEach(() => { vi.clearAllMocks(); });
itIfWindowDefined('should return network information object properly', () => { const mockConnection = { downlink: 10, effectiveType: '4g', rtt: 50, saveData: false, addEventListener: () => { // Intentionally empty }, removeEventListener: () => { // Intentionally empty }, };
Object.defineProperty(navigator, 'connection', { configurable: true, enumerable: true, get: () => mockConnection, });
const { result } = renderHook(() => useNetworkInformation());
expect(result.current).toEqual(mockConnection); });
itIfWindowDefined('should subscribe to network information changes', () => { const mockConnection = { downlink: 10, effectiveType: '4g', rtt: 50, saveData: false, addEventListener: vi.fn(), removeEventListener: () => { // Intentionally empty }, };
Object.defineProperty(navigator, 'connection', { configurable: true, enumerable: true, get: () => mockConnection, });
renderHook(() => useNetworkInformation());
expect(mockConnection.addEventListener).toHaveBeenCalledTimes(1); });
itIfWindowDefined('should update the object when event listener is called', () => { let changeCallback: (() => void) | null = null;
const mockConnection1 = { downlink: 10, effectiveType: '4g', rtt: 50, saveData: false, addEventListener: (event: string, callback: () => void) => { if (event === 'change') { changeCallback = callback; } }, removeEventListener: (event: string, callback: () => void) => { if (event === 'change' && changeCallback === callback) { changeCallback = null; } }, };
Object.defineProperty(navigator, 'connection', { configurable: true, enumerable: true, get: () => mockConnection1, });
const { result, unmount } = renderHook(() => useNetworkInformation());
expect(result.current).toEqual(mockConnection1);
const mockConnection2 = { downlink: 20, effectiveType: '3g', rtt: 100, saveData: true, addEventListener: mockConnection1.addEventListener, removeEventListener: mockConnection1.removeEventListener, };
Object.defineProperty(navigator, 'connection', { configurable: true, enumerable: true, get: () => mockConnection2, });
act(() => { changeCallback?.(); });
expect(result.current).toEqual(mockConnection2);
unmount();
expect(changeCallback).toBe(null); });
itIfWindowUndefined('should return null', () => { const { result } = renderHook(() => useNetworkInformation());
expect(result.current).toBe(null); });});