diff --git a/src/control/modify.js b/src/control/modify.js index 877a4c19..d951646f 100644 --- a/src/control/modify.js +++ b/src/control/modify.js @@ -25,7 +25,7 @@ class ModifyControl extends Control { * @param {Object} [options.deleteInteractionOptions] Options for the delete interaction. * @param {Object} [options.deselectInteractionOptions] Options for the deselect interaction. Default: features are deselected on click on map. */ - constructor(options) { + constructor(options = {}) { super({ title: 'Modify geometry', className: 'ole-control-modify', @@ -376,14 +376,14 @@ class ModifyControl extends Control { } super.setMap(map); this.addListeners(); - this.map.addInteraction(this.deselectInteraction); - this.map.addInteraction(this.deleteInteraction); - this.map.addInteraction(this.selectModify); + this.map?.addInteraction(this.deselectInteraction); + this.map?.addInteraction(this.deleteInteraction); + this.map?.addInteraction(this.selectModify); // For the default behvior it's very important to add selectMove after selectModify. // It will avoid single/dbleclick mess. - this.map.addInteraction(this.selectMove); - this.map.addInteraction(this.moveInteraction); - this.map.addInteraction(this.modifyInteraction); + this.map?.addInteraction(this.selectMove); + this.map?.addInteraction(this.moveInteraction); + this.map?.addInteraction(this.modifyInteraction); } /** diff --git a/src/editor.test.js b/src/editor.test.js index b587abdd..30752440 100644 --- a/src/editor.test.js +++ b/src/editor.test.js @@ -3,11 +3,13 @@ import { expect, test, describe, beforeEach } from 'vitest'; import Map from 'ol/Map'; import Editor from './editor'; import CAD from './control/cad'; +import ModifyControl from './control/modify'; describe('editor', () => { let map; let editor; let cad; + let modify; beforeEach(() => { // In the test we use pixel as coordinates. @@ -16,6 +18,7 @@ describe('editor', () => { }); editor = new Editor(map); cad = new CAD(); + modify = new ModifyControl(); }); test('adds a control', () => { @@ -46,14 +49,20 @@ describe('editor', () => { }); test('is removed', () => { + editor.addControl(modify); editor.addControl(cad); - cad.activate(); - expect(cad.getActive()).toBe(true); - expect(editor.controls.getArray()[0]).toBe(cad); - expect(editor.activeControls.getArray()[0]).toBe(cad); + modify.activate(); + expect(modify.getActive()).toBe(true); + expect(editor.controls.getArray()[0]).toBe(modify); + expect(editor.controls.getArray()[1]).toBe(cad); + expect(editor.activeControls.getArray()[0]).toBe(modify); + expect(editor.activeControls.getArray()[0]).toBe(modify); editor.remove(); expect(editor.controls.getLength()).toBe(0); expect(editor.activeControls.getLength()).toBe(0); + expect(modify.map).toBe(); + expect(modify.editor).toBe(); + expect(modify.getActive()).toBe(false); expect(cad.map).toBe(); expect(cad.editor).toBe(); expect(cad.getActive()).toBe(false);