diff options
| author | Misko Hevery | 2011-02-10 11:20:49 -0800 | 
|---|---|---|
| committer | Misko Hevery | 2011-02-16 08:59:42 -0500 | 
| commit | 00cc9eb32a9387040d0175fcfd21cf9dcab6514f (patch) | |
| tree | 0edaac339a3ec69ff769e20b28b6ebe51d040272 /test | |
| parent | ef4bb28be13e99f96c9ace5936cf26a174a0e5f0 (diff) | |
| download | angular.js-00cc9eb32a9387040d0175fcfd21cf9dcab6514f.tar.bz2 | |
rewrite of JQuery lite implementation, which now better supports selected sets
Diffstat (limited to 'test')
| -rw-r--r-- | test/jqLiteSpec.js | 283 | ||||
| -rw-r--r-- | test/testabilityPatch.js | 9 | 
2 files changed, 288 insertions, 4 deletions
| diff --git a/test/jqLiteSpec.js b/test/jqLiteSpec.js index da8ab206..c5e92c0f 100644 --- a/test/jqLiteSpec.js +++ b/test/jqLiteSpec.js @@ -1,8 +1,52 @@  describe('jqLite', function(){    var scope; +  var a, b, c; +  beforeEach(function(){ +    a = jqLite('<div>A</div>')[0]; +    b = jqLite('<div>B</div>')[0]; +    c = jqLite('<div>C</div>')[0]; +  });    beforeEach(function(){      scope = angular.scope(); +    this.addMatchers({ +      toJqEqual: function(expected) { +        var msg = "Unequal length"; +        this.message = function() { return msg; }; + +        var value = this.actual && expected && this.actual.length == expected.length; +        for (var i = 0; value && i < expected.length; i++) { +          var actual = jqLite(this.actual[i])[0]; +          var expect = jqLite(expected[i])[0]; +          value = value && equals(expect, actual); +          msg = "Not equal at index: " + i +              + " - Expected: " + expect +              + " - Actual: " + actual; +        } +        return value; +      } +    }); +  }); + +  afterEach(function(){ +    dealoc(a); +    dealoc(b); +    dealoc(c); +  }); + +  describe('construction', function(){ +    it('should allow construction with text node', function(){ +      var text = a.firstChild; +      var selected = jqLite(text); +      expect(selected.length).toEqual(1); +      expect(selected[0]).toEqual(text); +    }); +    it('should allow construction with html', function(){ +      var nodes = jqLite('<div>1</div><span>2</span>'); +      expect(nodes.length).toEqual(2); +      expect(nodes[0].innerHTML).toEqual('1'); +      expect(nodes[1].innerHTML).toEqual('2'); +    });    });    describe('scope', function() { @@ -24,8 +68,245 @@ describe('jqLite', function(){      it('should return undefined when no scope was found', function() {        var element = jqLite('<ul><li><p><b>deep deep</b><p></li></ul>');        var deepChild = jqLite(element[0].getElementsByTagName('b')[0]); -      expect(deepChild.scope()).toBeNull(); +      expect(deepChild.scope()).toBeFalsy();        dealoc(element);      });    }); + +  describe('data', function(){ +    it('should set and get ande remove data', function(){ +      var selected = jqLite([a, b, c]); + +      expect(selected.data('prop', 'value')).toEqual(selected); +      expect(selected.data('prop')).toEqual('value'); +      expect(jqLite(a).data('prop')).toEqual('value'); +      expect(jqLite(b).data('prop')).toEqual('value'); +      expect(jqLite(c).data('prop')).toEqual('value'); + +      jqLite(a).data('prop', 'new value'); +      expect(jqLite(a).data('prop')).toEqual('new value'); +      expect(selected.data('prop')).toEqual('new value'); +      expect(jqLite(b).data('prop')).toEqual('value'); +      expect(jqLite(c).data('prop')).toEqual('value'); + +      expect(selected.removeData('prop')).toEqual(selected); +      expect(jqLite(a).data('prop')).toEqual(undefined); +      expect(jqLite(b).data('prop')).toEqual(undefined); +      expect(jqLite(c).data('prop')).toEqual(undefined); +    }); +  }); + +  describe('attr', function(){ +    it('shoul read wirite and remove attr', function(){ +      var selector = jqLite([a, b]); + +      expect(selector.attr('prop', 'value')).toEqual(selector); +      expect(jqLite(a).attr('prop')).toEqual('value'); +      expect(jqLite(b).attr('prop')).toEqual('value'); + +      expect(selector.attr({'prop': 'new value'})).toEqual(selector); +      expect(jqLite(a).attr('prop')).toEqual('new value'); +      expect(jqLite(b).attr('prop')).toEqual('new value'); + +      jqLite(b).attr({'prop': 'new value 2'}); +      expect(jqLite(selector).attr('prop')).toEqual('new value'); +      expect(jqLite(b).attr('prop')).toEqual('new value 2'); + +      selector.removeAttr('prop'); +      expect(jqLite(a).attr('prop')).toBeFalsy(); +      expect(jqLite(b).attr('prop')).toBeFalsy(); +    }); +  }); +  describe('class', function(){ +    describe('hasClass', function(){ +      it('should check class', function(){ +        var selector = jqLite([a, b]); +        expect(selector.hasClass('abc')).toEqual(false); +      }); +    }); +    describe('addClass', function(){ +      it('should allow adding of class', function(){ +        var selector = jqLite([a, b]); +        expect(selector.addClass('abc')).toEqual(selector); +        expect(jqLite(a).hasClass('abc')).toEqual(true); +        expect(jqLite(b).hasClass('abc')).toEqual(true); +      }); +    }); +    describe('toggleClass', function(){ +      it('should allow toggling of class', function(){ +        var selector = jqLite([a, b]); +        expect(selector.toggleClass('abc')).toEqual(selector); +        expect(jqLite(a).hasClass('abc')).toEqual(true); +        expect(jqLite(b).hasClass('abc')).toEqual(true); + +        expect(selector.toggleClass('abc')).toEqual(selector); +        expect(jqLite(a).hasClass('abc')).toEqual(false); +        expect(jqLite(b).hasClass('abc')).toEqual(false); + +        expect(selector.toggleClass('abc'), true).toEqual(selector); +        expect(jqLite(a).hasClass('abc')).toEqual(true); +        expect(jqLite(b).hasClass('abc')).toEqual(true); + +        expect(selector.toggleClass('abc'), false).toEqual(selector); +        expect(jqLite(a).hasClass('abc')).toEqual(false); +        expect(jqLite(b).hasClass('abc')).toEqual(false); + +      }); +    }); +    describe('removeClass', function(){ +      it('should allow removal of class', function(){ +        var selector = jqLite([a, b]); +        expect(selector.addClass('abc')).toEqual(selector); +        expect(selector.removeClass('abc')).toEqual(selector); +        expect(jqLite(a).hasClass('abc')).toEqual(false); +        expect(jqLite(b).hasClass('abc')).toEqual(false); +      }); +    }); +  }); +  describe('css', function(){ +    it('should set and read css', function(){ +      var selector = jqLite([a, b]); + +      expect(selector.css('prop', 'value')).toEqual(selector); +      expect(jqLite(a).css('prop')).toEqual('value'); +      expect(jqLite(b).css('prop')).toEqual('value'); + +      expect(selector.css({'prop': 'new value'})).toEqual(selector); +      expect(jqLite(a).css('prop')).toEqual('new value'); +      expect(jqLite(b).css('prop')).toEqual('new value'); + +      jqLite(b).css({'prop': 'new value 2'}); +      expect(jqLite(selector).css('prop')).toEqual('new value'); +      expect(jqLite(b).css('prop')).toEqual('new value 2'); + +      selector.css('prop', null); +      expect(jqLite(a).css('prop')).toBeFalsy(); +      expect(jqLite(b).css('prop')).toBeFalsy(); +    }); +  }); +  describe('text', function(){ +    it('should return null on empty', function(){ +      expect(jqLite().length).toEqual(0); +      expect(jqLite().text()).toEqual(''); +    }); + +    it('should read/write value', function(){ +      var element = jqLite('<div>abc</div>'); +      expect(element.length).toEqual(1); +      expect(element[0].innerHTML).toEqual('abc'); +      expect(element.text()).toEqual('abc'); +      expect(element.text('xyz') == element).toBeTruthy(); +      expect(element.text()).toEqual('xyz'); +    }); +  }); +  describe('val', function(){ +    it('should read, write value', function(){ +      var input = jqLite('<input type="text"/>'); +      expect(input.val('abc')).toEqual(input); +      expect(input[0].value).toEqual('abc'); +      expect(input.val()).toEqual('abc'); +    }); +  }); +  describe('html', function(){ +    it('should return null on empty', function(){ +      expect(jqLite().length).toEqual(0); +      expect(jqLite().html()).toEqual(null); +    }); + +    it('should read/write value', function(){ +      var element = jqLite('<div>abc</div>'); +      expect(element.length).toEqual(1); +      expect(element[0].innerHTML).toEqual('abc'); +      expect(element.html()).toEqual('abc'); +      expect(element.html('xyz') == element).toBeTruthy(); +      expect(element.html()).toEqual('xyz'); +    }); +  }); + +  describe('bind', function(){ +    it('should bind to all elements and return functions', function(){ +      var selected = jqLite([a, b]); +      var log = ''; +      expect(selected.bind('click', function(){ +        log += 'click on: ' + jqLite(this).text() + ';'; +      })).toEqual(selected); +      browserTrigger(a, 'click'); +      expect(log).toEqual('click on: A;'); +      browserTrigger(b, 'click'); +      expect(log).toEqual('click on: A;click on: B;'); +    }); +  }); + +  describe('replaceWith', function(){ +    it('should replaceWith', function(){ +      var root = jqLite('<div>').html('before-<div></div>after'); +      var div = root.find('div'); +      expect(div.replaceWith('<span>span-</span><b>bold-</b>')).toEqual(div); +      expect(root.text()).toEqual('before-span-bold-after'); +    }); +    it('should replaceWith text', function(){ +      var root = jqLite('<div>').html('before-<div></div>after'); +      var div = root.find('div'); +      expect(div.replaceWith('text-')).toEqual(div); +      expect(root.text()).toEqual('before-text-after'); +    }); +  }); +  describe('children', function(){ +    it('should select non-text children', function(){ +      var root = jqLite('<div>').html('before-<div></div>after-<span></span>'); +      var div = root.find('div'); +      var span = root.find('span'); +      expect(root.children()).toJqEqual([div, span]); +    }); +  }); +  describe('append', function(){ +    it('should append', function(){ +      var root = jqLite('<div>'); +      expect(root.append('<span>abc</span>')).toEqual(root); +      expect(root.html().toLowerCase()).toEqual('<span>abc</span>'); +    }); +    it('should append text', function(){ +      var root = jqLite('<div>'); +      expect(root.append('text')).toEqual(root); +      expect(root.html()).toEqual('text'); +    }); +  }); +  describe('remove', function(){ +    it('should remove', function(){ +      var root = jqLite('<div><span>abc</span></div>'); +      var span = root.find('span'); +      expect(span.remove()).toEqual(span); +      expect(root.html()).toEqual(''); +    }); +  }); +  describe('after', function(){ +    it('should after', function(){ +      var root = jqLite('<div><span></span></div>'); +      var span = root.find('span'); +      expect(span.after('<i></i><b></b>')).toEqual(span); +      expect(root.html().toLowerCase()).toEqual('<span></span><i></i><b></b>'); +    }); +    it('should allow taking text', function(){ +      var root = jqLite('<div><span></span></div>'); +      var span = root.find('span'); +      span.after('abc'); +      expect(root.html().toLowerCase()).toEqual('<span></span>abc'); +    }); +  }); +  describe('parent', function(){ +    it('should return empty set when no parent', function(){ +      var element = jqLite('<div>abc</div>'); +      expect(element.parent()).toBeTruthy(); +      expect(element.parent().length).toEqual(0); +    }); +  }); +  describe('find', function(){ +    it('should find child by name', function(){ +      var root = jqLite('<div><div>text</div></div>'); +      var innerDiv = root.find('div'); +      expect(innerDiv.length).toEqual(1); +      expect(innerDiv.html()).toEqual('text'); +    }); +  }); +  }); diff --git a/test/testabilityPatch.js b/test/testabilityPatch.js index dc6acf8b..b5c2c3f4 100644 --- a/test/testabilityPatch.js +++ b/test/testabilityPatch.js @@ -142,8 +142,11 @@ function childNode(element, index) {  }  function dealoc(obj) { -  var element = (obj||{}).$element || obj; -  if (element && element.dealoc) element.dealoc(); +  if (obj) { +    var element = obj.$element || obj || {}; +    if (element.nodeName) element = jqLite(element); +    if (element.dealoc) element.dealoc(); +  }  }  extend(angular, { @@ -179,7 +182,7 @@ function sortedHtml(element, showNgClass) {          replace(/</g, '<').          replace(/>/g, '>');      } else { -      html += '<' + node.nodeName.toLowerCase(); +      html += '<' + (node.nodeName || '?NOT_A_NODE?').toLowerCase();        var attributes = node.attributes || [];        var attrs = [];        var className = node.className || ''; | 
