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.4 kB · 138 lines
TypeScript
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';import { renderHook } from 'vitest-browser-react';import { act } from 'react';
import useCurrentPosition from './useCurrentPosition.js';
import type { Mock } from 'vitest';
const itIfWindowDefined = it.runIf(typeof window !== 'undefined');
describe('useCurrentPosition()', () => { let getCurrentPosition: Mock; let watchPosition: Mock;
beforeEach(() => { if (typeof window !== 'undefined') { getCurrentPosition = vi.fn(); watchPosition = vi.fn();
const geolocation: Partial<Geolocation> = { getCurrentPosition, watchPosition, clearWatch: vi.fn(), };
Object.defineProperty(navigator, 'geolocation', { configurable: true, enumerable: true, get: () => undefined, });
vi.spyOn(navigator, 'geolocation', 'get').mockReturnValue(geolocation as Geolocation); } });
afterEach(() => { vi.clearAllMocks(); });
it('should return null initially', async () => { const { result } = await renderHook(() => useCurrentPosition());
expect(result.current).toBe(null); });
itIfWindowDefined('should get initial position', async () => { await renderHook(() => useCurrentPosition());
expect(getCurrentPosition).toHaveBeenCalledTimes(1); });
itIfWindowDefined('should subscribe to position changes', async () => { await renderHook(() => useCurrentPosition());
expect(watchPosition).toHaveBeenCalledTimes(1); });
itIfWindowDefined( 'should update the flag when getCurrentPosition listener is called', async () => { let listener: PositionCallback; getCurrentPosition.mockImplementationOnce((successCallback) => { listener = successCallback;
return () => null; });
const { result } = await renderHook(() => useCurrentPosition());
act(() => { listener({ coords: { accuracy: 0, altitude: 0, altitudeAccuracy: 0, heading: 0, latitude: 0, longitude: 0, speed: 0, toJSON: () => '<mocked json>', }, timestamp: 0, toJSON: () => '<mocked json>', }); });
expect(result.current).toEqual({ accuracy: 0, altitude: 0, altitudeAccuracy: 0, heading: 0, latitude: 0, longitude: 0, speed: 0, toJSON: expect.any(Function), }); }, );
itIfWindowDefined('should update the flag when watchPosition listener is called', async () => { let listener: PositionCallback; watchPosition.mockImplementationOnce((successCallback) => { listener = successCallback;
return () => 0; });
const { result } = await renderHook(() => useCurrentPosition());
act(() => { listener({ coords: { accuracy: 0, altitude: 0, altitudeAccuracy: 0, heading: 0, latitude: 0, longitude: 0, speed: 0, toJSON: () => '<mocked json>', }, timestamp: 0, toJSON: () => '<mocked json>', }); });
expect(result.current).toEqual({ accuracy: 0, altitude: 0, altitudeAccuracy: 0, heading: 0, latitude: 0, longitude: 0, speed: 0, toJSON: expect.any(Function), }); });});