2011-07-17 08:05:43 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
2011-10-07 18:27:49 +00:00
|
|
|
describe('Binder', function() {
|
|
|
|
|
beforeEach(function() {
|
2011-01-06 22:34:21 +00:00
|
|
|
this.compileToHtml = function (content) {
|
2011-10-17 23:56:56 +00:00
|
|
|
var html;
|
2011-10-26 05:21:21 +00:00
|
|
|
inject(function($rootScope, $compile){
|
2011-10-17 23:56:56 +00:00
|
|
|
content = jqLite(content);
|
2011-10-26 05:21:21 +00:00
|
|
|
$compile(content)($rootScope);
|
2011-10-17 23:56:56 +00:00
|
|
|
html = sortedHtml(content);
|
|
|
|
|
}).call(this);
|
|
|
|
|
return html;
|
2011-01-06 22:34:21 +00:00
|
|
|
};
|
|
|
|
|
});
|
2011-01-19 23:42:11 +00:00
|
|
|
|
2011-10-07 18:27:49 +00:00
|
|
|
afterEach(function() {
|
2011-01-06 22:34:21 +00:00
|
|
|
if (this.element && this.element.dealoc) {
|
|
|
|
|
this.element.dealoc();
|
|
|
|
|
}
|
|
|
|
|
});
|
2011-01-19 23:42:11 +00:00
|
|
|
|
2011-10-26 05:21:21 +00:00
|
|
|
it('BindUpdate', inject(function($rootScope, $compile) {
|
|
|
|
|
$compile('<div ng:init="a=123"/>')($rootScope);
|
2011-10-17 23:56:56 +00:00
|
|
|
$rootScope.$digest();
|
|
|
|
|
assertEquals(123, $rootScope.a);
|
|
|
|
|
}));
|
2011-01-19 23:42:11 +00:00
|
|
|
|
2011-10-26 05:21:21 +00:00
|
|
|
it('ExecuteInitialization', inject(function($rootScope, $compile) {
|
|
|
|
|
$compile('<div ng:init="a=123">')($rootScope);
|
2011-10-17 23:56:56 +00:00
|
|
|
assertEquals($rootScope.a, 123);
|
|
|
|
|
}));
|
2011-01-19 23:42:11 +00:00
|
|
|
|
2011-10-26 05:21:21 +00:00
|
|
|
it('ExecuteInitializationStatements', inject(function($rootScope, $compile) {
|
|
|
|
|
$compile('<div ng:init="a=123;b=345">')($rootScope);
|
2011-10-17 23:56:56 +00:00
|
|
|
assertEquals($rootScope.a, 123);
|
|
|
|
|
assertEquals($rootScope.b, 345);
|
|
|
|
|
}));
|
2011-01-19 23:42:11 +00:00
|
|
|
|
2011-10-26 05:21:21 +00:00
|
|
|
it('ApplyTextBindings', inject(function($rootScope, $compile) {
|
|
|
|
|
var element = $compile('<div ng:bind="model.a">x</div>')($rootScope);
|
2011-10-17 23:56:56 +00:00
|
|
|
$rootScope.model = {a:123};
|
|
|
|
|
$rootScope.$apply();
|
|
|
|
|
assertEquals('123', element.text());
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
it('ReplaceBindingInTextWithSpan preserve surounding text', function() {
|
|
|
|
|
assertEquals(this.compileToHtml("<b>a{{b}}c</b>"), '<b>a<span ng:bind="b"></span>c</b>');
|
2011-01-06 22:34:21 +00:00
|
|
|
});
|
2011-01-19 23:42:11 +00:00
|
|
|
|
2011-10-07 18:27:49 +00:00
|
|
|
it('ReplaceBindingInTextWithSpan', function() {
|
2011-01-06 22:34:21 +00:00
|
|
|
assertEquals(this.compileToHtml("<b>{{b}}</b>"), '<b><span ng:bind="b"></span></b>');
|
|
|
|
|
});
|
2011-01-19 23:42:11 +00:00
|
|
|
|
2011-10-26 05:21:21 +00:00
|
|
|
it('BindingSpaceConfusesIE', inject(function($rootScope, $compile) {
|
2011-01-06 22:34:21 +00:00
|
|
|
if (!msie) return;
|
|
|
|
|
var span = document.createElement("span");
|
|
|
|
|
span.innerHTML = ' ';
|
|
|
|
|
var nbsp = span.firstChild.nodeValue;
|
|
|
|
|
assertEquals(
|
|
|
|
|
'<b><span ng:bind="a"></span><span>'+nbsp+'</span><span ng:bind="b"></span></b>',
|
|
|
|
|
this.compileToHtml("<b>{{a}} {{b}}</b>"));
|
2011-10-17 23:56:56 +00:00
|
|
|
dealoc(($rootScope));
|
2011-01-06 22:34:21 +00:00
|
|
|
assertEquals(
|
|
|
|
|
'<b><span ng:bind="A"></span><span>'+nbsp+'x </span><span ng:bind="B"></span><span>'+nbsp+'(</span><span ng:bind="C"></span>)</b>',
|
|
|
|
|
this.compileToHtml("<b>{{A}} x {{B}} ({{C}})</b>"));
|
2011-10-17 23:56:56 +00:00
|
|
|
}));
|
2011-01-19 23:42:11 +00:00
|
|
|
|
2011-10-26 05:21:21 +00:00
|
|
|
it('BindingOfAttributes', inject(function($rootScope, $compile) {
|
|
|
|
|
var element = $compile("<a href='http://s/a{{b}}c' foo='x'></a>")($rootScope);
|
2011-10-17 23:56:56 +00:00
|
|
|
var attrbinding = element.attr("ng:bind-attr");
|
2011-01-06 22:34:21 +00:00
|
|
|
var bindings = fromJson(attrbinding);
|
|
|
|
|
assertEquals("http://s/a{{b}}c", decodeURI(bindings.href));
|
|
|
|
|
assertTrue(!bindings.foo);
|
2011-10-17 23:56:56 +00:00
|
|
|
}));
|
2011-01-19 23:42:11 +00:00
|
|
|
|
2011-10-26 05:21:21 +00:00
|
|
|
it('MarkMultipleAttributes', inject(function($rootScope, $compile) {
|
|
|
|
|
var element = $compile('<a href="http://s/a{{b}}c" foo="{{d}}"></a>')($rootScope);
|
2011-10-17 23:56:56 +00:00
|
|
|
var attrbinding = element.attr("ng:bind-attr");
|
2011-01-06 22:34:21 +00:00
|
|
|
var bindings = fromJson(attrbinding);
|
|
|
|
|
assertEquals(bindings.foo, "{{d}}");
|
|
|
|
|
assertEquals(decodeURI(bindings.href), "http://s/a{{b}}c");
|
2011-10-17 23:56:56 +00:00
|
|
|
}));
|
2011-01-19 23:42:11 +00:00
|
|
|
|
2011-10-26 05:21:21 +00:00
|
|
|
it('AttributesNoneBound', inject(function($rootScope, $compile) {
|
|
|
|
|
var a = $compile("<a href='abc' foo='def'></a>")($rootScope);
|
2011-01-06 22:34:21 +00:00
|
|
|
assertEquals(a[0].nodeName, "A");
|
|
|
|
|
assertTrue(!a.attr("ng:bind-attr"));
|
2011-10-17 23:56:56 +00:00
|
|
|
}));
|
2011-01-19 23:42:11 +00:00
|
|
|
|
2011-10-26 05:21:21 +00:00
|
|
|
it('ExistingAttrbindingIsAppended', inject(function($rootScope, $compile) {
|
|
|
|
|
var a = $compile("<a href='http://s/{{abc}}' ng:bind-attr='{\"b\":\"{{def}}\"}'></a>")($rootScope);
|
2011-01-06 22:34:21 +00:00
|
|
|
assertEquals('{"b":"{{def}}","href":"http://s/{{abc}}"}', a.attr('ng:bind-attr'));
|
2011-10-17 23:56:56 +00:00
|
|
|
}));
|
2011-01-19 23:42:11 +00:00
|
|
|
|
2011-10-26 05:21:21 +00:00
|
|
|
it('AttributesAreEvaluated', inject(function($rootScope, $compile) {
|
|
|
|
|
var a = $compile('<a ng:bind-attr=\'{"a":"a", "b":"a+b={{a+b}}"}\'></a>')($rootScope);
|
2011-10-17 23:56:56 +00:00
|
|
|
$rootScope.$eval('a=1;b=2');
|
|
|
|
|
$rootScope.$apply();
|
2011-01-06 22:34:21 +00:00
|
|
|
assertEquals(a.attr('a'), 'a');
|
|
|
|
|
assertEquals(a.attr('b'), 'a+b=3');
|
2011-10-17 23:56:56 +00:00
|
|
|
}));
|
2011-01-19 23:42:11 +00:00
|
|
|
|
2011-10-26 05:21:21 +00:00
|
|
|
it('InputTypeButtonActionExecutesInScope', inject(function($rootScope, $compile) {
|
2011-01-06 22:34:21 +00:00
|
|
|
var savedCalled = false;
|
2011-10-26 05:21:21 +00:00
|
|
|
var element = $compile(
|
2011-10-17 23:56:56 +00:00
|
|
|
'<input type="button" ng:click="person.save()" value="Apply">')($rootScope);
|
|
|
|
|
$rootScope.person = {};
|
|
|
|
|
$rootScope.person.save = function() {
|
2011-01-06 22:34:21 +00:00
|
|
|
savedCalled = true;
|
2011-03-23 16:33:29 +00:00
|
|
|
};
|
2011-10-17 23:56:56 +00:00
|
|
|
browserTrigger(element, 'click');
|
2011-01-06 22:34:21 +00:00
|
|
|
assertTrue(savedCalled);
|
2011-10-17 23:56:56 +00:00
|
|
|
}));
|
2011-01-19 23:42:11 +00:00
|
|
|
|
2011-10-26 05:21:21 +00:00
|
|
|
it('InputTypeButtonActionExecutesInScope2', inject(function($rootScope, $compile) {
|
2011-01-06 22:34:21 +00:00
|
|
|
var log = "";
|
2011-10-26 05:21:21 +00:00
|
|
|
var element = $compile('<input type="image" ng:click="action()">')($rootScope);
|
2011-10-17 23:56:56 +00:00
|
|
|
$rootScope.action = function() {
|
2011-01-06 22:34:21 +00:00
|
|
|
log += 'click;';
|
2011-03-23 16:33:29 +00:00
|
|
|
};
|
2011-01-06 22:34:21 +00:00
|
|
|
expect(log).toEqual('');
|
2011-10-17 23:56:56 +00:00
|
|
|
browserTrigger(element, 'click');
|
2011-01-06 22:34:21 +00:00
|
|
|
expect(log).toEqual('click;');
|
2011-10-17 23:56:56 +00:00
|
|
|
}));
|
2011-01-19 23:42:11 +00:00
|
|
|
|
2011-10-26 05:21:21 +00:00
|
|
|
it('ButtonElementActionExecutesInScope', inject(function($rootScope, $compile) {
|
2011-01-06 22:34:21 +00:00
|
|
|
var savedCalled = false;
|
2011-10-26 05:21:21 +00:00
|
|
|
var element = $compile('<button ng:click="person.save()">Apply</button>')($rootScope);
|
2011-10-17 23:56:56 +00:00
|
|
|
$rootScope.person = {};
|
|
|
|
|
$rootScope.person.save = function() {
|
2011-01-06 22:34:21 +00:00
|
|
|
savedCalled = true;
|
2011-03-23 16:33:29 +00:00
|
|
|
};
|
2011-10-17 23:56:56 +00:00
|
|
|
browserTrigger(element, 'click');
|
2011-01-06 22:34:21 +00:00
|
|
|
assertTrue(savedCalled);
|
2011-10-17 23:56:56 +00:00
|
|
|
}));
|
2011-01-19 23:42:11 +00:00
|
|
|
|
2011-10-26 05:21:21 +00:00
|
|
|
it('RepeaterUpdateBindings', inject(function($rootScope, $compile) {
|
|
|
|
|
var form = $compile(
|
2011-10-17 23:56:56 +00:00
|
|
|
'<ul>' +
|
|
|
|
|
'<LI ng:repeat="item in model.items" ng:bind="item.a"></LI>' +
|
|
|
|
|
'</ul>')($rootScope);
|
2011-01-06 22:34:21 +00:00
|
|
|
var items = [{a:"A"}, {a:"B"}];
|
2011-10-17 23:56:56 +00:00
|
|
|
$rootScope.model = {items:items};
|
2011-01-19 23:42:11 +00:00
|
|
|
|
2011-10-17 23:56:56 +00:00
|
|
|
$rootScope.$apply();
|
2011-01-06 22:34:21 +00:00
|
|
|
assertEquals('<ul>' +
|
|
|
|
|
'<#comment></#comment>' +
|
2011-08-17 06:08:13 +00:00
|
|
|
'<li ng:bind="item.a">A</li>' +
|
|
|
|
|
'<li ng:bind="item.a">B</li>' +
|
2011-01-06 22:34:21 +00:00
|
|
|
'</ul>', sortedHtml(form));
|
2011-01-19 23:42:11 +00:00
|
|
|
|
2011-01-06 22:34:21 +00:00
|
|
|
items.unshift({a:'C'});
|
2011-10-17 23:56:56 +00:00
|
|
|
$rootScope.$apply();
|
2011-01-06 22:34:21 +00:00
|
|
|
assertEquals('<ul>' +
|
|
|
|
|
'<#comment></#comment>' +
|
2011-08-17 06:08:13 +00:00
|
|
|
'<li ng:bind="item.a">C</li>' +
|
|
|
|
|
'<li ng:bind="item.a">A</li>' +
|
|
|
|
|
'<li ng:bind="item.a">B</li>' +
|
2011-01-06 22:34:21 +00:00
|
|
|
'</ul>', sortedHtml(form));
|
2011-01-19 23:42:11 +00:00
|
|
|
|
2011-01-06 22:34:21 +00:00
|
|
|
items.shift();
|
2011-10-17 23:56:56 +00:00
|
|
|
$rootScope.$apply();
|
2011-01-06 22:34:21 +00:00
|
|
|
assertEquals('<ul>' +
|
|
|
|
|
'<#comment></#comment>' +
|
2011-08-17 06:08:13 +00:00
|
|
|
'<li ng:bind="item.a">A</li>' +
|
|
|
|
|
'<li ng:bind="item.a">B</li>' +
|
2011-01-06 22:34:21 +00:00
|
|
|
'</ul>', sortedHtml(form));
|
2011-01-19 23:42:11 +00:00
|
|
|
|
2011-01-06 22:34:21 +00:00
|
|
|
items.shift();
|
|
|
|
|
items.shift();
|
2011-10-17 23:56:56 +00:00
|
|
|
$rootScope.$apply();
|
|
|
|
|
}));
|
|
|
|
|
|
2011-10-26 05:21:21 +00:00
|
|
|
it('RepeaterContentDoesNotBind', inject(function($rootScope, $compile) {
|
|
|
|
|
var element = $compile(
|
2011-10-17 23:56:56 +00:00
|
|
|
'<ul>' +
|
|
|
|
|
'<LI ng:repeat="item in model.items"><span ng:bind="item.a"></span></li>' +
|
|
|
|
|
'</ul>')($rootScope);
|
|
|
|
|
$rootScope.model = {items:[{a:"A"}]};
|
|
|
|
|
$rootScope.$apply();
|
2011-01-06 22:34:21 +00:00
|
|
|
assertEquals('<ul>' +
|
|
|
|
|
'<#comment></#comment>' +
|
2011-08-17 06:08:13 +00:00
|
|
|
'<li><span ng:bind="item.a">A</span></li>' +
|
2011-10-17 23:56:56 +00:00
|
|
|
'</ul>', sortedHtml(element));
|
|
|
|
|
}));
|
2011-01-19 23:42:11 +00:00
|
|
|
|
2011-10-07 18:27:49 +00:00
|
|
|
it('DoNotOverwriteCustomAction', function() {
|
2011-01-06 22:34:21 +00:00
|
|
|
var html = this.compileToHtml('<input type="submit" value="Save" action="foo();">');
|
|
|
|
|
assertTrue(html.indexOf('action="foo();"') > 0 );
|
|
|
|
|
});
|
2011-01-19 23:42:11 +00:00
|
|
|
|
2011-10-26 05:21:21 +00:00
|
|
|
it('RepeaterAdd', inject(function($rootScope, $compile) {
|
|
|
|
|
var element = $compile('<div><input type="text" ng:model="item.x" ng:repeat="item in items"></div>')($rootScope);
|
2011-10-17 23:56:56 +00:00
|
|
|
$rootScope.items = [{x:'a'}, {x:'b'}];
|
|
|
|
|
$rootScope.$apply();
|
|
|
|
|
var first = childNode(element, 1);
|
|
|
|
|
var second = childNode(element, 2);
|
2011-03-23 16:33:29 +00:00
|
|
|
expect(first.val()).toEqual('a');
|
|
|
|
|
expect(second.val()).toEqual('b');
|
2011-01-19 23:42:11 +00:00
|
|
|
|
2011-01-06 22:34:21 +00:00
|
|
|
first.val('ABC');
|
2010-12-10 21:55:18 +00:00
|
|
|
browserTrigger(first, 'keydown');
|
2011-10-17 23:56:56 +00:00
|
|
|
$rootScope.$service('$browser').defer.flush();
|
|
|
|
|
expect($rootScope.items[0].x).toEqual('ABC');
|
|
|
|
|
}));
|
2011-01-19 23:42:11 +00:00
|
|
|
|
2011-10-26 05:21:21 +00:00
|
|
|
it('ItShouldRemoveExtraChildrenWhenIteratingOverHash', inject(function($rootScope, $compile) {
|
|
|
|
|
var element = $compile('<div><div ng:repeat="i in items">{{i}}</div></div>')($rootScope);
|
2011-01-06 22:34:21 +00:00
|
|
|
var items = {};
|
2011-10-17 23:56:56 +00:00
|
|
|
$rootScope.items = items;
|
2011-01-19 23:42:11 +00:00
|
|
|
|
2011-10-17 23:56:56 +00:00
|
|
|
$rootScope.$apply();
|
|
|
|
|
expect(element[0].childNodes.length - 1).toEqual(0);
|
2011-01-19 23:42:11 +00:00
|
|
|
|
2011-01-06 22:34:21 +00:00
|
|
|
items.name = "misko";
|
2011-10-17 23:56:56 +00:00
|
|
|
$rootScope.$apply();
|
|
|
|
|
expect(element[0].childNodes.length - 1).toEqual(1);
|
2011-01-19 23:42:11 +00:00
|
|
|
|
2011-01-06 22:34:21 +00:00
|
|
|
delete items.name;
|
2011-10-17 23:56:56 +00:00
|
|
|
$rootScope.$apply();
|
|
|
|
|
expect(element[0].childNodes.length - 1).toEqual(0);
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
it('IfTextBindingThrowsErrorDecorateTheSpan', inject(
|
2011-11-02 04:09:54 +00:00
|
|
|
function($provide){
|
|
|
|
|
$provide.factory('$exceptionHandler', $exceptionHandlerMockFactory);
|
2011-10-17 23:56:56 +00:00
|
|
|
},
|
2011-10-26 05:21:21 +00:00
|
|
|
function($rootScope, $exceptionHandler, $compile) {
|
|
|
|
|
$compile('<div>{{error.throw()}}</div>', null, true)($rootScope);
|
2011-10-17 23:56:56 +00:00
|
|
|
var errorLogs = $exceptionHandler.errors;
|
|
|
|
|
|
|
|
|
|
$rootScope.error = {
|
|
|
|
|
'throw': function() {throw "ErrorMsg1";}
|
|
|
|
|
};
|
|
|
|
|
$rootScope.$apply();
|
|
|
|
|
|
|
|
|
|
$rootScope.error['throw'] = function() {throw "MyError";};
|
|
|
|
|
errorLogs.length = 0;
|
|
|
|
|
$rootScope.$apply();
|
|
|
|
|
assertEquals(['MyError'], errorLogs.shift());
|
|
|
|
|
|
|
|
|
|
$rootScope.error['throw'] = function() {return "ok";};
|
|
|
|
|
$rootScope.$apply();
|
|
|
|
|
assertEquals(0, errorLogs.length);
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
|
2011-11-02 04:09:54 +00:00
|
|
|
it('IfAttrBindingThrowsErrorDecorateTheAttribute', inject(function($provide){
|
|
|
|
|
$provide.factory('$exceptionHandler', $exceptionHandlerMockFactory);
|
2011-10-26 05:21:21 +00:00
|
|
|
}, function($rootScope, $exceptionHandler, $compile) {
|
|
|
|
|
$compile('<div attr="before {{error.throw()}} after"></div>', null, true)($rootScope);
|
2011-10-17 23:56:56 +00:00
|
|
|
var errorLogs = $exceptionHandler.errors;
|
2011-03-23 16:33:29 +00:00
|
|
|
var count = 0;
|
2011-01-19 23:42:11 +00:00
|
|
|
|
2011-10-17 23:56:56 +00:00
|
|
|
$rootScope.error = {
|
2011-10-07 18:27:49 +00:00
|
|
|
'throw': function() {throw new Error("ErrorMsg" + (++count));}
|
2011-03-23 16:33:29 +00:00
|
|
|
};
|
2011-10-17 23:56:56 +00:00
|
|
|
$rootScope.$apply();
|
2011-08-10 20:15:43 +00:00
|
|
|
expect(errorLogs.length).not.toEqual(0);
|
2011-03-23 16:33:29 +00:00
|
|
|
expect(errorLogs.shift()).toMatch(/ErrorMsg1/);
|
2011-08-10 20:15:43 +00:00
|
|
|
errorLogs.length = 0;
|
2011-03-23 16:33:29 +00:00
|
|
|
|
2011-10-17 23:56:56 +00:00
|
|
|
$rootScope.error['throw'] = function() { return 'X';};
|
|
|
|
|
$rootScope.$apply();
|
2011-03-23 16:33:29 +00:00
|
|
|
expect(errorLogs.length).toMatch(0);
|
2011-10-17 23:56:56 +00:00
|
|
|
}));
|
2011-01-19 23:42:11 +00:00
|
|
|
|
2011-10-26 05:21:21 +00:00
|
|
|
it('NestedRepeater', inject(function($rootScope, $compile) {
|
|
|
|
|
var element = $compile(
|
2011-10-17 23:56:56 +00:00
|
|
|
'<div>' +
|
|
|
|
|
'<div ng:repeat="m in model" name="{{m.name}}">' +
|
|
|
|
|
'<ul name="{{i}}" ng:repeat="i in m.item"></ul>' +
|
|
|
|
|
'</div>' +
|
|
|
|
|
'</div>')($rootScope);
|
2011-01-19 23:42:11 +00:00
|
|
|
|
2011-10-17 23:56:56 +00:00
|
|
|
$rootScope.model = [{name:'a', item:['a1', 'a2']}, {name:'b', item:['b1', 'b2']}];
|
|
|
|
|
$rootScope.$apply();
|
2011-01-19 23:42:11 +00:00
|
|
|
|
2011-01-06 22:34:21 +00:00
|
|
|
assertEquals('<div>'+
|
|
|
|
|
'<#comment></#comment>'+
|
2011-08-17 06:08:13 +00:00
|
|
|
'<div name="a" ng:bind-attr="{"name":"{{m.name}}"}">'+
|
2011-01-06 22:34:21 +00:00
|
|
|
'<#comment></#comment>'+
|
2011-08-17 06:08:13 +00:00
|
|
|
'<ul name="a1" ng:bind-attr="{"name":"{{i}}"}"></ul>'+
|
|
|
|
|
'<ul name="a2" ng:bind-attr="{"name":"{{i}}"}"></ul>'+
|
2011-01-06 22:34:21 +00:00
|
|
|
'</div>'+
|
2011-08-17 06:08:13 +00:00
|
|
|
'<div name="b" ng:bind-attr="{"name":"{{m.name}}"}">'+
|
2011-01-06 22:34:21 +00:00
|
|
|
'<#comment></#comment>'+
|
2011-08-17 06:08:13 +00:00
|
|
|
'<ul name="b1" ng:bind-attr="{"name":"{{i}}"}"></ul>'+
|
|
|
|
|
'<ul name="b2" ng:bind-attr="{"name":"{{i}}"}"></ul>'+
|
2011-10-17 23:56:56 +00:00
|
|
|
'</div></div>', sortedHtml(element));
|
|
|
|
|
}));
|
2011-01-19 23:42:11 +00:00
|
|
|
|
2011-10-26 05:21:21 +00:00
|
|
|
it('HideBindingExpression', inject(function($rootScope, $compile) {
|
|
|
|
|
var element = $compile('<div ng:hide="hidden == 3"/>')($rootScope);
|
2011-01-19 23:42:11 +00:00
|
|
|
|
2011-10-17 23:56:56 +00:00
|
|
|
$rootScope.hidden = 3;
|
|
|
|
|
$rootScope.$apply();
|
2011-01-19 23:42:11 +00:00
|
|
|
|
2011-10-17 23:56:56 +00:00
|
|
|
assertHidden(element);
|
2011-01-19 23:42:11 +00:00
|
|
|
|
2011-10-17 23:56:56 +00:00
|
|
|
$rootScope.hidden = 2;
|
|
|
|
|
$rootScope.$apply();
|
2011-01-19 23:42:11 +00:00
|
|
|
|
2011-10-17 23:56:56 +00:00
|
|
|
assertVisible(element);
|
|
|
|
|
}));
|
2011-01-19 23:42:11 +00:00
|
|
|
|
2011-10-26 05:21:21 +00:00
|
|
|
it('HideBinding', inject(function($rootScope, $compile) {
|
|
|
|
|
var element = $compile('<div ng:hide="hidden"/>')($rootScope);
|
2011-01-19 23:42:11 +00:00
|
|
|
|
2011-10-17 23:56:56 +00:00
|
|
|
$rootScope.hidden = 'true';
|
|
|
|
|
$rootScope.$apply();
|
2011-01-19 23:42:11 +00:00
|
|
|
|
2011-10-17 23:56:56 +00:00
|
|
|
assertHidden(element);
|
2011-01-19 23:42:11 +00:00
|
|
|
|
2011-10-17 23:56:56 +00:00
|
|
|
$rootScope.hidden = 'false';
|
|
|
|
|
$rootScope.$apply();
|
2011-01-19 23:42:11 +00:00
|
|
|
|
2011-10-17 23:56:56 +00:00
|
|
|
assertVisible(element);
|
2011-01-19 23:42:11 +00:00
|
|
|
|
2011-10-17 23:56:56 +00:00
|
|
|
$rootScope.hidden = '';
|
|
|
|
|
$rootScope.$apply();
|
2011-01-19 23:42:11 +00:00
|
|
|
|
2011-10-17 23:56:56 +00:00
|
|
|
assertVisible(element);
|
|
|
|
|
}));
|
2011-01-19 23:42:11 +00:00
|
|
|
|
2011-10-26 05:21:21 +00:00
|
|
|
it('ShowBinding', inject(function($rootScope, $compile) {
|
|
|
|
|
var element = $compile('<div ng:show="show"/>')($rootScope);
|
2011-01-19 23:42:11 +00:00
|
|
|
|
2011-10-17 23:56:56 +00:00
|
|
|
$rootScope.show = 'true';
|
|
|
|
|
$rootScope.$apply();
|
2011-01-19 23:42:11 +00:00
|
|
|
|
2011-10-17 23:56:56 +00:00
|
|
|
assertVisible(element);
|
2011-01-19 23:42:11 +00:00
|
|
|
|
2011-10-17 23:56:56 +00:00
|
|
|
$rootScope.show = 'false';
|
|
|
|
|
$rootScope.$apply();
|
2011-01-19 23:42:11 +00:00
|
|
|
|
2011-10-17 23:56:56 +00:00
|
|
|
assertHidden(element);
|
2011-01-19 23:42:11 +00:00
|
|
|
|
2011-10-17 23:56:56 +00:00
|
|
|
$rootScope.show = '';
|
|
|
|
|
$rootScope.$apply();
|
2011-01-19 23:42:11 +00:00
|
|
|
|
2011-10-17 23:56:56 +00:00
|
|
|
assertHidden(element);
|
|
|
|
|
}));
|
2011-01-19 23:42:11 +00:00
|
|
|
|
|
|
|
|
|
2011-10-26 05:21:21 +00:00
|
|
|
it('BindClass', inject(function($rootScope, $compile) {
|
|
|
|
|
var element = $compile('<div ng:class="clazz"/>')($rootScope);
|
2011-01-19 23:42:11 +00:00
|
|
|
|
2011-10-17 23:56:56 +00:00
|
|
|
$rootScope.clazz = 'testClass';
|
|
|
|
|
$rootScope.$apply();
|
2011-01-19 23:42:11 +00:00
|
|
|
|
2011-10-17 23:56:56 +00:00
|
|
|
assertEquals('<div class="testClass" ng:class="clazz"></div>', sortedHtml(element));
|
2011-01-19 23:42:11 +00:00
|
|
|
|
2011-10-17 23:56:56 +00:00
|
|
|
$rootScope.clazz = ['a', 'b'];
|
|
|
|
|
$rootScope.$apply();
|
2011-01-19 23:42:11 +00:00
|
|
|
|
2011-10-17 23:56:56 +00:00
|
|
|
assertEquals('<div class="a b" ng:class="clazz"></div>', sortedHtml(element));
|
|
|
|
|
}));
|
2011-01-19 23:42:11 +00:00
|
|
|
|
2011-10-26 05:21:21 +00:00
|
|
|
it('BindClassEvenOdd', inject(function($rootScope, $compile) {
|
|
|
|
|
var element = $compile(
|
2011-10-17 23:56:56 +00:00
|
|
|
'<div>' +
|
|
|
|
|
'<div ng:repeat="i in [0,1]" ng:class-even="\'e\'" ng:class-odd="\'o\'"></div>' +
|
|
|
|
|
'</div>')($rootScope);
|
|
|
|
|
$rootScope.$apply();
|
|
|
|
|
var d1 = jqLite(element[0].childNodes[1]);
|
|
|
|
|
var d2 = jqLite(element[0].childNodes[2]);
|
2011-01-06 22:34:21 +00:00
|
|
|
expect(d1.hasClass('o')).toBeTruthy();
|
|
|
|
|
expect(d2.hasClass('e')).toBeTruthy();
|
|
|
|
|
assertEquals(
|
|
|
|
|
'<div><#comment></#comment>' +
|
2011-08-17 06:08:13 +00:00
|
|
|
'<div class="o" ng:class-even="\'e\'" ng:class-odd="\'o\'"></div>' +
|
|
|
|
|
'<div class="e" ng:class-even="\'e\'" ng:class-odd="\'o\'"></div></div>',
|
2011-10-17 23:56:56 +00:00
|
|
|
sortedHtml(element));
|
|
|
|
|
}));
|
|
|
|
|
|
2011-10-26 05:21:21 +00:00
|
|
|
it('BindStyle', inject(function($rootScope, $compile) {
|
|
|
|
|
var element = $compile('<div ng:style="style"/>')($rootScope);
|
2011-10-17 23:56:56 +00:00
|
|
|
|
|
|
|
|
$rootScope.$eval('style={height: "10px"}');
|
|
|
|
|
$rootScope.$apply();
|
|
|
|
|
|
|
|
|
|
assertEquals("10px", element.css('height'));
|
|
|
|
|
|
|
|
|
|
$rootScope.$eval('style={}');
|
|
|
|
|
$rootScope.$apply();
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
it('ActionOnAHrefThrowsError', inject(
|
2011-11-02 04:09:54 +00:00
|
|
|
function($provide){
|
|
|
|
|
$provide.factory('$exceptionHandler', $exceptionHandlerMockFactory);
|
2011-10-17 23:56:56 +00:00
|
|
|
},
|
2011-10-26 05:21:21 +00:00
|
|
|
function($rootScope, $exceptionHandler, $compile) {
|
|
|
|
|
var input = $compile('<a ng:click="action()">Add Phone</a>')($rootScope);
|
2011-10-17 23:56:56 +00:00
|
|
|
$rootScope.action = function() {
|
|
|
|
|
throw new Error('MyError');
|
|
|
|
|
};
|
|
|
|
|
browserTrigger(input, 'click');
|
|
|
|
|
expect($exceptionHandler.errors[0]).toMatch(/MyError/);
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
|
2011-10-26 05:21:21 +00:00
|
|
|
it('ShoulIgnoreVbNonBindable', inject(function($rootScope, $compile) {
|
|
|
|
|
var element = $compile(
|
2011-10-17 23:56:56 +00:00
|
|
|
"<div>{{a}}" +
|
2011-01-06 22:34:21 +00:00
|
|
|
"<div ng:non-bindable>{{a}}</div>" +
|
|
|
|
|
"<div ng:non-bindable=''>{{b}}</div>" +
|
2011-10-17 23:56:56 +00:00
|
|
|
"<div ng:non-bindable='true'>{{c}}</div>" +
|
|
|
|
|
"</div>")($rootScope);
|
|
|
|
|
$rootScope.a = 123;
|
|
|
|
|
$rootScope.$apply();
|
|
|
|
|
assertEquals('123{{a}}{{b}}{{c}}', element.text());
|
|
|
|
|
}));
|
|
|
|
|
|
2011-10-26 05:21:21 +00:00
|
|
|
it('ShouldTemplateBindPreElements', inject(function ($rootScope, $compile) {
|
|
|
|
|
var element = $compile('<pre>Hello {{name}}!</pre>')($rootScope);
|
2011-10-17 23:56:56 +00:00
|
|
|
$rootScope.name = "World";
|
|
|
|
|
$rootScope.$apply();
|
2011-01-19 23:42:11 +00:00
|
|
|
|
2011-10-17 23:56:56 +00:00
|
|
|
assertEquals(
|
|
|
|
|
'<pre ng:bind-template="Hello {{name}}!">Hello World!</pre>',
|
|
|
|
|
sortedHtml(element));
|
|
|
|
|
}));
|
2011-01-19 23:42:11 +00:00
|
|
|
|
2011-10-26 05:21:21 +00:00
|
|
|
it('FillInOptionValueWhenMissing', inject(function($rootScope, $compile) {
|
|
|
|
|
var element = $compile(
|
2011-09-08 20:56:29 +00:00
|
|
|
'<select ng:model="foo">' +
|
|
|
|
|
'<option selected="true">{{a}}</option>' +
|
|
|
|
|
'<option value="">{{b}}</option>' +
|
|
|
|
|
'<option>C</option>' +
|
2011-10-17 23:56:56 +00:00
|
|
|
'</select>')($rootScope);
|
|
|
|
|
$rootScope.a = 'A';
|
|
|
|
|
$rootScope.b = 'B';
|
|
|
|
|
$rootScope.$apply();
|
|
|
|
|
var optionA = childNode(element, 0);
|
|
|
|
|
var optionB = childNode(element, 1);
|
|
|
|
|
var optionC = childNode(element, 2);
|
2011-01-19 23:42:11 +00:00
|
|
|
|
2011-01-06 22:34:21 +00:00
|
|
|
expect(optionA.attr('value')).toEqual('A');
|
|
|
|
|
expect(optionA.text()).toEqual('A');
|
2011-01-19 23:42:11 +00:00
|
|
|
|
2011-01-06 22:34:21 +00:00
|
|
|
expect(optionB.attr('value')).toEqual('');
|
|
|
|
|
expect(optionB.text()).toEqual('B');
|
2011-01-19 23:42:11 +00:00
|
|
|
|
2011-01-06 22:34:21 +00:00
|
|
|
expect(optionC.attr('value')).toEqual('C');
|
|
|
|
|
expect(optionC.text()).toEqual('C');
|
2011-10-17 23:56:56 +00:00
|
|
|
}));
|
2011-01-19 23:42:11 +00:00
|
|
|
|
2011-10-26 05:21:21 +00:00
|
|
|
it('DeleteAttributeIfEvaluatesFalse', inject(function($rootScope, $compile) {
|
|
|
|
|
var element = $compile(
|
2011-10-17 23:56:56 +00:00
|
|
|
'<div>' +
|
2011-09-08 20:56:29 +00:00
|
|
|
'<input ng:model="a0" ng:bind-attr="{disabled:\'{{true}}\'}">' +
|
|
|
|
|
'<input ng:model="a1" ng:bind-attr="{disabled:\'{{false}}\'}">' +
|
|
|
|
|
'<input ng:model="b0" ng:bind-attr="{disabled:\'{{1}}\'}">' +
|
|
|
|
|
'<input ng:model="b1" ng:bind-attr="{disabled:\'{{0}}\'}">' +
|
|
|
|
|
'<input ng:model="c0" ng:bind-attr="{disabled:\'{{[0]}}\'}">' +
|
2011-10-17 23:56:56 +00:00
|
|
|
'<input ng:model="c1" ng:bind-attr="{disabled:\'{{[]}}\'}">' +
|
|
|
|
|
'</div>')($rootScope);
|
|
|
|
|
$rootScope.$apply();
|
2011-01-06 22:34:21 +00:00
|
|
|
function assertChild(index, disabled) {
|
2011-10-17 23:56:56 +00:00
|
|
|
var child = childNode(element, index);
|
2011-01-06 22:34:21 +00:00
|
|
|
assertEquals(sortedHtml(child), disabled, !!child.attr('disabled'));
|
|
|
|
|
}
|
2011-01-19 23:42:11 +00:00
|
|
|
|
2011-01-06 22:34:21 +00:00
|
|
|
assertChild(0, true);
|
|
|
|
|
assertChild(1, false);
|
|
|
|
|
assertChild(2, true);
|
|
|
|
|
assertChild(3, false);
|
|
|
|
|
assertChild(4, true);
|
|
|
|
|
assertChild(5, false);
|
2011-10-17 23:56:56 +00:00
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
it('ItShouldDisplayErrorWhenActionIsSyntacticlyIncorrect', inject(
|
2011-11-02 04:09:54 +00:00
|
|
|
function($provide){
|
|
|
|
|
$provide.factory('$exceptionHandler', $exceptionHandlerMockFactory);
|
2011-10-17 23:56:56 +00:00
|
|
|
},
|
2011-10-26 05:21:21 +00:00
|
|
|
function($rootScope, $exceptionHandler, $log, $compile) {
|
|
|
|
|
var element = $compile(
|
2011-10-17 23:56:56 +00:00
|
|
|
'<div>' +
|
|
|
|
|
'<input type="button" ng:click="greeting=\'ABC\'"/>' +
|
|
|
|
|
'<input type="button" ng:click=":garbage:"/>' +
|
|
|
|
|
'</div>')($rootScope);
|
|
|
|
|
var first = jqLite(element.find('input')[0]);
|
|
|
|
|
var second = jqLite(element.find('input')[1]);
|
|
|
|
|
var errorLogs = $log.error.logs;
|
|
|
|
|
|
|
|
|
|
browserTrigger(first, 'click');
|
|
|
|
|
assertEquals("ABC", $rootScope.greeting);
|
|
|
|
|
expect(errorLogs).toEqual([]);
|
|
|
|
|
|
|
|
|
|
browserTrigger(second, 'click');
|
|
|
|
|
expect($exceptionHandler.errors[0]).
|
|
|
|
|
toMatchError(/Syntax Error: Token ':' not a primary expression/);
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
|
2011-10-26 05:21:21 +00:00
|
|
|
it('ItShouldSelectTheCorrectRadioBox', inject(function($rootScope, $compile) {
|
|
|
|
|
var element = $compile(
|
2011-10-17 23:56:56 +00:00
|
|
|
'<div>' +
|
2011-09-08 20:56:29 +00:00
|
|
|
'<input type="radio" ng:model="sex" value="female">' +
|
2011-10-17 23:56:56 +00:00
|
|
|
'<input type="radio" ng:model="sex" value="male">' +
|
|
|
|
|
'</div>')($rootScope);
|
|
|
|
|
var female = jqLite(element[0].childNodes[0]);
|
|
|
|
|
var male = jqLite(element[0].childNodes[1]);
|
2011-01-19 23:42:11 +00:00
|
|
|
|
2011-01-06 22:34:21 +00:00
|
|
|
browserTrigger(female);
|
2011-10-17 23:56:56 +00:00
|
|
|
assertEquals("female", $rootScope.sex);
|
2011-01-06 22:34:21 +00:00
|
|
|
assertEquals(true, female[0].checked);
|
|
|
|
|
assertEquals(false, male[0].checked);
|
|
|
|
|
assertEquals("female", female.val());
|
2011-01-19 23:42:11 +00:00
|
|
|
|
2011-01-06 22:34:21 +00:00
|
|
|
browserTrigger(male);
|
2011-10-17 23:56:56 +00:00
|
|
|
assertEquals("male", $rootScope.sex);
|
2011-01-06 22:34:21 +00:00
|
|
|
assertEquals(false, female[0].checked);
|
|
|
|
|
assertEquals(true, male[0].checked);
|
|
|
|
|
assertEquals("male", male.val());
|
2011-10-17 23:56:56 +00:00
|
|
|
}));
|
|
|
|
|
|
2011-10-26 05:21:21 +00:00
|
|
|
it('ItShouldRepeatOnHashes', inject(function($rootScope, $compile) {
|
|
|
|
|
var element = $compile(
|
2011-10-17 23:56:56 +00:00
|
|
|
'<ul>' +
|
|
|
|
|
'<li ng:repeat="(k,v) in {a:0,b:1}" ng:bind=\"k + v\"></li>' +
|
|
|
|
|
'</ul>')($rootScope);
|
|
|
|
|
$rootScope.$apply();
|
2011-01-06 22:34:21 +00:00
|
|
|
assertEquals('<ul>' +
|
|
|
|
|
'<#comment></#comment>' +
|
2011-08-17 06:08:13 +00:00
|
|
|
'<li ng:bind=\"k + v\">a0</li>' +
|
|
|
|
|
'<li ng:bind=\"k + v\">b1</li>' +
|
2011-01-06 22:34:21 +00:00
|
|
|
'</ul>',
|
2011-10-17 23:56:56 +00:00
|
|
|
sortedHtml(element));
|
|
|
|
|
}));
|
|
|
|
|
|
2011-10-26 05:21:21 +00:00
|
|
|
it('ItShouldFireChangeListenersBeforeUpdate', inject(function($rootScope, $compile) {
|
|
|
|
|
var element = $compile('<div ng:bind="name"></div>')($rootScope);
|
2011-10-17 23:56:56 +00:00
|
|
|
$rootScope.name = "";
|
|
|
|
|
$rootScope.$watch("watched", "name=123");
|
|
|
|
|
$rootScope.watched = "change";
|
|
|
|
|
$rootScope.$apply();
|
|
|
|
|
assertEquals(123, $rootScope.name);
|
2011-01-06 22:34:21 +00:00
|
|
|
assertEquals(
|
|
|
|
|
'<div ng:bind="name">123</div>',
|
2011-10-17 23:56:56 +00:00
|
|
|
sortedHtml(element));
|
|
|
|
|
}));
|
|
|
|
|
|
2011-10-26 05:21:21 +00:00
|
|
|
it('ItShouldHandleMultilineBindings', inject(function($rootScope, $compile) {
|
|
|
|
|
var element = $compile('<div>{{\n 1 \n + \n 2 \n}}</div>')($rootScope);
|
2011-10-17 23:56:56 +00:00
|
|
|
$rootScope.$apply();
|
|
|
|
|
assertEquals("3", element.text());
|
|
|
|
|
}));
|
2011-01-19 23:42:11 +00:00
|
|
|
|
|
|
|
|
});
|