FakeMapAdapter
A pure in-memory MapAdapter: no engine, no DOM, no timers.
This is a class exported by @mapcairn/core, checked against version 0.2.0.
Signature
import { FakeMapAdapter } from "@mapcairn/core";
class FakeMapAdapter implements MapAdapter
Description
How to script it:
- Pass
failMount: "webgl-unavailable"(or"load-failed") to makemountreject with aMapEngineError. - Mount it into a
FakeMapHost; the host's size (800 by 600 by default) sets the pixel space used byprojectandunproject. - Drive events with
simulateClick,simulateTileError,simulateLoading,simulateUserZoom(a wheel, pinch or double-click)simulateFatalError(the engine stopping after it loaded), andsimulatePointerDown,simulatePointerMoveandsimulatePointerUp(a mouse or finger on the map, which drive edit handles). CallfailNextBasemapto make the next swap fail.setView,zoomIn,panByand the rest emit realmove,zoom,rotateandmoveEndevents, and only when something changed. - Inspect
mountCount,destroyed,animationEnabled,keyboardEnabledandfocusCount.
Animation options are accepted and ignored, because every change is
immediate. project ignores the bearing.
Constructors
(constructor)
constructor(options?: FakeMapAdapterOptions);
Constructs a new instance of the FakeMapAdapter class
options?(FakeMapAdapterOptions)
Properties
animationEnabled
animationEnabled: boolean;
Whether animation is on. It starts on and follows mount's reducedMotion and setAnimationEnabled; the fake itself never animates.
destroyed
readonly get destroyed(): boolean;
True after destroy, until the next successful mount.
focusCount
focusCount: number;
How many times focus has been called.
imageRequests
readonly imageRequests: (MapImageOptions | undefined)[];
The options of every toImage call, in order.
keyboardEnabled
keyboardEnabled: boolean;
Whether the engine's own key handling is on, as setKeyboardEnabled last set it.
mountCount
mountCount: number;
How many times mount has succeeded.
projection
projection: MapProjection;
The shape the map was last asked to draw.
Methods
deferNextBasemap
deferNextBasemap(): {
readonly resolve: () => void;
readonly reject: () => void;
};
Makes the next setBasemap wait, as a real style that is still
downloading does, until resolve (the swap loads) or reject (it fails
with a load-failed MapEngineError and the last basemap stays). While
it waits the basemap is the old one, and toImage refuses.
destroy
destroy(): void;
Marks the adapter destroyed and drops every listener. Safe to call twice.
drawnLayers
drawnLayers(): DrawnLayer[];
The data layers drawn, lowest first: imagery, vector tiles, styled shapes, detail shapes, heatmaps, then points.
editHandles
editHandles(): readonly MapEditHandle[];
The edit handles last set.
failNextBasemap
failNextBasemap(): void;
Makes the next setBasemap reject with a load-failed MapEngineError,
leaving the current basemap, its zoom limits and the view as they were,
as a real engine does when a new style cannot load.
failNextImage
failNextImage(): void;
Makes the next toImage reject with an export-failed MapEngineError.
featuresAt
featuresAt(_point: PixelPoint): readonly MapFeatureHit[];
The features most recently scripted under the pointer with
simulateFeatureHits or simulateFeatureTap, still drawn, in the order
scripted. A real engine finds them by position; the fake cannot, so a
test says what is there.
_point(PixelPoint)
featuresOf
featuresOf(id: string): readonly MapFeature[] | undefined;
The points drawn for a feed layer, or undefined when none is drawn under id.
id(string)
fitBounds
fitBounds(bounds: Bounds, options?: MapFitOptions): void;
Sets the view to the box's centre and the zoom that fits it inside the padding and the host's size, not past maxZoom.
bounds(Bounds)options?(MapFitOptions)
focus
focus(): void;
Counts the call and focuses the mounted host without scrolling.
getBounds
getBounds(): Bounds;
The box covering the host's four corners, unprojected.
getMaxZoom
getMaxZoom(): number;
The mounted basemap's maxZoom, or 18 before one is set.
getMinZoom
getMinZoom(): number;
The mounted basemap's minZoom, or 0 before one is set.
getView
getView(): ViewState;
The current view.
heatmapOf
heatmapOf(id: string): MapHeatmap | undefined;
The heatmap drawn under id, or undefined.
id(string)
isVisible
isVisible(latlng: LatLng): boolean;
On a globe, a place more than a quarter of the way round from the centre is out of sight.
latlng(LatLng)
metresPerPixel
metresPerPixel(lat?: number): number;
The ground resolution at a latitude, for the flat or globe shape the map was last given.
lat?(number)
mount
mount(host: MapHostElement, options: MapMountOptions): Promise<void>;
Records the host and its size and emits load. It rejects with a MapEngineError when failMount was given.
host(MapHostElement)options(MapMountOptions)
on
on<K extends keyof MapEvents>(event: K, handler: (payload: MapEvents[K]) => void): () => void;
Subscribes to an event. Returns the function that unsubscribes.
event(K)handler((payload: MapEvents[K]) => void)
panBy
panBy(dx: number, dy: number): void;
Moves the centre by screen pixels, keeping it on the world, through setView.
dx(number)dy(number)
project
project(latlng: LatLng): PixelPoint;
The pixel of a position in the host's size, centred on the view. It ignores the bearing.
latlng(LatLng)
removeLayer
removeLayer(id: string): void;
Forgets the layer under the id. Does nothing for an unknown id.
id(string)
resetBearing
resetBearing(): void;
Sets the bearing to zero, through setView.
rotateBy
rotateBy(degrees: number): void;
Adds the degrees to the bearing, through setView.
degrees(number)
setAnimationEnabled
setAnimationEnabled(enabled: boolean): void;
Records whether animation is on in animationEnabled.
enabled(boolean)
setBasemap
setBasemap(basemap: BasemapDefinition): Promise<void>;
Swaps the basemap and emits load. It rejects with a MapEngineError when failNextBasemap or a deferred swap says so, and the old basemap stays.
basemap(BasemapDefinition)
setEditHandles
setEditHandles(handles: readonly MapEditHandle[]): void;
Replaces the handles, and ends a hover on a handle that is no longer there.
handles(readonly MapEditHandle[])
setFeatureLayer
setFeatureLayer(id: string, features: readonly MapFeature[]): void;
Stores the layer under the id, replacing and restacking whatever was there. drawnLayers reports it.
id(string)features(readonly MapFeature[])
setHeatmapLayer
setHeatmapLayer(id: string, heatmap: MapHeatmap): void;
Stores the heatmap under the id, replacing and restacking whatever was there.
id(string)heatmap(MapHeatmap)
setKeyboardEnabled
setKeyboardEnabled(enabled: boolean): void;
Records whether the engine's own key handling is on in keyboardEnabled.
enabled(boolean)
setProjection
setProjection(projection: MapProjection): void;
Records the shape the map is asked to draw. It changes isVisible and metresPerPixel.
projection(MapProjection)
setShapeLayer
setShapeLayer(id: string, shapes: MapShapes): void;
Stores the shapes under the id, replacing and restacking whatever was there.
id(string)shapes(MapShapes)
setStyledShapes
setStyledShapes(id: string, layer: MapStyledShapes): void;
Stores the styled shapes under the id, replacing and restacking whatever was there.
id(string)layer(MapStyledShapes)
setTileLayer
setTileLayer(id: string, overlay: MapTileOverlay): void;
Stores the overlay under the id. A new tiles template forgets any slow or unavailable report for the layer.
id(string)overlay(MapTileOverlay)
setVectorLayer
setVectorLayer(id: string, layer: MapVectorTiles): void;
Stores the vector layer under the id, replacing and restacking whatever was there.
id(string)layer(MapVectorTiles)
setView
setView(view: Partial<ViewState>): void;
Applies the given parts of the view at once and emits move, zoom, rotate and moveEnd for what changed. Nothing is emitted when nothing changed.
view(Partial<ViewState>)
simulateBasemapUnavailable
simulateBasemapUnavailable(): void;
Emits basemapUnavailable for the basemap it draws, as an engine does
when every tile of it failed and none loaded. Before mount it does
nothing, because nothing is drawn.
simulateClick
simulateClick(latlng: LatLng, features?: MapClickEvent["features"]): void;
Emits a click at a position, with the given features.
latlng(LatLng)features?(MapClickEvent["features"])
simulateFatalError
simulateFatalError(): void;
Emits fatalError, as an engine that stopped working after it loaded.
simulateFeatureHits
simulateFeatureHits(hits: readonly MapFeatureHit[]): void;
Emits featureTap for a tap that lands on several features at once,
topmost first, and then click. Only features that are drawn count, each
once; with none drawn nothing is emitted. They are also what
featuresAt then reports.
hits(readonly MapFeatureHit[])
simulateFeatureTap
simulateFeatureTap(layerId: string, featureId: string): void;
Emits featureTap and then click for a tap on a drawn point, polygon or
line, as an engine does. A feature that is not drawn gets nothing.
layerId(string)featureId(string)
simulateLoading
simulateLoading(loading: boolean): void;
Emits loading with the given state.
loading(boolean)
simulatePointerDown
simulatePointerDown(latlng: LatLng, pointer?: HandlePointer): void;
A mouse or finger going down at a position. On a handle it starts a
handleDrag; anywhere else it starts nothing.
latlng(LatLng)pointer?(HandlePointer)
simulatePointerMove
simulatePointerMove(latlng: LatLng, pointer?: HandlePointer): void;
The pointer moving to a position. During a press on a handle, once it
has left the click tolerance, it reports move; otherwise a mouse
reports handleHover as it crosses handles.
latlng(LatLng)pointer?(HandlePointer)
simulatePointerUp
simulatePointerUp(latlng: LatLng): void;
The pointer going up at a position. It ends a press on a handle, and the
click that would follow it is then dropped, as an engine's is.
latlng(LatLng)
simulateTileError
simulateTileError(): void;
Emits a tileError.
simulateTileLayerFailure
simulateTileLayerFailure(layerId: string): void;
Emits tileLayerUnavailable for the tile layer layerId as it is drawn,
as an engine does when every tile of it failed and none loaded. A layer
that is not a drawn tile layer has no tiles, so nothing is emitted.
layerId(string)
simulateTileLayerRecovery
simulateTileLayerRecovery(layerId: string): void;
Emits tileLayerRecovered for the tile layer layerId, as an engine does
when a tile loads after the layer reported slow tiles or no tiles. A layer
that has not reported (or has since been set to other tiles, or removed)
emits nothing, and the reports of one drawing are ended by one recovery.
layerId(string)
simulateTileLayerSlow
simulateTileLayerSlow(layerId: string): void;
Emits tileLayerSlow for the tile layer layerId as it is drawn, as an
engine does when none of its tiles has loaded for longer than its wait. A
layer that is not a drawn tile layer has no tiles, so nothing is emitted,
and a layer reports it at most once for the tiles it is set to, and not
once it has reported no tiles.
layerId(string)
simulateUserZoom
simulateUserZoom(zoom: number): void;
Zooms to zoom the way a gesture the engine handles would: the view
events fire as for setView, and userZoom fires with them.
zoom(number)
stallNextImageTiles
stallNextImageTiles(): void;
Makes the next toImage that asks for a size, a pixel ratio or a view
reject with a timed-out MapImageExportError, as when its tiles never
finish loading.
styledShapesOf
styledShapesOf(id: string): MapStyledShapes | undefined;
The polygons or lines drawn for a styled layer, or undefined when none is drawn under id.
id(string)
toImage
toImage(options?: MapImageOptions): Promise<Blob>;
Resolves with a PNG header of the requested size, enough to read the size back. It rejects as export-failed while a basemap swap is loading, or when failNextImage or stallNextImageTiles says so.
options?(MapImageOptions)
unproject
unproject(point: PixelPoint): LatLng;
The position under a pixel, with its longitude wrapped, the inverse of project.
point(PixelPoint)
vectorLayerOf
vectorLayerOf(id: string): MapVectorTiles | undefined;
The vector tile layer drawn under id, or undefined.
id(string)
zoomIn
zoomIn(): void;
Raises the zoom by one, through setView.
zoomOut
zoomOut(): void;
Lowers the zoom by one, through setView.
See also
MapAdapterFakeMapAdapterOptionsDrawnLayerMapEditHandlePixelPointMapFeatureHitMapFeatureBoundsMapFitOptionsViewStateMapHeatmapMapImageOptionsLatLngMapHostElementMapMountOptionsMapEventsMapProjectionBasemapDefinitionMapShapesMapStyledShapesMapTileOverlayMapVectorTilesMapClickEventHandlePointer