2016-08-21 36 views
0

我有一個類,它正在使用Chokidar,現在需要用茉莉花來測試它。這是方法,其中chokidar亮起板:如何觸發Chokidar事件programmaticaly

public watch(path: string) { 
    var watchOptions: chokidar.WatchOptions = <chokidar.WatchOptions> { 
     persistent: true 
    }; 
    this.watcher = chokidar.watch(path, watchOptions); 
    this.watcher.on("add", (fileName: string, stats: fs.Stats) => { 
     this.sendNotifyAction(new NotifyAction(PathEvent.Add, fileName)); 
    }).on("change", (fileName: string, stats: fs.Stats) => { 
     this.sendNotifyAction(new NotifyAction(PathEvent.Change, fileName)); 
    }) 
} 

我想在我的測試是這樣的:

it("Should send notification, when internal directory is added",() => { 
     runs(() => { 
      flag = false; 
      pathWatch.watch(dirPath); 

      //TRIGGER chokidar on.add event?? 
      //spyOn(pathWatch.watcher, "on").and.callFake((params) => { 
      // flag = true; 
      //}); 

     }); 
     waitsFor((): boolean => { 
      return flag; 
     }, "failure", 5000); 

     runs(() => { 
      var expectedNotifyAction = new NotifyAction(PathEvent.Add, notificationInternalDirPath); 
      expect(subscriber.processNotifyAction).toHaveBeenCalledWith(expectedNotifyAction); 
     }); 
    }); 

的問題是,我不知道如何來模擬Chokidar事件。任何幫助深表感謝。

+0

爲什麼不直接寫一個文件,並添加一個帶有node.js文件API的文件,它應該會觸發chokidar – qballer

+0

這就是我現在的做法,但它有點混亂,我很在意如何模仿chokidar事件 –

+2

chokidar測試https://github.com/paulmillr /chokidar/blob/master/test.js#L162 – qballer

回答

1

。觀察者例如從EventEmitter繼承,這樣你就可以調用它.emit()(雖然我不知道如何/是否會與打字稿工作):

pathWatch.watcher.emit('add', PATH, STATS); 
+0

謝謝,它適合我! –