Add tests for MapView camera methods and CameraPresets functionality

This commit is contained in:
2026-02-05 23:06:21 +05:30
parent 986266348a
commit c5281d8a59
2 changed files with 108 additions and 0 deletions

View File

@@ -0,0 +1,85 @@
import React from 'react';
import renderer from 'react-test-renderer';
// Mock react-native-maps with accessible jest fns so we can assert calls
const animateToRegion = jest.fn();
const animateCamera = jest.fn();
const fitToCoordinates = jest.fn();
const getCamera = jest.fn().mockResolvedValue({ center: { latitude: 0, longitude: 0 }, pitch: 0 });
jest.mock('react-native-maps', () => {
const React = require('react');
const RNMapView = React.forwardRef((props: any, ref: any) => {
React.useImperativeHandle(ref, () => ({
animateToRegion,
animateCamera,
fitToCoordinates,
getCamera,
}));
// Render something simple to satisfy React
return React.createElement('div', props, props.children);
});
const Marker = () => null;
const Polyline = () => null;
const PROVIDER_GOOGLE = 'google';
return {
__esModule: true,
default: RNMapView,
Marker,
Polyline,
PROVIDER_GOOGLE,
__mocked__: { animateToRegion, animateCamera, fitToCoordinates, getCamera },
};
});
import { MapView } from '..';
import type { MapHandle } from '../MapView';
const mocked = require('react-native-maps');
describe('MapView camera methods', () => {
beforeEach(() => {
jest.clearAllMocks();
});
it('animateToRegion delegates to underlying animateToRegion', () => {
const ref = React.createRef<MapHandle>();
renderer.create(<MapView ref={ref} />);
const region = { latitude: 1, longitude: 2, latitudeDelta: 0.01, longitudeDelta: 0.01 };
ref.current?.animateToRegion(region, 400);
expect(mocked.__mocked__.animateToRegion).toHaveBeenCalledWith(region, 400);
});
it('animateCamera delegates to underlying animateCamera', () => {
const ref = React.createRef<MapHandle>();
renderer.create(<MapView ref={ref} />);
const camera = { center: { latitude: 1, longitude: 2 }, pitch: 45 };
ref.current?.animateCamera(camera, { duration: 600 });
expect(mocked.__mocked__.animateCamera).toHaveBeenCalledWith(camera, { duration: 600 });
});
it('fitBounds delegates to fitToCoordinates', () => {
const ref = React.createRef<MapHandle>();
renderer.create(<MapView ref={ref} />);
const ne = { latitude: 1.1, longitude: 2.1 };
const sw = { latitude: 0.9, longitude: 1.9 };
ref.current?.fitBounds(ne, sw, { edgePadding: { top: 10, left: 10, bottom: 10, right: 10 }, animated: true });
expect(mocked.__mocked__.fitToCoordinates).toHaveBeenCalledWith([ne, sw], { edgePadding: { top: 10, left: 10, bottom: 10, right: 10 }, animated: true });
});
it('getCamera returns underlying camera', async () => {
const ref = React.createRef<MapHandle>();
renderer.create(<MapView ref={ref} />);
const cam = await ref.current?.getCamera();
expect(mocked.__mocked__.getCamera).toHaveBeenCalled();
expect(cam).toEqual({ center: { latitude: 0, longitude: 0 }, pitch: 0 });
});
});

View File

@@ -0,0 +1,23 @@
import { CameraPresets } from '../cameraPresets';
describe('CameraPresets', () => {
it('viewOverlook returns center, pitch, heading and zoom', () => {
const center = { latitude: 1, longitude: 2 };
const preset = CameraPresets.viewOverlook(center);
expect(preset).toMatchObject({ center, pitch: expect.any(Number), heading: expect.any(Number), zoom: expect.any(Number) });
});
it('streetLevel has no pitch and high zoom', () => {
const center = { latitude: 1, longitude: 2 };
const preset = CameraPresets.streetLevel(center);
expect(preset).toMatchObject({ center, pitch: 0, heading: 0, zoom: expect.any(Number) });
expect((preset.zoom as number) > 15).toBe(true);
});
it('overview returns a wider zoom level', () => {
const center = { latitude: 1, longitude: 2 };
const preset = CameraPresets.overview(center);
expect(preset).toMatchObject({ center, pitch: 0, heading: 0, zoom: expect.any(Number) });
expect((preset.zoom as number) < 15).toBe(true);
});
});