From 5143e7bf065a3cbdf8400cf095b653d51bc83b8f Mon Sep 17 00:00:00 2001 From: Misko Hevery Date: Fri, 6 Jan 2012 18:10:47 -0800 Subject: feat(module): new module loader --- test/AngularSpec.js | 163 ++++++++++++++++------------------------------------ 1 file changed, 49 insertions(+), 114 deletions(-) (limited to 'test/AngularSpec.js') diff --git a/test/AngularSpec.js b/test/AngularSpec.js index 70997240..55842acd 100644 --- a/test/AngularSpec.js +++ b/test/AngularSpec.js @@ -264,148 +264,83 @@ describe('angular', function() { }); - describe('angularJsConfig', function() { - it('should always consider angular.js script tag to be the last script tag', function() { - var doc = { - getElementsByTagName: function(tagName) { - expect(tagName).toEqual('script'); - return [{nodeName: 'SCRIPT', src: 'random.js', - attributes: [{name: 'ng:autobind', value: 'wrong'}]}, - {nodeName: 'SCRIPT', src: 'angular.js', - attributes: [{name: 'ng:autobind', value: 'correct'}]}]; - } - }; + describe('angularInit', function() { + var bootstrap; + var element; - expect(angularJsConfig(doc)).toEqual({autobind: 'correct'}); + beforeEach(function() { + element = { + getElementById: function (id) { + return element.getElementById[id] || []; + }, - doc = { - getElementsByTagName: function(tagName) { - expect(tagName).toEqual('script'); - return [{nodeName: 'SCRIPT', src: 'angular.js', - attributes: [{name: 'ng:autobind', value: 'wrong'}]}, - {nodeName: 'SCRIPT', src: 'concatinatedAndObfuscadedScriptWithOurScript.js', - attributes: [{name: 'ng:autobind', value: 'correct'}]}]; + getAttribute: function(name) { + return element[name]; } }; - - expect(angularJsConfig(doc)).toEqual({autobind: 'correct'}); + bootstrap = jasmine.createSpy('bootstrap'); }); - it('should extract angular config from the ng: attributes', function() { - var doc = { getElementsByTagName: function(tagName) { - expect(lowercase(tagName)).toEqual('script'); - return [{ - nodeName: 'SCRIPT', - src: 'angularjs/angular.js', - attributes: [{name: 'ng:autobind', value:'elementIdToCompile'}, - {name: 'ng:css', value: 'css/my_custom_angular.css'}] }]; - }}; - - expect(angularJsConfig(doc)).toEqual({ - autobind: 'elementIdToCompile', - css: 'css/my_custom_angular.css' - }); + it('should do nothing when not found', function() { + angularInit(element, bootstrap); + expect(bootstrap).not.toHaveBeenCalled(); }); - it('should extract angular config and default autobind value to true if present', function() { - var doc = { getElementsByTagName: function(tagName) { - expect(lowercase(tagName)).toEqual('script'); - return [{ - nodeName: 'SCRIPT', - src: 'angularjs/angular.js', - attributes: [{name: 'ng:autobind', value:undefined}]}]; - }}; - - expect(angularJsConfig(doc)).toEqual({autobind: true}); + it('should look for ng:app directive in id', function() { + var appElement = jqLite('
')[0]; + jqLite(document.body).append(appElement); + angularInit(element, bootstrap); + expect(bootstrap).toHaveBeenCalledOnceWith(appElement, ['ABC']); }); - it('should extract angular autobind config from the script hashpath attributes', function() { - var doc = { getElementsByTagName: function(tagName) { - expect(lowercase(tagName)).toEqual('script'); - return [{ - nodeName: 'SCRIPT', - src: 'angularjs/angular.js#autobind'}]; - }}; - - expect(angularJsConfig(doc)).toEqual({autobind: true}); + it('should look for ng:app directive in className', function() { + var appElement = jqLite('
')[0]; + element.querySelectorAll = function(arg) { return element.querySelectorAll[arg] || []; } + element.querySelectorAll['.ng\\:app'] = [appElement]; + angularInit(element, bootstrap); + expect(bootstrap).toHaveBeenCalledOnceWith(appElement, ['ABC']); }); - it('should extract autobind config with element id from the script hashpath', function() { - var doc = { getElementsByTagName: function(tagName) { - expect(lowercase(tagName)).toEqual('script'); - return [{ - nodeName: 'SCRIPT', - src: 'angularjs/angular.js#autobind=foo'}]; - }}; - - expect(angularJsConfig(doc)).toEqual({autobind: 'foo'}); + it('should look for ng:app directive using querySelectorAll', function() { + var appElement = jqLite('
')[0]; + element.querySelectorAll = function(arg) { return element.querySelectorAll[arg] || []; } + element.querySelectorAll['[ng\\:app]'] = [ appElement ]; + angularInit(element, bootstrap); + expect(bootstrap).toHaveBeenCalledOnceWith(appElement, ['ABC']); }); - it('should default to versioned ie-compat file if angular file is versioned', function() { - var doc = { getElementsByTagName: function(tagName) { - expect(lowercase(tagName)).toEqual('script'); - return [{ - nodeName: 'SCRIPT', - src: 'js/angular-0.9.0.js'}]; - }}; - - expect(angularJsConfig(doc)).toEqual({}); + it('should bootstrap using class name', function() { + var appElement = jqLite('
')[0]; + angularInit(jqLite('
').append(appElement)[0], bootstrap); + expect(bootstrap).toHaveBeenCalledOnceWith(appElement, ['ABC']); }); - }); - describe('angularInit', function() { - var dom; - - beforeEach(function() { - dom = jqLite('
{{2+3}}' + - '
{{4+5}}
' + - '
')[0]; - }); - - - afterEach(function() { - dealoc(dom); - }); - - - it('should not compile anything if autobind is missing or false', function() { - angularInit({}, dom); - expect(sortedHtml(dom)).toEqual('
{{2+3}}' + - '
{{4+5}}
' + - '
'); + it('should bootstrap anonymously', function() { + var appElement = jqLite('
')[0]; + element.querySelectorAll = function(arg) { return element.querySelectorAll[arg] || []; } + element.querySelectorAll['[x-ng-app]'] = [ appElement ]; + angularInit(element, bootstrap); + expect(bootstrap).toHaveBeenCalledOnceWith(appElement, []); }); - it('should compile the document if autobind is true', function() { - angularInit({autobind: true}, dom); - expect(sortedHtml(dom)).toEqual('
' + - '5' + - '
'+ - '9' + - '
' + - '
'); + it('should bootstrap anonymously using class only', function() { + var appElement = jqLite('
')[0]; + angularInit(jqLite('
').append(appElement)[0], bootstrap); + expect(bootstrap).toHaveBeenCalledOnceWith(appElement, []); }); - it('should compile only the element specified via autobind', function() { - dom.getElementById = function() { - return this.childNodes[1]; - }; - - - angularInit({autobind: 'child'}, dom); - - expect(sortedHtml(dom)).toEqual('
{{2+3}}' + - '
'+ - '9' + - '
' + - '
'); + it('should bootstrap if the annotation is on the root element', function() { + var appElement = jqLite('
')[0]; + angularInit(appElement, bootstrap); + expect(bootstrap).toHaveBeenCalledOnceWith(appElement, []); }); }); -- cgit v1.2.3