javascript - How to test if a template has rendered for a iron:router route in a mocha test? -


in meteor app, want able test if template has rendered specific route/path. current setup includes following: iron:router, practicalmeteor:mocha, , using blaze rendering.

there 2 issues in particular cannot work:

  • waiting route finish without using settimeout (i prefer callback of sort)
  • figuring out whether or not blaze template has rendered on page.

how can test if template has rendered after calling router.go()?

import { router } 'meteor/iron:router'; import { template } 'meteor/templating'; import { chai } 'meteor/practicalmeteor:chai';  router.route('/example', { name: 'exampletemp' });  describe('example route', function() {     it('renders template exampletemp', function() {         router.go('/example');         // not sure put here wait route finish          // don't know how achieve function below         chai.assert.istrue(template.exampletemp.isrendered());     }); }); 

this isn't perfect solution because overwrites onafteraction hook if have 1 defined. also, adds onrendered functions templates creates messy testing environment

router-helpers.test.js

import { template } 'meteor/templating'; import { tracker } 'meteor/tracker'; import { router } 'meteor/iron:router';  export const withrenderedroute = function(templates, callback) {     let routerendered = new reactivevar(false);     router.onafteraction(function() {         routerendered.set(true);     });     let templatesrendered = [];     if (array.isarray(templates)) {         templates.foreach(function(templatename) {             let rendered = new reactivevar(false);             template[templatename].onrendered(function() {                 rendered.set(true);             });             templatesrendered.push(rendered);         });     }     tracker.autorun(function() {         const aretemplatesrendered = templatesrendered.every(function(rendered) {             return rendered.get();         });         if (routerendered.get() && aretemplatesrendered) {             router.onafteraction(function() {});             if (callback) {                 callback();             }         }     }); }; 

router.test.html

<template name="dummylayout">{{> yield}}</template> <template name="dummytemplate"></template> 

router.test.js

import { chai } 'meteor/practicalmeteor:chai'; import { withrenderedroute } './router-helpers.test.js'; import './router.test.html'; import './router.js';  const routestotest = [     { name: 'home', path: '/', template: 'home', layout: 'layoutdefault' }     // more routes ];  describe('router', function() {     before(function() {         router.route('/dummyroute', { name: 'dummyroute', template: 'dummytemplate', layouttemplate: 'dummylayout' });     });     beforeeach(function(done) {         router.go('dummyroute');         withrenderedroute(['dummytemplate'], done);     });     after(function() {         router.go('/');     });     routestotest.foreach(function(testroute) {         let message = 'route ' + testroute.name + ' path ' + testroute.path;         message += ' should render template ' + testroute.template + ' layout ' + testroute.layout;         it(message, function(done) {             router.go(testroute.name);             withrenderedroute([testroute.template, testroute.layout], function() {                 // route , templates have been rendered correctly @ point, otherwise test timeout                 chai.assert.equal(router.routes[testroute.name].path(), testroute.path);                 done();             });         });     }); }); 

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -