angular.js/test/delete/WidgetsTest.js

269 lines
8.6 KiB
JavaScript
Raw Normal View History

2010-01-06 00:36:58 +00:00
WidgetTest = TestCase('WidgetTest');
WidgetTest.prototype.testRequired = function () {
var view = $('<input name="a" ng-required>');
2010-01-09 23:02:43 +00:00
var scope = new Scope({$invalidWidgets:[]});
2010-01-29 06:10:49 +00:00
var cntl = new TextController(view[0], 'a', angularFormatter.noop);
2010-01-06 00:36:58 +00:00
cntl.updateView(scope);
assertTrue(view.hasClass('ng-validation-error'));
assertEquals("Required Value", view.attr('ng-error'));
scope.set('a', 'A');
cntl.updateView(scope);
assertFalse(view.hasClass('ng-validation-error'));
assertEquals("undefined", typeof view.attr('ng-error'));
};
WidgetTest.prototype.testValidator = function () {
var view = $('<input name="a" ng-validate="testValidator:\'ABC\'">');
2010-01-09 23:02:43 +00:00
var scope = new Scope({$invalidWidgets:[]});
2010-01-29 06:10:49 +00:00
var cntl = new TextController(view[0], 'a', angularFormatter.noop);
2010-01-06 00:36:58 +00:00
angular.validator.testValidator = function(value, expect){
return value == expect ? false : "Error text";
2010-01-06 00:36:58 +00:00
};
scope.set('a', '');
cntl.updateView(scope);
assertEquals(view.hasClass('ng-validation-error'), false);
assertEquals(null, view.attr('ng-error'));
scope.set('a', 'X');
cntl.updateView(scope);
assertEquals(view.hasClass('ng-validation-error'), true);
assertEquals(view.attr('ng-error'), "Error text");
assertEquals("Error text", view.attr('ng-error'));
scope.set('a', 'ABC');
cntl.updateView(scope);
assertEquals(view.hasClass('ng-validation-error'), false);
assertEquals(view.attr('ng-error'), null);
assertEquals(null, view.attr('ng-error'));
delete angular.validator['testValidator'];
};
WidgetTest.prototype.testRequiredValidator = function () {
var view = $('<input name="a" ng-required ng-validate="testValidator:\'ABC\'">');
2010-01-09 23:02:43 +00:00
var scope = new Scope({$invalidWidgets:[]});
2010-01-29 06:10:49 +00:00
var cntl = new TextController(view[0], 'a', angularFormatter.noop);
2010-01-06 00:36:58 +00:00
angular.validator.testValidator = function(value, expect){
return value == expect ? null : "Error text";
};
scope.set('a', '');
cntl.updateView(scope);
assertEquals(view.hasClass('ng-validation-error'), true);
assertEquals("Required Value", view.attr('ng-error'));
scope.set('a', 'X');
cntl.updateView(scope);
assertEquals(view.hasClass('ng-validation-error'), true);
assertEquals("Error text", view.attr('ng-error'));
scope.set('a', 'ABC');
cntl.updateView(scope);
assertEquals(view.hasClass('ng-validation-error'), false);
assertEquals(null, view.attr('ng-error'));
delete angular.validator['testValidator'];
};
2010-01-09 23:02:43 +00:00
TextControllerTest = TestCase("TextControllerTest");
2010-01-06 00:36:58 +00:00
2010-01-09 23:02:43 +00:00
TextControllerTest.prototype.testDatePicker = function() {
2010-01-06 00:36:58 +00:00
var input = $('<input type="text" ng-widget="datepicker">');
2010-01-09 23:02:43 +00:00
input.data('scope', new Scope());
2010-01-06 00:36:58 +00:00
var body = $(document.body);
body.append(input);
2010-01-09 23:02:43 +00:00
var binder = new Binder(input[0], new WidgetFactory());
2010-01-06 00:36:58 +00:00
assertTrue('before', input.data('datepicker') === undefined);
binder.compile();
assertTrue('after', input.data('datepicker') !== null);
assertTrue(body.html(), input.hasClass('hasDatepicker'));
};
2010-01-09 23:02:43 +00:00
RepeaterUpdaterTest = TestCase("RepeaterUpdaterTest");
2010-01-06 00:36:58 +00:00
2010-01-09 23:02:43 +00:00
RepeaterUpdaterTest.prototype.testRemoveThenAdd = function() {
2010-01-06 00:36:58 +00:00
var view = $("<div><span/></div>");
var template = function () {
return $("<li/>");
};
2010-01-09 23:02:43 +00:00
var repeater = new RepeaterUpdater(view.find("span"), "a in b", template, "");
var scope = new Scope();
2010-01-06 00:36:58 +00:00
scope.set('b', [1,2]);
repeater.updateView(scope);
scope.set('b', []);
repeater.updateView(scope);
scope.set('b', [1]);
repeater.updateView(scope);
assertEquals(1, view.find("li").size());
};
2010-01-09 23:02:43 +00:00
RepeaterUpdaterTest.prototype.testShouldBindWidgetOnRepeaterClone = function(){
2010-01-06 00:36:58 +00:00
//fail();
};
2010-01-09 23:02:43 +00:00
RepeaterUpdaterTest.prototype.testShouldThrowInformativeSyntaxError= function(){
2010-01-06 00:36:58 +00:00
expectAsserts(1);
try {
2010-01-09 23:02:43 +00:00
var repeater = new RepeaterUpdater(null, "a=b");
2010-01-06 00:36:58 +00:00
} catch (e) {
assertEquals("Expected ng-repeat in form of 'item in collection' but got 'a=b'.", e);
}
};
SelectControllerTest = TestCase("SelectControllerTest");
SelectControllerTest.prototype.testShouldUpdateModelNullOnNothingSelected = function(){
2010-01-09 23:02:43 +00:00
var scope = new Scope();
2010-01-06 00:36:58 +00:00
var view = {selectedIndex:-1, options:[]};
2010-01-09 23:02:43 +00:00
var cntl = new SelectController(view, 'abc');
2010-01-06 00:36:58 +00:00
cntl.updateModel(scope);
assertNull(scope.get('abc'));
};
SelectControllerTest.prototype.testShouldUpdateModelWhenNothingSelected = function(){
2010-01-09 23:02:43 +00:00
var scope = new Scope();
2010-01-06 00:36:58 +00:00
var view = {value:'123'};
2010-01-09 23:02:43 +00:00
var cntl = new SelectController(view, 'abc');
2010-01-06 00:36:58 +00:00
cntl.updateView(scope);
assertEquals("123", scope.get('abc'));
};
BindUpdaterTest = TestCase("BindUpdaterTest");
BindUpdaterTest.prototype.testShouldDisplayNothingForUndefined = function () {
var view = $('<span />');
2010-01-09 23:02:43 +00:00
var controller = new BindUpdater(view[0], "{{a}}");
var scope = new Scope();
2010-01-06 00:36:58 +00:00
scope.set('a', undefined);
controller.updateView(scope);
assertEquals("", view.text());
scope.set('a', null);
controller.updateView(scope);
assertEquals("", view.text());
};
BindUpdaterTest.prototype.testShouldDisplayJsonForNonStrings = function () {
var view = $('<span />');
2010-01-09 23:02:43 +00:00
var controller = new BindUpdater(view[0], "{{obj}}");
2010-01-06 00:36:58 +00:00
2010-01-09 23:02:43 +00:00
controller.updateView(new Scope({obj:[]}));
2010-01-06 00:36:58 +00:00
assertEquals("[]", view.text());
2010-01-09 23:02:43 +00:00
controller.updateView(new Scope({obj:{text:'abc'}}));
assertEquals('abc', fromJson(view.text()).text);
2010-01-06 00:36:58 +00:00
};
BindUpdaterTest.prototype.testShouldInsertHtmlNode = function () {
var view = $('<span />');
2010-01-09 23:02:43 +00:00
var controller = new BindUpdater(view[0], "<fake>&{{obj}}</fake>");
var scope = new Scope();
2010-01-06 00:36:58 +00:00
scope.set("obj", $('<div>myDiv</div>')[0]);
controller.updateView(scope);
assertEquals("<fake>&myDiv</fake>", view.text());
};
BindUpdaterTest.prototype.testShouldDisplayTextMethod = function () {
var view = $('<div />');
2010-01-09 23:02:43 +00:00
var controller = new BindUpdater(view[0], "{{obj}}");
var scope = new Scope();
2010-01-06 00:36:58 +00:00
scope.set("obj", new angular.filter.Meta({text:function(){return "abc";}}));
controller.updateView(scope);
assertEquals("abc", view.text());
scope.set("obj", new angular.filter.Meta({text:"123"}));
controller.updateView(scope);
assertEquals("123", view.text());
scope.set("obj", {text:"123"});
controller.updateView(scope);
2010-01-09 23:02:43 +00:00
assertEquals("123", fromJson(view.text()).text);
2010-01-06 00:36:58 +00:00
};
BindUpdaterTest.prototype.testShouldDisplayHtmlMethod = function () {
var view = $('<div />');
2010-01-09 23:02:43 +00:00
var controller = new BindUpdater(view[0], "{{obj}}");
var scope = new Scope();
2010-01-06 00:36:58 +00:00
scope.set("obj", new angular.filter.Meta({html:function(){return "a<div>b</div>c";}}));
controller.updateView(scope);
assertEquals("abc", view.text());
scope.set("obj", new angular.filter.Meta({html:"1<div>2</div>3"}));
controller.updateView(scope);
assertEquals("123", view.text());
scope.set("obj", {html:"123"});
controller.updateView(scope);
2010-01-09 23:02:43 +00:00
assertEquals("123", fromJson(view.text()).html);
2010-01-06 00:36:58 +00:00
};
BindUpdaterTest.prototype.testUdateBoolean = function() {
var view = $('<div />');
2010-01-09 23:02:43 +00:00
var controller = new BindUpdater(view[0], "{{true}}, {{false}}");
controller.updateView(new Scope());
2010-01-06 00:36:58 +00:00
assertEquals('true, false', view.text());
};
BindAttrUpdaterTest = TestCase("BindAttrUpdaterTest");
BindAttrUpdaterTest.prototype.testShouldLoadBlankImageWhenBindingIsUndefined = function () {
var view = $('<img />');
2010-01-09 23:02:43 +00:00
var controller = new BindAttrUpdater(view[0], {src: '{{imageUrl}}'});
2010-01-06 00:36:58 +00:00
2010-01-09 23:02:43 +00:00
var scope = new Scope();
2010-01-06 00:36:58 +00:00
scope.set('imageUrl', undefined);
2010-01-25 03:12:01 +00:00
scope.set('$config.blankImage', 'http://server/blank.gif');
2010-01-06 00:36:58 +00:00
controller.updateView(scope);
2010-01-25 03:12:01 +00:00
assertEquals("http://server/blank.gif", view.attr('src'));
2010-01-06 00:36:58 +00:00
};
RepeaterUpdaterTest.prototype.testShouldNotDieWhenRepeatExpressionIsNull = function() {
2010-01-09 23:02:43 +00:00
var rep = new RepeaterUpdater(null, "$item in items", null, null);
var scope = new Scope();
2010-01-06 00:36:58 +00:00
scope.set('items', undefined);
rep.updateView(scope);
};
RepeaterUpdaterTest.prototype.testShouldIterateOverKeys = function() {
2010-01-09 23:02:43 +00:00
var rep = new RepeaterUpdater(null, "($k,_v) in items", null, null);
2010-01-06 00:36:58 +00:00
assertEquals("items", rep.iteratorExp);
assertEquals("_v", rep.valueExp);
assertEquals("$k", rep.keyExp);
};
EvalUpdaterTest = TestCase("EvalUpdaterTest");
EvalUpdaterTest.prototype.testEvalThrowsException = function(){
var view = $('<div/>');
2010-01-09 23:02:43 +00:00
var eval = new EvalUpdater(view[0], 'undefined()');
2010-01-06 00:36:58 +00:00
2010-01-09 23:02:43 +00:00
eval.updateView(new Scope());
2010-01-06 00:36:58 +00:00
assertTrue(!!view.attr('ng-error'));
assertTrue(view.hasClass('ng-exception'));
eval.exp = "1";
2010-01-09 23:02:43 +00:00
eval.updateView(new Scope());
2010-01-06 00:36:58 +00:00
assertFalse(!!view.attr('ng-error'));
assertFalse(view.hasClass('ng-exception'));
};
RadioControllerTest = TestCase("RadioController");
RadioControllerTest.prototype.testItShouldTreatTrueStringAsBoolean = function () {
var view = $('<input type="radio" name="select" value="true"/>');
2010-01-09 23:02:43 +00:00
var radio = new RadioController(view[0], 'select');
var scope = new Scope({select:true});
2010-01-06 00:36:58 +00:00
radio.updateView(scope);
assertTrue(view[0].checked);
};