');
assertEquals(scope.a, 123);
});
it('ExecuteInitializationStatements', function(){
var scope = this.compile('
');
assertEquals(scope.a, 123);
assertEquals(scope.b, 345);
});
it('ApplyTextBindings', function(){
var scope = this.compile('
x
');
scope.model = {a:123};
scope.$apply();
assertEquals('123', scope.$element.text());
});
it('ReplaceBindingInTextWithSpan', function(){
assertEquals(this.compileToHtml("
a{{b}}c "), '
a c ');
assertEquals(this.compileToHtml("
{{b}} "), '
');
});
it('BindingSpaceConfusesIE', function(){
if (!msie) return;
var span = document.createElement("span");
span.innerHTML = ' ';
var nbsp = span.firstChild.nodeValue;
assertEquals(
'
'+nbsp+' ',
this.compileToHtml("
{{a}} {{b}} "));
assertEquals(
'
'+nbsp+'x '+nbsp+'( )',
this.compileToHtml("
{{A}} x {{B}} ({{C}}) "));
});
it('BindingOfAttributes', function(){
var scope = this.compile("
");
var attrbinding = scope.$element.attr("ng:bind-attr");
var bindings = fromJson(attrbinding);
assertEquals("http://s/a{{b}}c", decodeURI(bindings.href));
assertTrue(!bindings.foo);
});
it('MarkMultipleAttributes', function(){
var scope = this.compile('
');
var attrbinding = scope.$element.attr("ng:bind-attr");
var bindings = fromJson(attrbinding);
assertEquals(bindings.foo, "{{d}}");
assertEquals(decodeURI(bindings.href), "http://s/a{{b}}c");
});
it('AttributesNoneBound', function(){
var scope = this.compile("
");
var a = scope.$element;
assertEquals(a[0].nodeName, "A");
assertTrue(!a.attr("ng:bind-attr"));
});
it('ExistingAttrbindingIsAppended', function(){
var scope = this.compile("
");
var a = scope.$element;
assertEquals('{"b":"{{def}}","href":"http://s/{{abc}}"}', a.attr('ng:bind-attr'));
});
it('AttributesAreEvaluated', function(){
var scope = this.compile('
');
scope.$eval('a=1;b=2');
scope.$apply();
var a = scope.$element;
assertEquals(a.attr('a'), 'a');
assertEquals(a.attr('b'), 'a+b=3');
});
it('InputTypeButtonActionExecutesInScope', function(){
var savedCalled = false;
var scope = this.compile('
');
scope.person = {};
scope.person.save = function(){
savedCalled = true;
};
browserTrigger(scope.$element, 'click');
assertTrue(savedCalled);
});
it('InputTypeButtonActionExecutesInScope2', function(){
var log = "";
var scope = this.compile('
');
scope.action = function(){
log += 'click;';
};
expect(log).toEqual('');
browserTrigger(scope.$element, 'click');
expect(log).toEqual('click;');
});
it('ButtonElementActionExecutesInScope', function(){
var savedCalled = false;
var scope = this.compile('
Apply ');
scope.person = {};
scope.person.save = function(){
savedCalled = true;
};
browserTrigger(scope.$element, 'click');
assertTrue(savedCalled);
});
it('RepeaterUpdateBindings', function(){
var scope = this.compile('
');
var form = scope.$element;
var items = [{a:"A"}, {a:"B"}];
scope.model = {items:items};
scope.$apply();
assertEquals('
' +
'<#comment>#comment>' +
'A ' +
'B ' +
' ', sortedHtml(form));
items.unshift({a:'C'});
scope.$apply();
assertEquals('
' +
'<#comment>#comment>' +
'C ' +
'A ' +
'B ' +
' ', sortedHtml(form));
items.shift();
scope.$apply();
assertEquals('
' +
'<#comment>#comment>' +
'A ' +
'B ' +
' ', sortedHtml(form));
items.shift();
items.shift();
scope.$apply();
});
it('RepeaterContentDoesNotBind', function(){
var scope = this.compile('
');
scope.model = {items:[{a:"A"}]};
scope.$apply();
assertEquals('
' +
'<#comment>#comment>' +
'A ' +
' ', sortedHtml(scope.$element));
});
it('DoNotOverwriteCustomAction', function(){
var html = this.compileToHtml('
');
assertTrue(html.indexOf('action="foo();"') > 0 );
});
it('RepeaterAdd', function(){
var scope = this.compile('
');
scope.items = [{x:'a'}, {x:'b'}];
scope.$apply();
var first = childNode(scope.$element, 1);
var second = childNode(scope.$element, 2);
expect(first.val()).toEqual('a');
expect(second.val()).toEqual('b');
return
first.val('ABC');
browserTrigger(first, 'keydown');
scope.$service('$browser').defer.flush();
expect(scope.items[0].x).toEqual('ABC');
});
it('ItShouldRemoveExtraChildrenWhenIteratingOverHash', function(){
var scope = this.compile('
');
var items = {};
scope.items = items;
scope.$apply();
expect(scope.$element[0].childNodes.length - 1).toEqual(0);
items.name = "misko";
scope.$apply();
expect(scope.$element[0].childNodes.length - 1).toEqual(1);
delete items.name;
scope.$apply();
expect(scope.$element[0].childNodes.length - 1).toEqual(0);
});
it('IfTextBindingThrowsErrorDecorateTheSpan', function(){
var scope = this.compile('
{{error.throw()}}
', null, true);
var doc = scope.$element;
var errorLogs = scope.$service('$exceptionHandler').errors;
scope.error = {
'throw': function(){throw "ErrorMsg1";}
};
scope.$apply();
var span = childNode(doc, 0);
assertTrue(span.hasClass('ng-exception'));
assertTrue(!!span.text().match(/ErrorMsg1/));
assertTrue(!!span.attr('ng-exception').match(/ErrorMsg1/));
assertEquals(['ErrorMsg1'], errorLogs.shift());
scope.error['throw'] = function(){throw "MyError";};
errorLogs.length = 0;
scope.$apply();
span = childNode(doc, 0);
assertTrue(span.hasClass('ng-exception'));
assertTrue(span.text(), span.text().match('MyError') !== null);
assertEquals('MyError', span.attr('ng-exception'));
assertEquals(['MyError'], errorLogs.shift());
scope.error['throw'] = function(){return "ok";};
scope.$apply();
assertFalse(span.hasClass('ng-exception'));
assertEquals('ok', span.text());
assertEquals(null, span.attr('ng-exception'));
assertEquals(0, errorLogs.length);
});
it('IfAttrBindingThrowsErrorDecorateTheAttribute', function(){
var scope = this.compile('
', null, true);
var doc = scope.$element;
var errorLogs = scope.$service('$exceptionHandler').errors;
var count = 0;
scope.error = {
'throw': function(){throw new Error("ErrorMsg" + (++count));}
};
scope.$apply();
expect(errorLogs.length).not.toEqual(0);
expect(errorLogs.shift()).toMatch(/ErrorMsg1/);
errorLogs.length = 0;
scope.error['throw'] = function(){ return 'X';};
scope.$apply();
expect(errorLogs.length).toMatch(0);
});
it('NestedRepeater', function(){
var scope = this.compile('
');
scope.model = [{name:'a', item:['a1', 'a2']}, {name:'b', item:['b1', 'b2']}];
scope.$apply();
assertEquals('
'+
'<#comment>#comment>'+
'
'+
'<#comment>#comment>'+
'
'+
'
'+
'
'+
'
'+
'<#comment>#comment>'+
'
'+
'
'+
'
', sortedHtml(scope.$element));
});
it('HideBindingExpression', function(){
var scope = this.compile('
');
scope.hidden = 3;
scope.$apply();
assertHidden(scope.$element);
scope.hidden = 2;
scope.$apply();
assertVisible(scope.$element);
});
it('HideBinding', function(){
var scope = this.compile('
');
scope.hidden = 'true';
scope.$apply();
assertHidden(scope.$element);
scope.hidden = 'false';
scope.$apply();
assertVisible(scope.$element);
scope.hidden = '';
scope.$apply();
assertVisible(scope.$element);
});
it('ShowBinding', function(){
var scope = this.compile('
');
scope.show = 'true';
scope.$apply();
assertVisible(scope.$element);
scope.show = 'false';
scope.$apply();
assertHidden(scope.$element);
scope.show = '';
scope.$apply();
assertHidden(scope.$element);
});
it('BindClassUndefined', function(){
var scope = this.compile('
');
scope.$apply();
assertEquals(
'
',
sortedHtml(scope.$element));
});
it('BindClass', function(){
var scope = this.compile('
');
scope.clazz = 'testClass';
scope.$apply();
assertEquals('
', sortedHtml(scope.$element));
scope.clazz = ['a', 'b'];
scope.$apply();
assertEquals('
', sortedHtml(scope.$element));
});
it('BindClassEvenOdd', function(){
var scope = this.compile('
');
scope.$apply();
var d1 = jqLite(scope.$element[0].childNodes[1]);
var d2 = jqLite(scope.$element[0].childNodes[2]);
expect(d1.hasClass('o')).toBeTruthy();
expect(d2.hasClass('e')).toBeTruthy();
assertEquals(
'
<#comment>#comment>' +
'
' +
'
',
sortedHtml(scope.$element));
});
it('BindStyle', function(){
var scope = this.compile('
');
scope.$eval('style={height: "10px"}');
scope.$apply();
assertEquals("10px", scope.$element.css('height'));
scope.$eval('style={}');
scope.$apply();
});
it('ActionOnAHrefThrowsError', function(){
var scope = this.compile('
Add Phone ', null, true);
scope.action = function(){
throw new Error('MyError');
};
var input = scope.$element;
browserTrigger(input, 'click');
expect(scope.$service('$exceptionHandler').errors[0]).toMatch(/MyError/);
});
it('ShoulIgnoreVbNonBindable', function(){
var scope = this.compile("
{{a}}" +
"
{{a}}
" +
"
{{b}}
" +
"
{{c}}
");
scope.a = 123;
scope.$apply();
assertEquals('123{{a}}{{b}}{{c}}', scope.$element.text());
});
it('RepeaterShouldBindInputsDefaults', function () {
var scope = this.compile('
');
scope.items = [{}, {name:'misko'}];
scope.$apply();
assertEquals("123", scope.$eval('items[0].name'));
assertEquals("misko", scope.$eval('items[1].name'));
});
it('ShouldTemplateBindPreElements', function () {
var scope = this.compile('
Hello {{name}}! ');
scope.name = "World";
scope.$apply();
assertEquals('
Hello World! ', sortedHtml(scope.$element));
});
it('FillInOptionValueWhenMissing', function(){
var scope = this.compile(
'
{{a}} {{b}} C ');
scope.a = 'A';
scope.b = 'B';
scope.$apply();
var optionA = childNode(scope.$element, 0);
var optionB = childNode(scope.$element, 1);
var optionC = childNode(scope.$element, 2);
expect(optionA.attr('value')).toEqual('A');
expect(optionA.text()).toEqual('A');
expect(optionB.attr('value')).toEqual('');
expect(optionB.text()).toEqual('B');
expect(optionC.attr('value')).toEqual('C');
expect(optionC.text()).toEqual('C');
});
it('ValidateForm', function(){
var scope = this.compile('
' +
'
',
jqLite(document.body));
var items = [{}, {}];
scope.items = items;
scope.$apply();
assertEquals(3, scope.$service('$invalidWidgets').length);
scope.name = '';
scope.$apply();
assertEquals(3, scope.$service('$invalidWidgets').length);
scope.name = ' ';
scope.$apply();
assertEquals(3, scope.$service('$invalidWidgets').length);
scope.name = 'abc';
scope.$apply();
assertEquals(2, scope.$service('$invalidWidgets').length);
items[0].name = 'abc';
scope.$apply();
assertEquals(1, scope.$service('$invalidWidgets').length);
items[1].name = 'abc';
scope.$apply();
assertEquals(0, scope.$service('$invalidWidgets').length);
});
it('ValidateOnlyVisibleItems', function(){
var scope = this.compile('
', jqLite(document.body));
scope.show = true;
scope.$apply();
assertEquals(2, scope.$service('$invalidWidgets').length);
scope.show = false;
scope.$apply();
assertEquals(1, scope.$service('$invalidWidgets').visible());
});
it('DeleteAttributeIfEvaluatesFalse', function(){
var scope = this.compile('
' +
' ' +
' ' +
'
');
scope.$apply();
function assertChild(index, disabled) {
var child = childNode(scope.$element, index);
assertEquals(sortedHtml(child), disabled, !!child.attr('disabled'));
}
assertChild(0, true);
assertChild(1, false);
assertChild(2, true);
assertChild(3, false);
assertChild(4, true);
assertChild(5, false);
});
it('ItShouldDisplayErrorWhenActionIsSyntacticlyIncorrect', function(){
var scope = this.compile('
' +
' ' +
'
', null, true);
var first = jqLite(scope.$element[0].childNodes[0]);
var second = jqLite(scope.$element[0].childNodes[1]);
var errorLogs = scope.$service('$log').error.logs;
browserTrigger(first, 'click');
assertEquals("ABC", scope.greeting);
expect(errorLogs).toEqual([]);
browserTrigger(second, 'click');
expect(scope.$service('$exceptionHandler').errors[0]).
toMatchError(/Syntax Error: Token ':' not a primary expression/);
});
it('ItShouldSelectTheCorrectRadioBox', function(){
var scope = this.compile('
' +
' ' +
'
');
var female = jqLite(scope.$element[0].childNodes[0]);
var male = jqLite(scope.$element[0].childNodes[1]);
browserTrigger(female);
assertEquals("female", scope.sex);
assertEquals(true, female[0].checked);
assertEquals(false, male[0].checked);
assertEquals("female", female.val());
browserTrigger(male);
assertEquals("male", scope.sex);
assertEquals(false, female[0].checked);
assertEquals(true, male[0].checked);
assertEquals("male", male.val());
});
it('ItShouldRepeatOnHashes', function(){
var scope = this.compile('
');
scope.$apply();
assertEquals('
' +
'<#comment>#comment>' +
'a0 ' +
'b1 ' +
' ',
sortedHtml(scope.$element));
});
it('ItShouldFireChangeListenersBeforeUpdate', function(){
var scope = this.compile('
');
scope.name = "";
scope.$watch("watched", "name=123");
scope.watched = "change";
scope.$apply();
assertEquals(123, scope.name);
assertEquals(
'
123
',
sortedHtml(scope.$element));
});
it('ItShouldHandleMultilineBindings', function(){
var scope = this.compile('
{{\n 1 \n + \n 2 \n}}
');
scope.$apply();
assertEquals("3", scope.$element.text());
});
it('ItBindHiddenInputFields', function(){
var scope = this.compile('
');
scope.$apply();
assertEquals("abc", scope.myName);
});
it('ItShouldUseFormaterForText', function(){
var scope = this.compile('
');
scope.$apply();
assertEquals(['a','b'], scope.a);
var input = scope.$element;
input[0].value = ' x,,yz';
browserTrigger(input, 'change');
assertEquals(['x','yz'], scope.a);
scope.a = [1 ,2, 3];
scope.$apply();
assertEquals('1, 2, 3', input[0].value);
});
});