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

Commit df53955

Browse files
test(ngTransclude): check that fallback content is compiled correctly
1 parent 40b657b commit df53955

File tree

1 file changed

+49
-41
lines changed

1 file changed

+49
-41
lines changed

test/ng/compileSpec.js

Lines changed: 49 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7971,16 +7971,14 @@ describe('$compile', function() {
79717971

79727972

79737973
it('should not compile the fallback content if transcluded content is provided', function() {
7974-
var contentsDidLink = false;
7974+
var linkSpy = jasmine.createSpy('postlink');
79757975

79767976
module(function() {
79777977
directive('inner', function() {
79787978
return {
79797979
restrict: 'E',
79807980
template: 'old stuff! ',
7981-
link: function() {
7982-
contentsDidLink = true;
7983-
}
7981+
link: linkSpy
79847982
};
79857983
});
79867984

@@ -7995,21 +7993,19 @@ describe('$compile', function() {
79957993
element = $compile('<div trans>unicorn!</div>')($rootScope);
79967994
$rootScope.$apply();
79977995
expect(sortedHtml(element.html())).toEqual('<div ng-transclude="">unicorn!</div>');
7998-
expect(contentsDidLink).toBe(false);
7996+
expect(linkSpy).not.toHaveBeenCalled();
79997997
});
80007998
});
80017999

80028000
it('should compile and link the fallback content if no transcluded content is provided', function() {
8003-
var contentsDidLink = false;
8001+
var linkSpy = jasmine.createSpy('postlink');
80048002

80058003
module(function() {
80068004
directive('inner', function() {
80078005
return {
80088006
restrict: 'E',
80098007
template: 'old stuff! ',
8010-
link: function() {
8011-
contentsDidLink = true;
8012-
}
8008+
link: linkSpy
80138009
};
80148010
});
80158011

@@ -8024,7 +8020,50 @@ describe('$compile', function() {
80248020
element = $compile('<div trans></div>')($rootScope);
80258021
$rootScope.$apply();
80268022
expect(sortedHtml(element.html())).toEqual('<div ng-transclude=""><inner>old stuff! </inner></div>');
8027-
expect(contentsDidLink).toBe(true);
8023+
expect(linkSpy).toHaveBeenCalled();
8024+
});
8025+
});
8026+
8027+
it('should compile and link the fallback content if an optional transclusion slot is not provided', function() {
8028+
var linkSpy = jasmine.createSpy('postlink');
8029+
8030+
module(function() {
8031+
directive('inner', function() {
8032+
return {
8033+
restrict: 'E',
8034+
template: 'old stuff! ',
8035+
link: linkSpy
8036+
};
8037+
});
8038+
8039+
directive('trans', function() {
8040+
return {
8041+
transclude: { optionalSlot: '?optional'},
8042+
template: '<div ng-transclude="optionalSlot"><inner></inner></div>'
8043+
};
8044+
});
8045+
});
8046+
inject(function(log, $rootScope, $compile) {
8047+
element = $compile('<div trans></div>')($rootScope);
8048+
$rootScope.$apply();
8049+
expect(sortedHtml(element.html())).toEqual('<div ng-transclude="optionalSlot"><inner>old stuff! </inner></div>');
8050+
expect(linkSpy).toHaveBeenCalled();
8051+
});
8052+
});
8053+
8054+
it('should cope if there is neither transcluded content nor fallback content', function() {
8055+
module(function() {
8056+
directive('trans', function() {
8057+
return {
8058+
transclude: true,
8059+
template: '<div ng-transclude></div>'
8060+
};
8061+
});
8062+
});
8063+
inject(function($rootScope, $compile) {
8064+
element = $compile('<div trans></div>')($rootScope);
8065+
$rootScope.$apply();
8066+
expect(sortedHtml(element.html())).toEqual('<div ng-transclude=""></div>');
80288067
});
80298068
});
80308069

@@ -9824,37 +9863,6 @@ describe('$compile', function() {
98249863
expect(element.children().eq(2).text()).toEqual('dorothy');
98259864
});
98269865
});
9827-
9828-
it('should not overwrite the contents of an `ng-transclude` element, if the matching optional slot is not filled', function() {
9829-
module(function() {
9830-
directive('minionComponent', function() {
9831-
return {
9832-
restrict: 'E',
9833-
scope: {},
9834-
transclude: {
9835-
minionSlot: 'minion',
9836-
bossSlot: '?boss'
9837-
},
9838-
template:
9839-
'<div class="boss" ng-transclude="bossSlot">default boss content</div>' +
9840-
'<div class="minion" ng-transclude="minionSlot">default minion content</div>' +
9841-
'<div class="other" ng-transclude>default content</div>'
9842-
};
9843-
});
9844-
});
9845-
inject(function($rootScope, $compile) {
9846-
element = $compile(
9847-
'<minion-component>' +
9848-
'<minion>stuart</minion>' +
9849-
'<span>dorothy</span>' +
9850-
'<minion>kevin</minion>' +
9851-
'</minion-component>')($rootScope);
9852-
$rootScope.$apply();
9853-
expect(element.children().eq(0).text()).toEqual('default boss content');
9854-
expect(element.children().eq(1).text()).toEqual('stuartkevin');
9855-
expect(element.children().eq(2).text()).toEqual('dorothy');
9856-
});
9857-
});
98589866
});
98599867

98609868

0 commit comments

Comments
 (0)