// Copyright 2006 Google Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or // implied. See the License for the specific language governing // permissions and limitations under the License. /** * @author Steffen Meschkat (mesch@google.com) * @fileoverview Unittest and examples for jstemplates. */ function jstWrap(data, template) { return jstProcess(new JsEvalContext(data), template); } function testJstSelect() { // Template cardinality from jsselect. var t = document.getElementById('t1'); var d = { items: [ 'A', 'B', 'C', '' ] } jstWrap(d, t); var h = t.innerHTML; var clone = domCloneNode(t); assertTrue(/>A<\/div>/.test(h)); assertTrue(/>B<\/div>/.test(h)); assertTrue(/>C<\/div>/.test(h)); assertTrue(/><\/div>/.test(h)); // Reprocessing with identical data. jstWrap(d, t); assertAttributesMatch(t, clone); // Reprocessing with changed data. d.items[1] = 'BB'; jstWrap(d, t); h = t.innerHTML; assertTrue(/>A<\/div>/.test(h)); assertFalse(/>B<\/div>/.test(h)); assertTrue(/>BB<\/div>/.test(h)); assertTrue(/>C<\/div>/.test(h)); // Reprocessing with dropped data. d.items.pop(); d.items.pop(); jstWrap(d, t); h = t.innerHTML; assertTrue(/>A<\/div>/.test(h)); assertTrue(/>BB<\/div>/.test(h)); assertFalse(/>C<\/div>/.test(h)); assertFalse(/><\/div>/.test(h)); // Reprocessing with dropped data, once more. d.items.pop(); jstWrap(d, t); h = t.innerHTML; assertTrue(/>A<\/div>/.test(h)); assertFalse(/>BB<\/div>/.test(h)); assertFalse(/>C<\/div>/.test(h)); // Reprocessing with empty data -- the last template instance is // preserved, and only hidden. d.items.pop(); jstWrap(d, t); assertTrue(/>A<\/div>/.test(h)); assertFalse(/>BB<\/div>/.test(h)); assertFalse(/>C<\/div>/.test(h)); // Reprocessing with added data. d.items.push('D'); jstWrap(d, t); h = t.innerHTML; assertFalse(/>A<\/div>/.test(h)); assertTrue(/>D<\/div>/.test(h)); } function testJstDisplay() { var t = document.getElementById('t2'); var d = { display: true } jstWrap(d, t); var h = t.innerHTML; assertFalse(/display:\s*none/.test(h)); d.display = false; jstWrap(d, t); h = t.innerHTML; assertTrue(/display:\s*none/.test(h)); // Check that 'this' within js expressions is the template node t = document.getElementById('t2a'); d = { showId: 'x' }; jstWrap(d, t); h = t.innerHTML; assertFalse(/display:\s*none/.test(h)); d.showId = 'y'; jstWrap(d, t); h = t.innerHTML; assertTrue(/display:\s*none/.test(h)); } function stringContains(str, sub) { return str.indexOf(sub) != -1; } function testJseval() { var data = {}; var counter = 0; var ctx = new JsEvalContext(data); ctx.setVariable("callback1", function() { ++counter; }); ctx.setVariable("callback2", function() { counter *= 2; }); jstProcess(ctx, document.getElementById('testJseval1')); assertEquals("testJseval1", 1, counter); jstProcess(ctx, document.getElementById('testJseval2')); assertEquals("testJseval2", 4, counter); } function testJstValues() { var t = document.getElementById('t3'); var d = {}; jstWrap(d, t); var h = t.innerHTML; assertTrue(stringContains(h, 'http://maps.google.com/')); var t3a = document.getElementById('t3a'); assertEquals('http://maps.google.com/', t3a.foo.bar.baz); assertEquals('http://maps.google.com/', t3a.bar); assertEquals('red', t3a.style.backgroundColor); } function testJstTransclude() { var t = document.getElementById('t4'); var p = document.getElementById('parent'); var d = {}; jstWrap(d, t); var h = p.innerHTML; assertTrue(h, stringContains(h, 'http://maps.google.com/')); } function assertAttributesMatch(first, second) { assertEquals('assertAttributesMatch: number of child nodes', jsLength(first.childNodes), jsLength(second.childNodes)); var b = second.firstChild; for (var a = first.firstChild; a; a = a.nextSibling) { var att = a.attributes; if (att) { assertTrue(b.attributes != null); assertEquals('assertAttributesMatch: number of attribute nodes', att.length, b.attributes.length); for (var i = 0; i < jsLength(att); i++) { var a = att[i]; assertEquals('assertAttributesMatch: value of attribute ' + a.name, a.value, b.getAttribute(a.name)); } } else { assertNull(b.attributes); } b = b.nextSibling; } } function testJsskip() { var div = domCreateElement(document, "DIV"); div.innerHTML = [ '