diff options
Diffstat (limited to 'test')
| -rw-r--r-- | test/CompilerSpec.js | 135 | ||||
| -rw-r--r-- | test/ParserTest.js | 19 | ||||
| -rw-r--r-- | test/ResourceSpec.js | 7 | ||||
| -rw-r--r-- | test/directivesSpec.js | 85 | ||||
| -rw-r--r-- | test/scenario/StepsTest.js | 2 |
5 files changed, 239 insertions, 9 deletions
diff --git a/test/CompilerSpec.js b/test/CompilerSpec.js new file mode 100644 index 00000000..3ea2e473 --- /dev/null +++ b/test/CompilerSpec.js @@ -0,0 +1,135 @@ +describe('compiler', function(){ + function element(html) { + return jQuery(html)[0]; + } + + var compiler, markup, directives, widgets, compile, log; + + beforeEach(function(){ + log = ""; + directives = { + hello: function(expression, element){ + log += "hello "; + return function() { + log += expression; + }; + }, + + watch: function(expression, element){ + return function() { + this.$watch(expression, function(val){ + log += ":" + val; + }); + }; + } + + }; + markup = []; + widgets = {}; + compiler = new Compiler(markup, directives, widgets); + compile = function(html){ + var e = element("<div>" + html + "</div>"); + var view = compiler.compile(e)(e); + view.init(); + return view.scope; + }; + }); + + it('should recognize a directive', function(){ + 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); + return function initFn() { + log += ":init"; + }; + }; + var template = compiler.compile(e); + var init = template(e).init; + expect(log).toEqual("found"); + init(); + expect(log).toEqual("found:init"); + }); + + it('should recurse to children', function(){ + var scope = compile('<div><span hello="misko"/></div>'); + expect(log).toEqual("hello misko"); + }); + + it('should watch scope', function(){ + var scope = compile('<span watch="name"/>'); + expect(log).toEqual(""); + scope.updateView(); + scope.set('name', 'misko'); + scope.updateView(); + scope.updateView(); + scope.set('name', 'adam'); + scope.updateView(); + scope.updateView(); + expect(log).toEqual(":misko:adam"); + }); + + 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.removeAttr("duplicate"); + var template = this.compile(element); + return function(marker) { + this.$addEval(function() { + marker.after(template(element.clone()).element); + }); + }; + }; + 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'); + scope.updateView(); + expect($(scope.element).html()).toEqual('before<!--marker--><span>x</span><span>x</span>after'); + }); + + it('should allow for exculsive tags which suppress others', function(){ + directives.exclusive = function(){ + return function() { + log += ('exclusive'); + }; + }; + directives.exclusive.exclusive = true; + + compile('<span hello="misko", exclusive/>'); + expect(log).toEqual('exclusive'); + }); + + it('should process markup before directives', function(){ + markup.push(function(text, textNode, parentNode) { + if (text == 'middle') { + expect(textNode.text()).toEqual(text); + parentNode.attr('hello', text); + textNode.text('replaced'); + } + }); + var scope = compile('before<span>middle</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['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('<div>button</div>'); + expect(log).toEqual('init'); + }); + +}); diff --git a/test/ParserTest.js b/test/ParserTest.js index 09c3b8de..53ca9eda 100644 --- a/test/ParserTest.js +++ b/test/ParserTest.js @@ -41,7 +41,7 @@ LexerTest.prototype.testTokenizeAString = function(){ i++; assertEquals(tokens[i].index, 15); - assertEquals(tokens[i].text, "a'c"); + assertEquals(tokens[i].string, "a'c"); i++; assertEquals(tokens[i].index, 21); @@ -49,7 +49,7 @@ LexerTest.prototype.testTokenizeAString = function(){ i++; assertEquals(tokens[i].index, 22); - assertEquals(tokens[i].text, 'd"e'); + assertEquals(tokens[i].string, 'd"e'); }; @@ -68,10 +68,10 @@ LexerTest.prototype.testQuotedString = function(){ var tokens = lexer.parse(); assertEquals(1, tokens[1].index); - assertEquals("'", tokens[1].text); + assertEquals("'", tokens[1].string); assertEquals(7, tokens[3].index); - assertEquals('"', tokens[3].text); + assertEquals('"', tokens[3].string); }; @@ -80,14 +80,14 @@ LexerTest.prototype.testQuotedStringEscape = function(){ var lexer = new Lexer(str); var tokens = lexer.parse(); - assertEquals('"\n\f\r\t\v\u00A0', tokens[0].text); + assertEquals('"\n\f\r\t\v\u00A0', tokens[0].string); }; LexerTest.prototype.testTokenizeUnicode = function(){ var lexer = new Lexer('"\\u00A0"'); var tokens = lexer.parse(); assertEquals(1, tokens.length); - assertEquals('\u00a0', tokens[0].text); + assertEquals('\u00a0', tokens[0].string); }; LexerTest.prototype.testTokenizeRegExpWithOptions = function(){ @@ -408,7 +408,7 @@ ParserTest.prototype.testItShouldParseOnChangeIntoHashSet = function () { ParserTest.prototype.testItShouldParseOnChangeBlockIntoHashSet = function () { var scope = new Scope({count:0}); var listeners = {a:[], b:[]}; - scope.watch("a:{count=count+1;count=count+20;};b:count=count+300", + scope.watch("a:{count=count+1;count=count+20;};b:count=count+300", function(n, fn){listeners[n].push(fn);}); assertEquals(1, scope.watchListeners.a.listeners.length); @@ -477,3 +477,8 @@ ParserTest.prototype.testNegationBug = function () { assertEquals(12/6/2, scope.eval("12/6/2")); }; +ParserTest.prototype.testBugStringConfusesParser = function() { + var scope = new Scope(); + assertEquals('!', scope.eval('suffix = "!"')); +}; + diff --git a/test/ResourceSpec.js b/test/ResourceSpec.js index 799c7378..0c7af00a 100644 --- a/test/ResourceSpec.js +++ b/test/ResourceSpec.js @@ -61,7 +61,7 @@ describe("resource", function() { beforeEach(function(){ xhr = new MockXHR(); - resource = new ResourceFactory(xhr); + resource = new ResourceFactory(_(xhr.method).bind(xhr)); CreditCard = resource.route('/CreditCard/:id:verb', {id:'@id.key'}, { charge:{ method:'POST', @@ -80,6 +80,11 @@ describe("resource", function() { expect(typeof CreditCard.query).toBe('function'); }); + it('should default to empty parameters', function(){ + xhr.expectGET('URL').respond({}); + resource.route('URL').query(); + }); + it("should build resource with default param", function(){ xhr.expectGET('/Order/123/Line/456.visa?minimum=0.05').respond({id:'abc'}); var LineItem = resource.route('/Order/:orderId/Line/:id:verb', {orderId: '123', id: '@id.key', verb:'.visa', minimum:0.05}); diff --git a/test/directivesSpec.js b/test/directivesSpec.js new file mode 100644 index 00000000..2cee20d1 --- /dev/null +++ b/test/directivesSpec.js @@ -0,0 +1,85 @@ +describe("directives", function(){ + + var compile, element; + + beforeEach(function() { + var compiler = new Compiler(angularMarkup, angularDirective, angularWidget); + compile = function(html) { + element = jqLite(html); + var view = compiler.compile(element.element)(element.element); + view.init(); + return view.scope; + }; + }); + + 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-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 = ""; + element.eachNode(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;"); + }); +}); diff --git a/test/scenario/StepsTest.js b/test/scenario/StepsTest.js index 9d64d0a9..32ef637d 100644 --- a/test/scenario/StepsTest.js +++ b/test/scenario/StepsTest.js @@ -2,6 +2,6 @@ StepsTest = TestCase("StepsTest"); StepsTest.prototype.testGivenDataset=function(){ var self = {frame:{}, dataset:[]}; - angular.test.GIVEN.dataset.call(self); + angular.scenario.GIVEN.dataset.call(self); assertEquals('$DATASET:{"dataset":[]}', self.frame.name); }; |
