From c5281d8a594156d9dec9e20542a0ef00a3f8cb1d Mon Sep 17 00:00:00 2001 From: mansi-dev Date: Thu, 5 Feb 2026 23:06:21 +0530 Subject: [PATCH] Add tests for MapView camera methods and CameraPresets functionality --- .../src/__tests__/MapView.camera.test.tsx | 85 +++++++++++++++++++ .../src/__tests__/cameraPresets.test.ts | 23 +++++ 2 files changed, 108 insertions(+) create mode 100644 packages/map-sdk/src/__tests__/MapView.camera.test.tsx create mode 100644 packages/map-sdk/src/__tests__/cameraPresets.test.ts diff --git a/packages/map-sdk/src/__tests__/MapView.camera.test.tsx b/packages/map-sdk/src/__tests__/MapView.camera.test.tsx new file mode 100644 index 0000000..7d5bf47 --- /dev/null +++ b/packages/map-sdk/src/__tests__/MapView.camera.test.tsx @@ -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(); + renderer.create(); + + 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(); + renderer.create(); + + 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(); + renderer.create(); + + 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(); + renderer.create(); + + const cam = await ref.current?.getCamera(); + expect(mocked.__mocked__.getCamera).toHaveBeenCalled(); + expect(cam).toEqual({ center: { latitude: 0, longitude: 0 }, pitch: 0 }); + }); +}); diff --git a/packages/map-sdk/src/__tests__/cameraPresets.test.ts b/packages/map-sdk/src/__tests__/cameraPresets.test.ts new file mode 100644 index 0000000..c2b53ae --- /dev/null +++ b/packages/map-sdk/src/__tests__/cameraPresets.test.ts @@ -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); + }); +});