Skip to content
Permalink
Browse files

refactor(compileSpec): make tests consistent

PR (#15141)
  • Loading branch information
Narretz committed Oct 14, 2016
1 parent d5d9ad1 commit 3bc3b40ed7f9d99b1ae3445179c405f776e69f2c
Showing with 72 additions and 134 deletions.
  1. +72 −134 test/ng/compileSpec.js
@@ -1189,41 +1189,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: '<div></div><div></div>'
};
});
directive('singleRootWithWhiteSpace', function() {
describe('replace and not exactly one root element', function() {
var templateVar;

beforeEach(module(function() {
directive('template', function() {
return {
replace: true,
template: ' <div></div> \n'
template: function() {
return templateVar;
}
};
});
});
}));

inject(function($compile) {
expect(function() {
$compile('<p no-root-elem></p>');
}).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': '<div></div><div></div>'
}, function(directiveTemplate) {

expect(function() {
$compile('<p multi-root-elem></p>');
}).toThrowMinErr('$compile', 'tplrt', 'Template for directive \'multiRootElem\' must have exactly one root element. ');
inject(function($compile) {
templateVar = directiveTemplate;
expect(function() {
$compile('<p template></p>');
}).toThrowMinErr('$compile', 'tplrt',
'Template for directive \'template\' must have exactly one root element.'
);
});
});

// ws is ok
expect(function() {
$compile('<p single-root-with-white-space></p>');
}).not.toThrow();
they('should not throw if the root element is accompanied by: $prop',
{
'whitespace': ' <div>Hello World!</div> \n',
'comments': '<!-- oh hi --><div>Hello World!</div> \n',
'comments + whitespace': ' <!-- oh hi --> <div>Hello World!</div> <!-- oh hi -->\n'
}, function(directiveTemplate) {

inject(function($compile, $rootScope) {
templateVar = directiveTemplate;
var element;
expect(function() {
element = $compile('<p template></p>')($rootScope);
}).not.toThrow();
expect(element.length).toBe(1);
expect(element.text()).toBe('Hello World!');
});
});
});

@@ -1336,38 +1347,6 @@ describe('$compile', function() {
});
}

it('should ignore comment nodes when replacing with a template', function() {
module(function() {
directive('replaceWithComments', valueFn({
replace: true,
template: '<!-- ignored comment --><p>Hello, world!</p><!-- ignored comment-->'
}));
});
inject(function($compile, $rootScope) {
expect(function() {
element = $compile('<div><div replace-with-comments></div></div>')($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: '<!-- ignored comment --> <p>Hello, world!</p> <!-- ignored comment-->'
}));
});
inject(function($compile, $rootScope) {
expect(function() {
element = $compile('<div><div replace-with-whitespace></div></div>')($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() {
@@ -2063,57 +2042,55 @@ 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 {
replace: true,
templateUrl: 'template.html'
};
});
});
}));

inject(function($compile, $templateCache, $rootScope, $exceptionHandler) {
// no root element
$templateCache.put('template.html', 'dada');
$compile('<p template></p>');
$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': '<div></div><div></div>'
}, function(directiveTemplate) {

// multi root
$templateCache.put('template.html', '<div></div><div></div>');
$compile('<p template></p>');
$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('<p template></p>')($rootScope);
$rootScope.$digest();

// ws is ok
$templateCache.put('template.html', ' <div></div> \n');
$compile('<p template></p>');
$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', '<!-- oh hi --><div></div> \n');
$compile('<p template></p>');
$rootScope.$apply();
expect($exceptionHandler.errors).toEqual([]);
they('should not throw if the root element is accompanied by: $prop',
{
'whitespace': ' <div>Hello World!</div> \n',
'comments': '<!-- oh hi --><div>Hello World!</div> \n',
'comments + whitespace': ' <!-- oh hi --> <div>Hello World!</div> <!-- oh hi -->\n'
}, function(directiveTemplate) {

// white space around comments is ok
$templateCache.put('template.html', ' <!-- oh hi --> <div></div> <!-- oh hi -->\n');
$compile('<p template></p>');
$rootScope.$apply();
expect($exceptionHandler.errors).toEqual([]);
inject(function($compile, $templateCache, $rootScope) {
$templateCache.put('template.html', directiveTemplate);
element = $compile('<p template></p>')($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
@@ -2302,45 +2279,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('<!-- ignored comment --><p>Hello, world!</p><!-- ignored comment-->');
expect(function() {
element = $compile('<div><div replace-with-comments></div></div>')($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('<!-- ignored comment --> <p>Hello, world!</p> <!-- ignored comment-->');
expect(function() {
element = $compile('<div><div replace-with-whitespace></div></div>')($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() {

0 comments on commit 3bc3b40

Please sign in to comment.
You can’t perform that action at this time.