How to write unit test for template helper which returns cursor


#1

How do I do a unit test for a template helper, which returns a cursor?

As a very simple example, this helper gives me all articles

Template.example.helpers({
	articles() { 
		return Articles.find( { title: { $exists: true } } ); 
	}
});

Now I start to write the test:

describe("template helpers", function () {
	it("article should return a cursor to the template", function () {
		var articles = Template.example.__helpers.get('articles');
		expect(articles).to.not.be.undefined;
	});
});

So this is working, but it is quite a bad test.


#2

Maybe just a hint for that…?


#3

It depends on what you want to verify. You can check if the returned object is indeed a cursor by checking its constructor or the functions it has (fetch, count, forEach, map, …)

Or do you want to verify if it fetches only articles that have a title? You could try to mock Articles.find then and assert that it has been called with {title: {$exists: true}}


#4

I think in this case I want to check if the returned object is a cursor.