{"_id":"55bf698f8b83a5370044507a","category":{"_id":"55c345418713ec1900d1732d","project":"55a92d71cf45e1390093f321","version":"55a92d71cf45e1390093f324","__v":2,"pages":["55c345838713ec1900d17330","55c34683b6e05719008a046e"],"sync":{"url":"","isSync":false},"reference":false,"createdAt":"2015-08-06T11:30:09.092Z","from_sync":false,"order":2,"slug":"jasmine-advanced","title":"Jasmine Advanced"},"version":{"_id":"55a92d71cf45e1390093f324","__v":12,"project":"55a92d71cf45e1390093f321","createdAt":"2015-07-17T16:29:37.746Z","releaseDate":"2015-07-17T16:29:37.746Z","categories":["55a92d72cf45e1390093f325","55b9aea1baf4cd2300bc56e7","55bf16593179dd1900a19521","55bf16b93179dd1900a19525","55bf16c53bbd4b17002c2369","55bf234703d092210084c07b","55bf2859ad601c2b00762c4c","55bf3217ad601c2b00762c69","55bf321b03d092210084c09d","55bf5ef78b83a5370044504e","55c345418713ec1900d1732d","55c3473057f7f32d0016ec0a"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"1.0.0","version":"1.0"},"__v":1,"parentDoc":null,"project":"55a92d71cf45e1390093f321","user":"55a4738a68c7e20d0036d4de","updates":[],"next":{"pages":[],"description":""},"createdAt":"2015-08-03T13:15:59.964Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":2,"body":"For integration tests you want to make sure that the page is loaded before running the tests.\nThis can be achieved with this helper.\n\nSave 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:\n\n```javascript\n(function (Meteor, Tracker, Router) {\n  var isRouterReady = false;\n  var callbacks = [];\n\n  window.waitForRouter = function (callback) {\n    if (isRouterReady) {\n      callback();\n    } else {\n      callbacks.push(callback);\n    }\n  };\n\n  Router.onAfterAction(function () {\n    if (!isRouterReady && this.ready()) {\n      Tracker.afterFlush(function () {\n        isRouterReady = true;\n        callbacks.forEach(function (callback) {\n          callback();\n        });\n        callbacks = []\n      })\n    }\n  });\n\n  Router.onRerun(function () {\n    isRouterReady = false;\n    this.next();\n  });\n\n  Router.onStop(function () {\n    isRouterReady = false;\n    if (this.next) {\n      this.next();\n    }\n  });\n})(Meteor, Tracker, Router);\n```\n\nThen add a `beforeEach(waitForRouter);` statement to you specs. For example:\n\n```javascript\ndescribe('My Spec', function () {\n  beforeEach(function (done) {\n    Router.go('/myPage');\n    Tracker.afterFlush(done);\n  });\n\n  beforeEach(waitForRouter);\n\n  it('should do something', function () {\n    // Your test\n  });\n})\n```","excerpt":"","slug":"jasmine-integration-tests-with-iron-router","type":"basic","title":"Integration tests with Iron Router"}

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: ```javascript (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: ```javascript describe('My Spec', function () { beforeEach(function (done) { Router.go('/myPage'); Tracker.afterFlush(done); }); beforeEach(waitForRouter); it('should do something', function () { // Your test }); }) ```