Brian's Waste of Time

Thu, 07 Sep 2006

Testing Dojo Widgets

I've been mucking with Dojo widgets a lot lately, and in writing a new base widget (for doing inplace widget creation using existing annotated dom nodes rather than templates) I finally ground my teeth against browser-refresh based testing enough to do something. I was stupid, automated testing of dojo widgets is easy!

Take this example:

    dojo.hostenv.setModulePrefix("skife", "/dojo-stuff/skife");
    dojo.require("skife.test");
    dojo.require("skife.widget.InplaceWidget");
    
    var create = function(s) {
        var node = document.createElement("div");
        node.innerHTML = s;
        var parser = new dojo.xml.Parse();
        var frag = parser.parseElement(node, null, true);
        dojo.widget.getParser().createSubComponents(frag);
        return node;
    }
    
    
    skife.test.suite("Testing org.skife.InplaceWidget", function(s) {
        s.test("Widget is instantiated declaratively", function(t) {
            var called = false;

            dojo.widget.defineWidget("skife.test.DummyWidget1", skife.widget.InplaceWidget, {
                postAttachDom: function() {
                    called = true;
                }
            });
                        
            var div = create("<div dojoType='DummyWidget1'></div>");
            
            t.assert("postAttachDom was not called", called)
        });
        
        
        s.test("In place nodes are attached", function(t) {

            dojo.widget.defineWidget("skife.test.DummyWidget2", skife.widget.InplaceWidget, {
                postAttachDom: function() {
                    t.assertNotNull("hi node not attached", this.hi)
                }
            });
                        
            var div = create("<div dojoType='DummyWidget2'>" + 
                             "<span dojoAttachPoint='hi'>Hi</span></div>");             
        });
    })

In this case I need to use declarative widget creation, so the hardest part was figuring out how to get the parser to run over the dom tree I had just made. The Dojo FAQ had two (different) ways of doing it, neither of which work anymore. Luckily the kindly folks in #dojo pointed me to ContentPane which does this (see the create function earlier).

Anyway, this served as a good reminder to me that not testing because you think something will be hard to test is stupid. Once you try and prove it is hard to test, then you have some legs to make excuses on, until then, kick yourself in the ass and go test some more :-)

Along the way I started cleaning up the JS test harness (re-namespaced into something less likely to have collisions!) I've been mucking with. I've been n the insane side of busy lately so pretty much everything not directly involved with $goal had fallen by the wayside for a bit. Fun to have some breathing room to pick things up!

0 writebacks [/src/javascript/dojo] permanent link