代码之家  ›  专栏  ›  技术社区  ›  Takuhii

我可以用茉莉花测试setinterval吗?

  •  0
  • Takuhii  · 技术社区  · 6 年前

    我的代码中有这个语句,我想知道如何使用jasmine测试setinterval。

    const x = setInterval(() => {
        const countdown = getElementById('countdownWrapper');
        const systemTime = ...
        const now = new Date().getTime();
        const endTime = systemTime - now;
    
        countdown.querySelector('.countdown-time').innerHTML = time();
    
        if (endTime < 0) {
            clearInterval(x);
            countdown.classList.remove('countdown-time--show');
        }
    }, 1000);
    

    SystemTime是从HTML中的数据内容属性中的epoch值提供的。

    任何帮助都将不胜感激

    1 回复  |  直到 6 年前
        1
  •  2
  •   Benny Code    5 年前
    beforeEach(function() {
      timerCallback = jasmine.createSpyObj("setInterval");
      jasmine.clock().install();
    });
    
    afterEach(function() {
      jasmine.clock().uninstall();
    });
    
    it("causes a timeout to be called", function() {
      setTimeout(function() {
        timerCallback();
      }, 1000);
    
      expect(setInterval).not.toHaveBeenCalled();
    
      jasmine.clock().tick(1001);
    
      expect(setInterval).toHaveBeenCalled();
    });