試用一下這些功能:
/**
* Call the function f the first time the template will be rendered
* And then remove the function inserted
* @param {Template} template A meteor template
* @param {callback} f The function to execute after the template has been rendered
*/
this.afterRendered = function(template, f) {
// Insert our rendered function
template._callbacks.rendered.push(function() {
template._callbacks.rendered.pop();
f();
});
}
/**
* @source http://stackoverflow.com/questions/5525071/how-to-wait-until-an-element-exists
* @brief Wait for something to be ready before triggering a timeout
* @param {callback} isready Function which returns true when the thing we're waiting for has happened
* @param {callback} success Function to call when the thing is ready
* @param {callback} error Function to call if we time out before the event becomes ready
* @param {int} count Number of times to retry the timeout (default 300 or 6s)
* @param {int} interval Number of milliseconds to wait between attempts (default 20ms)
*/
this.waitUntil = function (isready, success, error, count, interval) {
if (count === undefined) {
count = 300;
}
if (interval === undefined) {
interval = 20;
}
if (isready()) {
success();
return;
}
// The call back isn't ready. We need to wait for it
setTimeout(function(){
if (!count) {
// We have run out of retries
if (error !== undefined) {
error();
}
} else {
// Try again
waitUntil(isready, success, error, count -1, interval);
}
}, interval);
}
他們是完全做的工作與摩卡+速度。藉助摩卡,您只需在tests/mocha/client
文件夾中的任意位置創建一個文件lib.js
並粘貼即可。
摩卡例子(對不起,我不使用茉莉):
describe("check something", function() {
it ("test something", function(done) {
afterRendered(Template.homepage, function() {
chai.expect(...).to.not.be.undefined;
done();
});
FlowRouter.go('somewhere');
});
it ("test something else", function(done) {
FlowRouter.go('home');
var waitUntilOnError = function(done) {
return function() {
done("Failed to wait until")
}
};
var test = function() {
return $('div').length === 1;
}
var maxAttempt = 200;
var waitBetweenAttempt = 60;
this.timeout(maxAttempt * waitBetweenAttempt * 2);
waitUntil(test, done, waitUntilOnError, maxAttempt, waitBetweenAttempt);
});
});
來源
2016-03-01 16:33:12
Ser