How to use createTestHTML method in wpt

Best JavaScript code snippet using wpt

polymer-expressions-tests.js

Source:polymer-expressions-tests.js Github

copy

Full Screen

1// Copyright (c) 2014 The Polymer Project Authors. All rights reserved.2// This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt3// The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt4// The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt5// Code distributed by Google as part of the polymer project is also6// subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt7suite('PolymerExpressions', function() {8 var testDiv, originalConsoleError, errors;9 var getExpression;10 suiteSetup(function() {11 getExpression = PolymerExpressions.getExpression;12 });13 function clearAllTemplates(node) {14 if (node instanceof HTMLTemplateElement || node.iterator_)15 node.clear();16 for (var child = node.firstChild; child; child = child.nextSibling)17 clearAllTemplates(child);18 }19 setup(function() {20 errors = [];21 originalConsoleError = console.error;22 console.error = function() {23 errors.push(Array.prototype.slice.call(arguments));24 };25 testDiv = document.body.appendChild(document.createElement('div'));26 Observer._errorThrownDuringCallback = false;27 });28 teardown(function() {29 errors = [];30 console.error = originalConsoleError;31 assert.isFalse(!!Observer._errorThrownDuringCallback);32 document.body.removeChild(testDiv);33 clearAllTemplates(testDiv);34 Platform.performMicrotaskCheckpoint();35 assert.strictEqual(0, Observer._allObserversCount);36 });37 function then(fn) {38 setTimeout(function() {39 Platform.performMicrotaskCheckpoint();40 fn();41 }, 0);42 return {43 then: function(next) {44 return then(next);45 }46 };47 }48 function dispatchEvent(type, target) {49 var event = document.createEvent('Event');50 event.initEvent(type, true, false);51 target.dispatchEvent(event);52 Platform.performMicrotaskCheckpoint();53 }54 function hasClass(node, className) {55 return node.className.split(' ').some(function(name) {56 return name === className;57 });58 }59 function assertHasClass(node, className) {60 return assert.isTrue(hasClass(node, className))61 }62 function assertLacksClass(node, className) {63 return assert.isFalse(hasClass(node, className))64 }65 function createTestHtml(s) {66 var div = document.createElement('div');67 div.innerHTML = s;68 testDiv.appendChild(div);69 HTMLTemplateElement.forAllTemplatesFrom_(div, function(template) {70 HTMLTemplateElement.decorate(template);71 });72 return div;73 }74 function recursivelySetTemplateModel(node, model, delegate) {75 HTMLTemplateElement.forAllTemplatesFrom_(node, function(template) {76 delegate = delegate|| new PolymerExpressions;77 // testing filters78 delegate.hex = function(value) {79 return Number(value).toString(16);80 };81 // toModel as property on toDOM function82 delegate.hex.toModel = function(value) {83 return parseInt(value, 16);84 };85 delegate.toFixed = function(value, fractions) {86 return Number(value).toFixed(fractions);87 };88 delegate.upperCase = function(value) {89 return String(value).toUpperCase();90 };91 delegate.incrProp = function(value, obj, propName) {92 obj[propName]++;93 };94 // filter as full object with toDOM and toModel properties95 delegate.plusN = {96 toDOM: function(value, n) {97 return Number(value) + n;98 },99 toModel: function(value, n) {100 return Number(value) - n;101 }102 };103 delegate.staticSort = function(list) {104 var copy = list.slice(0);105 copy.sort();106 return copy;107 };108 template.bindingDelegate = delegate;109 template.model = model;110 });111 }112 function objToString(str) {113 return {114 toString: function() {115 return str;116 }117 };118 }119 test('ClassName Singular', function(done) {120 var div = createTestHtml(121 '<template bind><div class="{{ {foo: bar} | tokenList }}">' +122 '</div></template>');123 var model = {bar: 1};124 recursivelySetTemplateModel(div, model);125 var target;126 then(function() {127 target = div.childNodes[1];128 assertHasClass(target, 'foo');129 model.bar = 0;130 }).then(function() {131 assertLacksClass(target, 'foo');132 done();133 });134 });135 test('ClassName Singular Static', function(done) {136 var div = createTestHtml(137 '<template bind><div class="[[ {foo: bar} | tokenList ]]">' +138 '</div></template>');139 var model = {bar: 1};140 recursivelySetTemplateModel(div, model);141 var target;142 then(function() {143 target = div.childNodes[1];144 assertHasClass(target, 'foo');145 model.bar = 0;146 }).then(function() {147 assertHasClass(target, 'foo');148 done();149 });150 });151 test('ClassName Multiple', function(done) {152 var div = createTestHtml(153 '<template bind>' +154 '<div class="{{ {foo: bar, baz: bat > 1, boo: bot.bam} ' +155 '| tokenList }}">' +156 '</div></template>');157 var model = {bar: 1, bat: 1, bot: { bam: 1 }};158 recursivelySetTemplateModel(div, model);159 var target;160 then(function() {161 target = div.childNodes[1];162 assert.strictEqual('foo boo', target.className);163 assertHasClass(target, 'foo');164 assertLacksClass(target, 'baz');165 assertHasClass(target, 'boo');166 model.bar = 0;167 model.bat = 2;168 }).then(function() {169 assert.strictEqual('baz boo', target.className);170 assertLacksClass(target, 'foo');171 assertHasClass(target, 'baz');172 assertHasClass(target, 'boo');173 done();174 });175 });176 test('ClassName Multiple - static', function(done) {177 var div = createTestHtml(178 '<template bind>' +179 '<div class="[[ {foo: bar, baz: bat > 1, boo: bot.bam} ' +180 '| tokenList ]]">' +181 '</div></template>');182 var model = {bar: 1, bat: 1, bot: { bam: 1 }};183 recursivelySetTemplateModel(div, model);184 var target;185 then(function() {186 target = div.childNodes[1];187 assert.strictEqual('foo boo', target.className);188 assertHasClass(target, 'foo');189 assertLacksClass(target, 'baz');190 assertHasClass(target, 'boo');191 model.bar = 0;192 model.bat = 2;193 }).then(function() {194 assert.strictEqual('foo boo', target.className);195 assertHasClass(target, 'foo');196 assertLacksClass(target, 'baz');197 assertHasClass(target, 'boo');198 done();199 });200 });201 test('tokenList', function(done) {202 var div = createTestHtml(203 '<template bind>' +204 '<div class="{{ object | tokenList }}">' +205 '</div></template>');206 var model = {207 object: {bar: 1, bat: 1, bot: {bam: 1}}208 };209 recursivelySetTemplateModel(div, model);210 var target;211 then(function() {212 target = div.childNodes[1];213 assert.strictEqual('bar bat bot', target.className);214 model.object = {bar: 1, bot: 1};215 }).then(function() {216 assert.strictEqual('bar bot', target.className);217 done();218 });219 });220 test('tokenList - static', function(done) {221 var div = createTestHtml(222 '<template bind>' +223 '<div class="[[ object | tokenList ]]">' +224 '</div></template>');225 var model = {226 object: {bar: 1, bat: 1, bot: {bam: 1}}227 };228 recursivelySetTemplateModel(div, model);229 var target;230 then(function() {231 target = div.childNodes[1];232 assert.strictEqual('bar bat bot', target.className);233 model.object = {bar: 1, bot: 1};234 }).then(function() {235 assert.strictEqual('bar bat bot', target.className);236 done();237 });238 });239 test('styleObject', function(done) {240 // IE removes invalid style attribute values so we use xstyle in this test.241 var div = createTestHtml(242 '<template bind>' +243 '<div xstyle="{{ object | styleObject }}">' +244 '</div></template>');245 var model = {246 object: {247 width: '100px',248 backgroundColor: 'blue',249 WebkitUserSelect: 'none'250 }251 };252 recursivelySetTemplateModel(div, model);253 var target;254 then(function() {255 target = div.childNodes[1];256 assert.strictEqual(target.getAttribute('xstyle'),257 'width: 100px; background-color: blue; -webkit-user-select: none');258 model.object = {259 left: '50px',260 whiteSpace: 'pre'261 };262 }).then(function() {263 assert.strictEqual(target.getAttribute('xstyle'),264 'left: 50px; white-space: pre');265 done();266 });267 });268 test('styleObject2', function(done) {269 // IE removes invalid style attribute values so we use xstyle in this test.270 var div = createTestHtml(271 '<template bind>' +272 '<div xstyle="{{ {width: w, backgroundColor: bc} | styleObject }}">' +273 '</div></template>');274 var model = {275 w: '100px',276 bc: 'blue'277 };278 recursivelySetTemplateModel(div, model);279 var target;280 then(function() {281 target = div.childNodes[1];282 assert.strictEqual(target.getAttribute('xstyle'),283 'width: 100px; background-color: blue');284 model.w = 0;285 }).then(function() {286 assert.strictEqual(target.getAttribute('xstyle'),287 'width: 0; background-color: blue');288 done();289 });290 });291 test('Named Scope Bind', function(done) {292 var div = createTestHtml(293 '<template bind>' +294 '<template bind="{{ foo.bar as baz }}">' +295 '{{ id }}:{{ baz.bat }}' +296 '</template>' +297 '</template>');298 var model = { id: 'id', foo: { bar: { bat: 'boo' }}};299 recursivelySetTemplateModel(div, model);300 then(function() {301 assert.strictEqual('id:boo', div.childNodes[2].textContent);302 done();303 });304 });305 test('Named Scope Repeat', function(done) {306 var div = createTestHtml(307 '<template bind>' +308 '<template repeat="{{ user in users }}">' +309 '{{ id }}:{{ user.name }}' +310 '</template>' +311 '</template>');312 var model = {313 id: 'id',314 users: [315 { name: 'Tim' },316 { name: 'Sally'}317 ]318 };319 recursivelySetTemplateModel(div, model);320 then(function() {321 assert.strictEqual('id:Tim', div.childNodes[2].textContent);322 assert.strictEqual('id:Sally', div.childNodes[3].textContent);323 done();324 });325 });326 test('Named Scope Repeat - semantic template', function(done) {327 var div = createTestHtml(328 '<template bind>' +329 '<table><tr template repeat="{{ user in users }}">' +330 '<td>{{ id }}:{{ user.name }}</td>' +331 '</tr></table>' +332 '</template>');333 var model = {334 id: 'id',335 users: [336 { name: 'Tim' },337 { name: 'Sally'}338 ]339 };340 recursivelySetTemplateModel(div, model);341 then(function() {342 var tbody = div.firstChild.nextSibling.firstChild;343 assert.strictEqual('id:Tim', tbody.childNodes[1].firstChild.textContent);344 assert.strictEqual('id:Sally',345 tbody.childNodes[2].firstChild.textContent);346 done();347 });348 });349 test('Named Scope Deep Nesting', function(done) {350 var div = createTestHtml(351 '<template bind>' +352 '<template repeat="{{ user in users }}">' +353 '{{ id }}:{{ user.name }}' +354 '<template repeat="{{ employee in user.employees }}">' +355 '{{ id }}:{{ user.name }}:{{ employee.name }}' +356 '</template>' +357 '</template>' +358 '</template>');359 var model = {360 id: 'id',361 users: [362 { name: 'Tim', employees: [{ name: 'Bob'}, { name: 'Sam'}]},363 { name: 'Sally', employees: [{ name: 'Steve' }]}364 ]365 };366 recursivelySetTemplateModel(div, model);367 then(function() {368 assert.strictEqual('id:Tim', div.childNodes[2].textContent);369 assert.strictEqual('id:Tim:Bob', div.childNodes[4].textContent);370 assert.strictEqual('id:Tim:Sam', div.childNodes[5].textContent);371 assert.strictEqual('id:Sally', div.childNodes[6].textContent);372 assert.strictEqual('id:Sally:Steve', div.childNodes[8].textContent);373 done();374 });375 });376 test('Named Scope Unnamed resets', function(done) {377 var div = createTestHtml(378 '<template bind>' +379 '<template bind="{{ foo as bar }}">' +380 '{{ bar.id }}' +381 '<template bind="{{ bar.bat }}">' +382 '{{ boo }}:{{ bar.id }}' +383 '</template>' +384 '</template>' +385 '</template>');386 var model = {387 foo: {388 id: 2,389 bat: {390 boo: 'bot'391 }392 },393 };394 recursivelySetTemplateModel(div, model);395 then(function() {396 assert.strictEqual('2', div.childNodes[2].textContent);397 assert.strictEqual('bot:', div.childNodes[4].textContent);398 done();399 });400 });401 test('Expressions Arithmetic, + - / *', function(done) {402 var div = createTestHtml(403 '<template bind>' +404 '{{ (a.b + c.d)/e - f * g.h }}' +405 '</template>');406 var model = {407 a: {408 b: 5409 },410 c: {411 d: 5412 },413 e: 2,414 f: 3,415 g: {416 h: 2417 }418 };419 recursivelySetTemplateModel(div, model);420 then(function() {421 assert.strictEqual('-1', div.childNodes[1].textContent);422 model.a.b = 11;423 model.f = -2;424 }).then(function() {425 assert.strictEqual('12', div.childNodes[1].textContent);426 done();427 });428 });429 test('Expressions Unary - +', function(done) {430 var div = createTestHtml(431 '<template bind>' +432 '{{ (-a.b) - (+c) }}' +433 '</template>');434 var model = {435 a: {436 b: 5437 },438 c: 3439 };440 recursivelySetTemplateModel(div, model);441 then(function() {442 assert.strictEqual('-8', div.childNodes[1].textContent);443 model.a.b = -1;444 model.c = -4;445 }).then(function() {446 assert.strictEqual('5', div.childNodes[1].textContent);447 done();448 });449 });450 test('Expressions Logical !', function(done) {451 var div = createTestHtml(452 '<template bind>' +453 '{{ !a.b }}:{{ !c }}:{{ !d }}' +454 '</template>');455 var model = {456 a: {457 b: 5458 },459 c: '',460 d: false461 };462 recursivelySetTemplateModel(div, model);463 then(function() {464 assert.strictEqual('false:true:true', div.childNodes[1].textContent);465 model.a.b = 0;466 model.c = 'foo'467 }).then(function() {468 assert.strictEqual('true:false:true', div.childNodes[1].textContent);469 done();470 });471 });472 test('Expressions Arithmetic, Additive', function(done) {473 var div = createTestHtml(474 '<template bind>' +475 '{{ (a.b + c.d) - (f + g.h) }}' +476 '</template>');477 var model = {478 a: {479 b: 5480 },481 c: {482 d: 5483 },484 e: 2,485 f: 3,486 g: {487 h: 2488 }489 };490 recursivelySetTemplateModel(div, model);491 then(function() {492 assert.strictEqual('5', div.childNodes[1].textContent);493 model.a.b = 7;494 model.g.h = -5;495 }).then(function() {496 assert.strictEqual('14', div.childNodes[1].textContent);497 done();498 });499 });500 test('Expressions Arithmetic, Multiplicative', function(done) {501 var div = createTestHtml(502 '<template bind>' +503 '{{ (a.b * c.d) / (f % g.h) }}' +504 '</template>');505 var model = {506 a: {507 b: 5508 },509 c: {510 d: 6511 },512 e: 2,513 f: 8,514 g: {515 h: 5516 }517 };518 recursivelySetTemplateModel(div, model);519 then(function() {520 assert.strictEqual('10', div.childNodes[1].textContent);521 model.a.b = 10;522 model.f = 16;523 }).then(function() {524 assert.strictEqual('60', div.childNodes[1].textContent);525 done();526 });527 });528 test('Expressions Relational', function(done) {529 var div = createTestHtml(530 '<template bind>' +531 '{{ a.b > c }}:{{ a.b < c }}:{{ c >= d }}:{{ d <= e }}' +532 '</template>');533 var model = {534 a: {535 b: 5536 },537 c: 3,538 d: 3,539 e: 2540 };541 recursivelySetTemplateModel(div, model);542 then(function() {543 assert.strictEqual('true:false:true:false',544 div.childNodes[1].textContent);545 model.a.b = 1;546 model.d = -5;547 }).then(function() {548 assert.strictEqual('false:true:true:true', div.childNodes[1].textContent);549 done();550 });551 });552 test('Expressions Equality', function(done) {553 var div = createTestHtml(554 '<template bind>' +555 '{{ a.b == c }}:{{ a.b != c }}:{{ c === d }}:{{ d !== e }}' +556 '</template>');557 var model = {558 a: {559 b: 5560 },561 c: '5',562 d: {}563 };564 model.e = model.d;565 recursivelySetTemplateModel(div, model);566 then(function() {567 assert.strictEqual('true:false:false:false',568 div.childNodes[1].textContent);569 model.a.b = 3;570 model.e = {};571 }).then(function() {572 assert.strictEqual('false:true:false:true',573 div.childNodes[1].textContent);574 done();575 });576 });577 test('Expressions Binary Logical', function(done) {578 var div = createTestHtml(579 '<template bind>' +580 '{{ a.b && c }}:{{ a.b || c }}:{{ c && d }}:{{ d || e }}' +581 '</template>');582 var model = {583 a: {584 b: 0585 },586 c: 5,587 d: true,588 e: ''589 };590 recursivelySetTemplateModel(div, model);591 then(function() {592 assert.strictEqual('0:5:true:true', div.childNodes[1].textContent);593 model.a.b = true;594 model.d = 0;595 }).then(function() {596 assert.strictEqual('5:true:0:', div.childNodes[1].textContent);597 done();598 });599 });600 test('Expressions Conditional', function(done) {601 var div = createTestHtml(602 '<template bind>' +603 '{{ a.b ? c : d.e }}:{{ f ? g.h : i }}' +604 '</template>');605 var model = {606 a: {607 b: 1608 },609 c: 5,610 d: {611 e: 2612 },613 f: 0,614 g: {615 h: 'foo'616 },617 i: 'bar'618 };619 recursivelySetTemplateModel(div, model);620 then(function() {621 assert.strictEqual('5:bar', div.childNodes[1].textContent);622 model.c = 6;623 model.f = '';624 model.i = 'bat'625 }).then(function() {626 assert.strictEqual('6:bat', div.childNodes[1].textContent);627 done();628 });629 });630 test('Expressions Conditional 2', function(done) {631 var div = createTestHtml(632 '<template bind>' +633 '{{ checked ? a : b }}' +634 '</template>');635 var model = {636 checked: false,637 a: 'A',638 b: 'B'639 };640 recursivelySetTemplateModel(div, model);641 then(function() {642 assert.strictEqual('B', div.childNodes[1].textContent);643 model.checked = true;644 }).then(function() {645 assert.strictEqual('A', div.childNodes[1].textContent);646 model.a = 'AAA';647 }).then(function() {648 assert.strictEqual('AAA', div.childNodes[1].textContent);649 model.checked = false;650 }).then(function() {651 assert.strictEqual('B', div.childNodes[1].textContent);652 model.b = 'BBB';653 }).then(function() {654 assert.strictEqual('BBB', div.childNodes[1].textContent);655 done();656 });657 });658 test('Expressions Literals', function(done) {659 var div = createTestHtml(660 '<template bind>' +661 '{{ +1 }}:{{ "foo" }}:{{ true ? true : false }}:' +662 '{{ true ? null : false}}' +663 '</template>');664 var model = {};665 recursivelySetTemplateModel(div, model);666 then(function() {667 assert.strictEqual('1:foo:true:null', div.childNodes[1].textContent);668 done();669 });670 });671 test('Expressions Array Literals', function(done) {672 var div = createTestHtml(673 '<template repeat="{{ [foo, bar] }}">' +674 '{{}}' +675 '</template>');676 var model = {677 foo: 'bar',678 bar: 'bat'679 };680 recursivelySetTemplateModel(div, model);681 then(function() {682 assert.strictEqual('bar', div.childNodes[1].textContent);683 assert.strictEqual('bat', div.childNodes[2].textContent);684 model.foo = 'boo';685 model.bar = 'blat';686 }).then(function() {687 assert.strictEqual('boo', div.childNodes[1].textContent);688 assert.strictEqual('blat', div.childNodes[2].textContent);689 done();690 });691 });692 test('Expressions Object Literals', function(done) {693 var div = createTestHtml(694 '<template bind="{{ { \'id\': 1, foo: bar } }}">' +695 '{{id}}:{{foo}}' +696 '</template>');697 var model = {698 bar: 'bat'699 };700 recursivelySetTemplateModel(div, model);701 then(function() {702 assert.strictEqual('1:bat', div.childNodes[1].textContent);703 model.bar = 'blat';704 }).then(function() {705 assert.strictEqual('1:blat', div.childNodes[1].textContent);706 done();707 });708 });709 test('Expressions Array Literals, Named Scope', function(done) {710 var div = createTestHtml(711 '<template repeat="{{ user in [foo, bar] }}">' +712 '{{ user }}' +713 '</template>');714 var model = {715 foo: 'bar',716 bar: 'bat'717 };718 recursivelySetTemplateModel(div, model);719 then(function() {720 assert.strictEqual('bar', div.childNodes[1].textContent);721 assert.strictEqual('bat', div.childNodes[2].textContent);722 model.foo = 'boo';723 model.bar = 'blat';724 }).then(function() {725 assert.strictEqual('boo', div.childNodes[1].textContent);726 assert.strictEqual('blat', div.childNodes[2].textContent);727 done();728 });729 });730 test('Expressions Object Literals, Named Scope', function(done) {731 var div = createTestHtml(732 '<template bind="{{ { \'id\': 1, foo: bar } as t }}">' +733 '{{t.id}}:{{t.foo}}' +734 '</template>');735 var model = {736 bar: 'bat'737 };738 recursivelySetTemplateModel(div, model);739 then(function() {740 assert.strictEqual('1:bat', div.childNodes[1].textContent);741 model.bar = 'blat';742 }).then(function() {743 assert.strictEqual('1:blat', div.childNodes[1].textContent);744 done();745 });746 });747 test('filter without arguments', function(done) {748 var div = createTestHtml(749 '<template bind="{{ }}">' +750 '{{ bar | upperCase }}' +751 '{{ bar | upperCase() }}' +752 '</template>');753 var model = {754 bar: 'bat'755 };756 recursivelySetTemplateModel(div, model);757 then(function() {758 assert.equal('BATBAT', div.childNodes[1].textContent);759 model.bar = 'blat';760 }).then(function() {761 assert.equal('BLATBLAT', div.childNodes[1].textContent);762 done();763 });764 });765 test('filter with arguments', function(done) {766 var div = createTestHtml(767 '<template bind="{{ }}">' +768 '{{ bar | toFixed(4) }}' +769 '</template>');770 var model = {771 bar: 1.23456789772 };773 recursivelySetTemplateModel(div, model);774 then(function() {775 assert.equal('1.2346', div.childNodes[1].textContent);776 model.bar = 9.87654321;777 }).then(function() {778 assert.equal('9.8765', div.childNodes[1].textContent);779 done();780 });781 });782 test('Inline functions', function(done) {783 var div = createTestHtml(784 '<template bind="{{ }}">' +785 '{{ addTwo(2, 3) + addTwo(a, b) }}:{{ minus(addTwo(a, b), c) }}' +786 '</template>');787 var model = {788 a: 4,789 b: 5,790 c: 3,791 addTwo: function(a, b) {792 assert.strictEqual(this, model);793 return a + b;794 },795 minus: function(a, amount) {796 assert.strictEqual(this, model);797 return a - amount;798 }799 };800 recursivelySetTemplateModel(div, model);801 then(function() {802 assert.equal('14:6', div.childNodes[1].textContent);803 model.a = 10;804 }).then(function() {805 assert.equal('20:12', div.childNodes[1].textContent);806 model.c = 10;807 }).then(function() {808 assert.equal('20:5', div.childNodes[1].textContent);809 done();810 });811 });812 test('Expression execution count', function(done) {813 var div = createTestHtml(814 '<template bind>' +815 '{{ dep | incrProp(obj, "count") }}' +816 '</template>');817 var model = {818 dep: 1,819 obj: { count: 0 }820 };821 recursivelySetTemplateModel(div, model);822 then(function() {823 assert.equal(1, model.obj.count);824 model.dep++;825 }).then(function() {826 assert.equal(2, model.obj.count);827 done();828 });829 });830 test('chained filters', function(done) {831 var div = createTestHtml(832 '<template bind="{{ }}">' +833 '{{ bar | toFixed(0) | hex | upperCase }}' +834 '</template>');835 var model = {836 bar: 12.34837 };838 recursivelySetTemplateModel(div, model);839 then(function() {840 assert.equal('C', div.childNodes[1].textContent);841 model.bar = 14.56;842 }).then(function() {843 assert.equal('F', div.childNodes[1].textContent);844 done();845 });846 });847 test('complex computed property expression', function(done) {848 var div = createTestHtml(849 '<template bind="{{ }}">' +850 '<div foo="{{ foo[bar + 2].baz + bat }}">' +851 '</template>');852 var model = {853 foo: [{ baz: 'bo' }, { baz: 'ba' }],854 bar: -2,855 bat: 't'856 };857 recursivelySetTemplateModel(div, model);858 then(function() {859 assert.equal('bot', div.childNodes[1].getAttribute('foo'));860 model.myIndex = 0;861 model.bar = -1;862 model.bat = 'r';863 }).then(function() {864 assert.equal('bar', div.childNodes[1].getAttribute('foo'));865 done();866 });867 });868 test('computed - newly reachable objects', function(done) {869 var div = createTestHtml(870 '<template bind>' +871 '<div foo="{{ 1 == foo.bar.bat }}">' +872 '</template>');873 var model = {};874 recursivelySetTemplateModel(div, model);875 then(function() {876 assert.equal('false', div.childNodes[1].getAttribute('foo'));877 model.foo = {};878 }).then(function() {879 assert.equal('false', div.childNodes[1].getAttribute('foo'));880 model.foo.bar = {};881 }).then(function() {882 assert.equal('false', div.childNodes[1].getAttribute('foo'));883 model.foo.bar.bat = 1;884 }).then(function() {885 assert.equal('true', div.childNodes[1].getAttribute('foo'));886 done();887 });888 });889 test('computed property with ident index', function(done) {890 var div = createTestHtml(891 '<template bind="{{ }}">' +892 '<div foo="{{ myArray[myIndex] }}">' +893 '</template>');894 var model = {895 myArray: ['baz', 'bar'],896 myIndex: 1897 };898 recursivelySetTemplateModel(div, model);899 then(function() {900 assert.equal('bar', div.childNodes[1].getAttribute('foo'));901 model.myIndex = 0;902 }).then(function() {903 assert.equal('baz', div.childNodes[1].getAttribute('foo'));904 model.myArray = ['hello', 'world'];905 }).then(function() {906 assert.equal('hello', div.childNodes[1].getAttribute('foo'));907 done();908 });909 });910 test('computed property with object index', function(done) {911 var div = createTestHtml(912 '<template bind="{{ }}">' +913 '<div foo="{{ myObj[dateObj] }}">' +914 '</template>');915 var model = {916 myObj: {917 'Tue Jul 08 2014 12:00:00 GMT-0700 (PDT)': 'bar',918 'Wed Jul 09 2014 12:00:00 GMT-0700 (PDT)': 'baz'919 },920 dateObj: objToString('Tue Jul 08 2014 12:00:00 GMT-0700 (PDT)')921 };922 recursivelySetTemplateModel(div, model);923 then(function() {924 assert.equal('bar', div.childNodes[1].getAttribute('foo'));925 model.dateObj = objToString('Wed Jul 09 2014 12:00:00 GMT-0700 (PDT)');926 }).then(function() {927 assert.equal('baz', div.childNodes[1].getAttribute('foo'));928 done();929 });930 });931 test('computed property with object index - assignment', function(done) {932 var div = createTestHtml(933 '<template bind="{{ }}">' +934 '<input value="{{ myObj[dateObj] }}">' +935 '</template>');936 var model = {937 myObj: {938 'Tue Jul 08 2014 12:00:00 GMT-0700 (PDT)': 'bar',939 },940 dateObj: objToString('Tue Jul 08 2014 12:00:00 GMT-0700 (PDT)')941 };942 recursivelySetTemplateModel(div, model);943 then(function() {944 assert.equal('bar', div.childNodes[1].value);945 div.childNodes[1].value = 'baz';946 dispatchEvent('input', div.childNodes[1]);947 }).then(function() {948 assert.equal('baz',949 model.myObj['Tue Jul 08 2014 12:00:00 GMT-0700 (PDT)']);950 done();951 });952 });953 test('two-way computed property', function(done) {954 var div = createTestHtml(955 '<template bind="{{ }}">' +956 '<input value="{{ bar[\'contains space\'] }}">' +957 '</template>');958 var model = {959 bar: {960 'contains space': 'a'961 }962 };963 recursivelySetTemplateModel(div, model);964 then(function() {965 assert.equal('a', div.childNodes[1].value);966 div.childNodes[1].value = 'b';967 dispatchEvent('input', div.childNodes[1]);968 }).then(function() {969 assert.equal('b', model.bar['contains space']);970 done();971 });972 });973 test('two-way computed property 2', function(done) {974 var div = createTestHtml(975 '<template bind="{{ }}">' +976 '<input value="{{ bar[0].bat }}">' +977 '</template>');978 var model = {979 bar: [{ bat: 'a' }]980 };981 recursivelySetTemplateModel(div, model);982 then(function() {983 assert.equal('a', div.childNodes[1].value);984 div.childNodes[1].value = 'b';985 dispatchEvent('input', div.childNodes[1]);986 }).then(function() {987 assert.equal('b', model.bar[0].bat);988 done();989 });990 });991 test('dynamic two-way computed property', function(done) {992 var div = createTestHtml(993 '<template bind="{{ }}">' +994 '<input value="{{ foo[bar] }}">' +995 '</template>');996 var model = {997 foo: {998 a: '1',999 b: '3'1000 },1001 bar: 'a'1002 };1003 recursivelySetTemplateModel(div, model);1004 then(function() {1005 assert.equal('1', div.childNodes[1].value);1006 div.childNodes[1].value = '2';1007 dispatchEvent('input', div.childNodes[1]);1008 }).then(function() {1009 assert.equal('2', model.foo.a);1010 assert.equal('3', model.foo.b);1011 model.bar = 'b';1012 }).then(function() {1013 assert.equal('3', div.childNodes[1].value);1014 div.childNodes[1].value = '4';1015 dispatchEvent('input', div.childNodes[1]);1016 }).then(function() {1017 assert.equal('2', model.foo.a);1018 assert.equal('4', model.foo.b);1019 done();1020 });1021 });1022 test('two-way filter', function(done) {1023 var div = createTestHtml(1024 '<template bind="{{ }}">' +1025 '<input value="{{ bar | plusN(bat) | plusN(boo) }}">' +1026 '</template>');1027 var model = {1028 bar: 10,1029 bat: 1,1030 boo: 31031 };1032 recursivelySetTemplateModel(div, model);1033 then(function() {1034 assert.equal('14', div.childNodes[1].value);1035 div.childNodes[1].value = 8;1036 dispatchEvent('input', div.childNodes[1]);1037 }).then(function() {1038 assert.equal(4, model.bar);1039 assert.equal(1, model.bat);1040 assert.equal(3, model.boo);1041 model.bar = 5;1042 model.bat = 3;1043 model.boo = -2;1044 }).then(function() {1045 assert.equal('6', div.childNodes[1].value);1046 div.childNodes[1].value = 10;1047 dispatchEvent('input', div.childNodes[1]);1048 }).then(function() {1049 assert.equal(9, model.bar);1050 assert.equal(3, model.bat);1051 assert.equal(-2, model.boo);1052 done();1053 })1054 });1055 test('two-way binding to root scope', function(done) {1056 var div = createTestHtml(1057 '<template bind>' +1058 '<template bind="{{ foo as foo }}">' +1059 '<input value="{{ bar }}">' +1060 '</template>' +1061 '</template>');1062 var model = { foo: {}, bar: 'bar' };1063 recursivelySetTemplateModel(div, model);1064 then(function() {1065 assert.equal('bar', div.childNodes[2].value);1066 div.childNodes[2].value = 'baz';1067 dispatchEvent('input', div.childNodes[2]);1068 }).then(function() {1069 assert.equal('baz', model.bar);1070 done();1071 })1072 });1073 test('two-way binding to root scope with transform', function(done) {1074 var div = createTestHtml(1075 '<template bind>' +1076 '<template bind="{{ foo as foo }}">' +1077 '<input value="{{ bar | plusN(2) }}">' +1078 '</template>' +1079 '</template>');1080 var model = { foo: {}, bar: 3 };1081 recursivelySetTemplateModel(div, model);1082 then(function() {1083 assert.equal('5', div.childNodes[2].value);1084 div.childNodes[2].value = 8;1085 dispatchEvent('input', div.childNodes[2]);1086 }).then(function() {1087 assert.equal(6, model.bar);1088 done();1089 })1090 });1091 test('two-way filter too many paths', function(done) {1092 var div = createTestHtml(1093 '<template bind="{{ }}">' +1094 '<input value="{{ bar + num | hex }}">' +1095 '</template>');1096 var model = {1097 bar: 32,1098 num: 101099 };1100 recursivelySetTemplateModel(div, model);1101 then(function() {1102 assert.equal('2a', div.childNodes[1].value);1103 div.childNodes[1].value = 'ff';1104 dispatchEvent('input', div.childNodes[1]);1105 }).then(function() {1106 assert.equal(32, model.bar);1107 assert.equal(10, model.num);1108 model.bar = 15;1109 }).then(function() {1110 assert.equal('19', div.childNodes[1].value);1111 model.num = 5;1112 }).then(function() {1113 assert.equal('14', div.childNodes[1].value);1114 done();1115 });1116 });1117 test('two-way filter chained', function(done) {1118 var div = createTestHtml(1119 '<template bind="{{ }}">' +1120 '<input value="{{ bar | plusN(10) | hex }}">' +1121 '</template>');1122 var model = {1123 bar: 221124 };1125 recursivelySetTemplateModel(div, model);1126 then(function() {1127 assert.equal('20', div.childNodes[1].value);1128 div.childNodes[1].value = 'ff';1129 dispatchEvent('input', div.childNodes[1]);1130 }).then(function() {1131 assert.equal(245, model.bar);1132 model.bar = 5;1133 }).then(function() {1134 assert.equal('f', div.childNodes[1].value);1135 done();1136 });1137 });1138 test('filter unexpected EOF', function(done) {1139 var div = createTestHtml(1140 '<template bind="{{ }}">' +1141 '{{ bar | }}' +1142 '</template>');1143 var model = {1144 bar: 'bat'1145 };1146 recursivelySetTemplateModel(div, model);1147 then(function() {1148 assert.equal('', div.childNodes[1].textContent);1149 model.bar = 'blat';1150 }).then(function() {1151 assert.equal('', div.childNodes[1].textContent);1152 assert.equal(errors[0][0], 'Invalid expression syntax: bar |');1153 done();1154 });1155 });1156 test('filter not at EOF', function(done) {1157 var div = createTestHtml(1158 '<template bind="{{ }}">' +1159 '{{ bar | upperCase + 42 }}' +1160 '</template>');1161 var model = {1162 bar: 'bat'1163 };1164 recursivelySetTemplateModel(div, model);1165 then(function() {1166 assert.equal('', div.childNodes[1].textContent);1167 model.bar = 'blat';1168 }).then(function() {1169 assert.equal('', div.childNodes[1].textContent);1170 assert.equal(errors[0][0],1171 'Invalid expression syntax: bar | upperCase + 42');1172 done();1173 });1174 });1175 test('Member lookup with constant expressions', function(done) {1176 var div = createTestHtml(1177 '<template bind>' +1178 '{{ array[0] }} {{ object["a"] }}' +1179 '</template>');1180 var model = {1181 array: ['a', 'b'],1182 object: {1183 a: 'A'1184 }1185 };1186 recursivelySetTemplateModel(div, model);1187 then(function() {1188 assert.equal('a A', div.childNodes[1].textContent);1189 model.array = ['c', 'd'];1190 }).then(function() {1191 assert.equal('c A', div.childNodes[1].textContent);1192 model.object = {a: 'E'};1193 }).then(function() {1194 assert.equal('c E', div.childNodes[1].textContent);1195 done();1196 });1197 });1198 test('Member lookup', function(done) {1199 var div = createTestHtml(1200 '<template bind>' +1201 '{{ array[index] }} {{ object[key] }}' +1202 '</template>');1203 var model = {1204 array: ['a', 'b'],1205 index: 0,1206 object: {1207 a: 'A',1208 b: 'B'1209 },1210 key: 'a'1211 };1212 recursivelySetTemplateModel(div, model);1213 then(function() {1214 assert.equal('a A', div.childNodes[1].textContent);1215 model.index = 1;1216 }).then(function() {1217 assert.equal('b A', div.childNodes[1].textContent);1218 model.key = 'b';1219 }).then(function() {1220 assert.equal('b B', div.childNodes[1].textContent);1221 model.array = ['c', 'd'];1222 }).then(function() {1223 assert.equal('d B', div.childNodes[1].textContent);1224 model.object = {1225 a: 'C',1226 b: 'D'1227 };1228 }).then(function() {1229 assert.equal('d D', div.childNodes[1].textContent);1230 done();1231 });1232 });1233 test('Member lookup nested', function(done) {1234 var div = createTestHtml(1235 '<template bind>' +1236 '{{ object[array[index]] }}' +1237 '</template>');1238 var model = {1239 array: ['a', 'b'],1240 index: 0,1241 object: {1242 a: 'A',1243 b: 'B'1244 }1245 };1246 recursivelySetTemplateModel(div, model);1247 then(function() {1248 assert.equal('A', div.childNodes[1].textContent);1249 model.index = 1;1250 }).then(function() {1251 Platform.performMicrotaskCheckpoint();1252 assert.equal('B', div.childNodes[1].textContent);1253 done();1254 });1255 });1256 test('in expression with index scope', function(done) {1257 var div = createTestHtml(1258 '<template repeat="{{ value, i in array }}">' +1259 '{{ i }}. {{ value }}' +1260 '</template>');1261 var model = {1262 array: ['a', 'b', 'c']1263 };1264 recursivelySetTemplateModel(div, model);1265 then(function() {1266 assert.strictEqual('0. a', div.childNodes[1].textContent);1267 assert.strictEqual('1. b', div.childNodes[2].textContent);1268 assert.strictEqual('2. c', div.childNodes[3].textContent);1269 model.array.splice(1, 1, 'd', 'e');1270 }).then(function() {1271 assert.strictEqual('0. a', div.childNodes[1].textContent);1272 assert.strictEqual('1. d', div.childNodes[2].textContent);1273 assert.strictEqual('2. e', div.childNodes[3].textContent);1274 assert.strictEqual('3. c', div.childNodes[4].textContent);1275 model.array.reverse();1276 }).then(function() {1277 assert.strictEqual('0. c', div.childNodes[1].textContent);1278 assert.strictEqual('1. e', div.childNodes[2].textContent);1279 assert.strictEqual('2. d', div.childNodes[3].textContent);1280 assert.strictEqual('3. a', div.childNodes[4].textContent);1281 model.array.sort();1282 }).then(function() {1283 assert.strictEqual('0. a', div.childNodes[1].textContent);1284 assert.strictEqual('1. c', div.childNodes[2].textContent);1285 assert.strictEqual('2. d', div.childNodes[3].textContent);1286 assert.strictEqual('3. e', div.childNodes[4].textContent);1287 model.array.shift();1288 }).then(function() {1289 assert.strictEqual('0. c', div.childNodes[1].textContent);1290 assert.strictEqual('1. d', div.childNodes[2].textContent);1291 assert.strictEqual('2. e', div.childNodes[3].textContent);1292 model.array.unshift('f');1293 model.array.push('g');1294 }).then(function() {1295 assert.strictEqual('0. f', div.childNodes[1].textContent);1296 assert.strictEqual('1. c', div.childNodes[2].textContent);1297 assert.strictEqual('2. d', div.childNodes[3].textContent);1298 assert.strictEqual('3. e', div.childNodes[4].textContent);1299 assert.strictEqual('4. g', div.childNodes[5].textContent);1300 done();1301 });1302 });1303 test('in expression with nested index scopes', function(done) {1304 var div = createTestHtml(1305 '<template repeat="{{ foo, i in foos }}">' +1306 '<template repeat="{{ value, j in foo }}">' +1307 '{{ i }}:{{ j }}. {{ value }}' +1308 '</template>' +1309 '</template>');1310 var model = {1311 foos: [1312 [ 'a', 'b'],1313 [ 'c', 'd']1314 ]1315 };1316 recursivelySetTemplateModel(div, model);1317 then(function() {1318 assert.strictEqual('0:0. a', div.childNodes[2].textContent);1319 assert.strictEqual('0:1. b', div.childNodes[3].textContent);1320 assert.strictEqual('1:0. c', div.childNodes[5].textContent);1321 assert.strictEqual('1:1. d', div.childNodes[6].textContent);1322 model.foos.reverse();1323 }).then(function() {1324 assert.strictEqual('0:0. c', div.childNodes[2].textContent);1325 assert.strictEqual('0:1. d', div.childNodes[3].textContent);1326 assert.strictEqual('1:0. a', div.childNodes[5].textContent);1327 assert.strictEqual('1:1. b', div.childNodes[6].textContent);1328 model.foos[0].reverse();1329 model.foos[1].reverse();1330 }).then(function() {1331 assert.strictEqual('0:0. d', div.childNodes[2].textContent);1332 assert.strictEqual('0:1. c', div.childNodes[3].textContent);1333 assert.strictEqual('1:0. b', div.childNodes[5].textContent);1334 assert.strictEqual('1:1. a', div.childNodes[6].textContent);1335 done();1336 });1337 });1338 test('Named Scope Bind with filter', function(done) {1339 var div = createTestHtml(1340 '<template bind>' +1341 '<template bind="{{ value | hex as hexValue }}">' +1342 '{{ hexValue }}' +1343 '</template>' +1344 '</template>');1345 var model = {1346 value: 321347 };1348 recursivelySetTemplateModel(div, model);1349 then(function() {1350 var target = div.childNodes[2];1351 assert.strictEqual('20', div.childNodes[2].textContent);1352 model.value = 255;1353 }).then(function() {1354 assert.strictEqual('ff', div.childNodes[2].textContent);1355 done();1356 });1357 });1358 test('Named Scope Repeat with filter', function(done) {1359 var div = createTestHtml(1360 '<template bind>' +1361 '<template repeat="{{ value in [ 3, 2, 1 ] | staticSort }}">' +1362 '{{ value }}' +1363 '</template>' +1364 '</template>');1365 var model = {};1366 recursivelySetTemplateModel(div, model);1367 then(function() {1368 assert.strictEqual('1', div.childNodes[2].textContent);1369 assert.strictEqual('2', div.childNodes[3].textContent);1370 assert.strictEqual('3', div.childNodes[4].textContent);1371 done();1372 });1373 });1374 test('filter on model', function(done) {1375 var div = createTestHtml(1376 '<template bind="{{ }}">' +1377 '<input value="{{ value | multiple }}">' +1378 '</template>');1379 var model = {1380 factor: 2,1381 multiple: function(value) {1382 return Number(value) * this.factor;1383 },1384 value: 81385 };1386 model.multiple.toModel = function(value) {1387 return Number(value) / this.factor;1388 }1389 recursivelySetTemplateModel(div, model);1390 then(function() {1391 assert.equal('16', div.childNodes[1].value);1392 div.childNodes[1].value = 20;1393 dispatchEvent('input', div.childNodes[1]);1394 }).then(function() {1395 assert.equal('10', model.value);1396 done();1397 });1398 });1399 // https://github.com/Polymer/polymer-expressions/issues/191400 test('issue-19', function(done) {1401 var div = createTestHtml(1402 '<template id="t" bind="{{ foo.bar as b }}">' +1403 '<span>{{ b }}</span>' +1404 '</template>');1405 var model = {};1406 recursivelySetTemplateModel(div, model);1407 then(function() {1408 var target = div.childNodes[1];1409 done();1410 });1411 });1412 test('Inline function parsing', function() {1413 assert.isDefined(getExpression('a(1, 2)'));1414 assert.isDefined(getExpression('a(b + c, d + e)'));1415 assert.isDefined(getExpression('a(true) + b(false)'));1416 assert.throws(function() {1417 getExpression('a.b()');1418 });1419 assert.throws(function() {1420 getExpression('a[1]()');1421 });1422 assert.throws(function() {1423 getExpression('(a + b)()');1424 });1425 });1426 test('Dynamic deps path expressions', function() {1427 assert.isFalse(getExpression('a + b').dynamicDeps);1428 assert.isFalse(getExpression('a + b > 3 + hello["kitty"]').dynamicDeps);1429 assert.isTrue(getExpression('a[a.b]').dynamicDeps);1430 assert.isTrue(getExpression('a[a.b] + d[e]').dynamicDeps);1431 assert.isFalse(getExpression('a[0].c').dynamicDeps);1432 assert.isFalse(getExpression('a[1][0]').dynamicDeps);1433 assert.isTrue(getExpression('a[b].c').dynamicDeps);1434 assert.isTrue(getExpression('(a + 1).c').dynamicDeps);1435 assert.isTrue(getExpression('a[a.b].c').dynamicDeps);1436 assert.isTrue(getExpression('a[a][0]').dynamicDeps);1437 assert.isTrue(getExpression('a[a.b] + d[e].f').dynamicDeps);1438 assert.isTrue(getExpression('a[b + c]').dynamicDeps);1439 assert.isTrue(getExpression('a[b + 2].c + d').dynamicDeps);1440 });1441 // https://github.com/Polymer/polymer-expressions/issues/241442 test('issue-24 - 1', function(done) {1443 var div = createTestHtml(1444 '<template id="t" bind if="{{ true }}">' +1445 '<span>{{ b }}</span>' +1446 '</template>');1447 var model = {1448 b: 'hi',1449 true: false1450 };1451 recursivelySetTemplateModel(div, model);1452 then(function() {1453 assert.equal(div.childNodes.length, 2);1454 var target = div.childNodes[1];1455 assert.equal(target.textContent, 'hi');1456 done();1457 });1458 });1459 test('issue-24 - 2', function(done) {1460 var div = createTestHtml(1461 '<template id="t" bind if="{{ false }}">' +1462 '<span>{{ b }}</span>' +1463 '</template>');1464 var model = {1465 b: 'hi',1466 false: true1467 };1468 recursivelySetTemplateModel(div, model);1469 then(function() {1470 assert.equal(div.childNodes.length, 1);1471 done();1472 });1473 });1474 test('issue-24 - 3', function(done) {1475 var div = createTestHtml(1476 '<template id="t" bind if="{{ null }}">' +1477 '<span>{{ b }}</span>' +1478 '</template>');1479 var model = {1480 b: 'hi',1481 null: true1482 };1483 recursivelySetTemplateModel(div, model);1484 then(function() {1485 assert.equal(div.childNodes.length, 1);1486 done();1487 });1488 });1489 test('issue-24 - 4', function(done) {1490 var div = createTestHtml(1491 '<template id="t" bind if="{{ 0 }}">' +1492 '<span>{{ b }}</span>' +1493 '</template>');1494 var model = {1495 b: 'hi',1496 0: true1497 };1498 recursivelySetTemplateModel(div, model);1499 then(function() {1500 assert.equal(div.childNodes.length, 1);1501 done();1502 });1503 });1504 test('issue-24 - 5', function(done) {1505 var div = createTestHtml(1506 '<template id="t" bind>' +1507 '<span>{{ true }}</span>' +1508 '</template>');1509 var model = {true: false};1510 recursivelySetTemplateModel(div, model);1511 then(function() {1512 var target = div.childNodes[1];1513 assert.equal(div.childNodes.length, 2);1514 assert.equal(target.textContent, 'true');1515 done();1516 });1517 });1518 test('issue-24 - 6', function(done) {1519 var div = createTestHtml(1520 '<template id="t" bind>' +1521 '<span>{{ 111 }}</span>' +1522 '</template>');1523 var model = {111: 222};1524 recursivelySetTemplateModel(div, model);1525 then(function() {1526 var target = div.childNodes[1];1527 assert.equal(div.childNodes.length, 2);1528 assert.equal(target.textContent, '111');1529 done();1530 });1531 });1532 test('issue-24 - 7', function(done) {1533 var div = createTestHtml(1534 '<template id="t" bind>' +1535 '<span>{{ 1.2 }}</span>' +1536 '</template>');1537 var model = {1538 1: {1539 2: 31540 }1541 };1542 recursivelySetTemplateModel(div, model);1543 then(function() {1544 var target = div.childNodes[1];1545 assert.equal(div.childNodes.length, 2);1546 assert.equal(target.textContent, '1.2');1547 done();1548 });1549 });1550 test('issue-29 - 1', function(done) {1551 var div = createTestHtml(1552 '<template id="t" bind>' +1553 '{{ items[index] }}' +1554 '</template>');1555 var model = {1556 index: 0,1557 items: [1, 2]1558 };1559 recursivelySetTemplateModel(div, model);1560 then(function() {1561 var target = div.childNodes[1];1562 assert.equal(target.textContent, '1');1563 model.index = 1;1564 }).then(function() {1565 var target = div.childNodes[1];1566 assert.equal(target.textContent, '2');1567 model.items[1] = 3;1568 }).then(function() {1569 var target = div.childNodes[1];1570 assert.equal(target.textContent, '3');1571 done();1572 });1573 });1574 test('issue-29 - 2', function(done) {1575 var div = createTestHtml(1576 '<template id="t" bind>' +1577 '{{ index }} - {{ items[index] }}' +1578 '</template>');1579 var model = {1580 index: 0,1581 items: [1, 2]1582 };1583 recursivelySetTemplateModel(div, model);1584 then(function() {1585 var target = div.childNodes[1];1586 assert.equal(target.textContent, '0 - 1');1587 model.index++;1588 }).then(function() {1589 var target = div.childNodes[1];1590 assert.equal(target.textContent, '1 - 2');1591 model.items[1] = 3;1592 }).then(function() {1593 var target = div.childNodes[1];1594 assert.equal(target.textContent, '1 - 3');1595 done();1596 });1597 });1598 test('issue-29 - 3', function(done) {1599 var div = createTestHtml(1600 '<template id="t" bind="{{items[index] as item}}">' +1601 '{{ index }} - {{ item }}' +1602 '</template>');1603 var model = {1604 index: 0,1605 items: [1, 2]1606 };1607 recursivelySetTemplateModel(div, model);1608 then(function() {1609 var target = div.childNodes[1];1610 assert.equal(target.textContent, '0 - 1');1611 model.index++;1612 }).then(function() {1613 var target = div.childNodes[1];1614 assert.equal(target.textContent, '1 - 2');1615 model.items[1] = 3;1616 }).then(function() {1617 var target = div.childNodes[1];1618 assert.equal(target.textContent, '1 - 3');1619 done();1620 });1621 });1622 test('issue-33', function(done) {1623 var div = createTestHtml(1624 '<template repeat="{{ id in data }}">' +1625 '{{ id }}' +1626 '</template>');1627 var model = document.createElement('div');1628 model.id = 'foo';1629 model.data = ['a', 'b', 'c'];1630 recursivelySetTemplateModel(div, model);1631 then(function() {1632 assert.strictEqual(div.childNodes.length, 4);1633 assert.strictEqual(model.id, 'foo');1634 done();1635 });1636 });1637 test('issue-34', function(done) {1638 var div = createTestHtml(1639 '<template bind>' +1640 '{{ comments[\'f-irst\'] }}' +1641 '</template>');1642 var model = {1643 comments: {1644 'f-irst': 'foo'1645 }1646 };1647 recursivelySetTemplateModel(div, model);1648 then(function() {1649 model.comments['f-irst'] = 'bar'1650 }).then(function() {1651 assert.strictEqual(div.childNodes[1].textContent, 'bar');1652 done();1653 });1654 });1655 test('issue-35', function(done) {1656 var div = createTestHtml(1657 '<template bind="{{ data[0] as foo }}">' +1658 '{{ foo.value }}' +1659 '</template>');1660 var model = {1661 data: [{ value: 'bar' }]1662 };1663 recursivelySetTemplateModel(div, model);1664 then(function() {1665 assert.strictEqual(div.childNodes[1].textContent, 'bar');1666 done();1667 });1668 });1669 test('lazy conditional', function(done) {1670 var div = createTestHtml(1671 '<template bind=>' +1672 '{{ a ? b : c }}' +1673 '</template>');1674 var count = 0;1675 var model = {1676 a: true,1677 b: 'B',1678 get c() {1679 count++;1680 }1681 };1682 recursivelySetTemplateModel(div, model);1683 then(function() {1684 assert.equal('B', div.childNodes[1].textContent);1685 assert.equal(0, count);1686 model.b = 'BB';1687 }).then(function() {1688 assert.equal('BB', div.childNodes[1].textContent);1689 assert.equal(0, count);1690 done();1691 });1692 });1693 test('lazy conditional 2', function(done) {1694 var div = createTestHtml(1695 '<template bind=>' +1696 '{{ a ? b : c }}' +1697 '</template>');1698 var count = 0;1699 var model = {1700 a: false,1701 get b() {1702 count++;1703 },1704 c: 'C'1705 };1706 recursivelySetTemplateModel(div, model);1707 then(function() {1708 assert.equal('C', div.childNodes[1].textContent);1709 assert.equal(0, count);1710 model.c = 'CC';1711 }).then(function() {1712 assert.equal('CC', div.childNodes[1].textContent);1713 assert.equal(0, count);1714 done();1715 });1716 });1717 test('lazy or', function(done) {1718 var div = createTestHtml(1719 '<template bind=>' +1720 '{{ a || b }}' +1721 '</template>');1722 var count = 0;1723 var model = {1724 a: 'A',1725 get b() {1726 count++;1727 },1728 };1729 recursivelySetTemplateModel(div, model);1730 then(function() {1731 assert.equal('A', div.childNodes[1].textContent);1732 assert.equal(0, count);1733 model.a = 'AA';1734 }).then(function() {1735 assert.equal('AA', div.childNodes[1].textContent);1736 assert.equal(0, count);1737 done();1738 });1739 });1740 test('lazy and', function(done) {1741 var div = createTestHtml(1742 '<template bind=>' +1743 '{{ a && b }}' +1744 '</template>');1745 var count = 0;1746 var model = {1747 a: false,1748 get b() {1749 count++;1750 },1751 };1752 recursivelySetTemplateModel(div, model);1753 then(function() {1754 assert.equal('false', div.childNodes[1].textContent);1755 assert.equal(0, count);1756 model.a = 0;1757 }).then(function() {1758 assert.equal('0', div.childNodes[1].textContent);1759 assert.equal(0, count);1760 done();1761 });1762 });...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('wpt');2var options = {3};4wpt.createTest(options, function(err, data) {5 if (err) {6 console.log('Error: ' + err.message);7 } else {8 console.log('Test Created: ' + data.data.testId);9 }10});11## createTestHTML(options, callback)12- `url` (string, required) - The URL to test13- `runs` (number, optional, default: 3) - The number of test runs to perform14- `firstViewOnly` (boolean, optional, default: false) - If true, only the first view of the test will be run (i.e. no repeat view)15- `private` (boolean, optional, default: false) - If true, the test will not be publicly visible on the WebPagetest results page (this also prevents the test from being included in the public API results)16- `connectivity` (string, optional, default: Cable) - The network connectivity to test with. Valid values are: Cable, DSL, 3G, 3GFast, 3GSlow, 2G, 2GFast, 2GSlow, Native17- `pollResults` (number, optional, default: 5) - The number of seconds to wait between polling for test results18- `timeout` (number, optional, default: 60) - The number of seconds to wait before timing out the test19- `video` (

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('wpt');2var wptObject = new wpt('API_KEY');3 if (err) {4 console.error(err);5 } else {6 console.log(data);7 }8});9`createTest(url, options, callback)`10* **pollResults** `number` - If set, will poll the results every X seconds. Default is 0 (no polling)11* **pollResults** `function` - Callback function to run when polling results. Default is null (not used)

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('./wpt.js');2var fs = require('fs');3var testJSON = JSON.parse(fs.readFileSync('./test.json', 'utf8'));4var testHTML = wpt.createTestHTML(testJSON);5fs.writeFile('./test.html', testHTML, function(err) {6 if(err) {7 return console.log(err);8 }9 console.log("The file was saved!");10});

