Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit c22615c

Browse files
authored
refactor(compileSpec): make tests consistent
PR (#15141)
1 parent fc0c11d commit c22615c

File tree

1 file changed

+72
-134
lines changed

1 file changed

+72
-134
lines changed

test/ng/compileSpec.js

+72-134
Original file line numberDiff line numberDiff line change
@@ -1201,41 +1201,52 @@ describe('$compile', function() {
12011201
});
12021202
});
12031203

1204-
it('should fail if replacing and template doesn\'t have a single root element', function() {
1205-
module(function() {
1206-
directive('noRootElem', function() {
1207-
return {
1208-
replace: true,
1209-
template: 'dada'
1210-
};
1211-
});
1212-
directive('multiRootElem', function() {
1213-
return {
1214-
replace: true,
1215-
template: '<div></div><div></div>'
1216-
};
1217-
});
1218-
directive('singleRootWithWhiteSpace', function() {
1204+
describe('replace and not exactly one root element', function() {
1205+
var templateVar;
1206+
1207+
beforeEach(module(function() {
1208+
directive('template', function() {
12191209
return {
12201210
replace: true,
1221-
template: ' <div></div> \n'
1211+
template: function() {
1212+
return templateVar;
1213+
}
12221214
};
12231215
});
1224-
});
1216+
}));
12251217

1226-
inject(function($compile) {
1227-
expect(function() {
1228-
$compile('<p no-root-elem></p>');
1229-
}).toThrowMinErr('$compile', 'tplrt', 'Template for directive \'noRootElem\' must have exactly one root element. ');
1218+
they('should throw if: $prop',
1219+
{
1220+
'no root element': 'dada',
1221+
'multiple root elements': '<div></div><div></div>'
1222+
}, function(directiveTemplate) {
12301223

1231-
expect(function() {
1232-
$compile('<p multi-root-elem></p>');
1233-
}).toThrowMinErr('$compile', 'tplrt', 'Template for directive \'multiRootElem\' must have exactly one root element. ');
1224+
inject(function($compile) {
1225+
templateVar = directiveTemplate;
1226+
expect(function() {
1227+
$compile('<p template></p>');
1228+
}).toThrowMinErr('$compile', 'tplrt',
1229+
'Template for directive \'template\' must have exactly one root element.'
1230+
);
1231+
});
1232+
});
12341233

1235-
// ws is ok
1236-
expect(function() {
1237-
$compile('<p single-root-with-white-space></p>');
1238-
}).not.toThrow();
1234+
they('should not throw if the root element is accompanied by: $prop',
1235+
{
1236+
'whitespace': ' <div>Hello World!</div> \n',
1237+
'comments': '<!-- oh hi --><div>Hello World!</div> \n',
1238+
'comments + whitespace': ' <!-- oh hi --> <div>Hello World!</div> <!-- oh hi -->\n'
1239+
}, function(directiveTemplate) {
1240+
1241+
inject(function($compile, $rootScope) {
1242+
templateVar = directiveTemplate;
1243+
var element;
1244+
expect(function() {
1245+
element = $compile('<p template></p>')($rootScope);
1246+
}).not.toThrow();
1247+
expect(element.length).toBe(1);
1248+
expect(element.text()).toBe('Hello World!');
1249+
});
12391250
});
12401251
});
12411252

@@ -1348,38 +1359,6 @@ describe('$compile', function() {
13481359
});
13491360
}
13501361

1351-
it('should ignore comment nodes when replacing with a template', function() {
1352-
module(function() {
1353-
directive('replaceWithComments', valueFn({
1354-
replace: true,
1355-
template: '<!-- ignored comment --><p>Hello, world!</p><!-- ignored comment-->'
1356-
}));
1357-
});
1358-
inject(function($compile, $rootScope) {
1359-
expect(function() {
1360-
element = $compile('<div><div replace-with-comments></div></div>')($rootScope);
1361-
}).not.toThrow();
1362-
expect(element.find('p').length).toBe(1);
1363-
expect(element.find('p').text()).toBe('Hello, world!');
1364-
});
1365-
});
1366-
1367-
it('should ignore whitespace betwee comment and root node when replacing with a template', function() {
1368-
module(function() {
1369-
directive('replaceWithWhitespace', valueFn({
1370-
replace: true,
1371-
template: '<!-- ignored comment --> <p>Hello, world!</p> <!-- ignored comment-->'
1372-
}));
1373-
});
1374-
inject(function($compile, $rootScope) {
1375-
expect(function() {
1376-
element = $compile('<div><div replace-with-whitespace></div></div>')($rootScope);
1377-
}).not.toThrow();
1378-
expect(element.find('p').length).toBe(1);
1379-
expect(element.find('p').text()).toBe('Hello, world!');
1380-
});
1381-
});
1382-
13831362
it('should keep prototype properties on directive', function() {
13841363
module(function() {
13851364
function DirectiveClass() {
@@ -2078,57 +2057,55 @@ describe('$compile', function() {
20782057
}
20792058
));
20802059

2060+
describe('replace and not exactly one root element', function() {
20812061

2082-
it('should fail if replacing and template doesn\'t have a single root element', function() {
2083-
module(function($exceptionHandlerProvider) {
2084-
$exceptionHandlerProvider.mode('log');
2062+
beforeEach(module(function() {
20852063

20862064
directive('template', function() {
20872065
return {
20882066
replace: true,
20892067
templateUrl: 'template.html'
20902068
};
20912069
});
2092-
});
2070+
}));
20932071

2094-
inject(function($compile, $templateCache, $rootScope, $exceptionHandler) {
2095-
// no root element
2096-
$templateCache.put('template.html', 'dada');
2097-
$compile('<p template></p>');
2098-
$rootScope.$digest();
2099-
expect($exceptionHandler.errors.pop()).toEqualMinErr('$compile', 'tplrt',
2100-
'Template for directive \'template\' must have exactly one root element. ' +
2101-
'template.html');
2072+
they('should throw if: $prop',
2073+
{
2074+
'no root element': 'dada',
2075+
'multiple root elements': '<div></div><div></div>'
2076+
}, function(directiveTemplate) {
21022077

2103-
// multi root
2104-
$templateCache.put('template.html', '<div></div><div></div>');
2105-
$compile('<p template></p>');
2106-
$rootScope.$digest();
2107-
expect($exceptionHandler.errors.pop()).toEqualMinErr('$compile', 'tplrt',
2108-
'Template for directive \'template\' must have exactly one root element. ' +
2109-
'template.html');
2078+
inject(function($compile, $templateCache, $rootScope, $exceptionHandler) {
2079+
$templateCache.put('template.html', directiveTemplate);
2080+
$compile('<p template></p>')($rootScope);
2081+
$rootScope.$digest();
21102082

2111-
// ws is ok
2112-
$templateCache.put('template.html', ' <div></div> \n');
2113-
$compile('<p template></p>');
2114-
$rootScope.$apply();
2115-
expect($exceptionHandler.errors).toEqual([]);
2083+
expect($exceptionHandler.errors.pop()).toEqualMinErr('$compile', 'tplrt',
2084+
'Template for directive \'template\' must have exactly one root element. ' +
2085+
'template.html'
2086+
);
2087+
});
2088+
});
21162089

2117-
// comments are ok
2118-
$templateCache.put('template.html', '<!-- oh hi --><div></div> \n');
2119-
$compile('<p template></p>');
2120-
$rootScope.$apply();
2121-
expect($exceptionHandler.errors).toEqual([]);
2090+
they('should not throw if the root element is accompanied by: $prop',
2091+
{
2092+
'whitespace': ' <div>Hello World!</div> \n',
2093+
'comments': '<!-- oh hi --><div>Hello World!</div> \n',
2094+
'comments + whitespace': ' <!-- oh hi --> <div>Hello World!</div> <!-- oh hi -->\n'
2095+
}, function(directiveTemplate) {
21222096

2123-
// white space around comments is ok
2124-
$templateCache.put('template.html', ' <!-- oh hi --> <div></div> <!-- oh hi -->\n');
2125-
$compile('<p template></p>');
2126-
$rootScope.$apply();
2127-
expect($exceptionHandler.errors).toEqual([]);
2097+
inject(function($compile, $templateCache, $rootScope) {
2098+
$templateCache.put('template.html', directiveTemplate);
2099+
element = $compile('<p template></p>')($rootScope);
2100+
expect(function() {
2101+
$rootScope.$digest();
2102+
}).not.toThrow();
2103+
expect(element.length).toBe(1);
2104+
expect(element.text()).toBe('Hello World!');
2105+
});
21282106
});
21292107
});
21302108

2131-
21322109
it('should resume delayed compilation without duplicates when in a repeater', function() {
21332110
// this is a test for a regression
21342111
// scope creation, isolate watcher setup, controller instantiation, etc should happen
@@ -2317,45 +2294,6 @@ describe('$compile', function() {
23172294
});
23182295
}
23192296

2320-
it('should ignore comment nodes when replacing with a templateUrl', function() {
2321-
module(function() {
2322-
directive('replaceWithComments', valueFn({
2323-
replace: true,
2324-
templateUrl: 'templateWithComments.html'
2325-
}));
2326-
});
2327-
inject(function($compile, $rootScope, $httpBackend) {
2328-
$httpBackend.whenGET('templateWithComments.html').
2329-
respond('<!-- ignored comment --><p>Hello, world!</p><!-- ignored comment-->');
2330-
expect(function() {
2331-
element = $compile('<div><div replace-with-comments></div></div>')($rootScope);
2332-
}).not.toThrow();
2333-
$httpBackend.flush();
2334-
expect(element.find('p').length).toBe(1);
2335-
expect(element.find('p').text()).toBe('Hello, world!');
2336-
});
2337-
});
2338-
2339-
it('should ignore whitespace between comment and root node when replacing with a templateUrl', function() {
2340-
module(function() {
2341-
directive('replaceWithWhitespace', valueFn({
2342-
replace: true,
2343-
templateUrl: 'templateWithWhitespace.html'
2344-
}));
2345-
});
2346-
inject(function($compile, $rootScope, $httpBackend) {
2347-
$httpBackend.whenGET('templateWithWhitespace.html').
2348-
respond('<!-- ignored comment --> <p>Hello, world!</p> <!-- ignored comment-->');
2349-
expect(function() {
2350-
element = $compile('<div><div replace-with-whitespace></div></div>')($rootScope);
2351-
}).not.toThrow();
2352-
$httpBackend.flush();
2353-
expect(element.find('p').length).toBe(1);
2354-
expect(element.find('p').text()).toBe('Hello, world!');
2355-
});
2356-
});
2357-
2358-
23592297
it('should keep prototype properties on sync version of async directive', function() {
23602298
module(function() {
23612299
function DirectiveClass() {

0 commit comments

Comments
 (0)