Integration tests with Iron Router
For integration tests you want to make sure that the page is loaded before running the tests.
This can be achieved with this helper.
Save this helper to tests/jasmine/client/integration/lib/wait_for_router_helper.js
or tests/jasmine/client/unit/_wait_for_router_helper.js
depending on the mode you want to use:
(function (Meteor, Tracker, Router) {
var isRouterReady = false;
var callbacks = [];
window.waitForRouter = function (callback) {
if (isRouterReady) {
callback();
} else {
callbacks.push(callback);
}
};
Router.onAfterAction(function () {
if (!isRouterReady && this.ready()) {
Tracker.afterFlush(function () {
isRouterReady = true;
callbacks.forEach(function (callback) {
callback();
});
callbacks = []
})
}
});
Router.onRerun(function () {
isRouterReady = false;
this.next();
});
Router.onStop(function () {
isRouterReady = false;
if (this.next) {
this.next();
}
});
})(Meteor, Tracker, Router);
Then add a beforeEach(waitForRouter);
statement to you specs. For example:
describe('My Spec', function () {
beforeEach(function (done) {
Router.go('/myPage');
Tracker.afterFlush(done);
});
beforeEach(waitForRouter);
it('should do something', function () {
// Your test
});
})
Updated less than a minute ago