')($rootScope);
expect(element.text()).toEqual('SUCCESS');
expect(log).toEqual('OK');
})
});
it('should allow registration of multiple directives with same name', function() {
module(function() {
directive('div', function(log) {
return {
restrict: 'ECA',
link: log.fn('1')
};
});
directive('div', function(log) {
return {
restrict: 'ECA',
link: log.fn('2')
};
});
});
inject(function($compile, $rootScope, log) {
element = $compile('')($rootScope);
expect(log).toEqual('1; 2');
});
});
});
describe('compile phase', function() {
it('should attach scope to the document node when it is compiled explicitly', inject(function($document){
$compile($document)($rootScope);
expect($document.scope()).toBe($rootScope);
}));
it('should wrap root text nodes in spans', inject(function($compile, $rootScope) {
element = jqLite('
A<a>B</a>C
');
var text = element.contents();
expect(text[0].nodeName).toEqual('#text');
text = $compile(text)($rootScope);
expect(text[0].nodeName).toEqual('SPAN');
expect(element.find('span').text()).toEqual('ABC');
}));
it('should not wrap root whitespace text nodes in spans', function() {
element = jqLite(
'
A
\n '+ // The spaces and newlines here should not get wrapped
'
B
C\t\n '+ // The "C", tabs and spaces here will be wrapped
'
');
$compile(element.contents())($rootScope);
var spans = element.find('span');
expect(spans.length).toEqual(1);
expect(spans.text().indexOf('C')).toEqual(0);
});
it('should not leak memory when there are top level empty text nodes', function() {
var calcCacheSize = function() {
var size = 0;
forEach(jqLite.cache, function(item, key) { size++; });
return size;
};
// We compile the contents of element (i.e. not element itself)
// Then delete these contents and check the cache has been reset to zero
// First with only elements at the top level
element = jqLite('
');
$compile(element.contents())($rootScope);
element.html('');
expect(calcCacheSize()).toEqual(0);
// Next with non-empty text nodes at the top level
// (in this case the compiler will wrap them in a )
element = jqLite('
xxx
');
$compile(element.contents())($rootScope);
element.html('');
expect(calcCacheSize()).toEqual(0);
// Next with comment nodes at the top level
element = jqLite('');
$compile(element.contents())($rootScope);
element.html('');
expect(calcCacheSize()).toEqual(0);
// Finally with empty text nodes at the top level
element = jqLite('
\n
');
$compile(element.contents())($rootScope);
element.html('');
expect(calcCacheSize()).toEqual(0);
});
describe('multiple directives per element', function() {
it('should allow multiple directives per element', inject(function($compile, $rootScope, log){
element = $compile(
'')
($rootScope);
expect(element.text()).toEqual('Hello angular');
expect(log).toEqual('H; M; L');
}));
it('should recurse to children', inject(function($compile, $rootScope){
element = $compile('
')($rootScope);
expect(element.text()).toEqual('0hello2angular4');
}));
it('should allow directives in classes', inject(function($compile, $rootScope, log) {
element = $compile('')($rootScope);
expect(element.html()).toEqual('Hello angular');
expect(log).toEqual('123');
}));
it('should ignore not set CSS classes on SVG elements', inject(function($compile, $rootScope, log) {
if (!window.SVGElement) return;
// According to spec SVG element className property is readonly, but only FF
// implements it this way which causes compile exceptions.
element = $compile('')($rootScope);
$rootScope.$digest();
expect(element.text()).toEqual('1');
}));
it('should allow directives in comments', inject(
function($compile, $rootScope, log) {
element = $compile('
')($rootScope);
expect(element.text()).toEqual('OK');
}
));
it('should prevent further directives from running, but finish current priority level',
inject(function($rootScope, $compile, log) {
// class is processed after attrs, so putting log in class will put it after
// the stop in the current level. This proves that the log runs after stop
element = $compile(
'
',
compile: function(element, attr) {
attr.$set('compiled', 'COMPILED');
expect(element).toBe(attr.$$element);
}
}));
}));
it('should replace element with template', inject(function($compile, $rootScope) {
element = $compile('
ignore
')($rootScope);
expect(element.text()).toEqual('Replace!');
expect(element.find('div').attr('compiled')).toEqual('COMPILED');
}));
it('should append element with template', inject(function($compile, $rootScope) {
element = $compile('
ignore
')($rootScope);
expect(element.text()).toEqual('Append!');
expect(element.find('div').attr('compiled')).toEqual('COMPILED');
}));
it('should compile template when replacing', inject(function($compile, $rootScope, log) {
element = $compile('
ignore
')
($rootScope);
$rootScope.$digest();
expect(element.text()).toEqual('Replace!');
// HIGH goes after MEDIUM since it executes as part of replaced template
expect(log).toEqual('MEDIUM; HIGH; LOG');
}));
it('should compile template when appending', inject(function($compile, $rootScope, log) {
element = $compile('
')
($rootScope);
var div = element.find('div');
expect(div.hasClass('medium-log')).toBe(true);
expect(div.hasClass('log')).toBe(true);
expect(div.css('width')).toBe('10px');
expect(div.css('height')).toBe('20px');
expect(div.attr('replace')).toEqual('');
expect(div.attr('high-log')).toEqual('');
}));
it('should prevent multiple templates per element', inject(function($compile) {
try {
$compile('
')
fail();
} catch(e) {
expect(e.message).toMatch(/Multiple directives .* asking for template/);
}
}));
it('should play nice with repeater when replacing', inject(function($compile, $rootScope) {
element = $compile(
'
' +
'' +
'
')($rootScope);
$rootScope.$digest();
expect(element.text()).toEqual('Replace!Replace!');
}));
it('should play nice with repeater when appending', inject(function($compile, $rootScope) {
element = $compile(
'
' +
'' +
'
')($rootScope);
$rootScope.$digest();
expect(element.text()).toEqual('Append!Append!');
}));
it('should handle interpolated css from replacing directive', inject(
function($compile, $rootScope) {
element = $compile('')($rootScope);
$rootScope.$digest();
expect(element).toHaveClass('class_2');
}));
it('should merge interpolated css class', inject(function($compile, $rootScope) {
element = $compile('')($rootScope);
$rootScope.$apply(function() {
$rootScope.cls = 'two';
});
expect(element).toHaveClass('one');
expect(element).toHaveClass('two'); // interpolated
expect(element).toHaveClass('three');
expect(element).toHaveClass('log'); // merged from replace directive template
}));
it('should merge interpolated css class with ngRepeat',
inject(function($compile, $rootScope) {
element = $compile(
'
');
var e1;
var e2;
e1 = template($rootScope.$new(), noop); // clone
expect(e1.text()).toEqual('');
$httpBackend.flush();
e2 = template($rootScope.$new(), noop); // clone
$rootScope.$digest();
expect(e1.text()).toEqual('Hello Elvis');
expect(e2.text()).toEqual('Hello Elvis');
expect($exceptionHandler.errors.length).toEqual(2);
expect($exceptionHandler.errors[0][0].message).toEqual('cError');
expect($exceptionHandler.errors[1][0].message).toEqual('lError');
dealoc(e1);
dealoc(e2);
});
});
it('should be implicitly terminal and not compile placeholder content in append', inject(
function($compile, $templateCache, $rootScope, log) {
// we can't compile the contents because that would result in a memory leak
$templateCache.put('hello.html', 'Hello!');
element = $compile('
')($rootScope);
expect(log).toEqual('');
}
));
it('should be implicitly terminal and not compile placeholder content in inline', inject(
function($compile, $templateCache, $rootScope, log) {
// we can't compile the contents because that would result in a memory leak
$templateCache.put('hello.html', 'Hello!');
element = $compile('
')($rootScope);
expect(log).toEqual('');
}
));
it('should throw an error and clear element content if the template fails to load', inject(
function($compile, $httpBackend, $rootScope) {
$httpBackend.expect('GET', 'hello.html').respond(404, 'Not Found!');
element = $compile('
content
')($rootScope);
expect(function() {
$httpBackend.flush();
}).toThrow('Failed to load template: hello.html');
expect(sortedHtml(element)).toBe('
');
expect(log).toEqual('iFirst-C');
log('FLUSH');
$httpBackend.flush();
expect(log).toEqual('iFirst-C; FLUSH; iSecond-C; iThird-C; iLast-C');
element = template($rootScope);
$rootScope.$digest();
expect(log).toEqual(
'iFirst-C; FLUSH; iSecond-C; iThird-C; iLast-C; ' +
'iFirst-L; iSecond-L; iThird-L; iLast-L');
var div = element.find('div');
expect(div.attr('i-first')).toEqual('');
expect(div.attr('i-second')).toEqual('');
expect(div.attr('i-third')).toEqual('');
expect(div.attr('i-last')).toEqual('');
expect(div.text()).toEqual('3');
}));
});
it('should allow multiple elements in template', inject(function($compile, $httpBackend) {
$httpBackend.expect('GET', 'hello.html').respond('before mid after');
element = jqLite('');
$compile(element);
$httpBackend.flush();
expect(element.text()).toEqual('before mid after');
}));
it('should work when directive is on the root element', inject(
function($compile, $httpBackend, $rootScope) {
$httpBackend.expect('GET', 'hello.html').
respond('3==');
element = jqLite('{{1+2}}');
$compile(element)($rootScope);
$httpBackend.flush();
expect(element.text()).toEqual('3==3');
}
));
it('should work when directive is a repeater', inject(
function($compile, $httpBackend, $rootScope) {
$httpBackend.expect('GET', 'hello.html').
respond('i=;');
element = jqLite('
{{i}}
');
$compile(element)($rootScope);
$httpBackend.flush();
expect(element.text()).toEqual('i=1;i=2;');
}
));
it("should fail if replacing and template doesn't have a single root element", function() {
module(function($exceptionHandlerProvider) {
$exceptionHandlerProvider.mode('log');
directive('template', function() {
return {
replace: true,
templateUrl: 'template.html'
}
});
});
inject(function($compile, $templateCache, $rootScope, $exceptionHandler) {
// no root element
$templateCache.put('template.html', 'dada');
$compile('');
$rootScope.$digest();
expect($exceptionHandler.errors.pop().message).
toBe('Template must have exactly one root element. was: dada');
// multi root
$templateCache.put('template.html', '');
$compile('');
$rootScope.$digest();
expect($exceptionHandler.errors.pop().message).
toBe('Template must have exactly one root element. was: ');
// ws is ok
$templateCache.put('template.html', ' \n');
$compile('');
$rootScope.$apply();
expect($exceptionHandler.errors).toEqual([]);
});
});
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
// only once even if we are dealing with delayed compilation of a node due to templateUrl
// and the template node is in a repeater
var controllerSpy = jasmine.createSpy('controller');
module(function($compileProvider) {
$compileProvider.directive('delayed', valueFn({
controller: controllerSpy,
templateUrl: 'delayed.html',
scope: {
title: '@'
}
}));
});
inject(function($templateCache, $compile, $rootScope) {
$rootScope.coolTitle = 'boom!';
$templateCache.put('delayed.html', '
'
)($rootScope);
expect(log).toEqual('LOG; log-003-002; 003; LOG; log-002-001; 002; LOG; log-004-001; 004');
})
);
it('should allow more one new scope directives per element, but directives should share' +
'the scope', inject(
function($rootScope, $compile, log) {
element = $compile('')($rootScope);
expect(log).toEqual('002; 002');
})
);
it('should not allow more then one isolate scope creation per element', inject(
function($rootScope, $compile) {
expect(function(){
$compile('');
}).toThrow('Multiple directives [iscopeA, scopeB] asking for isolated scope on: ' +
'
');
})
);
it('should not allow more then one isolate scope creation per element', inject(
function($rootScope, $compile) {
expect(function(){
$compile('');
}).toThrow('Multiple directives [iscopeA, iscopeB] asking for isolated scope on: ' +
'
');
})
);
it('should create new scope even at the root of the template', inject(
function($rootScope, $compile, log) {
element = $compile('')($rootScope);
expect(log).toEqual('002');
})
);
it('should create isolate scope even at the root of the template', inject(
function($rootScope, $compile, log) {
element = $compile('')($rootScope);
expect(log).toEqual('002');
})
);
});
});
});
describe('interpolation', function() {
var observeSpy, directiveAttrs;
beforeEach(module(function() {
directive('observer', function() {
return function(scope, elm, attr) {
directiveAttrs = attr;
observeSpy = jasmine.createSpy('$observe attr');
expect(attr.$observe('someAttr', observeSpy)).toBe(observeSpy);
};
});
}));
it('should compile and link both attribute and text bindings', inject(
function($rootScope, $compile) {
$rootScope.name = 'angular';
element = $compile('
text: {{name}}
')($rootScope);
$rootScope.$digest();
expect(element.text()).toEqual('text: angular');
expect(element.attr('name')).toEqual('attr: angular');
}));
it('should decorate the binding with ng-binding and interpolation function', inject(
function($compile, $rootScope) {
element = $compile('
');
var linkingFn = $compile(element);
// Now prune the branches with no directives
element.find('span').remove();
expect(element.find('span').length).toBe(0);
// and we should still be able to compile without errors
linkingFn($rootScope);
}));
it('should compile from top to bottom but link from bottom up', inject(
function($compile, $rootScope, log) {
element = $compile('')($rootScope);
expect(log).toEqual('tA; tB; tC; preA; preB; preC; postC; postA; postB');
}
));
it('should support link function on directive object', function() {
module(function() {
directive('abc', valueFn({
link: function(scope, element, attrs) {
element.text(attrs.abc);
}
}));
});
inject(function($compile, $rootScope) {
element = $compile('
FAIL
')($rootScope);
expect(element.text()).toEqual('WORKS');
});
});
it('should support $observe inside link function on directive object', function() {
module(function() {
directive('testLink', valueFn({
templateUrl: 'test-link.html',
link: function(scope, element, attrs) {
attrs.$observe( 'testLink', function ( val ) {
scope.testAttr = val;
});
}
}));
});
inject(function($compile, $rootScope, $templateCache) {
$templateCache.put('test-link.html', '{{testAttr}}' );
element = $compile('')($rootScope);
$rootScope.$apply();
expect(element.text()).toBe('3');
});
});
});
describe('attrs', function() {
it('should allow setting of attributes', function() {
module(function() {
directive({
setter: valueFn(function(scope, element, attr) {
attr.$set('name', 'abc');
attr.$set('disabled', true);
expect(attr.name).toBe('abc');
expect(attr.disabled).toBe(true);
})
});
});
inject(function($rootScope, $compile) {
element = $compile('')($rootScope);
expect(element.attr('name')).toEqual('abc');
expect(element.attr('disabled')).toEqual('disabled');
});
});
it('should read boolean attributes as boolean only on control elements', function() {
var value;
module(function() {
directive({
input: valueFn({
restrict: 'ECA',
link:function(scope, element, attr) {
value = attr.required;
}
})
});
});
inject(function($rootScope, $compile) {
element = $compile('')($rootScope);
expect(value).toEqual(true);
});
});
it('should read boolean attributes as text on non-controll elements', function() {
var value;
module(function() {
directive({
div: valueFn({
restrict: 'ECA',
link:function(scope, element, attr) {
value = attr.required;
}
})
});
});
inject(function($rootScope, $compile) {
element = $compile('')($rootScope);
expect(value).toEqual('some text');
});
});
it('should allow setting of attributes', function() {
module(function() {
directive({
setter: valueFn(function(scope, element, attr) {
attr.$set('name', 'abc');
attr.$set('disabled', true);
expect(attr.name).toBe('abc');
expect(attr.disabled).toBe(true);
})
});
});
inject(function($rootScope, $compile) {
element = $compile('')($rootScope);
expect(element.attr('name')).toEqual('abc');
expect(element.attr('disabled')).toEqual('disabled');
});
});
it('should create new instance of attr for each template stamping', function() {
module(function($provide) {
var state = { first: [], second: [] };
$provide.value('state', state);
directive({
first: valueFn({
priority: 1,
compile: function(templateElement, templateAttr) {
return function(scope, element, attr) {
state.first.push({
template: {element: templateElement, attr:templateAttr},
link: {element: element, attr: attr}
});
}
}
}),
second: valueFn({
priority: 2,
compile: function(templateElement, templateAttr) {
return function(scope, element, attr) {
state.second.push({
template: {element: templateElement, attr:templateAttr},
link: {element: element, attr: attr}
});
}
}
})
});
});
inject(function($rootScope, $compile, state) {
var template = $compile('
');
dealoc(template($rootScope.$new(), noop));
dealoc(template($rootScope.$new(), noop));
// instance between directives should be shared
expect(state.first[0].template.element).toBe(state.second[0].template.element);
expect(state.first[0].template.attr).toBe(state.second[0].template.attr);
// the template and the link can not be the same instance
expect(state.first[0].template.element).not.toBe(state.first[0].link.element);
expect(state.first[0].template.attr).not.toBe(state.first[0].link.attr);
// each new template needs to be new instance
expect(state.first[0].link.element).not.toBe(state.first[1].link.element);
expect(state.first[0].link.attr).not.toBe(state.first[1].link.attr);
expect(state.second[0].link.element).not.toBe(state.second[1].link.element);
expect(state.second[0].link.attr).not.toBe(state.second[1].link.attr);
});
});
it('should properly $observe inside ng-repeat', function() {
var spies = [];
module(function() {
directive('observer', function() {
return function(scope, elm, attr) {
spies.push(jasmine.createSpy('observer ' + spies.length));
attr.$observe('some', spies[spies.length - 1]);
};
});
});
inject(function($compile, $rootScope) {
element = $compile('
');
expect(componentScope.attr).toEqual('some text');
expect(componentScope.attrAlias).toEqual('some text');
expect(componentScope.attrAlias).toEqual(componentScope.attr);
}));
it('should set up the interpolation before it reaches the link function', inject(function() {
$rootScope.name = 'misko';
compile('