javascriptmeteorjestjsmeteor-tracker

How to implement a Meteor Tracker enabled feature?


I am writing a module supporting Meteor Tracker, but I am unable to test it. I added meteor-standalone-tracker as dev dependency, and wrote a test case, but the autorun function is called only once.

For example, I wrote a dummy test inspired from the docs as follow

import assert from 'assert';
import Tracker from 'trackr';

describe('Testing Tracker', () => {

  it('should do as expected', () => {

    var favoriteFood = "apples";
    var favoriteFoodDep = new Tracker.Dependency;

    var getFavoriteFood = function () {
      favoriteFoodDep.depend();
      return favoriteFood;
    };

    var setFavoriteFood = function (newValue) {
      favoriteFood = newValue;
      favoriteFoodDep.changed();
    };

    console.log("GET:" , getFavoriteFood());
    // "apples"

    var handle = Tracker.autorun(function () {
      console.log("Your favorite food is " + getFavoriteFood());
    });
    // "Your favorite food is apples"

    setFavoriteFood("mangoes");
    // "Your favorite food is mangoes"
    setFavoriteFood("peaches");
    // "Your favorite food is peaches"
    setFavoriteFood("bananas");
    // "Your favorite food is bananas"
    handle.stop();
    setFavoriteFood("cake");
    // (nothing printed)

  });

});

The the only output I get is this :

console.log test/tracker.spec.js:27
   GET: apples

console.log test/tracker.spec.js:31
   Your favorite food is apples

What am I missing?


Solution

  • My current solution is to flush all calculations.

    Tracker.flush();
    

    This will force the autorun functions with dependencies that have changed to be called.