Full Screen

Using AI Code Generation

copy

Full Screen

1var WPT = require("wpt");2var wpt = new WPT("API_KEY");3var options = {4};5wpt.createTestHTML(url, options, function(err, data) {6 if (err) {7 console.log("Error: " + err);8 } else {9 console.log("Test ID: " + data.testId);10 console.log("Test URL: " + data.data.summary);11 }12});13### `WPT(key, options)`14Default: `{}`15### `wpt.getLocations(callback)`

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org');3var testOptions = {4 videoParams: {5 }6};7wpt.createTestHTML(testURL, testOptions, function (err, data) {8 if (err) {9 console.log(err);10 } else {11 console.log(data);12 }13});14var wpt = require('webpagetest');15var wpt = new WebPageTest('www.webpagetest.org');16var testOptions = {17 videoParams: {18 }19};20wpt.createTest(testURL, testOptions, function (err, data) {21 if (err) {22 console.log(err);23 } else {24 console.log(data);25 }26});27var wpt = require('webpagetest');28var wpt = new WebPageTest('www.webpagetest.org');29wpt.getLocations(function (err, data) {30 if (err) {31 console.log(err);32 } else {33 console.log(data);34 }35});36var wpt = require('webpagetest');37var wpt = new WebPageTest('www.webpagetest.org');

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run wpt automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful