Test autorun cleanup and jQuery removal

This commit is contained in:
David Greenspan
2014-07-03 14:46:06 -07:00
parent beed9644f9
commit 1a8136e4ef
2 changed files with 81 additions and 0 deletions

View File

@@ -842,3 +842,29 @@ Hi there!
<span>{{greeting}} {{r}}</span>
</template>
<template name="spacebars_test_parent_removal">
<div class="a">
<div class="b">
<div class="toremove">
<div class="c">
{{A 1}}
{{{A 2}}}
{{#with A 3}}
{{A 4}}
{{#with A 5}}
{{A 6}}
{{/with}}
{{/with}}
{{#each B}}
{{A 7}}
{{/each}}
{{#if A 8}}
{{A 9}}
{{else}}
{{A 'a'}}
{{/if}}
</div>
</div>
</div>
</div>
</template>

View File

@@ -2350,3 +2350,58 @@ Tinytest.add(
test.isTrue(span);
test.equal(UI.getElementData(span), {foo: "bar"});
});
Tinytest.add(
"spacebars-tests - template_tests - autorun cleanup",
function (test) {
var tmpl = Template.spacebars_test_parent_removal;
var Acalls = '';
var A = ReactiveVar('hi');
tmpl.A = function (chr) {
Acalls += chr;
return A.get();
};
var Bcalls = 0;
var B = ReactiveVar(['one', 'two']);
tmpl.B = function () {
Bcalls++;
return B.get();
};
// Assert how many times A and B were accessed (since last time)
// and how many autoruns are listening to them.
var assertCallsAndListeners =
function (a_calls, b_calls, a_listeners, b_listeners) {
test.equal('A calls: ' + Acalls.length,
'A calls: ' + a_calls,
Acalls);
test.equal('B calls: ' + Bcalls,
'B calls: ' + b_calls);
test.equal('A listeners: ' + A.numListeners(),
'A listeners: ' + a_listeners);
test.equal('B listeners: ' + B.numListeners(),
'B listeners: ' + b_listeners);
Acalls = '';
Bcalls = 0;
};
var div = renderToDiv(tmpl);
assertCallsAndListeners(10, 1, 10, 1);
A.set('');
Deps.flush();
// a_calls could be as low as 7, but
// the if/else feature of a "with" makes use of an
// intermediary ReactiveVar that keeps the contents
// from being torn down right away, so they
// re-run (template tags 4, 5, and 6).
assertCallsAndListeners(10, 0, 7, 1);
A.set('hi');
Deps.flush();
assertCallsAndListeners(10, 0, 10, 1);
// Now see that removing the DOM with jQuery, below
// the level of the entire template, stops everything.
$(div.querySelector('.toremove')).remove();
assertCallsAndListeners(0, 0, 0, 0);
});