mirror of
https://github.com/meteor/meteor.git
synced 2026-05-02 03:01:46 -04:00
Tests for Template.currentData and Template.parentData
A recent change fixed the behavior of `Template.currentData` and `Template.parentData` in event handlers and helpers. These are tests for the new, correct behavior. (The old behavior read the data of the template instance. The new behavior read the data context around the DOM node where the event or helper ran)
This commit is contained in:
@@ -1027,3 +1027,11 @@ Hi there!
|
||||
<template name="spacebars_template_test_template_instance_wrapper_wrapper">
|
||||
{{> Template.contentBlock}}
|
||||
</template>
|
||||
|
||||
<template name="spacebars_template_test_currentData_and_parentData_in_events">
|
||||
{{#with x=1}}
|
||||
{{#with y=2}}
|
||||
<button>{{label}}</button>
|
||||
{{/with}}
|
||||
{{/with}}
|
||||
</template>
|
||||
|
||||
@@ -10,7 +10,20 @@ var nodesToArray = function (array) {
|
||||
return _.map(array, _.identity);
|
||||
};
|
||||
|
||||
var inDocument = function (elem) {
|
||||
while ((elem = elem.parentNode)) {
|
||||
if (elem == document) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
|
||||
var clickIt = function (elem) {
|
||||
if (!inDocument(elem))
|
||||
throw new Error("Can't click on elements without first adding them to the document");
|
||||
|
||||
// jQuery's bubbling change event polyfill for IE 8 seems
|
||||
// to require that the element in question have focus when
|
||||
// it receives a simulated click.
|
||||
@@ -3084,3 +3097,42 @@ Tinytest.add("spacebars-tests - template_tests - custom block helper doesn't bre
|
||||
var div = renderToDiv(tmpl);
|
||||
test.equal(canonicalizeHtml(div.innerHTML), "hello hello");
|
||||
});
|
||||
|
||||
Tinytest.add(
|
||||
"spacebars-tests - template_tests - currentData and parentData in event handlers",
|
||||
function (test) {
|
||||
var tmpl = Template.spacebars_template_test_currentData_and_parentData_in_events;
|
||||
|
||||
var clicked = false;
|
||||
var currentInEvent;
|
||||
var parentInEvent;
|
||||
var currentInHelper;
|
||||
var parentInHelper;
|
||||
|
||||
tmpl.events({
|
||||
'click button': function () {
|
||||
currentInEvent = Template.currentData();
|
||||
parentInEvent = Template.parentData(1);
|
||||
}
|
||||
});
|
||||
|
||||
tmpl.helpers({
|
||||
label: function () {
|
||||
currentInHelper = Template.currentData();
|
||||
parentInHelper = Template.parentData(1);
|
||||
}
|
||||
});
|
||||
|
||||
var div = renderToDiv(tmpl);
|
||||
var button = div.querySelector('button');
|
||||
document.body.appendChild(div);
|
||||
|
||||
clickIt(button);
|
||||
|
||||
test.equal(currentInEvent, {y: 2});
|
||||
test.equal(parentInEvent, {x: 1});
|
||||
test.equal(currentInHelper, {y: 2});
|
||||
test.equal(parentInHelper, {x: 1});
|
||||
|
||||
document.body.removeChild(div);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user