0

I have a method to create and display a tooltip on the map on mouse hover over a feature on the map.

Code sample as follows:

createTooltip(map) {
  map.on('pointermove', (e) => {
    const featureArray = map.getFeaturesAtPixel(e.pixel);
    if (featureArray[0]?.getGeometry().getType() === 'Point') {
      logic to create tooltip
    }
    logic to create tooltip
  })
}

I'm trying to increase the code coverage on this part:

const featureArray = map.getFeaturesAtPixel(e.pixel);
if (featureArray[0]?.getGeometry().getType() === 'Point') {
  logic to create tooltip
}
logic to create tooltip

but it seems that I don't have any better ways to tap into this part of the code.

In my unit test, after I set up my map component I have this to trigger/simulate a mouse moving event:

const mapEvent = new MapBrowserEvent('pointermove', component.map, new UIEvent('pointermove'));
mapEvent.map = component.map;
mapEvent.target = component.map;
mapEvent.coordinate_ = coord;
mapEvent.coordinate = coord;
mapEvent.pixel_ = pixel;
mapEvent.pixel = pixel;
component.map.dispatchEvent(mapEvent);

This somehow worked as the code coverage report showed code being covered until

map.getFeaturesAtPixel(e.pixel)

but the mapEvent.coordinate and mapEvent.pixel doesn't seem to work as intended as the there are features on that coordinate, but console logging shows that map.getFeaturesAtPixel(e.pixel) on unit test is returning an empty array[], or not getting any features.

What can I do in order to test this?

1
  • @TomazicM updated the description amd added some more details. Jul 11, 2023 at 14:05

1 Answer 1

1

I tried with much simpler approach to event construction. I created two mouse move event firing functions, one for given coordinate and one for given pixel:

function firePointerMoveAtPixel(pixel) {
  var evt = {};
  evt.type = 'pointermove';
  evt.pixel = pixel;
  evt.coordinate = map.getCoordinateFromPixel(pixel);
  map.dispatchEvent(evt);
}

function firePointerMoveAtCoordinate(coordinate) {
  var evt = {};
  evt.type = 'pointermove';
  evt.coordinate = coordinate;
  evt.pixel = map.getPixelFromCoordinate(coordinate);
  map.dispatchEvent(evt);
}

It works without any problems.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.