Add tests for MapView camera methods and CameraPresets functionality
This commit is contained in:
85
packages/map-sdk/src/__tests__/MapView.camera.test.tsx
Normal file
85
packages/map-sdk/src/__tests__/MapView.camera.test.tsx
Normal 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 });
|
||||
});
|
||||
});
|
||||
23
packages/map-sdk/src/__tests__/cameraPresets.test.ts
Normal file
23
packages/map-sdk/src/__tests__/cameraPresets.test.ts
Normal 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);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user