如何实现启用Meteor Tracker的功能?

时间:2018-09-07 17:12:43

标签: javascript meteor jestjs meteor-tracker

我正在编写一个支持Meteor Tracker的模块,但无法对其进行测试。我将meteor-standalone-tracker添加为dev依赖项,并编写了一个测试用例,但是autorun函数仅被调用一次。

例如,我写了一个受from the docs启发的虚拟测试,如下所述

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)

  });

});

我得到的 only 输出是这样的:

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

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

我想念什么?

1 个答案:

答案 0 :(得分:0)

我当前的解决方案是刷新所有计算。

Tracker.flush();

这将强制更改具有依赖项的autorun函数。