javascript - Testing Subject using TestScheduler in RxJs -
i using rxjs count how many packets arrive in particular time window. code looks this:
var packetsubject = new rx.subject(); var packetsinwindow = []; function startmonitoring() { var subscription = packetsubject .windowwithtime(1000) .select(function(window) { window.toarray().subscribe(function(elements) { packetsinwindow.push(elements.length); }); }) .subscribe(); } function newpacket(packet) { packetsubject.onnext(packet); }
how unit test code using rx testscheduler? not find suitable example testing subjects.
have example :
var x = 0, scheduler = new rx.testscheduler(); var subject = new rx.subject(); subject.throttle(100, scheduler).subscribe(function (value) { x = value; }); scheduler.schedulewithabsolute(0, function () { subject.onnext(1);//trigger first event value 1 }); scheduler.schedulewithabsolute(50, function () { expect(x).toequal(0);//value hasn't been updated }); scheduler.schedulewithabsolute(200, function () { expect(x).toequal(1);//value update after throttle's windowduration }); scheduler.start();
https://emmkong.wordpress.com/2015/03/18/how-to-unit-test-rxjs-throttle-with-rx-testscheduler/
Comments
Post a Comment