From 855284559a05abf259470d632f1c33022bb4b63f Mon Sep 17 00:00:00 2001 From: David Greenspan Date: Thu, 18 Sep 2014 17:29:04 -0700 Subject: [PATCH] Back-compat tests --- packages/spacebars-compiler/codegen.js | 1 + packages/spacebars-tests/template_tests.html | 28 +++++++++++++ packages/spacebars-tests/template_tests.js | 43 ++++++++++++++++++++ packages/spacebars/dynamic_tests.html | 4 ++ packages/spacebars/dynamic_tests.js | 29 +++++++++++++ 5 files changed, 105 insertions(+) diff --git a/packages/spacebars-compiler/codegen.js b/packages/spacebars-compiler/codegen.js index e655332024..5141075b5b 100644 --- a/packages/spacebars-compiler/codegen.js +++ b/packages/spacebars-compiler/codegen.js @@ -138,6 +138,7 @@ _.extend(CodeGen.prototype, { // XXX BACK COMPAT - UI is the old name, Template is the new if ((path[0] === 'UI' || path[0] === 'Template') && (path[1] === 'contentBlock' || path[1] === 'elseBlock')) { + // Call contentBlock and elseBlock in the appropriate scope includeCode = 'Blaze._InOuterTemplateScope(view, function () { return ' + includeCode + '; })'; } diff --git a/packages/spacebars-tests/template_tests.html b/packages/spacebars-tests/template_tests.html index af61bba09c..0295b86ddb 100644 --- a/packages/spacebars-tests/template_tests.html +++ b/packages/spacebars-tests/template_tests.html @@ -964,3 +964,31 @@ Hi there! + + + + + + diff --git a/packages/spacebars-tests/template_tests.js b/packages/spacebars-tests/template_tests.js index 067c7316a5..609c15fe67 100644 --- a/packages/spacebars-tests/template_tests.js +++ b/packages/spacebars-tests/template_tests.js @@ -2860,3 +2860,46 @@ Tinytest.add( document.body.removeChild(div); } ); + +Tinytest.add("spacebars-tests - template_tests - contentBlock back-compat", function (test) { + // adapted from another test, but this time make sure `UI.contentBlock` + // and `UI.elseBlock` correctly behave as `Template.contentBlock` + // and `Template.elseBlock`. + + var tmpl = Template.spacebars_template_test_content_backcompat; + var R = ReactiveVar(true); + tmpl.flag = function () { + return R.get(); + }; + var div = renderToDiv(tmpl); + test.equal(canonicalizeHtml(div.innerHTML), 'hello'); + R.set(false); + Tracker.flush(); + test.equal(canonicalizeHtml(div.innerHTML), 'world'); + R.set(true); + Tracker.flush(); + test.equal(canonicalizeHtml(div.innerHTML), 'hello'); +}); + +// For completeness (of coverage), make sure the code that calls +// `Template.contentBlock` in the correct scope also causes +// the old `UI.contentBlock` to be called in the correct scope. +Tinytest.add("spacebars-tests - template_tests - content context back-compat", function (test) { + var tmpl = Template.spacebars_template_test_content_context_backcompat; + var R = ReactiveVar(true); + tmpl.foo = { + firstLetter: 'F', + secondLetter: 'O', + bar: { + cond: function () { return R.get(); }, + firstLetter: 'B', + secondLetter: 'A' + } + }; + + var div = renderToDiv(tmpl); + test.equal(canonicalizeHtml(div.innerHTML), 'BO'); + R.set(false); + Tracker.flush(); + test.equal(canonicalizeHtml(div.innerHTML), 'FA'); +}); diff --git a/packages/spacebars/dynamic_tests.html b/packages/spacebars/dynamic_tests.html index 0671ed9f39..37c7b58420 100644 --- a/packages/spacebars/dynamic_tests.html +++ b/packages/spacebars/dynamic_tests.html @@ -43,3 +43,7 @@ + + diff --git a/packages/spacebars/dynamic_tests.js b/packages/spacebars/dynamic_tests.js index a8f1f63af2..bfacd10e35 100644 --- a/packages/spacebars/dynamic_tests.js +++ b/packages/spacebars/dynamic_tests.js @@ -145,3 +145,32 @@ Tinytest.add( test.equal(subtmplContext, {}); } ); + +Tinytest.add( + "spacebars - ui-dynamic-template - back-compat", function (test, expect) { + var tmpl = Template.ui_dynamic_backcompat; + + var nameVar = new ReactiveVar; + var dataVar = new ReactiveVar; + tmpl.templateName = function () { + return nameVar.get(); + }; + tmpl.templateData = function () { + return dataVar.get(); + }; + + // No template chosen + var div = renderToDiv(tmpl); + test.equal(canonicalizeHtml(div.innerHTML), ""); + + // Choose the "ui-dynamic-test-sub" template, with no data context + // passed in. + nameVar.set("ui_dynamic_test_sub"); + Tracker.flush(); + test.equal(canonicalizeHtml(div.innerHTML), "test"); + + // Set a data context. + dataVar.set({ foo: "bar" }); + Tracker.flush(); + test.equal(canonicalizeHtml(div.innerHTML), "testbar"); + });