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

Commit 0ff09f8

Browse files
committed
refactor(compileSpec): make tests consistent
1 parent 76d3daf commit 0ff09f8

File tree

1 file changed

+70
-134
lines changed

1 file changed

+70
-134
lines changed

test/ng/compileSpec.js

+70-134
Original file line numberDiff line numberDiff line change
@@ -1201,41 +1201,50 @@ 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($prop) {
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, $templateCache, $rootScope) {
1225+
templateVar = $prop;
1226+
expect(function() {
1227+
$compile('<p template></p>');
1228+
}).toThrowMinErr('$compile', 'tplrt', 'Template for directive \'template\' must have exactly one root element.');
1229+
});
1230+
});
12341231

1235-
// ws is ok
1236-
expect(function() {
1237-
$compile('<p single-root-with-white-space></p>');
1238-
}).not.toThrow();
1232+
they('should not throw if the root element is accompanied by: $prop',
1233+
{
1234+
'whitespace': ' <div>Hello World!</div> \n',
1235+
'comments': '<!-- oh hi --><div>Hello World!</div> \n',
1236+
'comments + whitespace': ' <!-- oh hi --> <div>Hello World!</div> <!-- oh hi -->\n'
1237+
}, function($prop) {
1238+
1239+
inject(function($compile, $templateCache, $rootScope) {
1240+
templateVar = $prop;
1241+
var element;
1242+
expect(function() {
1243+
element = $compile('<p template></p>')($rootScope);
1244+
}).not.toThrowMinErr('$compile', 'tplrt', 'Template for directive \'template\' must have exactly one root element.');
1245+
expect(element.length).toBe(1);
1246+
expect(element.text()).toBe('Hello World!');
1247+
});
12391248
});
12401249
});
12411250

@@ -1348,38 +1357,6 @@ describe('$compile', function() {
13481357
});
13491358
}
13501359

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-
13831360
it('should keep prototype properties on directive', function() {
13841361
module(function() {
13851362
function DirectiveClass() {
@@ -2075,55 +2052,53 @@ describe('$compile', function() {
20752052
}
20762053
));
20772054

2055+
describe('replace and not exactly one root element', function() {
20782056

2079-
it('should fail if replacing and template doesn\'t have a single root element', function() {
2080-
module(function($exceptionHandlerProvider) {
2081-
$exceptionHandlerProvider.mode('log');
2082-
2057+
beforeEach(module(function() {
20832058
directive('template', function() {
20842059
return {
20852060
replace: true,
20862061
templateUrl: 'template.html'
20872062
};
20882063
});
2089-
});
2090-
2091-
inject(function($compile, $templateCache, $rootScope, $exceptionHandler) {
2092-
// no root element
2093-
$templateCache.put('template.html', 'dada');
2094-
$compile('<p template></p>');
2095-
$rootScope.$digest();
2096-
expect($exceptionHandler.errors.pop().message).
2097-
toMatch(/\[\$compile:tplrt\] Template for directive 'template' must have exactly one root element\. template\.html/);
2064+
}));
20982065

2099-
// multi root
2100-
$templateCache.put('template.html', '<div></div><div></div>');
2101-
$compile('<p template></p>');
2102-
$rootScope.$digest();
2103-
expect($exceptionHandler.errors.pop().message).
2104-
toMatch(/\[\$compile:tplrt\] Template for directive 'template' must have exactly one root element\. template\.html/);
2066+
they('should throw if: $prop',
2067+
{
2068+
'no root element': 'dada',
2069+
'multiple root elements': '<div></div><div></div>'
2070+
}, function($prop) {
21052071

2106-
// ws is ok
2107-
$templateCache.put('template.html', ' <div></div> \n');
2108-
$compile('<p template></p>');
2109-
$rootScope.$apply();
2110-
expect($exceptionHandler.errors).toEqual([]);
2072+
inject(function($compile, $templateCache, $rootScope) {
2073+
// no root element
2074+
$templateCache.put('template.html', $prop);
2075+
$compile('<p template></p>')($rootScope);
2076+
expect(function() {
2077+
$rootScope.$digest();
2078+
}).toThrowMinErr('$compile', 'tplrt', 'Template for directive \'template\' must have exactly one root element. template.html');
2079+
});
2080+
});
21112081

2112-
// comments are ok
2113-
$templateCache.put('template.html', '<!-- oh hi --><div></div> \n');
2114-
$compile('<p template></p>');
2115-
$rootScope.$apply();
2116-
expect($exceptionHandler.errors).toEqual([]);
2082+
they('should not throw if the root element is accompanied by: $prop',
2083+
{
2084+
'whitespace': ' <div>Hello World!</div> \n',
2085+
'comments': '<!-- oh hi --><div>Hello World!</div> \n',
2086+
'comments + whitespace': ' <!-- oh hi --> <div>Hello World!</div> <!-- oh hi -->\n'
2087+
}, function($prop) {
21172088

2118-
// white space around comments is ok
2119-
$templateCache.put('template.html', ' <!-- oh hi --> <div></div> <!-- oh hi -->\n');
2120-
$compile('<p template></p>');
2121-
$rootScope.$apply();
2122-
expect($exceptionHandler.errors).toEqual([]);
2089+
inject(function($compile, $templateCache, $rootScope) {
2090+
// no root element
2091+
$templateCache.put('template.html', $prop);
2092+
element = $compile('<p template></p>')($rootScope);
2093+
expect(function() {
2094+
$rootScope.$digest();
2095+
}).not.toThrowMinErr('$compile', 'tplrt', 'Template for directive \'template\' must have exactly one root element. template.html');
2096+
expect(element.length).toBe(1);
2097+
expect(element.text()).toBe('Hello World!');
2098+
});
21232099
});
21242100
});
21252101

2126-
21272102
it('should resume delayed compilation without duplicates when in a repeater', function() {
21282103
// this is a test for a regression
21292104
// scope creation, isolate watcher setup, controller instantiation, etc should happen
@@ -2312,45 +2287,6 @@ describe('$compile', function() {
23122287
});
23132288
}
23142289

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

0 commit comments

Comments
 (0)