import { Feature } from "ol"; import * as olColor from "ol/color"; import { Geometry } from "ol/geom"; import { MockLayerState, MockMap } from "../mock/MockMap"; import { MockFeature } from "../mock/MockVTFeatures"; import { Icon, Style } from "ol/style"; import { capitalize, compact } from "lodash-es"; import { LolOpenLayersLayerDef } from "../../layerDefinition/LolOpenLayersLayerDef"; import { LolOpenLayersVectorTileLayerDef } from "../../layerDefinition/LolOpenLayersVectorTileLayerDef"; import { layerByName } from "../helpers"; import { StyleLike } from "ol/style/Style"; import { HighlightStyle, LolOpenLayersVectorLayerDef, StyleWithContext, } from "../../layerDefinition/LolOpenLayersVectorLayerDef"; import ImageStyle from "ol/style/Image"; import { HIGHLIGHT_LAYER } from "../../map/map-utils"; interface MapFeatureMatchers { toHaveFeatureCount(expectedCount: number): R; toHaveFeature(id: number): R; withAttribute(name: string, value: any): R; withFeatureStyleStroke(strokeColor: string): R; withNoFeatureStyleStroke(): R; withFeatureStyleFill(strokeColor: string): R; withNoFeatureStyleFill(): R; withFeatureLabelText(labelText: string | null | undefined): R; withFeatureIcon(iconPath: string | undefined): R; withFeatureStyleCount(expectedStyleCount: number): R; } declare global { namespace jest { interface Expect extends MapFeatureMatchers {} interface Matchers extends MapFeatureMatchers {} } } let stylesFromFunction = function ( styleParam: Style | StyleLike | StyleWithContext, feature: MockFeature, map: MockMap, layerName: string ) { return typeof styleParam === "function" ? styleParam( feature as unknown as Feature, map.mapResolution!, map?.mapContext?.getStyleContext(layerName) ) : styleParam; }; const styleFromParam = ( styleParam: Style | StyleLike | StyleWithContext, feature: MockFeature, map: MockMap, layerName: string ) => { const style = stylesFromFunction(styleParam, feature, map, layerName); return style ?? new Style(); }; const singleStyleFromParam = ( styleParam: Style | StyleLike | StyleWithContext, feature: MockFeature, map: MockMap, layerName: string ) => { const style = stylesFromFunction(styleParam, feature, map, layerName); return (Array.isArray(style) ? style[0] : style) ?? new Style(); }; const styleFromHighlightParam = ( highlightStyleParam: HighlightStyle, plainStyle: Style | Style[], feature: MockFeature, map: MockMap ) => { const style = typeof highlightStyleParam === "function" ? highlightStyleParam( plainStyle, feature as unknown as Feature, map.mapResolution! ) : highlightStyleParam; return style ?? new Style(); }; // Given a highlight style and a plain style // call the appropriate style functions // to get the highlighted or regular styles const singleStyleFromHighlightParam = ( highlightStyleParam: HighlightStyle, plainStyle: Style | Style[], feature: MockFeature, map: MockMap ) => { const style = typeof highlightStyleParam === "function" ? highlightStyleParam( plainStyle, feature as unknown as Feature, map.mapResolution! ) : highlightStyleParam; return (Array.isArray(style) ? style[0] : style) ?? new Style(); }; /** * returns the style being applied to a feature without only returning the first one if the style is an array */ const styleForFeature = ( map: MockMap, layer: LolOpenLayersLayerDef, feature: MockFeature, keys: ("labelStyle" | "style")[] ): Style | Style[] => { const styleParam = compact( keys.map((key) => { return (layer as LolOpenLayersVectorTileLayerDef)[key] as | StyleLike | StyleWithContext | undefined; }) )[0]; const plainStyle = styleFromParam(styleParam, feature, map, layer.name); // Consider here that we *may* have a highlight style defined, if so // use it in preference to the base style when a highlight has been set const highlightStyleParam = (layer as LolOpenLayersVectorLayerDef) .highlightStyle; if ( map?.mapContext ?.getStyleContext(layer.name) ?.highlightIds?.includes(feature.getId()) && highlightStyleParam ) { return styleFromHighlightParam( highlightStyleParam, plainStyle, feature, map ); } return plainStyle; }; const singleStyleForFeature = ( map: MockMap, layer: LolOpenLayersLayerDef, feature: MockFeature, keys: ("labelStyle" | "style")[] ): Style => { const styleParam = compact( keys.map((key) => { return (layer as LolOpenLayersVectorTileLayerDef)[key] as | StyleLike | StyleWithContext | undefined; }) )[0]; const plainStyle = singleStyleFromParam(styleParam, feature, map, layer.name); // Consider here that we *may* have a highlight style defined, if so // use it in preference to the base style when a highlight has been set const highlightStyleParam = (layer as LolOpenLayersVectorLayerDef) .highlightStyle; if ( map?.mapContext ?.getStyleContext(layer.name) ?.highlightIds?.includes(feature.getId()) && highlightStyleParam ) { return singleStyleFromHighlightParam( highlightStyleParam, plainStyle, feature, map ); } return plainStyle; }; // This is consumed by `expect.extend(mapFeatureAssertions)` in setupTests.js export default { toHaveFeatureCount(layerState: MockLayerState, expectedCount: number) { // If the layer is invisible then we have zero features const actualCount = (layerState.visible ?? true) ? layerState.features?.length : 0; const pass = actualCount === expectedCount; return { message: () => !pass ? `Expected map layer to have ${expectedCount} features, but has ${actualCount}` : `Expected map layer not to have ${expectedCount} features, but has ${actualCount}`, pass, }; }, toHaveFeature(layerState: MockLayerState, id: number) { const hasFeature = (layerState.visible ?? true) && layerState.features.find((f) => f.attributes.id === id) !== undefined; return { message: () => !hasFeature ? `Expected map to have a feature with id of ${id}` : `Expected map not to have a feature with id of ${id}`, pass: hasFeature, }; }, withAttribute(feature: MockFeature, name: string, expectedValue: any) { const id = feature.get("id"); const attrVal = feature.get(name); const pass = attrVal === expectedValue; return { message: () => !pass ? `Expected map feature ${id} to have a ${name} attribute with a value of ${expectedValue} but got ${attrVal}` : `Expected map feature ${id} to have a ${name} attribute with a value that is not ${expectedValue}`, pass, }; }, withFeatureStyleStroke(feature: MockFeature, strokeColor: string) { return checkFeatureStyleMethod(feature, strokeColor, "stroke"); }, withNoFeatureStyleStroke(feature: MockFeature) { return checkFeatureStyleMethod(feature, null, "stroke"); }, withFeatureStyleFill(feature: MockFeature, fillColor: string) { return checkFeatureStyleMethod(feature, fillColor, "fill"); }, withFeatureStyleCount(feature: MockFeature, expectedStyleCount: number) { const map = feature.mockMap; const id = feature.get("id"); const layer = layerByName(map, feature.layerName); if (layer) { const style = styleForFeature(map, layer, feature, ["style"]); const styleCount = Array.isArray(style) ? style.length : 1; if (styleCount === expectedStyleCount) { return { pass: true, message: () => `Expected feature ${id} not to have ${expectedStyleCount} styles but had ${styleCount}`, }; } return { pass: false, message: () => `Expected feature ${id} to have ${expectedStyleCount} styles but had ${styleCount}`, }; } return { pass: false, message: () => `${feature.layerName} Layer not found`, }; }, withNoFeatureStyleFill(feature: MockFeature) { return checkFeatureStyleMethod(feature, null, "fill"); }, withFeatureLabelText( feature: MockFeature, expectedLabelText: string | null | undefined ) { let pass: boolean; let text: string | string[] | undefined; let id: string | undefined; if (feature) { const mockMap = feature.mockMap; if (mockMap.layerState[`${feature.layerName}_labels`]?.visible ?? true) { const layer = layerByName(mockMap, feature.layerName); if (layer) { id = feature.get("id"); // NOTE: for parcels in survey map, labels are on a different layer and // source and get styled by `style` not `labelStyle` // this works round that by trying both // In the future, this will go const style = singleStyleForFeature(mockMap, layer, feature, [ "labelStyle", "style", ]); text = style?.getText()?.getText(); pass = text === expectedLabelText; } else { pass = false; text = "[label layer not found]"; } } else { pass = false; text = "[label layer invisible]"; } } else { pass = false; text = "[no feature]"; } return { message: () => !pass ? `Expected map feature ${id} to have a "${expectedLabelText}" label but got ${text}` : `Expected map feature ${id} to not have a "${expectedLabelText}" label but got ${text}`, pass, }; }, withFeatureIcon(feature: MockFeature, expectedIconPath: string | undefined) { let pass: boolean; let iconPath: string | string[] | undefined; let id: string | undefined; if (feature) { const mockMap = feature.mockMap; const layer = layerByName(mockMap, feature.layerName); if (layer) { id = feature.get("id"); const style = singleStyleForFeature(mockMap, layer, feature, ["style"]); iconPath = (style?.getImage() as Icon)?.getSrc(); pass = iconPath === expectedIconPath; } else { pass = false; iconPath = "[layer not found]"; } } else { pass = false; iconPath = "[no feature]"; } return { message: () => !pass ? `Expected map feature ${id} to have a "${expectedIconPath}" icon but got ${iconPath}` : `Expected map feature ${id} to not have a "${expectedIconPath}" icon but got ${iconPath}`, pass, }; }, }; const checkFeatureStyleMethod = ( feature: MockFeature, expectedColour: string | null, styleAttribute: string ) => { const map = feature.mockMap; const id = feature.get("id"); let style; if (feature.layerName === HIGHLIGHT_LAYER) { const styleParam = map.layerState[feature.layerName]?.style; if (!styleParam) throw Error(`highlight layer has no style defined`); style = singleStyleFromParam(styleParam, feature, map, feature.layerName); } else { const layer = layerByName(feature.mockMap, feature.layerName); if (!layer) throw Error(`layer ${feature.layerName} is not defined`); style = singleStyleForFeature(map, layer, feature, ["style"]); } if (!style) throw Error(`layer ${feature.layerName} has no style defined`); const getter = `get${capitalize(styleAttribute)}`; // @ts-ignore let styler = style[getter as keyof Style](); if (!styler) { // try image const imageStyle = (style as Style).getImage(); styler = (imageStyle?.[getter as keyof ImageStyle] as () => Style)?.(); } // @ts-ignore const styleColor = styler?.getColor() as olColor.Color; const colour = (styleColor && olColor.asString(styleColor)) ?? null; const matches = colour === expectedColour; return { message: () => matches ? `Expected map feature ${id} to not have a ${styleAttribute} colour but got ${colour}` : `Expected map feature ${id} to have a ${expectedColour} ${styleAttribute} colour but got ${colour}`, pass: matches, }; };