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

Commit 3bc3b40

Browse files
committed
refactor(compileSpec): make tests consistent
PR (#15141)
1 parent d5d9ad1 commit 3bc3b40

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
@@ -1189,41 +1189,52 @@ describe('$compile', function() {
11891189
});
11901190
});
11911191

1192-
it('should fail if replacing and template doesn\'t have a single root element', function() {
1193-
module(function() {
1194-
directive('noRootElem', function() {
1195-
return {
1196-
replace: true,
1197-
template: 'dada'
1198-
};
1199-
});
1200-
directive('multiRootElem', function() {
1201-
return {
1202-
replace: true,
1203-
template: '<div></div><div></div>'
1204-
};
1205-
});
1206-
directive('singleRootWithWhiteSpace', function() {
1192+
describe('replace and not exactly one root element', function() {
1193+
var templateVar;
1194+
1195+
beforeEach(module(function() {
1196+
directive('template', function() {
12071197
return {
12081198
replace: true,
1209-
template: ' <div></div> \n'
1199+
template: function() {
1200+
return templateVar;
1201+
}
12101202
};
12111203
});
1212-
});
1204+
}));
12131205

1214-
inject(function($compile) {
1215-
expect(function() {
1216-
$compile('<p no-root-elem></p>');
1217-
}).toThrowMinErr('$compile', 'tplrt', 'Template for directive \'noRootElem\' must have exactly one root element. ');
1206+
they('should throw if: $prop',
1207+
{
1208+
'no root element': 'dada',
1209+
'multiple root elements': '<div></div><div></div>'
1210+
}, function(directiveTemplate) {
12181211

1219-
expect(function() {
1220-
$compile('<p multi-root-elem></p>');
1221-
}).toThrowMinErr('$compile', 'tplrt', 'Template for directive \'multiRootElem\' must have exactly one root element. ');
1212+
inject(function($compile) {
1213+
templateVar = directiveTemplate;
1214+
expect(function() {
1215+
$compile('<p template></p>');
1216+
}).toThrowMinErr('$compile', 'tplrt',
1217+
'Template for directive \'template\' must have exactly one root element.'
1218+
);
1219+
});
1220+
});
12221221

1223-
// ws is ok
1224-
expect(function() {
1225-
$compile('<p single-root-with-white-space></p>');
1226-
}).not.toThrow();
1222+
they('should not throw if the root element is accompanied by: $prop',
1223+
{
1224+
'whitespace': ' <div>Hello World!</div> \n',
1225+
'comments': '<!-- oh hi --><div>Hello World!</div> \n',
1226+
'comments + whitespace': ' <!-- oh hi --> <div>Hello World!</div> <!-- oh hi -->\n'
1227+
}, function(directiveTemplate) {
1228+
1229+
inject(function($compile, $rootScope) {
1230+
templateVar = directiveTemplate;
1231+
var element;
1232+
expect(function() {
1233+
element = $compile('<p template></p>')($rootScope);
1234+
}).not.toThrow();
1235+
expect(element.length).toBe(1);
1236+
expect(element.text()).toBe('Hello World!');
1237+
});
12271238
});
12281239
});
12291240

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

1339-
it('should ignore comment nodes when replacing with a template', function() {
1340-
module(function() {
1341-
directive('replaceWithComments', valueFn({
1342-
replace: true,
1343-
template: '<!-- ignored comment --><p>Hello, world!</p><!-- ignored comment-->'
1344-
}));
1345-
});
1346-
inject(function($compile, $rootScope) {
1347-
expect(function() {
1348-
element = $compile('<div><div replace-with-comments></div></div>')($rootScope);
1349-
}).not.toThrow();
1350-
expect(element.find('p').length).toBe(1);
1351-
expect(element.find('p').text()).toBe('Hello, world!');
1352-
});
1353-
});
1354-
1355-
it('should ignore whitespace betwee comment and root node when replacing with a template', function() {
1356-
module(function() {
1357-
directive('replaceWithWhitespace', valueFn({
1358-
replace: true,
1359-
template: '<!-- ignored comment --> <p>Hello, world!</p> <!-- ignored comment-->'
1360-
}));
1361-
});
1362-
inject(function($compile, $rootScope) {
1363-
expect(function() {
1364-
element = $compile('<div><div replace-with-whitespace></div></div>')($rootScope);
1365-
}).not.toThrow();
1366-
expect(element.find('p').length).toBe(1);
1367-
expect(element.find('p').text()).toBe('Hello, world!');
1368-
});
1369-
});
1370-
13711350
it('should keep prototype properties on directive', function() {
13721351
module(function() {
13731352
function DirectiveClass() {
@@ -2063,57 +2042,55 @@ describe('$compile', function() {
20632042
}
20642043
));
20652044

2045+
describe('replace and not exactly one root element', function() {
20662046

2067-
it('should fail if replacing and template doesn\'t have a single root element', function() {
2068-
module(function($exceptionHandlerProvider) {
2069-
$exceptionHandlerProvider.mode('log');
2047+
beforeEach(module(function() {
20702048

20712049
directive('template', function() {
20722050
return {
20732051
replace: true,
20742052
templateUrl: 'template.html'
20752053
};
20762054
});
2077-
});
2055+
}));
20782056

2079-
inject(function($compile, $templateCache, $rootScope, $exceptionHandler) {
2080-
// no root element
2081-
$templateCache.put('template.html', 'dada');
2082-
$compile('<p template></p>');
2083-
$rootScope.$digest();
2084-
expect($exceptionHandler.errors.pop()).toEqualMinErr('$compile', 'tplrt',
2085-
'Template for directive \'template\' must have exactly one root element. ' +
2086-
'template.html');
2057+
they('should throw if: $prop',
2058+
{
2059+
'no root element': 'dada',
2060+
'multiple root elements': '<div></div><div></div>'
2061+
}, function(directiveTemplate) {
20872062

2088-
// multi root
2089-
$templateCache.put('template.html', '<div></div><div></div>');
2090-
$compile('<p template></p>');
2091-
$rootScope.$digest();
2092-
expect($exceptionHandler.errors.pop()).toEqualMinErr('$compile', 'tplrt',
2093-
'Template for directive \'template\' must have exactly one root element. ' +
2094-
'template.html');
2063+
inject(function($compile, $templateCache, $rootScope, $exceptionHandler) {
2064+
$templateCache.put('template.html', directiveTemplate);
2065+
$compile('<p template></p>')($rootScope);
2066+
$rootScope.$digest();
20952067

2096-
// ws is ok
2097-
$templateCache.put('template.html', ' <div></div> \n');
2098-
$compile('<p template></p>');
2099-
$rootScope.$apply();
2100-
expect($exceptionHandler.errors).toEqual([]);
2068+
expect($exceptionHandler.errors.pop()).toEqualMinErr('$compile', 'tplrt',
2069+
'Template for directive \'template\' must have exactly one root element. ' +
2070+
'template.html'
2071+
);
2072+
});
2073+
});
21012074

2102-
// comments are ok
2103-
$templateCache.put('template.html', '<!-- oh hi --><div></div> \n');
2104-
$compile('<p template></p>');
2105-
$rootScope.$apply();
2106-
expect($exceptionHandler.errors).toEqual([]);
2075+
they('should not throw if the root element is accompanied by: $prop',
2076+
{
2077+
'whitespace': ' <div>Hello World!</div> \n',
2078+
'comments': '<!-- oh hi --><div>Hello World!</div> \n',
2079+
'comments + whitespace': ' <!-- oh hi --> <div>Hello World!</div> <!-- oh hi -->\n'
2080+
}, function(directiveTemplate) {
21072081

2108-
// white space around comments is ok
2109-
$templateCache.put('template.html', ' <!-- oh hi --> <div></div> <!-- oh hi -->\n');
2110-
$compile('<p template></p>');
2111-
$rootScope.$apply();
2112-
expect($exceptionHandler.errors).toEqual([]);
2082+
inject(function($compile, $templateCache, $rootScope) {
2083+
$templateCache.put('template.html', directiveTemplate);
2084+
element = $compile('<p template></p>')($rootScope);
2085+
expect(function() {
2086+
$rootScope.$digest();
2087+
}).not.toThrow();
2088+
expect(element.length).toBe(1);
2089+
expect(element.text()).toBe('Hello World!');
2090+
});
21132091
});
21142092
});
21152093

2116-
21172094
it('should resume delayed compilation without duplicates when in a repeater', function() {
21182095
// this is a test for a regression
21192096
// scope creation, isolate watcher setup, controller instantiation, etc should happen
@@ -2302,45 +2279,6 @@ describe('$compile', function() {
23022279
});
23032280
}
23042281

2305-
it('should ignore comment nodes when replacing with a templateUrl', function() {
2306-
module(function() {
2307-
directive('replaceWithComments', valueFn({
2308-
replace: true,
2309-
templateUrl: 'templateWithComments.html'
2310-
}));
2311-
});
2312-
inject(function($compile, $rootScope, $httpBackend) {
2313-
$httpBackend.whenGET('templateWithComments.html').
2314-
respond('<!-- ignored comment --><p>Hello, world!</p><!-- ignored comment-->');
2315-
expect(function() {
2316-
element = $compile('<div><div replace-with-comments></div></div>')($rootScope);
2317-
}).not.toThrow();
2318-
$httpBackend.flush();
2319-
expect(element.find('p').length).toBe(1);
2320-
expect(element.find('p').text()).toBe('Hello, world!');
2321-
});
2322-
});
2323-
2324-
it('should ignore whitespace between comment and root node when replacing with a templateUrl', function() {
2325-
module(function() {
2326-
directive('replaceWithWhitespace', valueFn({
2327-
replace: true,
2328-
templateUrl: 'templateWithWhitespace.html'
2329-
}));
2330-
});
2331-
inject(function($compile, $rootScope, $httpBackend) {
2332-
$httpBackend.whenGET('templateWithWhitespace.html').
2333-
respond('<!-- ignored comment --> <p>Hello, world!</p> <!-- ignored comment-->');
2334-
expect(function() {
2335-
element = $compile('<div><div replace-with-whitespace></div></div>')($rootScope);
2336-
}).not.toThrow();
2337-
$httpBackend.flush();
2338-
expect(element.find('p').length).toBe(1);
2339-
expect(element.find('p').text()).toBe('Hello, world!');
2340-
});
2341-
});
2342-
2343-
23442282
it('should keep prototype properties on sync version of async directive', function() {
23452283
module(function() {
23462284
function DirectiveClass() {

0 commit comments

Comments
 (0)