aboutsummaryrefslogtreecommitdiffstats
path: root/test/BinderTest.js
diff options
context:
space:
mode:
authorMisko Hevery2010-01-25 20:02:24 -0800
committerMisko Hevery2010-01-25 20:02:24 -0800
commit0b630972b15676b1c1b6c59edd564e4ee331ec70 (patch)
treebd31ca5b69b6ea03d906a3107dfe38a8c1adcb8e /test/BinderTest.js
parent19bbee030ba012b8fc4835c1d17e039804b2b94b (diff)
parent473e57e22532f9b85fc9dcc1bcc53e12a10154c2 (diff)
downloadangular.js-0b630972b15676b1c1b6c59edd564e4ee331ec70.tar.bz2
merge
Diffstat (limited to 'test/BinderTest.js')
-rw-r--r--test/BinderTest.js472
1 files changed, 212 insertions, 260 deletions
diff --git a/test/BinderTest.js b/test/BinderTest.js
index d033996d..6ef46fae 100644
--- a/test/BinderTest.js
+++ b/test/BinderTest.js
@@ -3,96 +3,94 @@ BinderTest = TestCase('BinderTest');
function compile(content, initialScope, config) {
var h = html(content);
config = config || {autoSubmit:true};
- var scope = new nglr.Scope(initialScope, "ROOT");
+ var scope = new Scope(initialScope, "ROOT");
h.data('scope', scope);
- var binder = new nglr.Binder(h[0], new nglr.WidgetFactory(), new MockUrlWatcher(), config);
- var datastore = new nglr.DataStore();
- scope.set("$datastore", datastore);
- scope.set("$binder", binder);
+ var datastore = new DataStore();
+ var binder = new Binder(h[0], new WidgetFactory(), datastore, new MockLocation(), config);
+ scope.set("$updateView", _(binder.updateView).bind(binder));
scope.set("$anchor", binder.anchor);
binder.entity(scope);
binder.compile();
- return {node:h, binder:binder, scope:scope};
+ return {node:h, binder:binder, scope:scope, datastore:datastore};
}
function compileToHtml(content) {
return compile(content).node.sortedHtml();
}
-
BinderTest.prototype.testParseTextWithNoBindings = function(){
- var parts = nglr.Binder.parseBindings("a");
+ var parts = Binder.parseBindings("a");
assertEquals(parts.length, 1);
assertEquals(parts[0], "a");
- assertTrue(!nglr.Binder.binding(parts[0]));
+ assertTrue(!Binder.binding(parts[0]));
};
BinderTest.prototype.testParseEmptyText = function(){
- var parts = nglr.Binder.parseBindings("");
+ var parts = Binder.parseBindings("");
assertEquals(parts.length, 1);
assertEquals(parts[0], "");
- assertTrue(!nglr.Binder.binding(parts[0]));
+ assertTrue(!Binder.binding(parts[0]));
};
BinderTest.prototype.testParseInnerBinding = function(){
- var parts = nglr.Binder.parseBindings("a{{b}}c");
+ var parts = Binder.parseBindings("a{{b}}c");
assertEquals(parts.length, 3);
assertEquals(parts[0], "a");
- assertTrue(!nglr.Binder.binding(parts[0]));
+ assertTrue(!Binder.binding(parts[0]));
assertEquals(parts[1], "{{b}}");
- assertEquals(nglr.Binder.binding(parts[1]), "b");
+ assertEquals(Binder.binding(parts[1]), "b");
assertEquals(parts[2], "c");
- assertTrue(!nglr.Binder.binding(parts[2]));
+ assertTrue(!Binder.binding(parts[2]));
};
BinderTest.prototype.testParseEndingBinding = function(){
- var parts = nglr.Binder.parseBindings("a{{b}}");
+ var parts = Binder.parseBindings("a{{b}}");
assertEquals(parts.length, 2);
assertEquals(parts[0], "a");
- assertTrue(!nglr.Binder.binding(parts[0]));
+ assertTrue(!Binder.binding(parts[0]));
assertEquals(parts[1], "{{b}}");
- assertEquals(nglr.Binder.binding(parts[1]), "b");
+ assertEquals(Binder.binding(parts[1]), "b");
};
BinderTest.prototype.testParseBeggingBinding = function(){
- var parts = nglr.Binder.parseBindings("{{b}}c");
+ var parts = Binder.parseBindings("{{b}}c");
assertEquals(parts.length, 2);
assertEquals(parts[0], "{{b}}");
- assertEquals(nglr.Binder.binding(parts[0]), "b");
+ assertEquals(Binder.binding(parts[0]), "b");
assertEquals(parts[1], "c");
- assertTrue(!nglr.Binder.binding(parts[1]));
+ assertTrue(!Binder.binding(parts[1]));
};
BinderTest.prototype.testParseLoanBinding = function(){
- var parts = nglr.Binder.parseBindings("{{b}}");
+ var parts = Binder.parseBindings("{{b}}");
assertEquals(parts.length, 1);
assertEquals(parts[0], "{{b}}");
- assertEquals(nglr.Binder.binding(parts[0]), "b");
+ assertEquals(Binder.binding(parts[0]), "b");
};
BinderTest.prototype.testParseTwoBindings = function(){
- var parts = nglr.Binder.parseBindings("{{b}}{{c}}");
+ var parts = Binder.parseBindings("{{b}}{{c}}");
assertEquals(parts.length, 2);
assertEquals(parts[0], "{{b}}");
- assertEquals(nglr.Binder.binding(parts[0]), "b");
+ assertEquals(Binder.binding(parts[0]), "b");
assertEquals(parts[1], "{{c}}");
- assertEquals(nglr.Binder.binding(parts[1]), "c");
+ assertEquals(Binder.binding(parts[1]), "c");
};
BinderTest.prototype.testParseTwoBindingsWithTextInMiddle = function(){
- var parts = nglr.Binder.parseBindings("{{b}}x{{c}}");
+ var parts = Binder.parseBindings("{{b}}x{{c}}");
assertEquals(parts.length, 3);
assertEquals(parts[0], "{{b}}");
- assertEquals(nglr.Binder.binding(parts[0]), "b");
+ assertEquals(Binder.binding(parts[0]), "b");
assertEquals(parts[1], "x");
- assertTrue(!nglr.Binder.binding(parts[1]));
+ assertTrue(!Binder.binding(parts[1]));
assertEquals(parts[2], "{{c}}");
- assertEquals(nglr.Binder.binding(parts[2]), "c");
+ assertEquals(Binder.binding(parts[2]), "c");
};
BinderTest.prototype.testParseMultiline = function(){
- var parts = nglr.Binder.parseBindings('"X\nY{{A\nB}}C\nD"');
- assertTrue(!!nglr.Binder.binding('{{A\nB}}'));
+ var parts = Binder.parseBindings('"X\nY{{A\nB}}C\nD"');
+ assertTrue(!!Binder.binding('{{A\nB}}'));
assertEquals(parts.length, 3);
assertEquals(parts[0], '"X\nY');
assertEquals(parts[1], '{{A\nB}}');
@@ -100,9 +98,9 @@ BinderTest.prototype.testParseMultiline = function(){
};
BinderTest.prototype.testHasBinding = function(){
- assertTrue(nglr.Binder.hasBinding("{{a}}"));
- assertTrue(!nglr.Binder.hasBinding("a"));
- assertTrue(nglr.Binder.hasBinding("{{b}}x{{c}}"));
+ assertTrue(Binder.hasBinding("{{a}}"));
+ assertTrue(!Binder.hasBinding("a"));
+ assertTrue(Binder.hasBinding("{{b}}x{{c}}"));
};
@@ -118,34 +116,23 @@ BinderTest.prototype.testChangingTextfieldUpdatesModel = function(){
};
BinderTest.prototype.testChangingTextareaUpdatesModel = function(){
- var form = html('<textarea name="model.note">abc</textarea>');
- var scope = new nglr.Scope({model:{}});
- form.data('scope', scope);
- var binder = new nglr.Binder(form.get(0), new nglr.WidgetFactory(), new MockUrlWatcher());
- binder.compile();
- binder.updateView();
- assertEquals(scope.get('model').note, 'abc');
+ var c = compile('<textarea name="model.note">abc</textarea>');
+ c.binder.updateView();
+ assertEquals(c.scope.get('model').note, 'abc');
};
BinderTest.prototype.testChangingRadioUpdatesModel = function(){
- var form = html('<input type="radio" name="model.price" value="A" checked>' +
+ var c = compile('<input type="radio" name="model.price" value="A" checked>' +
'<input type="radio" name="model.price" value="B">');
- var scope = new nglr.Scope({model:{}});
- form.data('scope', scope);
- var binder = new nglr.Binder(form.get(0), new nglr.WidgetFactory(), new MockUrlWatcher());
- binder.compile();
- binder.updateView();
- assertEquals(scope.get('model').price, 'A');
+ c.binder.updateView();
+ assertEquals(c.scope.get('model').price, 'A');
};
BinderTest.prototype.testChangingCheckboxUpdatesModel = function(){
- var form = html('<input type="checkbox" name="model.price" value="A" checked>');
- var scope = new nglr.Scope({model:{}});
- form.data('scope', scope);
- var binder = new nglr.Binder(form.get(0), new nglr.WidgetFactory(), new MockUrlWatcher());
- binder.compile();
- binder.updateView();
- assertEquals('A', scope.get('model').price);
+ var form = compile('<input type="checkbox" name="model.price" value="A" checked>');
+ form.scope.set('model', {});
+ form.binder.updateView();
+ assertEquals('A', form.scope.get('model').price);
};
BinderTest.prototype.testBindUpdate = function() {
@@ -155,66 +142,55 @@ BinderTest.prototype.testBindUpdate = function() {
};
BinderTest.prototype.testChangingSelectNonSelectedUpdatesModel = function(){
- var form = html('<select name="model.price"><option value="A">A</option><option value="B">B</option></select>');
- var scope = new nglr.Scope({model:{}});
- form.data('scope', scope);
- var binder = new nglr.Binder(form.get(0), new nglr.WidgetFactory(), new MockUrlWatcher());
- binder.compile();
- binder.updateView();
- assertEquals('A', scope.get('model').price);
+ var form = compile('<select name="model.price"><option value="A">A</option><option value="B">B</option></select>');
+ form.scope.set('model', {});
+ form.binder.updateView();
+ assertEquals('A', form.scope.get('model').price);
};
BinderTest.prototype.testChangingMultiselectUpdatesModel = function(){
- var form = html('<select name="Invoice.options" multiple="multiple">' +
+ var form = compile('<select name="Invoice.options" multiple="multiple">' +
'<option value="A" selected>Gift wrap</option>' +
'<option value="B" selected>Extra padding</option>' +
'<option value="C">Expedite</option>' +
'</select>');
- var scope = new nglr.Scope({Invoice:{}});
- form.data('scope', scope);
- var binder = new nglr.Binder(form.get(0), new nglr.WidgetFactory(), new MockUrlWatcher());
- binder.compile();
- binder.updateView();
- assertJsonEquals(["A", "B"], scope.get('Invoice').options);
+ form.scope.set("Invoice", {});
+ form.binder.updateView();
+ assertJsonEquals(["A", "B"], form.scope.get('Invoice').options);
};
BinderTest.prototype.testChangingSelectSelectedUpdatesModel = function(){
- var form = html('<select name="model.price"><option>A</option><option selected value="b">B</option></select>');
- var scope = new nglr.Scope({model:{}});
- form.data('scope', scope);
- var binder = new nglr.Binder(form.get(0), new nglr.WidgetFactory(), new MockUrlWatcher());
- binder.compile();
- binder.updateView();
- assertEquals(scope.get('model').price, 'b');
+ var form = compile('<select name="model.price"><option>A</option><option selected value="b">B</option></select>');
+ form.scope.set('model', {});
+ form.binder.updateView();
+ assertEquals(form.scope.get('model').price, 'b');
};
BinderTest.prototype.testExecuteInitialization = function() {
var form = html('<div ng-init="a=123">');
- var scope = new nglr.Scope();
+ var scope = new Scope();
form.data('scope', scope);
- var binder = new nglr.Binder(form.get(0));
+ var binder = new Binder(form.get(0));
binder.executeInit();
assertEquals(scope.get('a'), 123);
};
BinderTest.prototype.testExecuteInitializationStatements = function() {
var form = html('<div ng-init="a=123;b=345">');
- var scope = new nglr.Scope();
+ var scope = new Scope();
form.data('scope', scope);
- var binder = new nglr.Binder(form.get(0));
+ var binder = new Binder(form.get(0));
binder.executeInit();
assertEquals(scope.get('a'), 123);
assertEquals(scope.get('b'), 345);
};
BinderTest.prototype.testApplyTextBindings = function(){
- var form = html('<div ng-bind="model.a">x</div>');
- var scope = new nglr.Scope({model:{a:123}});
- form.data('scope', scope);
- var binder = new nglr.Binder(form.get(0), null, new MockUrlWatcher());
- binder.compile();
- binder.updateView();
- assertEquals('123', form.text());
+ var form = compile('<div ng-bind="model.a">x</div>');
+ form.scope.set('model', {a:123});
+ form.binder.compile();
+ form.binder.updateView();
+ assertEquals('123', form.node.text());
};
BinderTest.prototype.testReplaceBindingInTextWithSpan = function() {
@@ -224,15 +200,15 @@ BinderTest.prototype.testReplaceBindingInTextWithSpan = function() {
BinderTest.prototype.testReplaceBindingCreatesCorrectNumberOfWidgets = function() {
var h = html("space{{a}}<b>{{a}}a{{a}}</b>{{a}}");
- h.data('scope', new nglr.Scope());
- var binder = new nglr.Binder(h.get(0), new nglr.WidgetFactory());
+ h.data('scope', new Scope());
+ var binder = new Binder(h.get(0), new WidgetFactory());
binder.compile();
assertEquals(4, h.scope().widgets.length);
};
BinderTest.prototype.testBindingSpaceConfusesIE = function() {
- if (!nglr.msie) return;
+ if (!msie) return;
var span = document.createElement("span");
span.innerHTML = '&nbsp;';
var nbsp = span.firstChild.nodeValue;
@@ -246,30 +222,30 @@ BinderTest.prototype.testBindingSpaceConfusesIE = function() {
BinderTest.prototype.testBindingOfAttributes = function() {
var form = html("<a href='http://s/a{{b}}c' foo='x'></a>");
- form.data('scope', new nglr.Scope());
- var binder = new nglr.Binder(form.get(0));
+ form.data('scope', new Scope());
+ var binder = new Binder(form.get(0));
binder.compile();
var attrbinding = form.find("a").attr("ng-bind-attr");
- var bindings = nglr.fromJson(attrbinding);
+ var bindings = fromJson(attrbinding);
assertEquals("http://s/a{{b}}c", decodeURI(bindings.href));
assertTrue(!bindings.foo);
};
BinderTest.prototype.testMarkMultipleAttributes = function() {
var form = html("<a href='http://s/a{{b}}c' foo='{{d}}'></a>");
- form.data('scope', new nglr.Scope());
- var binder = new nglr.Binder(form.get(0));
+ form.data('scope', new Scope());
+ var binder = new Binder(form.get(0));
binder.compile();
var attrbinding = form.find("a").attr("ng-bind-attr");
- var bindings = nglr.fromJson(attrbinding);
+ var bindings = fromJson(attrbinding);
assertEquals(decodeURI(bindings.href), "http://s/a{{b}}c");
assertEquals(bindings.foo, "{{d}}");
};
BinderTest.prototype.testAttributesNoneBound = function() {
var form = html("<a href='abc' foo='def'></a>");
- form.data('scope', new nglr.Scope());
- var binder = new nglr.Binder(form.get(0));
+ form.data('scope', new Scope());
+ var binder = new Binder(form.get(0));
binder.compile();
var a = form.find("a");
assertEquals(a.get(0).nodeName, "A");
@@ -278,18 +254,17 @@ BinderTest.prototype.testAttributesNoneBound = function() {
BinderTest.prototype.testExistingAttrbindingIsAppended = function() {
var form = html("<a href='http://s/{{abc}}' ng-bind-attr='{\"b\":\"{{def}}\"}'></a>");
- form.data('scope', new nglr.Scope());
- var binder = new nglr.Binder(form.get(0));
+ form.data('scope', new Scope());
+ var binder = new Binder(form.get(0));
binder.compile();
var a = form.find("a");
assertEquals('{"b":"{{def}}","href":"http://s/{{abc}}"}', a.attr('ng-bind-attr'));
};
BinderTest.prototype.testAttributesAreEvaluated = function(){
- var form = html('<a ng-bind-attr=\'{"a":"a", "b":"a+b={{a+b}}"}\'></a>');
- form.data('scope', new nglr.Scope({a:1, b:2}));
- var binder = new nglr.Binder(form.get(0), null, new MockUrlWatcher());
- binder.compile();
+ var c = compile('<a ng-bind-attr=\'{"a":"a", "b":"a+b={{a+b}}"}\'></a>');
+ var binder = c.binder, form = c.node;
+ c.scope.eval('a=1;b=2');
binder.updateView();
var a = form.find("a");
assertEquals(a.attr('a'), 'a');
@@ -297,16 +272,16 @@ BinderTest.prototype.testAttributesAreEvaluated = function(){
};
BinderTest.prototype.testInputsAreUpdated = function(){
- var form =
- html('<input type="tEXt" name="A.text"/>' +
- '<textarea name="A.textarea"/>' +
- '<input name="A.radio" type="rADio" value="r"/>' +
- '<input name="A.radioOff" type="rADio" value="r"/>' +
- '<input name="A.checkbox" type="checkbox" value="c" />' +
- '<input name="A.checkboxOff" type="checkbox" value="c" />' +
- '<select name="A.select"><option>a</option><option value="S">b</option></select>');
- var binder = new nglr.Binder(form.get(0), new nglr.WidgetFactory(), new MockUrlWatcher());
- form.data('scope', new nglr.Scope({A:{text:"t1", textarea:"t2", radio:"r", checkbox:"c", select:"S"}}));
+ var a =
+ compile('<input type="tEXt" name="A.text"/>' +
+ '<textarea name="A.textarea"></textarea>' +
+ '<input name="A.radio" type="rADio" value="r"/>' +
+ '<input name="A.radioOff" type="rADio" value="r"/>' +
+ '<input name="A.checkbox" type="checkbox" value="c" />' +
+ '<input name="A.checkboxOff" type="checkbox" value="c" />' +
+ '<select name="A.select"><option>a</option><option value="S">b</option></select>');
+ var binder = a.binder, form = a.node;
+ a.scope.set('A', {text:"t1", textarea:"t2", radio:"r", checkbox:"c", select:"S"});
binder.compile();
binder.updateView();
assertEquals(form.find("input[type=text]").attr('value'), 't1');
@@ -349,21 +324,27 @@ BinderTest.prototype.testButtonElementActionExecutesInScope = function(){
};
BinderTest.prototype.testParseEmptyAnchor = function(){
- var binder = new nglr.Binder(null, null, new MockUrlWatcher());
+ var binder = compile("<div/>").binder;
+ var location = binder.location;
var anchor = binder.anchor;
- binder.parseAnchor("a#x=1");
+ location.url = "a#x=1";
+ binder.parseAnchor();
assertEquals(1, binder.anchor.x);
- binder.parseAnchor("a#");
+ location.url = "a#";
+ binder.parseAnchor();
assertTrue("old values did not get removed", !binder.anchor.x);
assertTrue("anchor gor replaced", anchor === binder.anchor);
assertEquals('undefined', typeof (anchor[""]));
};
BinderTest.prototype.testParseAnchor = function(){
- var binder = new nglr.Binder(null, null, new MockUrlWatcher());
- binder.parseAnchor("a#x=1");
+ var binder = compile("<div/>").binder;
+ var location = binder.location;
+ location.url = "a#x=1";
+ binder.parseAnchor();
assertEquals(binder.anchor.x, "1");
- binder.parseAnchor("a#a=b&c=%20&d");
+ location.url = "a#a=b&c=%20&d";
+ binder.parseAnchor();
assertEquals(binder.anchor.a, 'b');
assertEquals(binder.anchor.c, ' ');
assertTrue(binder.anchor.d !== null);
@@ -371,32 +352,30 @@ BinderTest.prototype.testParseAnchor = function(){
};
BinderTest.prototype.testWriteAnchor = function(){
- var binder = new nglr.Binder(null, null, new MockUrlWatcher());
- binder.urlWatcher.setUrl('a');
+ var binder = compile("<div/>").binder;
+ binder.location.set('a');
binder.anchor.a = 'b';
binder.anchor.c = ' ';
binder.anchor.d = true;
binder.updateAnchor();
- assertEquals(binder.urlWatcher.getUrl(), "a#a=b&c=%20&d");
+ assertEquals(binder.location.get(), "a#a=b&c=%20&d");
};
BinderTest.prototype.testWriteAnchorAsPartOfTheUpdateView = function(){
- var binder = new nglr.Binder(html("<div/>")[0], null, new MockUrlWatcher());
- binder.urlWatcher.setUrl('a');
- $(binder.doc).data('scope', new nglr.Scope());
+ var binder = compile("<div/>").binder;
+ binder.location.set('a');
binder.anchor.a = 'b';
binder.updateView();
- assertEquals(binder.urlWatcher.getUrl(), "a#a=b");
+ assertEquals(binder.location.get(), "a#a=b");
};
BinderTest.prototype.testRepeaterUpdateBindings = function(){
- var form = html('<ul><LI ng-repeat="item in model.items" ng-bind="item.a"/></ul>');
- var binder = new nglr.Binder(form.get(0), null, new MockUrlWatcher());
+ var a = compile('<ul><LI ng-repeat="item in model.items" ng-bind="item.a"/></ul>');
+ var form = a.node;
var items = [{a:"A"}, {a:"B"}];
- form.data('scope', new nglr.Scope({model:{items:items}}));
- binder.compile();
+ a.scope.set('model', {items:items});
- binder.updateView();
+ a.binder.updateView();
assertEquals('<ul>' +
'<#comment></#comment>' +
'<li ng-bind="item.a" ng-repeat-index="0">A</li>' +
@@ -404,7 +383,7 @@ BinderTest.prototype.testRepeaterUpdateBindings = function(){
'</ul>', form.sortedHtml());
items.unshift({a:'C'});
- binder.updateView();
+ a.binder.updateView();
assertEquals('<ul>' +
'<#comment></#comment>' +
'<li ng-bind="item.a" ng-repeat-index="0">C</li>' +
@@ -413,7 +392,7 @@ BinderTest.prototype.testRepeaterUpdateBindings = function(){
'</ul>', form.sortedHtml());
items.shift();
- binder.updateView();
+ a.binder.updateView();
assertEquals('<ul>' +
'<#comment></#comment>' +
'<li ng-bind="item.a" ng-repeat-index="0">A</li>' +
@@ -422,15 +401,13 @@ BinderTest.prototype.testRepeaterUpdateBindings = function(){
};
BinderTest.prototype.testRepeaterContentDoesNotBind = function(){
- var form = html('<ul><LI ng-repeat="item in model.items"><span ng-bind="item.a"/></li></ul>');
- form.data('scope', new nglr.Scope({model:{items:[{a:"A"}]}}));
- var binder = new nglr.Binder(form.get(0), null, new MockUrlWatcher());
- binder.compile();
- binder.updateView();
+ var a = compile('<ul><LI ng-repeat="item in model.items"><span ng-bind="item.a"/></li></ul>');
+ a.scope.set('model', {items:[{a:"A"}]});
+ a.binder.updateView();
assertEquals('<ul>' +
'<#comment></#comment>' +
'<li ng-repeat-index="0"><span ng-bind="item.a">A</span></li>' +
- '</ul>', form.sortedHtml());
+ '</ul>', a.node.sortedHtml());
};
BinderTest.prototype.testShouldBindActionsOnRepeaterClone = function(){
@@ -450,9 +427,9 @@ BinderTest.prototype.testRepeaterInputContentDoesNotBind = function(){
var form =
html('<ul><LI repeater="item in model.items">' +
'<input type="text" name="item.a" value="OLD"/></li></ul>');
- var binder = new nglr.Binder(form.get(0), null, new MockUrlWatcher());
+ var binder = new Binder(form.get(0), null, new MockLocation());
var items = [{a:"A"}];
- form.data('scope', new nglr.Scope({model:{items:items}}));
+ form.data('scope', new Scope({model:{items:items}}));
assertEquals(form.find(":input").attr("value"), "OLD");
};
@@ -492,9 +469,9 @@ BinderTest.prototype.testDoNotOverwriteCustomAction = function(){
BinderTest.prototype.testReplaceFileUploadWithSwf = function(){
expectAsserts(1);
var form = jQuery("body").append('<div id="testTag"><input type="file"></div>');
- form.data('scope', new nglr.Scope());
+ form.data('scope', new Scope());
var factory = {};
- var binder = new nglr.Binder(form.get(0), factory, new MockUrlWatcher());
+ var binder = new Binder(form.get(0), factory, new MockLocation());
factory.createController = function(node){
assertEquals(node.attr('type'), 'file');
return {updateModel:function(){}};
@@ -504,11 +481,11 @@ BinderTest.prototype.testReplaceFileUploadWithSwf = function(){
};
BinderTest.prototype.testRepeaterAdd = function(){
- var doc = $('<div><input type="text" name="item.x" ng-repeat="item in items"></div>');
- var binder = new nglr.Binder(doc[0], new nglr.WidgetFactory(), new MockUrlWatcher());
- doc.data('scope', new nglr.Scope({items:[{x:'a'}, {x:'b'}], $binder:binder}));
- binder.compile();
- binder.updateView();
+ var c = compile('<div><input type="text" name="item.x" ng-repeat="item in items"></div>');
+ var doc = c.node;
+ c.scope.set('items', [{x:'a'}, {x:'b'}]);
+ c.binder.compile();
+ c.binder.updateView();
assertEquals('a', doc.find(':input')[0].value);
assertEquals('b', doc.find(':input')[1].value);
@@ -519,64 +496,55 @@ BinderTest.prototype.testRepeaterAdd = function(){
};
BinderTest.prototype.testIfTextBindingThrowsErrorDecorateTheSpan = function(){
- var doc = $('<div>{{error.throw()}}</div>');
- var scope = new nglr.Scope();
- doc.data('scope', scope);
- var binder = new nglr.Binder(doc[0], new nglr.WidgetFactory(), new MockUrlWatcher());
- binder.compile();
+ var a = compile('<div>{{error.throw()}}</div>');
+ var doc = a.node.find('div');
- scope.set('error.throw', function(){throw "ErrorMsg1";});
- binder.updateView();
+ a.scope.set('error.throw', function(){throw "ErrorMsg1";});
+ a.binder.updateView();
var span = doc.find('span');
assertTrue(span.hasClass('ng-exception'));
- assertEquals('ErrorMsg1', nglr.fromJson(span.text()));
+ assertEquals('ErrorMsg1', fromJson(span.text()));
assertEquals('"ErrorMsg1"', span.attr('ng-error'));
- scope.set('error.throw', function(){throw "MyError";});
- binder.updateView();
+ a.scope.set('error.throw', function(){throw "MyError";});
+ a.binder.updateView();
span = doc.find('span');
assertTrue(span.hasClass('ng-exception'));
assertTrue(span.text(), span.text().match('MyError') !== null);
assertEquals('"MyError"', span.attr('ng-error'));
- scope.set('error.throw', function(){return "ok";});
- binder.updateView();
+ a.scope.set('error.throw', function(){return "ok";});
+ a.binder.updateView();
assertFalse(span.hasClass('ng-exception'));
assertEquals('ok', span.text());
assertEquals(null, span.attr('ng-error'));
};
BinderTest.prototype.testIfAttrBindingThrowsErrorDecorateTheSpan = function(){
- var doc = $('<div attr="before {{error.throw()}} after"/>');
- var scope = new nglr.Scope();
- doc.data('scope', scope);
- var binder = new nglr.Binder(doc[0], new nglr.WidgetFactory(), new MockUrlWatcher());
- binder.compile();
+ var a = compile('<div attr="before {{error.throw()}} after"></div>');
+ var doc = a.node.find("div");
- scope.set('error.throw', function(){throw "ErrorMsg";});
- binder.updateView();
+ a.scope.set('error.throw', function(){throw "ErrorMsg";});
+ a.binder.updateView();
assertTrue('ng-exception', doc.hasClass('ng-exception'));
assertEquals('before ["ErrorMsg"] after', doc.attr('attr'));
assertEquals('"ErrorMsg"', doc.attr('ng-error'));
- scope.set('error.throw', function(){ return 'X';});
- binder.updateView();
+ a.scope.set('error.throw', function(){ return 'X';});
+ a.binder.updateView();
assertFalse('!ng-exception', doc.hasClass('ng-exception'));
assertEquals('before X after', doc.attr('attr'));
assertEquals(null, doc.attr('ng-error'));
+
};
BinderTest.prototype.testNestedRepeater = function() {
- var doc = html('<div ng-repeat="m in model" name="{{m.name}}">' +
+ var a = compile('<div ng-repeat="m in model" name="{{m.name}}">' +
'<ul name="{{i}}" ng-repeat="i in m.item"></ul>' +
'</div>');
- var scope = new nglr.Scope();
- doc.data('scope', scope);
- var binder = new nglr.Binder(doc[0], new nglr.WidgetFactory(), new MockUrlWatcher());
- binder.compile();
- scope.set('model', [{name:'a', item:['a1', 'a2']}, {name:'b', item:['b1', 'b2']}]);
- binder.updateView();
+ a.scope.set('model', [{name:'a', item:['a1', 'a2']}, {name:'b', item:['b1', 'b2']}]);
+ a.binder.updateView();
assertEquals(
//'<#comment></#comment>'+
@@ -589,88 +557,71 @@ BinderTest.prototype.testNestedRepeater = function() {
'<#comment></#comment>'+
'<ul name="b1" ng-bind-attr="{"name":"{{i}}"}" ng-repeat-index="0"></ul>'+
'<ul name="b2" ng-bind-attr="{"name":"{{i}}"}" ng-repeat-index="1"></ul>'+
- '</div>', doc.sortedHtml());
+ '</div>', a.node.sortedHtml());
};
BinderTest.prototype.testRadioButtonGetsPrefixed = function () {
- var doc = html('<input ng-repeat="m in model" type="radio" name="m.a" value="on"/>');
- var scope = new nglr.Scope();
- doc.data('scope', scope);
- var binder = new nglr.Binder(doc[0], new nglr.WidgetFactory(), new MockUrlWatcher());
- binder.compile();
-
- scope.set('model', ['a1', 'a2']);
- binder.updateView();
+ var a = compile('<input ng-repeat="m in model" type="radio" name="m.a" value="on"/>');
+ a.scope.set('model', ['a1', 'a2']);
+ a.binder.updateView();
assertEquals(
//'<#comment></#comment>'+
'<input name="0:m.a" ng-repeat-index="0" type="radio" value="on"></input>'+
'<input name="1:m.a" ng-repeat-index="1" type="radio" value="on"></input>',
- doc.sortedHtml());
+ a.node.sortedHtml());
};
BinderTest.prototype.testHideBindingExpression = function() {
- var doc = html('<div ng-hide="hidden == 3"/>');
- var scope = new nglr.Scope();
- doc.data('scope', scope);
- var binder = new nglr.Binder(doc[0], new nglr.WidgetFactory(), new MockUrlWatcher());
- binder.compile();
+ var a = compile('<div ng-hide="hidden == 3"/>');
- scope.set('hidden', 3);
- binder.updateView();
+ a.scope.set('hidden', 3);
+ a.binder.updateView();
- assertHidden(doc.children());
+ assertHidden(a.node.children());
- scope.set('hidden', 2);
- binder.updateView();
+ a.scope.set('hidden', 2);
+ a.binder.updateView();
- assertVisible(doc.children());
+ assertVisible(a.node.children());
};
BinderTest.prototype.testHideBinding = function() {
- var doc = html('<div ng-hide="hidden"/>');
- var scope = new nglr.Scope();
- doc.data('scope', scope);
- var binder = new nglr.Binder(doc[0], new nglr.WidgetFactory(), new MockUrlWatcher());
- binder.compile();
+ var c = compile('<div ng-hide="hidden"/>');
- scope.set('hidden', 'true');
- binder.updateView();
+ c.scope.set('hidden', 'true');
+ c.binder.updateView();
- assertHidden(doc.children());
+ assertHidden(c.node.children());
- scope.set('hidden', 'false');
- binder.updateView();
+ c.scope.set('hidden', 'false');
+ c.binder.updateView();
- assertVisible(doc.children());
+ assertVisible(c.node.children());
- scope.set('hidden', '');
- binder.updateView();
+ c.scope.set('hidden', '');
+ c.binder.updateView();
- assertVisible(doc.children());
+ assertVisible(c.node.children());
};
BinderTest.prototype.testShowBinding = function() {
- var doc = html('<div ng-show="show"/>');
- var scope = new nglr.Scope();
- doc.data('scope', scope);
- var binder = new nglr.Binder(doc[0], new nglr.WidgetFactory(), new MockUrlWatcher());
- binder.compile();
+ var c = compile('<div ng-show="show"/>');
- scope.set('show', 'true');
- binder.updateView();
+ c.scope.set('show', 'true');
+ c.binder.updateView();
- assertVisible(doc.children());
+ assertVisible(c.node.children());
- scope.set('show', 'false');
- binder.updateView();
+ c.scope.set('show', 'false');
+ c.binder.updateView();
- assertHidden(doc.children());
+ assertHidden(c.node.children());
- scope.set('show', '');
- binder.updateView();
+ c.scope.set('show', '');
+ c.binder.updateView();
- assertHidden(doc.children());
+ assertHidden(c.node.children());
};
BinderTest.prototype.testBindClassUndefined = function() {
@@ -683,22 +634,18 @@ BinderTest.prototype.testBindClassUndefined = function() {
};
BinderTest.prototype.testBindClass = function() {
- var doc = html('<div ng-class="class"/>');
- var scope = new nglr.Scope();
- doc.data('scope', scope);
- var binder = new nglr.Binder(doc[0], new nglr.WidgetFactory(), new MockUrlWatcher());
- binder.compile();
+ var c = compile('<div ng-class="class"/>');
- scope.set('class', 'testClass');
- binder.updateView();
+ c.scope.set('class', 'testClass');
+ c.binder.updateView();
- assertEquals(doc.sortedHtml(),
+ assertEquals(c.node.sortedHtml(),
'<div class="testClass" ng-class="class"></div>');
- scope.set('class', ['a', 'b']);
- binder.updateView();
+ c.scope.set('class', ['a', 'b']);
+ c.binder.updateView();
- assertEquals(doc.sortedHtml(),
+ assertEquals(c.node.sortedHtml(),
'<div class="a,b" ng-class="class"></div>');
};
@@ -712,21 +659,17 @@ BinderTest.prototype.testBindClassEvenOdd = function() {
};
BinderTest.prototype.testBindStyle = function() {
- var doc = html('<div ng-style="style"/>');
- var scope = new nglr.Scope();
- doc.data('scope', scope);
- var binder = new nglr.Binder(doc[0], new nglr.WidgetFactory(), new MockUrlWatcher());
- binder.compile();
+ var c = compile('<div ng-style="style"/>');
- scope.eval('style={color:"red"}');
- binder.updateView();
+ c.scope.eval('style={color:"red"}');
+ c.binder.updateView();
- assertEquals("red", doc.find('div').css('color'));
+ assertEquals("red", c.node.find('div').css('color'));
- scope.eval('style={}');
- binder.updateView();
+ c.scope.eval('style={}');
+ c.binder.updateView();
- assertEquals(doc.sortedHtml(), '<div ng-style="style"></div>');
+ assertEquals(c.node.sortedHtml(), '<div ng-style="style"></div>');
};
BinderTest.prototype.testActionOnAHrefThrowsError = function(){
@@ -734,7 +677,7 @@ BinderTest.prototype.testActionOnAHrefThrowsError = function(){
var state = compile('<a ng-action="throw {a:\'abc\', b:2};">Add Phone</a>', model);
var input = state.node.find('a');
input.click();
- assertEquals('abc', nglr.fromJson(input.attr('ng-error')).a);
+ assertEquals('abc', fromJson(input.attr('ng-error')).a);
assertNotNull(input.data('qtip'));
assertTrue("should have an error class", input.hasClass('ng-exception'));
@@ -798,7 +741,7 @@ BinderTest.prototype.testDissableAutoSubmit = function() {
BinderTest.prototype.testSettingAnchorToNullOrUndefinedRemovesTheAnchorFromURL = function() {
var c = compile('');
- c.binder.urlWatcher.setUrl("http://server/#a=1&b=2");
+ c.binder.location.set("http://server/#a=1&b=2");
c.binder.parseAnchor();
assertEquals('1', c.binder.anchor.a);
assertEquals('2', c.binder.anchor.b);
@@ -806,7 +749,7 @@ BinderTest.prototype.testSettingAnchorToNullOrUndefinedRemovesTheAnchorFromURL =
c.binder.anchor.a = null;
c.binder.anchor.b = null;
c.binder.updateAnchor();
- assertEquals('http://server/#', c.binder.urlWatcher.getUrl());
+ assertEquals('http://server/#', c.binder.location.get());
};
BinderTest.prototype.testFillInOptionValueWhenMissing = function() {
@@ -876,21 +819,30 @@ BinderTest.prototype.testItShouldCallListenersWhenAnchorChanges = function() {
log += oldValue + "->" + newValue + ";";
});
assertEquals(0, c.scope.get("count"));
- c.binder.onUrlChange("#counter=1");
+ c.binder.location.url = "#counter=1";
+ c.binder.onUrlChange();
assertEquals(1, c.scope.get("count"));
- c.binder.onUrlChange("#counter=1");
+
+ c.binder.location.url = "#counter=1";
+ c.binder.onUrlChange();
assertEquals(1, c.scope.get("count"));
- c.binder.onUrlChange("#counter=2");
+
+ c.binder.location.url = "#counter=2";
+ c.binder.onUrlChange();
assertEquals(2, c.scope.get("count"));
- c.binder.onUrlChange("#counter=2");
+
+ c.binder.location.url = "#counter=2";
+ c.binder.onUrlChange();
assertEquals(2, c.scope.get("count"));
- c.binder.onUrlChange("#");
+
+ c.binder.location.url = "#";
+ c.binder.onUrlChange();
assertEquals("undefined->1;1->2;2->undefined;", log);
assertEquals(3, c.scope.get("count"));
};
BinderTest.prototype.testParseQueryString = function(){
- var binder = new nglr.Binder();
+ var binder = new Binder();
assertJsonEquals({"a":"1"}, binder.parseQueryString("a=1"));
assertJsonEquals({"a":"1", "b":"two"}, binder.parseQueryString("a=1&b=two"));
assertJsonEquals({}, binder.parseQueryString(""));
@@ -905,8 +857,8 @@ BinderTest.prototype.testParseQueryString = function(){
BinderTest.prototype.testSetBinderAnchorTriggersListeners = function(){
expectAsserts(2);
var doc = html("<div/>")[0];
- var binder = new nglr.Binder(doc, null, new MockUrlWatcher());
- var scope = new nglr.Scope({$binder:binder, $anchor:binder.anchor});
+ var binder = new Binder(doc, null, null, new MockLocation());
+ var scope = new Scope({$binder:binder, $anchor:binder.anchor});
jQuery(doc).data('scope', scope);
scope.addWatchListener("$anchor.name", function(newVal, oldVal) {