Testing Blaze Components: How to get a hold of the Template instance?


So let’s say I’m building a Blaze Component, and I’m testing it as described in the guide, using the withRenderedTemplate helper.

So far, that’s all and well, as long as I only want to make assertions about the rendered HTML. But let’s say I want to test a method I attached to the Template instance (in the onCreated callback).

How can I get a hold of the Template instance from within the withRenderedTemplate callback?

I’ve been reading the docs for over an hour and tried different things, but I can’t get a hold of the instance. According to the docs, Blaze.renderWithData() returns a Blaze.View, which is supposed
to have a templateInstance() method. However, in my tests, that was not the case.

What am I missing here?


FWIW I’m wondering the same :slight_smile:



FWIW I posted the same question here and got a response from @tmeasday.

Haven’t been able to make sense of it yet, but maybe you can.



Maybe I am not answering your question directly, but in the generated template, withing helpers you can use Template.instance() to get the current template instance. In onRendered, onCreated etc. you simply call this.

For the data that you passed in your BlazeRenderWithData, you access it from the data variable (this.data.field).

I don’t think you really want to access the template instance from your external code, as that would defeat the purpose. Data flows into the template – out flows go to the DB or to methods.


I wrote this some time back, might help you