jeudi 2 juin 2016

Unit testing for global helpers in meteor application

This are two examples of global helpers, which I'm using:

client/lib/helpers.js

Template.registerHelper('hasDocId', function() {
    return !!FlowRouter.getParam('docId');
});

Template.registerHelper('epochToHuman', function(timestamp) {
    let length;
    timestamp = parseInt(timestamp);
    if (timestamp)
        length = timestamp.toString().length;
    if (length === 13)
        timestamp = timestamp / 1000;
    if (length === 10 || length === 13)
        return moment.unix(timestamp).format('MMMM Do, YYYY');
});

For testing with meteor (meteor test) I created the file helpers.test.js in the same directory:

client/lib/helpers.test.js

import { chai, expect } from 'meteor/practicalmeteor:chai';
import './helpers.js';

describe("The global helper", function () {
    it("epochToHuman() should provide a date string", function () {

        // SETUP
        const timestamp = Math.floor(Date.now() / 1000);

        // EXECUTE
        const date = UI._globalHelpers.epochToHuman(timestamp);

        // VERIFY
        expect(date).to.be.a('string');
    });
});

There are a few questions with that:

  1. I really have to explicitly import the files, which I want to test? import './helpers.js';
  2. Is this test a proper way to check the helper? I don't think so, as it is just testing for a string...
  3. How do I do a test for the hasDocId helper as this is a FlowRouter helper?

Aucun commentaire:

Enregistrer un commentaire