Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 779
Manuel Bitto Avatar asked Oct 01 '11 14:10

Manuel Bitto


1 Answers

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");
});
like image 110
pimvdb Avatar answered Oct 16 '22 23:10

pimvdb