QUnit Lab Solution

by ramyaaviji

HTML

<script src="http://code.jquery.com/qunit/qunit-1.12.0.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/qunit/qunit-1.12.0.css">
<h1 id="qunit-header">Test Suite</h1>

 <h2 id="qunit-banner"></h2>

<div id="qunit-testrunner-toolbar"></div>
 <h2 id="qunit-userAgent"></h2>

<ol id="qunit-tests"></ol>
<div id="qunit-fixture">
    <ul>
      <li>foo</li>
      <li>bar</li>
      <li>baz</li>
    </ul>
</div>

JavaScript

/*!
 * jQuery Enumerate - v1.0 - 1/2/2011
 * http://benalman.com/
 * 
 * Copyright (c) 2011 "Cowboy" Ben Alman
 * Dual licensed under the MIT and GPL licenses.
 * http://benalman.com/about/license/
 */
(function($){

  $.fn.enumerate = function(start) {
    if (typeof start !== "number") {
      start = 1;
    }
    return this.each(function(i) {
      $(this).prepend((start + i) + ". ");
    });
  };

}(jQuery));

module("jQuery#enumerate");
 
test("chainable", 1, function() {
  var items = $("#qunit-fixture li");
  strictEqual(items.enumerate(), items, "should be chainable");
});
 
test("no args passed", 3, function() {
  var items = $("#qunit-fixture li").enumerate();
  equal(items.eq(0).text(), "1. foo", "first item should have index 1");
  equal(items.eq(1).text(), "2. bar", "second item should have index 2");
  equal(items.eq(2).text(), "3. baz", "third item should have index 3");
});
 
test("0 passed", 3, function() {
  var items = $("#qunit-fixture li").enumerate(0);
  equal(items.eq(0).text(), "0. foo", "first item should have index 0");
  equal(items.eq(1).text(), "1. bar", "second item should have index 1");
  equal(items.eq(2).text(), "2. baz", "third item should have index 2");
});