Back-compat tests

This commit is contained in:
David Greenspan
2014-09-18 17:29:04 -07:00
parent e7cf040177
commit 855284559a
5 changed files with 105 additions and 0 deletions

View File

@@ -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 + '; })';
}

View File

@@ -964,3 +964,31 @@ Hi there!
<template name="spacebars_template_test_input_field_to_same_value">
<input type="text" value={{foo}}>
</template>
<template name="spacebars_template_test_content_backcompat">
{{#spacebars_template_test_iftemplate_backcompat condition=flag}}
hello
{{else}}
world
{{/spacebars_template_test_iftemplate_backcompat}}
</template>
<template name="spacebars_template_test_iftemplate_backcompat">
{{#if condition}}
{{> UI.contentBlock}}
{{else}}
{{> UI.elseBlock}}
{{/if}}
</template>
<template name="spacebars_template_test_content_context_backcompat">
{{#with foo}}
{{#with bar}}
{{#spacebars_template_test_iftemplate_backcompat condition=cond}}
{{firstLetter}}{{../secondLetter}}
{{else}}
{{../firstLetter}}{{secondLetter}}
{{/spacebars_template_test_iftemplate_backcompat}}
{{/with}}
{{/with}}
</template>

View File

@@ -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');
});

View File

@@ -43,3 +43,7 @@
<template name="ui_dynamic_test_falsey_context_sub">
{{foo}}
</template>
<template name="ui_dynamic_backcompat">
{{> UI.dynamic template=templateName data=templateData}}
</template>

View File

@@ -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");
});