From 9c0418cf1abd609bf0ffbe71fbdfa75905cf8e0f Mon Sep 17 00:00:00 2001 From: Igor Minar Date: Fri, 4 May 2012 01:24:46 -0700 Subject: fix($compile): ignore ws when checking if template has single root Also add the same error checking for sync templates. Closes #910 --- test/ng/compileSpec.js | 85 ++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 75 insertions(+), 10 deletions(-) (limited to 'test/ng/compileSpec.js') diff --git a/test/ng/compileSpec.js b/test/ng/compileSpec.js index b2a6856b..c5592816 100644 --- a/test/ng/compileSpec.js +++ b/test/ng/compileSpec.js @@ -487,10 +487,48 @@ describe('$compile', function() { expect(child).toHaveClass('three'); expect(child).toHaveClass('log'); // merged from replace directive template })); + + it("should fail if replacing and template doesn't have a single root element", function() { + module(function($compileProvider) { + $compileProvider.directive('noRootElem', function() { + return { + replace: true, + template: 'dada' + } + }); + $compileProvider.directive('multiRootElem', function() { + return { + replace: true, + template: '
' + } + }); + $compileProvider.directive('singleRootWithWhiteSpace', function() { + return { + replace: true, + template: '
\n' + } + }); + }); + + inject(function($compile) { + expect(function() { + $compile('

'); + }).toThrow('Template must have exactly one root element. was: dada'); + + expect(function() { + $compile('

'); + }).toThrow('Template must have exactly one root element. was:
'); + + // ws is ok + expect(function() { + $compile('

'); + }).not.toThrow(); + }); + }); }); - describe('async templates', function() { + describe('templateUrl', function() { beforeEach(module( function($compileProvider) { @@ -916,15 +954,6 @@ describe('$compile', function() { }); - it('should check that template has root element', inject(function($compile, $httpBackend) { - $httpBackend.expect('GET', 'hello.html').respond('before mid after'); - $compile('
'); - expect(function(){ - $httpBackend.flush(); - }).toThrow('Template must have exactly one root element: before mid after'); - })); - - it('should allow multiple elements in template', inject(function($compile, $httpBackend) { $httpBackend.expect('GET', 'hello.html').respond('before mid after'); element = jqLite('
'); @@ -958,6 +987,42 @@ describe('$compile', function() { expect(element.text()).toEqual('i=1;i=2;'); } )); + + + it("should fail if replacing and template doesn't have a single root element", function() { + module(function($exceptionHandlerProvider, $compileProvider) { + $exceptionHandlerProvider.mode('log'); + + $compileProvider.directive('template', function() { + return { + replace: true, + templateUrl: 'template.html' + } + }); + }); + + inject(function($compile, $templateCache, $rootScope, $exceptionHandler) { + // no root element + $templateCache.put('template.html', 'dada'); + $compile('

'); + $rootScope.$digest(); + expect($exceptionHandler.errors.pop().message). + toBe('Template must have exactly one root element. was: dada'); + + // multi root + $templateCache.put('template.html', '
'); + $compile('

'); + $rootScope.$digest(); + expect($exceptionHandler.errors.pop().message). + toBe('Template must have exactly one root element. was:
'); + + // ws is ok + $templateCache.put('template.html', '
\n'); + $compile('

'); + $rootScope.$apply(); + expect($exceptionHandler.errors).toEqual([]); + }); + }); }); -- cgit v1.2.3