// $Id$ /** * @file * Tests for jquery.once.js. */ (function($) { Drupal.tests.once = { getInfo: function() { return { 'name': 'jQuery once plugin', 'description': 'Tests the jQuery once plugin to make sure it does indeed run on elements only once.', 'group': 'System' }; }, setup: function() { $(Drupal.tests.iframe).html('
'); }, test: function() { // Test once, since this is the first time we should get both divs. var $resultOne = $('div', Drupal.tests.iframe).once('test'); ok($resultOne.size() == 2, Drupal.t('Both divs were returned initially.')); $resultOne.each(function() { ok($(this).hasClass('test-processed'), Drupal.t('Div has the "test-processed" class.')); }); // Now test again, and we should get neither div. var $resultTwo = $('div', Drupal.tests.iframe).once('test'); ok($resultTwo.size() == 0, Drupal.t('Neither div was returned the second time.')); // Now add a div to the page and try again, we should only get the new div. $(Drupal.tests.iframe).append('
'); var $resultThree = $('div', Drupal.tests.iframe).once('test'); ok($resultThree.size() == 1, Drupal.t('Only the unprocessed div was returned.')); $resultThree.each(function() { // We should only have gotten div 3. ok($(this).is('#3'), Drupal.t('Only the third div was returned.')); ok($(this).hasClass('test-processed'), Drupal.t('Div has the "test-processed" class.')); }); // Now remove the once behavior, and we should get all three divs returned. var $resultFour = $('div', Drupal.tests.iframe).removeOnce('test'); ok($resultFour.size() == 3, Drupal.t('All divs were returned after removing the once behavior.')); $resultFour.each(function() { ok(!$(this).hasClass('test-processed'), Drupal.t('Div does not have the "test-processed" class.')); }); // Now try doing something once again, all three divs should be returned. var $resultFive = $('div', Drupal.tests.iframe).once('test'); ok($resultFive.size() == 3, Drupal.t('All divs were returned when we attempt to get the divs once again.')); $resultFive.each(function() { ok($(this).hasClass('test-processed'), Drupal.t('Div has the "test-processed" class again.')); }); } }; })(jQuery);