BinderTest = TestCase('BinderTest'); function compile(content, initialScope, config) { var h = html(content); config = config || {autoSubmit:true}; var scope = new nglr.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); scope.set("$anchor", binder.anchor); binder.entity(scope); binder.compile(); return {node:h, binder:binder, scope:scope}; } function compileToHtml(content) { return compile(content).node.sortedHtml(); } BinderTest.prototype.testParseTextWithNoBindings = function(){ var parts = nglr.Binder.parseBindings("a"); assertEquals(parts.length, 1); assertEquals(parts[0], "a"); assertTrue(!nglr.Binder.binding(parts[0])); }; BinderTest.prototype.testParseEmptyText = function(){ var parts = nglr.Binder.parseBindings(""); assertEquals(parts.length, 1); assertEquals(parts[0], ""); assertTrue(!nglr.Binder.binding(parts[0])); }; BinderTest.prototype.testParseInnerBinding = function(){ var parts = nglr.Binder.parseBindings("a{{b}}c"); assertEquals(parts.length, 3); assertEquals(parts[0], "a"); assertTrue(!nglr.Binder.binding(parts[0])); assertEquals(parts[1], "{{b}}"); assertEquals(nglr.Binder.binding(parts[1]), "b"); assertEquals(parts[2], "c"); assertTrue(!nglr.Binder.binding(parts[2])); }; BinderTest.prototype.testParseEndingBinding = function(){ var parts = nglr.Binder.parseBindings("a{{b}}"); assertEquals(parts.length, 2); assertEquals(parts[0], "a"); assertTrue(!nglr.Binder.binding(parts[0])); assertEquals(parts[1], "{{b}}"); assertEquals(nglr.Binder.binding(parts[1]), "b"); }; BinderTest.prototype.testParseBeggingBinding = function(){ var parts = nglr.Binder.parseBindings("{{b}}c"); assertEquals(parts.length, 2); assertEquals(parts[0], "{{b}}"); assertEquals(nglr.Binder.binding(parts[0]), "b"); assertEquals(parts[1], "c"); assertTrue(!nglr.Binder.binding(parts[1])); }; BinderTest.prototype.testParseLoanBinding = function(){ var parts = nglr.Binder.parseBindings("{{b}}"); assertEquals(parts.length, 1); assertEquals(parts[0], "{{b}}"); assertEquals(nglr.Binder.binding(parts[0]), "b"); }; BinderTest.prototype.testParseTwoBindings = function(){ var parts = nglr.Binder.parseBindings("{{b}}{{c}}"); assertEquals(parts.length, 2); assertEquals(parts[0], "{{b}}"); assertEquals(nglr.Binder.binding(parts[0]), "b"); assertEquals(parts[1], "{{c}}"); assertEquals(nglr.Binder.binding(parts[1]), "c"); }; BinderTest.prototype.testParseTwoBindingsWithTextInMiddle = function(){ var parts = nglr.Binder.parseBindings("{{b}}x{{c}}"); assertEquals(parts.length, 3); assertEquals(parts[0], "{{b}}"); assertEquals(nglr.Binder.binding(parts[0]), "b"); assertEquals(parts[1], "x"); assertTrue(!nglr.Binder.binding(parts[1])); assertEquals(parts[2], "{{c}}"); assertEquals(nglr.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}}')); assertEquals(parts.length, 3); assertEquals(parts[0], '"X\nY'); assertEquals(parts[1], '{{A\nB}}'); assertEquals(parts[2], 'C\nD"'); }; BinderTest.prototype.testHasBinding = function(){ assertTrue(nglr.Binder.hasBinding("{{a}}")); assertTrue(!nglr.Binder.hasBinding("a")); assertTrue(nglr.Binder.hasBinding("{{b}}x{{c}}")); }; BinderTest.prototype.tearDown = function(){ jQuery("*", document).die(); jQuery(document).unbind(); }; BinderTest.prototype.testChangingTextfieldUpdatesModel = function(){ var state = compile('', {model:{}}); state.binder.updateView(); assertEquals('abc', state.scope.get('model').price); }; BinderTest.prototype.testChangingTextareaUpdatesModel = function(){ var form = html(''); 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'); }; BinderTest.prototype.testChangingRadioUpdatesModel = function(){ var form = html('' + ''); 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'); }; BinderTest.prototype.testChangingCheckboxUpdatesModel = function(){ var form = html(''); 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); }; BinderTest.prototype.testBindUpdate = function() { var c = compile('
'); c.binder.updateView(); assertEquals(123, c.scope.get('a')); }; BinderTest.prototype.testChangingSelectNonSelectedUpdatesModel = function(){ var form = html(''); 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); }; BinderTest.prototype.testChangingMultiselectUpdatesModel = function(){ var form = html(''); 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); }; BinderTest.prototype.testChangingSelectSelectedUpdatesModel = function(){ var form = html(''); 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'); }; BinderTest.prototype.testExecuteInitialization = function() { var form = html('
'); var scope = new nglr.Scope(); form.data('scope', scope); var binder = new nglr.Binder(form.get(0)); binder.executeInit(); assertEquals(scope.get('a'), 123); }; BinderTest.prototype.testExecuteInitializationStatements = function() { var form = html('
'); var scope = new nglr.Scope(); form.data('scope', scope); var binder = new nglr.Binder(form.get(0)); binder.executeInit(); assertEquals(scope.get('a'), 123); assertEquals(scope.get('b'), 345); }; BinderTest.prototype.testApplyTextBindings = function(){ var form = html('
x
'); 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()); }; BinderTest.prototype.testReplaceBindingInTextWithSpan = function() { assertEquals(compileToHtml("a{{b}}c"), 'ac'); assertEquals(compileToHtml("{{b}}"), ''); }; BinderTest.prototype.testReplaceBindingCreatesCorrectNumberOfWidgets = function() { var h = html("space{{a}}{{a}}a{{a}}{{a}}"); h.data('scope', new nglr.Scope()); var binder = new nglr.Binder(h.get(0), new nglr.WidgetFactory()); binder.compile(); assertEquals(4, h.scope().widgets.length); }; BinderTest.prototype.testBindingSpaceConfusesIE = function() { if (!nglr.msie) return; var span = document.createElement("span"); span.innerHTML = ' '; var nbsp = span.firstChild.nodeValue; assertEquals( ''+nbsp+'', compileToHtml("{{a}} {{b}}")); assertEquals( ''+nbsp+'x '+nbsp+'(', compileToHtml("{{A}} x {{B}} ({{C}})")); }; BinderTest.prototype.testBindingOfAttributes = function() { var form = html(""); form.data('scope', new nglr.Scope()); var binder = new nglr.Binder(form.get(0)); binder.compile(); var attrbinding = form.find("a").attr("ng-bind-attr"); var bindings = nglr.fromJson(attrbinding); assertEquals("http://s/a{{b}}c", decodeURI(bindings.href)); assertTrue(!bindings.foo); }; BinderTest.prototype.testMarkMultipleAttributes = function() { var form = html(""); form.data('scope', new nglr.Scope()); var binder = new nglr.Binder(form.get(0)); binder.compile(); var attrbinding = form.find("a").attr("ng-bind-attr"); var bindings = nglr.fromJson(attrbinding); assertEquals(decodeURI(bindings.href), "http://s/a{{b}}c"); assertEquals(bindings.foo, "{{d}}"); }; BinderTest.prototype.testAttributesNoneBound = function() { var form = html(""); form.data('scope', new nglr.Scope()); var binder = new nglr.Binder(form.get(0)); binder.compile(); var a = form.find("a"); assertEquals(a.get(0).nodeName, "A"); assertTrue(!a.attr("ng-bind-attr")); }; BinderTest.prototype.testExistingAttrbindingIsAppended = function() { var form = html(""); form.data('scope', new nglr.Scope()); var binder = new nglr.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(''); form.data('scope', new nglr.Scope({a:1, b:2})); var binder = new nglr.Binder(form.get(0), null, new MockUrlWatcher()); binder.compile(); binder.updateView(); var a = form.find("a"); assertEquals(a.attr('a'), 'a'); assertEquals(a.attr('b'), 'a+b=3'); }; BinderTest.prototype.testInputsAreUpdated = function(){ var form = html('' + '