Skip to content

Instantly share code, notes, and snippets.

@pavel-agarkov
Last active March 9, 2018 20:41
Show Gist options
  • Save pavel-agarkov/9e7badefd14b725ca7867a25774d7a0e to your computer and use it in GitHub Desktop.
Save pavel-agarkov/9e7badefd14b725ca7867a25774d7a0e to your computer and use it in GitHub Desktop.
Injects TestScheduler from jasmine-marbles into time-dependent Observable operations
import "rxjs/add/operator/debounceTime";
import "rxjs/add/operator/throttleTime";
import { TestScheduler } from "jasmine-marbles/src/scheduler";
import { Observable } from "rxjs/Observable";
export class MarbleSchedulerInjector
{
static inject(scheduler: TestScheduler, ...methods: (keyof Observable<any> | "all")[])
{
methods = methods || ["all"];
if (methods.length === 0) methods.push("all");
for (let method of methods)
{
switch (method)
{
case "all":
$this.injectIntoDebounceTime(scheduler);
$this.injectIntoThrottleTime(scheduler);
$this.injectIntoDelay(scheduler);
break;
case "debounceTime":
$this.injectIntoDebounceTime(scheduler);
break;
case "throttleTime":
$this.injectIntoThrottleTime(scheduler);
break;
case "delay":
$this.injectIntoDelay(scheduler);
break;
default:
throw new Error(`Method [${method}] is not supported for injection yet.`);
}
}
}
static readonly originalDebounceTime = Observable.prototype.debounceTime;
static injectIntoDebounceTime(scheduler: TestScheduler)
{
spyOn(Observable.prototype, 'debounceTime').and.callFake(
function (this: Observable<any>, duration: number)
{
return $this.originalDebounceTime.call(this, duration, scheduler);
});
}
static readonly originalThrottleTime = Observable.prototype.throttleTime;
static injectIntoThrottleTime(scheduler: TestScheduler)
{
spyOn(Observable.prototype, 'throttleTime').and.callFake(
function (this: Observable<any>, duration: number)
{
return $this.originalThrottleTime.call(this, duration, scheduler);
});
}
static readonly originalDelay = Observable.prototype.delay;
static injectIntoDelay(scheduler: TestScheduler)
{
spyOn(Observable.prototype, 'delay').and.callFake(
function (this: Observable<any>, delay: number | Date)
{
return $this.originalDelay.call(this, delay, scheduler);
});
}
}
const $this = MarbleSchedulerInjector;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment