Solved

jquery unit testing, how can I use it?

Posted on 2011-03-17
3
235 Views
Last Modified: 2012-05-11

Hello Experts,

Can someone please guide me to use Qunittesting?
I understand the concept but I am not sure how to use it. I read a tutorial from net.tuts but still I am not sure how to use it on my code... could someone please guide me? what can I test from my code?
The following is the code I would like to unit test, it works, but I would like to know how can I unit test it, perhaps if you guys give me an example using my own code (I've been seeing codes around but still I can;t get it) I may understand. it is a simple script pagination:

    var currentPage = 1;
    $("div[class^='section']").hide();
    showPage(currentPage);
    $("ul li.back a").live('click', function (e) {
        e.preventDefault();
    });
    $("ul li.next a").live('click', function (e) {
        e.preventDefault();
        if (currentPage != $("div[class^='section']").length) {
            currentPage++;
            showPage(currentPage);            
        }
    });
    function showPage() {
        $("div[class^='section']").hide();
        $(".section" + currentPage).fadeIn();

    }
And the following is the environment they use for unit testing. you don't need to download jquery files since everything in on a server:


<!DOCTYPE html>
<html>
<head>
      <title>QUnit Test Suite</title>
      <link rel="stylesheet" href="http://github.com/jquery/qunit/raw/master/qunit/qunit.css" type="text/css" media="screen">
      <script type="text/javascript" src="http://github.com/jquery/qunit/raw/master/qunit/qunit.js"></script>
      <!-- Your project file goes here -->
      <script type="text/javascript" src="myProject.js"></script>
      <!-- Your tests file goes here -->
      <script type="text/javascript" src="myTests.js"></script>
</head>
<body>
      <h1 id="qunit-header">QUnit Test Suite</h1>
      <h2 id="qunit-banner"></h2>
      <div id="qunit-testrunner-toolbar"></div>
      <h2 id="qunit-userAgent"></h2>
      <ol id="qunit-tests"></ol>
</body>
</html>

0
Comment
Question by:glepiza
  • 2
3 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 35190564
Check this :


<!DOCTYPE html>
<html>
<head>
      <title>QUnit Test Suite</title>
      <link rel="stylesheet" href="http://github.com/jquery/qunit/raw/master/qunit/qunit.css" type="text/css" media="screen">
      <script src="http://code.jquery.com/jquery-latest.js"></script>
      <script type="text/javascript" src="http://github.com/jquery/qunit/raw/master/qunit/qunit.js"></script>
      <script language="javascript" type="text/javascript">
      	$(document).ready(function() {

		    var currentPage = 1;

		    function showPage() {
		        $("div[class^='section']").hide();
		        $(".section" + currentPage).fadeIn();		
		    }

      		module("http://www.experts-exchange.com/Programming/Languages/Scripting/JScript/Q__26895431.html");

      		test("Phase 1 : hide all div where class start by \"section\"", function() {
			    // doing the job
			    $("div[class^='section']").hide();
			    // checking :
			    notEqual( $("div[class^='section']:visible").length, 0, "Some element(s) still visible(s)");
			});
			
      		test("Phase 2 : show the current page", function() {
			    showPage(currentPage);
			    equals( $(".section" + currentPage).is(":visible"), true, "page not visible");
			});

      	});
      </script>
</head>
<body>
      <h1 id="qunit-header">QUnit Test Suite</h1>
      <h2 id="qunit-banner"></h2>
      <div id="qunit-testrunner-toolbar"></div>
      <h2 id="qunit-userAgent"></h2>
      <ol id="qunit-tests"></ol>
</body>
</html>

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
ID: 35190890
0
 

Author Closing Comment

by:glepiza
ID: 35201302
Thanks!!, that was a great example!!!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This article discusses how to create an extensible mechanism for linked drop downs.
In this tutorial viewers will learn how to position items using CSS's three positioning types Create a new HTML document with an internal stylesheet.: Create another div in CSS and name it Absolute : Type "position:absolute;" and "top:10px; left:50p…
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now