import { inRange } from "lodash-es"; import { LayerType } from "../../layerDefinition/LolOpenLayersLayerDef"; import { MockMap } from "../mock/MockMap"; interface MapMatchers { toHaveCentre(x: number, y: number): R; toHaveZoom(z: number): R; toHaveLayer( layerName: string, layerType: LayerType, shouldBeVisible?: boolean ): R; } declare global { namespace jest { interface Expect extends MapMatchers {} interface Matchers extends MapMatchers {} } } export default { toHaveCentre(map: MockMap, x: number, y: number) { const startX = map.centreX !== undefined ? map.centreX - 0.1 : 0; const endX = map.centreX !== undefined ? map.centreX + 0.1 : 0; const passX = inRange(x, startX, endX); const startY = map.centreY !== undefined ? map.centreY - 0.1 : 0; const endY = map.centreY !== undefined ? map.centreY + 0.1 : 0; const passY = inRange(y, startY, endY); return { message: () => !passX || !passY ? `Expected map to have a centre of ${x}, ${y} actual ${map.centreX},${map.centreY}` : `Expected map not to have a centre of ${x}, ${y}`, pass: passX && passY, }; }, toHaveZoom(map: MockMap, z: number) { const passZ = Math.abs((map.zoom ?? 0) - z) <= 0.1; return { message: () => !passZ ? `Expected map to have a zoom level of ${z}, is ${map.zoom}` : `Expected map not to have a zoom level of ${z}, is ${map.zoom}`, pass: passZ, }; }, toHaveLayer( map: MockMap, layerName: string, layerType: LayerType, shouldBeVisible: boolean = true ) { const layer = map?.mockLayerDefs?.find( (l) => l.name === layerName && l.type === layerType ); const hasLayer = layer !== undefined; // Note that here if we don't set the visibility dynamically, it falls back // to the (initial) layer setting, and then defaults to true const isVisible = map.layerState?.[layerName]?.visible ?? layer?.visible ?? true; const passVis = (shouldBeVisible && isVisible) || (!shouldBeVisible && !isVisible); let message = ""; if (!hasLayer) { message = `Expected map to have ${layerName} layer of type ${LayerType[layerType]}`; } else { if ((shouldBeVisible && !isVisible) || (!shouldBeVisible && !isVisible)) { message = `Expected map to have a ${layerName} layer which is visible`; } else { message = `Expected map to have a ${layerName} layer which is not visible`; } } return { message: () => message, pass: hasLayer && passVis, }; }, };