Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing Javascript that Manipulates the DOM

I've been looking into javascript test suites and I have found QUnit to be very interesting. I understand how to test computational code, but...

How do you test javascript applications written primarily for DOM manipulation?

it seems like testing the position/color/etc of DOM elements would be a moot point because you'd end up doing somethign like this:

$("li.my_element").css("background-color", "#f00"); 

and then in your test...

$(function() {     module("coloring");     test("test_my_element", function() {         var li_element_color = $("li.my_element").css('background-color');         equals(li_element_color, "#f00");     }); }); 

this just doesn't feel right because it basically just doing this:

var my_li= $("li.my_element"); my_li.css("background-color", "#f00"); if ( my_li.css("background-color") == "#f00" ) {     return true; } 

Am I nuts? How is this supposed to be done?

edit: the heart of the question:

I guess what I'm getting at is, I need to make sure the code isn't broken before I deploy, but the vast majority of it is UI helpers and ajax. How do I test that things are appearing correctly?

A few examples:

  • test that a JQuery UI dialog is appearing on top of all other elements
  • test that the drag-n-drop is working properly
  • test that the color of a droppable changes when an element is dropped on it
  • test that the ajax is all working properly
  • test that there are no extraneous commas that will break IE
like image 642
Jiaaro Avatar asked Sep 16 '09 19:09

Jiaaro


People also ask

Can JavaScript be used to manipulate the DOM?

The DOM stands for Document Object Model. It can simply be understood as a tree of nodes created by the browser. Each of these nodes has its own properties and methods which can be manipulated using JavaScript. The ability to manipulate the DOM is one of the most unique and useful abilities of JavaScript.

What is DOM in Jest?

jest-dom is a companion library for Testing Library that provides custom DOM element matchers for Jest. npm.


1 Answers

I have found the Javascript/DOM tests, especially for the simple interactions that you are describing, are not that useful. You'll testing that things are set up right, and since jQuery is so declarative, your tests look a lot like your code.

My current thinking is that if you are writing larger JS components, it makes sense to pull out a set of interrelated behaviors both into a jQuery plugin and a set of tests for it.

But from the examples you mentioned, it sounds like you're really looking for a level of protection within your integrated website. A tool like Selenium will probably be more powerful and appropriate for you. In particular, it

  • can be automated
  • can run against multiple browsers, including IE
  • runs within the context of your web app and pages, so drag-n-drop can be tested where it really happens instead of in some test environment.
  • AJAX can be tested
like image 129
ndp Avatar answered Sep 21 '22 12:09

ndp