Skip to content

Instantly share code, notes, and snippets.

@feliperohdee
Last active February 15, 2017 21:25
Show Gist options
  • Save feliperohdee/a30d468984bc6e5ccee37924a8ac7809 to your computer and use it in GitHub Desktop.
Save feliperohdee/a30d468984bc6e5ccee37924a8ac7809 to your computer and use it in GitHub Desktop.
import * as _ from 'lodash';
import {TestScheduler, Observable, Notification} from 'rxjs/Rx.KitchenSink';
import {HotObservable} from 'rxjs/src/testing/HotObservable';
import {ColdObservable} from 'rxjs/src/testing/ColdObservable';
import {SubscriptionLog} from 'rxjs/src/testing/SubscriptionLog';
export type observableToBeFn = (marbles: string, values?: any, errorValue?: any) => void;
export type subscriptionLogsToBeFn = (marbles: string | string[]) => void;
export class ObservableTestsHelper {
public testScheduler: TestScheduler;
public static onBuild: Array<Function> = [];
constructor() {
this.testScheduler = new TestScheduler((a, b) => {
expect(JSON.stringify(a, null, 2)).toEqual(JSON.stringify(b, null, 2));
});
this.testScheduler.maxFrames = 35000;
// execute tasks on build
if (_.size(ObservableTestsHelper.onBuild)) {
_.each(ObservableTestsHelper.onBuild, task => task(this));
}
}
hot(marbles: string, values?: any, error?: any): HotObservable<any> {
return this.testScheduler.createHotObservable.apply(this.testScheduler, arguments);
}
cold(marbles: string, values?: any, error?: any): ColdObservable<any> {
return this.testScheduler.createColdObservable.apply(this.testScheduler, arguments);
}
expectObservable(observable: Observable<any>, unsubscriptionMarbles: string = null): ({ toBe: observableToBeFn }) {
let result = this.testScheduler.expectObservable.apply(this.testScheduler, arguments);
return {
toBe: (marbles: string, values?: any, errorValue?: any) => {
result.toBe(marbles, values, errorValue);
this.testScheduler.flush();
}
}
}
expectSubscriptions(actualSubscriptionLogs: SubscriptionLog[]): ({ toBe: subscriptionLogsToBeFn }) {
let result = this.testScheduler.expectSubscriptions.apply(this.testScheduler, arguments);
return {
toBe: (marbles: string | string[]) => {
result.toBe(marbles);
this.testScheduler.flush();
}
}
}
assertDeepEqual(actual: any, expected: any): void {
(<any>expect(actual)).toDeepEqual(expected);
}
time(marbles: string): number {
return this.testScheduler.createTime.apply(this.testScheduler, arguments);
}
parseMarbles(marbles: string, values?: any, errorValue?: any, materialize: boolean = false): any {
return TestScheduler.parseMarbles(marbles, values, errorValue, materialize)
}
next(value: any): Notification<any> {
return Notification.createNext(value);
}
error(value: any): Notification<any> {
return Notification.createError(value);
}
complete(): Notification<any> {
return Notification.createComplete();
}
mock(base: any, name: string, ...customArgs: Array<any>): jasmine.Spy {
let original: Function = _.get(base, name) as Function;
let testScheduler = this.testScheduler;
return spyOn(base, name).and.callFake(function(...args: Array<any>) {
if (_.size(customArgs)) {
args = customArgs;
}
args.push(testScheduler);
return original.apply(this, args);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment