Solved

jquery unit testing, how can I use it?

Posted on 2011-03-17
3
244 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
Originally, this post was published on Monitis Blog, you can check it here . Websites are getting bigger and more complicated by the day. Video, images and custom fonts are all great for showcasing your product or service. But the price to pay in…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

738 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