Programmatically fire map's click event

15166
12
Jump to solution
07-22-2014 02:49 PM
TomRippetoe
Occasional Contributor

Hello,

I am using the Esri's Javascript API 3.9. I have created functionality that allows a user to add a graphic to the map by clicking on the map. The type of graphic added depends on some previous selection that user has made. For example, selecting one option (say Option A) adds a circle graphic whereas selecting a different option (say Option B) adds a square graphic. 

My need for the programmatic map 'click' event is with the automated unit tests I am creating (I am using Jasmine for my unit tests). My hope is to write a unit test which selects programmatically Option A, programmatically triggers a click on the map, and verifies that the correct 'map click' event handler was called (the one that handles the map clicks when Option A is selected).

I have tried using dojo's emit function.  The code seems to execute with no errors, i.e. the function call returns an 'event' object, but no map click event handler is ever called. I am pretty sure the event is hooked up correctly because if I follow those same steps manually, the correct event handler is called. As part of my testing, i have tried passing the 'emit' function both the map object and the div which contains the map object. Neither way seems to work, i.e. the click event handler is not called. Its not critical to my testing that a 'correct' argument is passed to the event handler; i just want to know that the correct handler is called. 

So, my question is how can i programmatically fire/trigger a map's click event?

Thank you.

//relevant code

this.map = new Map(divId, {

                     basemap: "streets",

                     center: [-122, 45.6],

                     zoom: 5,

                     smartNavigation: false

                 });

//passing the div which contains the map

var theMapDiv = dom.byId(divId);

var testEvent = on.emit(theMapDiv, "click", { bubbles: true, cancelable: true });

//passing the map

var testEvent = on.emit(this.map, "click", { bubbles: true, cancelable: true });

12 Replies
deleted-user-YYhuCJuuEn9L
New Contributor III

Thanks. This was helpful.

0 Kudos
by Anonymous User
Not applicable

This was an awesome start to my solution, thanks so much!

I needed to generate a click based on a specific point. For example the current map center, like so:

//Get the map center point that outputs an object with: type (point), x, y, and spatialReference

var currentMapCenter = map.getExtent().getCenter();

//Emulate a click on the map center point

map.emit("click", { mapPoint: currentMapCenter });

0 Kudos
NathanielRindlaub
New Contributor III

Does anyone have any advice for how to fire a click event programmatically in the 4.x version of the API? It looks like the map.event method is no longer available. 

My goal is to be able to select a feature on a MapImageLayer programmatically, and have it's corresponding popup display as if it had been clicked.

Any thoughts would be much appreciated!

0 Kudos