Accessing the Meteor Template Helper feature in Jasmine to test the integration

I am trying to run Jasmine client integration tests on a meteor project. I am using the Jasmine meteor 0.9.4

package sanjo:jasmine

as well.

I wrote a test that looks like this:

describe("Template.dashboard.tasks", function() {

    it("ela displays correct assessment", function() {
        Session.set("selected_subject", "math");
        Session.set('selected_grade', "1");

        tasks = Template.dashboard.tasks();
        expect(true).toBe(true);
    });
});

      

I am getting an error before it can get to the end of the test:

Cannot read property 'tasks' of undefined

      

This means it Template.dashboard

does not exist within this test.

Template.dashboard.tasks()

is a helper function that works completely and is in a file js

in the view folder. Normal tests Jasmine

work as expected, but as soon as I try to use one of my own functions from another file it doesn't work.

My question is, is there something I need to do to give the tag Jasmine

access to my template helper functions?

+3


source to share


3 answers


Meteor uses Template helper functions such as:

Template.dashboard.tasks = function () {
    ...
};

      

But this is deprecated and the new format is:

Template.dashboard.helpers({
    tasks: function(){
        ...
    }
});

      

In Jasmine with the previous formatting, you can access helper functions like:



Template.dashboard.tasks();

      

But now you have to call the helper functions like this:

Template.dashboard.__helpers[' tasks']();

      

Sanjo (owner of the meteor jasmine repo ) suggested using a function like this to make it easier to call the helper functions (especially if the syntax ends up changing again):

function callHelper(template, helperName, context, args) {
    context = context || {};
    args = args || [];
    template.__helpers[' ' + helperName].apply(context, args);  
}

      

+6


source


Updated answer to this question for Meteor 1.3 (sorry, I'm using mocha, but it doesn't affect the answer):

The templates Template.foo and Template.foo won't be eagerly customized when testing, so you need to import foo.html

and then foo.js

.

Here's an example:



import { Meteor } from 'meteor/meteor';
import { Template } from 'meteor/templating';
import { Foo } from '/collections/foo.js';
import { assert } from 'meteor/practicalmeteor:chai';
import './foo.html';  // now Template.foo is defined
import './foo.js';    // now Template.foo.__helpers[' bar'] is defined


describe('foo handlers', () => {
    it("Should test bar", () => {
       // don't forget the space, helper key is ' bar' not 'bar'
       var bar = Template.foo.__helpers[' bar'].apply({foo:"bar"},3); 
       assert.Equal(bar,'bar');
    });
});

      

Of course, as said earlier, you should make sure to encapsulate the weird Template.foo.__helpers[' bar'].apply(context,args)

into a nice, clean helper.

+2


source


back-end tests work great right from the start and there is one more thing to do to run front-end tests. I will try to find you.

Also, consider reading or reading again the famous and explicit article from Dr. Llama's blog related to Jasmin / Meteor: Bullet-proof Meteor applications with Velocity, Device Testing, Integration Testing, and Jasmine

0


source







All Articles