Solved

jquery unit testing, how can I use it?

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

747 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

13 Experts available now in Live!

Get 1:1 Help Now