Permalink
Browse files

refactor toggleClass and tests

  • Loading branch information...
1 parent 441dd66 commit 025630b51ff1aa7bda4a6d3f28c31399d9e0ab5c @mislav mislav committed Dec 12, 2012
Showing with 43 additions and 46 deletions.
  1. +5 −5 src/zepto.js
  2. +38 −41 test/zepto.html
View
10 src/zepto.js
@@ -626,11 +626,11 @@ var Zepto = (function() {
},
toggleClass: function(name, when){
return this.each(function(idx){
- var newName = funcArg(this, name, idx, className(this));
- newName.split(/\s+/g).forEach(function(klass){
- (when === undefined ? !$(this).hasClass(klass) : when) ?
- $(this).addClass(klass) : $(this).removeClass(klass)
- }, this)
+ var $this = $(this), names = funcArg(this, name, idx, className(this))
+ names.split(/\s+/g).forEach(function(klass){
+ (when === undefined ? !$this.hasClass(klass) : when) ?
+ $this.addClass(klass) : $this.removeClass(klass)
+ })
})
},
scrollTop: function(){
View
79 test/zepto.html
@@ -2021,66 +2021,63 @@
},
testToggleClass: function(t){
- var el = $('#toggle_element').get(0)
- $('#toggle_element').toggleClass('green')
+ var el = $('#toggle_element').removeClass()
- t.assert($('#toggle_element').hasClass('green'))
- t.assert(!$('#toggle_element').hasClass('orange'))
+ t.assertIdentical(el, el.toggleClass('green'))
+ t.assertTrue(el.hasClass('green'))
+ t.assertFalse(el.hasClass('orange'))
- $('#toggle_element').toggleClass('orange')
- t.assert($('#toggle_element').hasClass('green'))
- t.assert($('#toggle_element').hasClass('orange'))
+ el.toggleClass('orange')
+ t.assertTrue(el.hasClass('green'))
+ t.assertTrue(el.hasClass('orange'))
- $('#toggle_element').toggleClass('green')
+ el.toggleClass('green')
+ t.assertFalse(el.hasClass('green'))
+ t.assertTrue(el.hasClass('orange'))
- t.assert(!$('#toggle_element').hasClass('green'))
- t.assert($('#toggle_element').hasClass('orange'))
+ el.toggleClass('orange')
+ t.assertFalse(el.hasClass('green'))
+ t.assertFalse(el.hasClass('orange'))
- $('#toggle_element').toggleClass('orange')
- t.assert(!$('#toggle_element').hasClass('green'))
- t.assert(!$('#toggle_element').hasClass('orange'))
+ el.toggleClass('orange green')
+ t.assertTrue(el.hasClass('orange'))
+ t.assertTrue(el.hasClass('green'))
- el.className = ''
- $(el).toggleClass('orange green')
- t.assert($(el).hasClass('orange'))
- t.assert($(el).hasClass('green'))
- $(el).toggleClass('orange green blue')
- t.assert(!$(el).hasClass('orange'))
- t.assert(!$(el).hasClass('green'))
- t.assert($(el).hasClass('blue'))
- el.className = ''
+ el.toggleClass('orange green blue')
+ t.assertFalse(el.hasClass('orange'))
+ t.assertFalse(el.hasClass('green'))
+ t.assertTrue(el.hasClass('blue'))
- $('#toggle_element').toggleClass('orange', false)
- t.assert(!$('#toggle_element').hasClass('orange'))
+ el.removeClass()
- $('#toggle_element').toggleClass('orange', false)
- t.assert(!$('#toggle_element').hasClass('orange'))
+ el.toggleClass('orange', false)
+ t.assertFalse(el.hasClass('orange'))
+ el.toggleClass('orange', false)
+ t.assertFalse(el.hasClass('orange'))
- $('#toggle_element').toggleClass('orange', true)
- t.assert($('#toggle_element').hasClass('orange'))
-
- $('#toggle_element').toggleClass('orange', true)
- t.assert($('#toggle_element').hasClass('orange'))
+ el.toggleClass('orange', true)
+ t.assertTrue(el.hasClass('orange'))
+ el.toggleClass('orange', true)
+ t.assertTrue(el.hasClass('orange'))
//function argument
- $('#toggle_element').toggleClass(function(idx,classes){
- //test the value of "this"
- t.assertEqualCollection($('#toggle_element'), $(this))
+ el.toggleClass(function(idx,classes){
+ t.assertIdentical(el.get(0), this)
//test original classes are being passed
t.assertEqual('orange', this.className)
return "brown"
})
- t.assert($('#toggle_element').hasClass('brown'))
+ t.assertTrue(el.hasClass('brown'))
- $('#toggle_element').toggleClass(function(idx,classes){
+ el.toggleClass(function(idx,classes){
return "yellow"
- },false)
- t.assert(!$('#toggle_element').hasClass('yellow'))
+ }, false)
+ t.assertFalse(el.hasClass('yellow'))
- $('#toggle_element').toggleClass(function(idx,classes){
+ el.toggleClass(function(idx,classes){
return "yellow"
- },true)
- t.assert($('#toggle_element').hasClass('yellow'))
+ }, true)
+ t.assert(el.hasClass('yellow'))
},
testClassSVG: function(t){

0 comments on commit 025630b

Please sign in to comment.