diff options
Diffstat (limited to 'test')
| -rw-r--r-- | test/BinderTest.js | 1 | ||||
| -rw-r--r-- | test/CompilerSpec.js | 46 | ||||
| -rw-r--r-- | test/directivesSpec.js | 162 | ||||
| -rw-r--r-- | test/markupSpec.js | 49 | ||||
| -rw-r--r-- | test/widgetsSpec.js | 28 |
5 files changed, 262 insertions, 24 deletions
diff --git a/test/BinderTest.js b/test/BinderTest.js index 9ddf38ab..e37026e7 100644 --- a/test/BinderTest.js +++ b/test/BinderTest.js @@ -703,7 +703,6 @@ BinderTest.prototype.testActionOnAHrefThrowsError = function(){ var input = state.node.find('a'); input.click(); assertEquals('abc', fromJson(input.attr('ng-error')).a); - assertNotNull(input.data('qtip')); assertTrue("should have an error class", input.hasClass('ng-exception')); input.attr('ng-action', '0'); diff --git a/test/CompilerSpec.js b/test/CompilerSpec.js index 8487b139..59b7ab6b 100644 --- a/test/CompilerSpec.js +++ b/test/CompilerSpec.js @@ -3,7 +3,7 @@ xdescribe('compiler', function(){ return jQuery(html)[0]; } - var compiler, markup, directives, widgets, compile, log; + var compiler, textMarkup, directives, widgets, compile, log; beforeEach(function(){ log = ""; @@ -24,9 +24,10 @@ xdescribe('compiler', function(){ } }; - markup = []; + textMarkup = []; + attrMarkup = []; widgets = {}; - compiler = new Compiler(markup, directives, widgets); + compiler = new Compiler(textMarkup, attrMarkup, directives, widgets); compile = function(html){ var e = element("<div>" + html + "</div>"); var view = compiler.compile(e)(e); @@ -36,11 +37,11 @@ xdescribe('compiler', function(){ }); it('should recognize a directive', function(){ - var e = element('<div ng-directive="expr" ignore="me"></div>'); + var e = element('<div directive="expr" ignore="me"></div>'); directives.directive = function(expression, element){ log += "found"; expect(expression).toEqual("expr"); - expect(element.element).toEqual(e); + expect(element[0]).toEqual(e); return function initFn() { log += ":init"; }; @@ -53,12 +54,12 @@ xdescribe('compiler', function(){ }); it('should recurse to children', function(){ - var scope = compile('<div><span ng-hello="misko"/></div>'); + var scope = compile('<div><span hello="misko"/></div>'); expect(log).toEqual("hello misko"); }); it('should watch scope', function(){ - var scope = compile('<span ng-watch="name"/>'); + var scope = compile('<span watch="name"/>'); expect(log).toEqual(""); scope.updateView(); scope.set('name', 'misko'); @@ -70,26 +71,24 @@ xdescribe('compiler', function(){ expect(log).toEqual(":misko:adam"); }); - it('should prevent recursion', function(){ - directives.stop = function(){ return false; }; - var scope = compile('<span ng-hello="misko" ng-stop="true"><span ng-hello="adam"/></span>'); + it('should prevent descend', function(){ + directives.stop = function(){ this.descend(false); }; + var scope = compile('<span hello="misko" stop="true"><span hello="adam"/></span>'); expect(log).toEqual("hello misko"); }); it('should allow creation of templates', function(){ directives.duplicate = function(expr, element){ element.replaceWith(document.createComment("marker")); - element.removeAttribute("ng-duplicate"); + element.removeAttr("duplicate"); var template = this.compile(element); return function(marker) { - this.$eval(function() { - dump("A"); + this.$addEval(function() { marker.after(template(element.clone()).element); - dump("B"); }); }; }; - var scope = compile('before<span ng-duplicate="expr">x</span>after'); + var scope = compile('before<span duplicate="expr">x</span>after'); expect($(scope.element).html()).toEqual('before<!--marker-->after'); scope.updateView(); expect($(scope.element).html()).toEqual('before<!--marker--><span>x</span>after'); @@ -105,32 +104,33 @@ xdescribe('compiler', function(){ }; directives.exclusive.exclusive = true; - compile('<span ng-hello="misko", ng-exclusive/>'); + compile('<span hello="misko", exclusive/>'); expect(log).toEqual('exclusive'); }); it('should process markup before directives', function(){ - markup.push(function(text, textNode, parentNode) { + textMarkup.push(function(text, textNode, parentNode) { if (text == 'middle') { expect(textNode.text()).toEqual(text); - parentNode.attr('ng-hello', text); - textNode.nodeValue = 'replaced'; + parentNode.attr('hello', text); + textNode.text('replaced'); } }); var scope = compile('before<span>middle</span>after'); - expect(scope.element.innerHTML).toEqual('before<span ng-hello="middle">replaced</span>after'); + expect(scope.element.innerHTML).toEqual('before<span hello="middle">replaced</span>after'); expect(log).toEqual("hello middle"); }); it('should replace widgets', function(){ - widgets.button = function(element) { - element.parentNode.replaceChild(button, element); + widgets['NG:BUTTON'] = function(element) { + element.replaceWith('<div>button</div>', element); return function(element) { log += 'init'; }; }; var scope = compile('<ng:button>push me</ng:button>'); - expect(scope.element.innerHTML).toEqual('before<span ng-hello="middle">replaced</span>after'); + expect(scope.element.innerHTML).toEqual('<div>button</div>'); + expect(log).toEqual('init'); }); }); diff --git a/test/directivesSpec.js b/test/directivesSpec.js new file mode 100644 index 00000000..18bedb64 --- /dev/null +++ b/test/directivesSpec.js @@ -0,0 +1,162 @@ +describe("directives", function(){ + + var compile, element; + + beforeEach(function() { + var compiler = new Compiler(angularTextMarkup, angularAttrMarkup, angularDirective, angularWidget); + compile = function(html) { + element = jqLite(html); + var view = compiler.compile(element)(element); + view.init(); + return view.scope; + }; + }); + + afterEach(function(){ + element.remove(); + expect(_(jqCache).size()).toEqual(0); + }); + + it("should ng-init", function() { + var scope = compile('<div ng-init="a=123"></div>'); + expect(scope.get('a')).toEqual(123); + }); + + it("should ng-eval", function() { + var scope = compile('<div ng-init="a=0" ng-eval="a = a + 1"></div>'); + expect(scope.get('a')).toEqual(0); + scope.updateView(); + expect(scope.get('a')).toEqual(1); + scope.updateView(); + expect(scope.get('a')).toEqual(2); + }); + + it('should ng-bind', function() { + var scope = compile('<div ng-bind="a"></div>'); + expect(element.text()).toEqual(''); + scope.set('a', 'misko'); + scope.updateView(); + expect(element.text()).toEqual('misko'); + }); + + it('should ng-bind-template', function() { + var scope = compile('<div ng-bind-template="Hello {{name}}!"></div>'); + expect(element.text()).toEqual(''); + scope.set('name', 'Misko'); + scope.updateView(); + expect(element.text()).toEqual('Hello Misko!'); + }); + + it('should ng-bind-attr', function(){ + var scope = compile('<img ng-bind-attr="{src:\'mysrc\', alt:\'myalt\'}"/>'); + expect(element.attr('src')).toEqual(null); + expect(element.attr('alt')).toEqual(null); + scope.updateView(); + expect(element.attr('src')).toEqual('mysrc'); + expect(element.attr('alt')).toEqual('myalt'); + }); + + it('should ng-non-bindable', function(){ + var scope = compile('<div ng-non-bindable><span ng-bind="name"></span></div>'); + scope.set('name', 'misko'); + scope.updateView(); + expect(element.text()).toEqual(''); + }); + + it('should ng-repeat over array', function(){ + var scope = compile('<ul><li ng-repeat="item in items" ng-init="suffix = \';\'" ng-bind="item + suffix"></li></ul>'); + + scope.set('items', ['misko', 'shyam']); + scope.updateView(); + expect(element.text()).toEqual('misko;shyam;'); + + scope.set('items', ['adam', 'kai', 'brad']); + scope.updateView(); + expect(element.text()).toEqual('adam;kai;brad;'); + + scope.set('items', ['brad']); + scope.updateView(); + expect(element.text()).toEqual('brad;'); + }); + + it('should ng-repeat over object', function(){ + var scope = compile('<ul><li ng-repeat="(key, value) in items" ng-bind="key + \':\' + value + \';\' "></li></ul>'); + scope.set('items', {misko:'swe', shyam:'set'}); + scope.updateView(); + expect(element.text()).toEqual('misko:swe;shyam:set;'); + }); + + it('should error on wrong parsing of ng-repeat', function(){ + var scope = compile('<ul><li ng-repeat="i dont parse"></li></ul>'); + var log = ""; + eachNode(element, function(li){ + log += li.attr('ng-error') + ';'; + log += li.hasClass('ng-exception') + ';'; + }); + expect(log).toEqual("\"Expected ng-repeat in form of 'item in collection' but got 'i dont parse'.\";true;"); + }); + + it('should ng-watch', function(){ + var scope = compile('<div ng-watch="i: count = count + 1" ng-init="count = 0">'); + scope.updateView(); + scope.updateView(); + expect(scope.get('count')).toEqual(0); + + scope.set('i', 0); + scope.updateView(); + scope.updateView(); + expect(scope.get('count')).toEqual(1); + }); + + it('should ng-action', function(){ + var scope = compile('<div ng-action="clicked = true"></div>'); + scope.updateView(); + expect(scope.get('clicked')).toBeFalsy(); + + element.click(); + expect(scope.get('clicked')).toEqual(true); + }); + + it('should ng-class', function(){ + var scope = compile('<div class="existing" ng-class="[\'A\', \'B\']"></div>'); + scope.updateView(); + expect(element.hasClass('existing')).toBeTruthy(); + expect(element.hasClass('A')).toBeTruthy(); + expect(element.hasClass('B')).toBeTruthy(); + }); + + it('should ng-class odd/even', function(){ + var scope = compile('<ul><li ng-repeat="i in [0,1]" class="existing" ng-class-odd="\'odd\'" ng-class-even="\'even\'"></li><ul>'); + scope.updateView(); + var e1 = jQuery(element.parent()[0]).find('li:first'); + var e2 = jQuery(element.parent()[0]).find('li:last'); + expect(e1.hasClass('existing')).toBeTruthy(); + expect(e1.hasClass('even')).toBeTruthy(); + expect(e2.hasClass('existing')).toBeTruthy(); + expect(e2.hasClass('odd')).toBeTruthy(); + }); + + it('should ng-style', function(){ + var scope = compile('<div ng-style="{color:\'red\'}"></div>'); + scope.updateView(); + expect(element.css('color')).toEqual('red'); + }); + + it('should ng-show', function(){ + var scope = compile('<div ng-hide="hide"></div>'); + scope.updateView(); + expect(element.css('display')).toEqual(''); + scope.set('hide', true); + scope.updateView(); + expect(element.css('display')).toEqual('none'); + }); + + it('should ng-hide', function(){ + var scope = compile('<div ng-show="show"></div>'); + scope.updateView(); + expect(element.css('display')).toEqual('none'); + scope.set('show', true); + scope.updateView(); + expect(element.css('display')).toEqual(''); + }); +}); diff --git a/test/markupSpec.js b/test/markupSpec.js new file mode 100644 index 00000000..9e89af7b --- /dev/null +++ b/test/markupSpec.js @@ -0,0 +1,49 @@ +describe("markups", function(){ + + var compile, element, scope; + + beforeEach(function() { + scope = null; + element = null; + var compiler = new Compiler(angularTextMarkup, angularAttrMarkup, angularDirective, angularWidget); + compile = function(html) { + element = jqLite(html); + var view = compiler.compile(element)(element); + view.init(); + scope = view.scope; + }; + }); + + afterEach(function(){ + if (element) { + element.remove(); + } + expect(_(jqCache).size()).toEqual(0); + }); + + it('should translate {{}} in text', function(){ + compile('<div>hello {{name}}!</div>'); + expect(element.html()).toEqual('hello <span ng-bind="name"></span>!'); + scope.set('name', 'Misko'); + scope.updateView(); + expect(element.html()).toEqual('hello <span ng-bind="name">Misko</span>!'); + }); + + it('should translate {{}} in terminal nodes', function(){ + compile('<select><option>Greet {{name}}!</option></select>'); + expect(element.html()).toEqual('<option ng-bind-template="Greet {{name}}!"></option>'); + scope.set('name', 'Misko'); + scope.updateView(); + expect(element.html()).toEqual('<option ng-bind-template="Greet {{name}}!">Greet Misko!</option>'); + }); + + it('should translate {{}} in attributes', function(){ + compile('<img src="http://server/{{path}}.png"/>'); + expect(element.attr('src')).toEqual(); + expect(element.attr('ng-bind-attr')).toEqual('{"src":"http://server/{{path}}.png"}'); + scope.set('path', 'a/b'); + scope.updateView(); + expect(element.attr('src')).toEqual("http://server/a/b.png"); + }); + +}); diff --git a/test/widgetsSpec.js b/test/widgetsSpec.js new file mode 100644 index 00000000..3b6be8ec --- /dev/null +++ b/test/widgetsSpec.js @@ -0,0 +1,28 @@ +describe("widgets", function(){ + + var compile, element, scope; + + beforeEach(function() { + scope = null; + element = null; + var compiler = new Compiler(angularTextMarkup, angularAttrMarkup, angularDirective, angularWidget); + compile = function(html) { + element = jqLite(html); + var view = compiler.compile(element)(element); + view.init(); + scope = view.scope; + }; + }); + + afterEach(function(){ + if (element) { + element.remove(); + } + expect(_(jqCache).size()).toEqual(0); + }); + + it('should fail', function(){ + fail('iueoi'); + }); + +}); |
