Unit Testing Objects Dependent on ArcGIS Server Javascript API

Wed, Apr 14, 2010 2-minute read

Recently, I’ve created a custom Dojo dijit that contains a esri.Map from the ArcGIS Server Javascript API.  The dijit, right now, creates the map on the fly, so it calls new esri.Map(…) and map.addLayer(new esri.layers.ArcGISTiledMapServiceLayer(url)), for example.  This can cause heartburn when trying to unit test the operations that my custom dijit is performing.  I am going to run through the current (somewhat hackish) way I am performing unit tests without having to create a full blown HTML representation of the dijit.

I’ll be using JSpec for this example, so you may want to swing over to that site and brush up on the syntax, which is pretty easy to grok, especially if you’ve done any BDD/spec type unit testing before.

The contrived, but relatively common, scenario for this post is:

[sourcecode language=“javascript”] describe ‘VersionDiff’

before_each vd = new esi.dijits.VersionDiff(); end describe ‘updateImages()’ it ‘should change the extent of the child map’ //Arrange esri={ Map:function(){ this.setExtent=function(obj){ this.extent=obj; }; }, geometry:{ Extent:function(xmin,ymin,xmax,ymax){ var obj={}; obj.xmin=xmin; obj.ymin=ymin; obj.xmax=xmax; obj.ymax=ymax; return obj; } } }; vd.childMap = new esri.Map(); var text = fixture("getFeatureVersionGraphics.txt") text = dojo.fromJson(text) //Act vd.updateMapToFeatureExtent(text) //Assert vd.childMap.extent.xmin.should.be 7660976.8567093275 vd.childMap.extent.xmax.should.be 7661002.6869258471 vd.childMap.extent.ymin.should.be 704520.0600393787 vd.childMap.extent.ymax.should.be 704553.8080708608 end end end

[/sourcecode]

I didn’t include the source to the operation being tested, because I don’t think it adds much to the point.  Suffice it to say that it calls setExtent() on the childMap property.