Solved

jquery unit testing, how can I use it?

Posted on 2011-03-17
3
240 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
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…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

830 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