Search code examples
javascriptdomqunit

How to test against a DOM object in qUnit?


I'm testing some JavaScript with qUnit. In one object I pass a DOM element, and some methods will change some properties of the element.

How can I mock a DOM object in qUnit?

I'd like to use a solution browser independent, as I test also XUL applications.


Solution

  • You can always create an element in JavaScript. If you don't append it (e.g. to the body), it won't be visible, so you could call it a mock element:

    document.createElement('div'); // 'div' will create a '<div>'
    

    So you can use this in a qUnit test function just as well: http://jsfiddle.net/LeMFH/.

    test("test", function() {
        expect(1);
    
        var elem = document.createElement("div");
    
        elem.style.position = "absolute";
    
        equals(elem.style.position, "absolute");
    });