0

How can I trigger a select interaction on openlayer map in angular unit testing? below is my select event code.

    let select = new Select({style: null});
    select.set('selectInteraction', 'name');
    select.on('select',  (e) => {
      console.log('selected');
    });
    this.map.addInteraction(select);

in the unit test I am trying to validate the select event as below but couldn't achieve it.

it('should verify select event trigger', () => {
    const mapEvent = new MapBrowserEvent('select', component.map, new UIEvent('select'));
    var evt = new SelectEvent('select', [], [], mapEvent);
    // Getting the select interaction to try for some action.
    const selectInteraction = component.map.getInteractions().getArray().filter(action => action.get('mySelectInteraction'));
    component.map.dispatchEvent(evt);
    // here will do my expectation validation.
    
  });

Need some help to perform this action.

Prabhakaran
  • 1,264
  • 2
  • 20
  • 47
  • See https://github.com/openlayers/openlayers/blob/main/test/browser/spec/ol/interaction/select.test.js#L97 – Mike Aug 28 '23 at 10:38

1 Answers1

0

I managed to trigger the event on my own. I post the answer incase if anybody is needed.

 var selectEvent = new SelectEvent('select', selectedFeatures, [], mapEvent);
    const selectInteraction = component.map.getInteractions().getArray().filter(action => action.get('mySelectInteraction'));
    selectInteraction[0].dispatchEvent(selectEvent);
Prabhakaran
  • 1,264
  • 2
  • 20
  • 47