diff --git a/test/ng/compileSpec.js b/test/ng/compileSpec.js index 1786d3cd603b..544baf8a74ca 100755 --- a/test/ng/compileSpec.js +++ b/test/ng/compileSpec.js @@ -1201,41 +1201,52 @@ describe('$compile', function() { }); }); - it('should fail if replacing and template doesn\'t have a single root element', function() { - module(function() { - directive('noRootElem', function() { - return { - replace: true, - template: 'dada' - }; - }); - directive('multiRootElem', function() { - return { - replace: true, - template: '
' - }; - }); - directive('singleRootWithWhiteSpace', function() { + describe('replace and not exactly one root element', function() { + var templateVar; + + beforeEach(module(function() { + directive('template', function() { return { replace: true, - template: '
\n' + template: function() { + return templateVar; + } }; }); - }); + })); - inject(function($compile) { - expect(function() { - $compile('

'); - }).toThrowMinErr('$compile', 'tplrt', 'Template for directive \'noRootElem\' must have exactly one root element. '); + they('should throw if: $prop', + { + 'no root element': 'dada', + 'multiple root elements': '
' + }, function(directiveTemplate) { - expect(function() { - $compile('

'); - }).toThrowMinErr('$compile', 'tplrt', 'Template for directive \'multiRootElem\' must have exactly one root element. '); + inject(function($compile) { + templateVar = directiveTemplate; + expect(function() { + $compile('

'); + }).toThrowMinErr('$compile', 'tplrt', + 'Template for directive \'template\' must have exactly one root element.' + ); + }); + }); - // ws is ok - expect(function() { - $compile('

'); - }).not.toThrow(); + they('should not throw if the root element is accompanied by: $prop', + { + 'whitespace': '
Hello World!
\n', + 'comments': '
Hello World!
\n', + 'comments + whitespace': '
Hello World!
\n' + }, function(directiveTemplate) { + + inject(function($compile, $rootScope) { + templateVar = directiveTemplate; + var element; + expect(function() { + element = $compile('

')($rootScope); + }).not.toThrow(); + expect(element.length).toBe(1); + expect(element.text()).toBe('Hello World!'); + }); }); }); @@ -1348,38 +1359,6 @@ describe('$compile', function() { }); } - it('should ignore comment nodes when replacing with a template', function() { - module(function() { - directive('replaceWithComments', valueFn({ - replace: true, - template: '

Hello, world!

' - })); - }); - inject(function($compile, $rootScope) { - expect(function() { - element = $compile('
')($rootScope); - }).not.toThrow(); - expect(element.find('p').length).toBe(1); - expect(element.find('p').text()).toBe('Hello, world!'); - }); - }); - - it('should ignore whitespace betwee comment and root node when replacing with a template', function() { - module(function() { - directive('replaceWithWhitespace', valueFn({ - replace: true, - template: '

Hello, world!

' - })); - }); - inject(function($compile, $rootScope) { - expect(function() { - element = $compile('
')($rootScope); - }).not.toThrow(); - expect(element.find('p').length).toBe(1); - expect(element.find('p').text()).toBe('Hello, world!'); - }); - }); - it('should keep prototype properties on directive', function() { module(function() { function DirectiveClass() { @@ -2078,10 +2057,9 @@ describe('$compile', function() { } )); + describe('replace and not exactly one root element', function() { - it('should fail if replacing and template doesn\'t have a single root element', function() { - module(function($exceptionHandlerProvider) { - $exceptionHandlerProvider.mode('log'); + beforeEach(module(function() { directive('template', function() { return { @@ -2089,46 +2067,45 @@ describe('$compile', function() { templateUrl: 'template.html' }; }); - }); + })); - inject(function($compile, $templateCache, $rootScope, $exceptionHandler) { - // no root element - $templateCache.put('template.html', 'dada'); - $compile('

'); - $rootScope.$digest(); - expect($exceptionHandler.errors.pop()).toEqualMinErr('$compile', 'tplrt', - 'Template for directive \'template\' must have exactly one root element. ' + - 'template.html'); + they('should throw if: $prop', + { + 'no root element': 'dada', + 'multiple root elements': '
' + }, function(directiveTemplate) { - // multi root - $templateCache.put('template.html', '
'); - $compile('

'); - $rootScope.$digest(); - expect($exceptionHandler.errors.pop()).toEqualMinErr('$compile', 'tplrt', - 'Template for directive \'template\' must have exactly one root element. ' + - 'template.html'); + inject(function($compile, $templateCache, $rootScope, $exceptionHandler) { + $templateCache.put('template.html', directiveTemplate); + $compile('

')($rootScope); + $rootScope.$digest(); - // ws is ok - $templateCache.put('template.html', '
\n'); - $compile('

'); - $rootScope.$apply(); - expect($exceptionHandler.errors).toEqual([]); + expect($exceptionHandler.errors.pop()).toEqualMinErr('$compile', 'tplrt', + 'Template for directive \'template\' must have exactly one root element. ' + + 'template.html' + ); + }); + }); - // comments are ok - $templateCache.put('template.html', '
\n'); - $compile('

'); - $rootScope.$apply(); - expect($exceptionHandler.errors).toEqual([]); + they('should not throw if the root element is accompanied by: $prop', + { + 'whitespace': '
Hello World!
\n', + 'comments': '
Hello World!
\n', + 'comments + whitespace': '
Hello World!
\n' + }, function(directiveTemplate) { - // white space around comments is ok - $templateCache.put('template.html', '
\n'); - $compile('

'); - $rootScope.$apply(); - expect($exceptionHandler.errors).toEqual([]); + inject(function($compile, $templateCache, $rootScope) { + $templateCache.put('template.html', directiveTemplate); + element = $compile('

')($rootScope); + expect(function() { + $rootScope.$digest(); + }).not.toThrow(); + expect(element.length).toBe(1); + expect(element.text()).toBe('Hello World!'); + }); }); }); - it('should resume delayed compilation without duplicates when in a repeater', function() { // this is a test for a regression // scope creation, isolate watcher setup, controller instantiation, etc should happen @@ -2317,45 +2294,6 @@ describe('$compile', function() { }); } - it('should ignore comment nodes when replacing with a templateUrl', function() { - module(function() { - directive('replaceWithComments', valueFn({ - replace: true, - templateUrl: 'templateWithComments.html' - })); - }); - inject(function($compile, $rootScope, $httpBackend) { - $httpBackend.whenGET('templateWithComments.html'). - respond('

Hello, world!

'); - expect(function() { - element = $compile('
')($rootScope); - }).not.toThrow(); - $httpBackend.flush(); - expect(element.find('p').length).toBe(1); - expect(element.find('p').text()).toBe('Hello, world!'); - }); - }); - - it('should ignore whitespace between comment and root node when replacing with a templateUrl', function() { - module(function() { - directive('replaceWithWhitespace', valueFn({ - replace: true, - templateUrl: 'templateWithWhitespace.html' - })); - }); - inject(function($compile, $rootScope, $httpBackend) { - $httpBackend.whenGET('templateWithWhitespace.html'). - respond('

Hello, world!

'); - expect(function() { - element = $compile('
')($rootScope); - }).not.toThrow(); - $httpBackend.flush(); - expect(element.find('p').length).toBe(1); - expect(element.find('p').text()).toBe('Hello, world!'); - }); - }); - - it('should keep prototype properties on sync version of async directive', function() { module(function() { function DirectiveClass() {