# FakeMapAdapter

A pure in-memory [`MapAdapter`](https://docs.mapcairn.com/api/core/MapAdapter.md): no engine, no DOM, no timers.

This is a class exported by `@mapcairn/core`, checked against version 0.2.0.

## Signature

```ts
import { FakeMapAdapter } from "@mapcairn/core";
 
class FakeMapAdapter implements MapAdapter
```

## Description

How to script it:
- Pass `failMount: "webgl-unavailable"` (or `"load-failed"`) to make
 `mount` reject with a `MapEngineError`.
- Mount it into a [`FakeMapHost`](https://docs.mapcairn.com/api/core/FakeMapHost.md); the host's size (800 by 600 by
 default) sets the pixel space used by `project` and `unproject`.
- Drive events with `simulateClick`, `simulateTileError`,
 `simulateLoading`, `simulateUserZoom` (a wheel, pinch or double-click)
 `simulateFatalError` (the engine stopping after it loaded), and
 `simulatePointerDown`, `simulatePointerMove` and `simulatePointerUp` (a
 mouse or finger on the map, which drive edit handles). Call
 `failNextBasemap` to make the next swap fail. `setView`, `zoomIn`, `panBy` and the rest emit real
 `move`, `zoom`, `rotate` and `moveEnd` events, and only when something
 changed.
- Inspect `mountCount`, `destroyed`, `animationEnabled`, `keyboardEnabled`
 and `focusCount`.

Animation options are accepted and ignored, because every change is
immediate. `project` ignores the bearing.

## Constructors

### (constructor)

```ts
constructor(options?: FakeMapAdapterOptions);
```

Constructs a new instance of the `FakeMapAdapter` class

- `options?` (`FakeMapAdapterOptions`)

## Properties

### animationEnabled

```ts
animationEnabled: boolean;
```

Whether animation is on. It starts on and follows `mount`'s `reducedMotion` and `setAnimationEnabled`; the fake itself never animates.

### destroyed

```ts
readonly get destroyed(): boolean;
```

True after `destroy`, until the next successful `mount`.

### focusCount

```ts
focusCount: number;
```

How many times `focus` has been called.

### imageRequests

```ts
readonly imageRequests: (MapImageOptions | undefined)[];
```

The options of every `toImage` call, in order.

### keyboardEnabled

```ts
keyboardEnabled: boolean;
```

Whether the engine's own key handling is on, as `setKeyboardEnabled` last set it.

### mountCount

```ts
mountCount: number;
```

How many times `mount` has succeeded.

### projection

```ts
projection: MapProjection;
```

The shape the map was last asked to draw.

## Methods

### deferNextBasemap

```ts
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

```ts
destroy(): void;
```

Marks the adapter destroyed and drops every listener. Safe to call twice.

### drawnLayers

```ts
drawnLayers(): DrawnLayer[];
```

The data layers drawn, lowest first: imagery, vector tiles, styled shapes, detail shapes, heatmaps, then points.

### editHandles

```ts
editHandles(): readonly MapEditHandle[];
```

The edit handles last set.

### failNextBasemap

```ts
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

```ts
failNextImage(): void;
```

Makes the next `toImage` reject with an `export-failed` `MapEngineError`.

### featuresAt

```ts
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

```ts
featuresOf(id: string): readonly MapFeature[] | undefined;
```

The points drawn for a feed layer, or undefined when none is drawn under `id`.

- `id` (`string`)

### fitBounds

```ts
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

```ts
focus(): void;
```

Counts the call and focuses the mounted host without scrolling.

### getBounds

```ts
getBounds(): Bounds;
```

The box covering the host's four corners, unprojected.

### getMaxZoom

```ts
getMaxZoom(): number;
```

The mounted basemap's `maxZoom`, or 18 before one is set.

### getMinZoom

```ts
getMinZoom(): number;
```

The mounted basemap's `minZoom`, or 0 before one is set.

### getView

```ts
getView(): ViewState;
```

The current view.

### heatmapOf

```ts
heatmapOf(id: string): MapHeatmap | undefined;
```

The heatmap drawn under `id`, or undefined.

- `id` (`string`)

### isVisible

```ts
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

```ts
metresPerPixel(lat?: number): number;
```

The ground resolution at a latitude, for the flat or globe shape the map was last given.

- `lat?` (`number`)

### mount

```ts
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

```ts
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

```ts
panBy(dx: number, dy: number): void;
```

Moves the centre by screen pixels, keeping it on the world, through `setView`.

- `dx` (`number`)
- `dy` (`number`)

### project

```ts
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

```ts
removeLayer(id: string): void;
```

Forgets the layer under the id. Does nothing for an unknown id.

- `id` (`string`)

### resetBearing

```ts
resetBearing(): void;
```

Sets the bearing to zero, through `setView`.

### rotateBy

```ts
rotateBy(degrees: number): void;
```

Adds the degrees to the bearing, through `setView`.

- `degrees` (`number`)

### setAnimationEnabled

```ts
setAnimationEnabled(enabled: boolean): void;
```

Records whether animation is on in `animationEnabled`.

- `enabled` (`boolean`)

### setBasemap

```ts
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

```ts
setEditHandles(handles: readonly MapEditHandle[]): void;
```

Replaces the handles, and ends a hover on a handle that is no longer there.

- `handles` (`readonly MapEditHandle[]`)

### setFeatureLayer

```ts
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

```ts
setHeatmapLayer(id: string, heatmap: MapHeatmap): void;
```

Stores the heatmap under the id, replacing and restacking whatever was there.

- `id` (`string`)
- `heatmap` (`MapHeatmap`)

### setKeyboardEnabled

```ts
setKeyboardEnabled(enabled: boolean): void;
```

Records whether the engine's own key handling is on in `keyboardEnabled`.

- `enabled` (`boolean`)

### setProjection

```ts
setProjection(projection: MapProjection): void;
```

Records the shape the map is asked to draw. It changes `isVisible` and `metresPerPixel`.

- `projection` (`MapProjection`)

### setShapeLayer

```ts
setShapeLayer(id: string, shapes: MapShapes): void;
```

Stores the shapes under the id, replacing and restacking whatever was there.

- `id` (`string`)
- `shapes` (`MapShapes`)

### setStyledShapes

```ts
setStyledShapes(id: string, layer: MapStyledShapes): void;
```

Stores the styled shapes under the id, replacing and restacking whatever was there.

- `id` (`string`)
- `layer` (`MapStyledShapes`)

### setTileLayer

```ts
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

```ts
setVectorLayer(id: string, layer: MapVectorTiles): void;
```

Stores the vector layer under the id, replacing and restacking whatever was there.

- `id` (`string`)
- `layer` (`MapVectorTiles`)

### setView

```ts
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

```ts
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

```ts
simulateClick(latlng: LatLng, features?: MapClickEvent["features"]): void;
```

Emits a `click` at a position, with the given `features`.

- `latlng` (`LatLng`)
- `features?` (`MapClickEvent["features"]`)

### simulateFatalError

```ts
simulateFatalError(): void;
```

Emits `fatalError`, as an engine that stopped working after it loaded.

### simulateFeatureHits

```ts
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

```ts
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

```ts
simulateLoading(loading: boolean): void;
```

Emits `loading` with the given state.

- `loading` (`boolean`)

### simulatePointerDown

```ts
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

```ts
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

```ts
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

```ts
simulateTileError(): void;
```

Emits a `tileError`.

### simulateTileLayerFailure

```ts
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

```ts
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

```ts
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

```ts
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

```ts
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

```ts
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

```ts
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

```ts
unproject(point: PixelPoint): LatLng;
```

The position under a pixel, with its longitude wrapped, the inverse of `project`.

- `point` (`PixelPoint`)

### vectorLayerOf

```ts
vectorLayerOf(id: string): MapVectorTiles | undefined;
```

The vector tile layer drawn under `id`, or undefined.

- `id` (`string`)

### zoomIn

```ts
zoomIn(): void;
```

Raises the zoom by one, through `setView`.

### zoomOut

```ts
zoomOut(): void;
```

Lowers the zoom by one, through `setView`.

## See also

- [`MapAdapter`](https://docs.mapcairn.com/api/core/MapAdapter.md)
- [`FakeMapAdapterOptions`](https://docs.mapcairn.com/api/core/FakeMapAdapterOptions.md)
- [`DrawnLayer`](https://docs.mapcairn.com/api/core/DrawnLayer.md)
- [`MapEditHandle`](https://docs.mapcairn.com/api/core/MapEditHandle.md)
- [`PixelPoint`](https://docs.mapcairn.com/api/core/PixelPoint.md)
- [`MapFeatureHit`](https://docs.mapcairn.com/api/core/MapFeatureHit.md)
- [`MapFeature`](https://docs.mapcairn.com/api/core/MapFeature.md)
- [`Bounds`](https://docs.mapcairn.com/api/core/Bounds.md)
- [`MapFitOptions`](https://docs.mapcairn.com/api/core/MapFitOptions.md)
- [`ViewState`](https://docs.mapcairn.com/api/core/ViewState.md)
- [`MapHeatmap`](https://docs.mapcairn.com/api/core/MapHeatmap.md)
- [`MapImageOptions`](https://docs.mapcairn.com/api/core/MapImageOptions.md)
- [`LatLng`](https://docs.mapcairn.com/api/core/LatLng.md)
- [`MapHostElement`](https://docs.mapcairn.com/api/core/MapHostElement.md)
- [`MapMountOptions`](https://docs.mapcairn.com/api/core/MapMountOptions.md)
- [`MapEvents`](https://docs.mapcairn.com/api/core/MapEvents.md)
- [`MapProjection`](https://docs.mapcairn.com/api/core/MapProjection.md)
- [`BasemapDefinition`](https://docs.mapcairn.com/api/core/BasemapDefinition.md)
- [`MapShapes`](https://docs.mapcairn.com/api/core/MapShapes.md)
- [`MapStyledShapes`](https://docs.mapcairn.com/api/core/MapStyledShapes.md)
- [`MapTileOverlay`](https://docs.mapcairn.com/api/core/MapTileOverlay.md)
- [`MapVectorTiles`](https://docs.mapcairn.com/api/core/MapVectorTiles.md)
- [`MapClickEvent`](https://docs.mapcairn.com/api/core/MapClickEvent.md)
- [`HandlePointer`](https://docs.mapcairn.com/api/core/HandlePointer.md)

Source: [packages/core/src/fakes/FakeMapAdapter.ts:195](https://github.com/MapCairn/mapcairn/blob/main/packages/core/src/fakes/FakeMapAdapter.ts#L195)
